|
| 1 | +package com.google.ai.sample |
| 2 | + |
| 3 | +import android.content.Intent |
| 4 | +import android.net.Uri |
| 5 | +import androidx.compose.foundation.layout.* |
| 6 | +import androidx.compose.foundation.lazy.LazyColumn |
| 7 | +import androidx.compose.foundation.lazy.itemsIndexed |
| 8 | +import androidx.compose.material3.* |
| 9 | +import androidx.compose.runtime.* |
| 10 | +import androidx.compose.ui.Alignment |
| 11 | +import androidx.compose.ui.Modifier |
| 12 | +import androidx.compose.ui.platform.LocalContext |
| 13 | +import androidx.compose.ui.text.style.TextAlign |
| 14 | +import androidx.compose.ui.unit.dp |
| 15 | +import androidx.compose.ui.window.Dialog |
| 16 | + |
| 17 | +/** |
| 18 | + * Dialog for API key input and management |
| 19 | + */ |
| 20 | +@Composable |
| 21 | +fun ApiKeyDialog( |
| 22 | + apiKeyManager: ApiKeyManager, |
| 23 | + isFirstLaunch: Boolean = false, |
| 24 | + onDismiss: () -> Unit |
| 25 | +) { |
| 26 | + var apiKeyInput by remember { mutableStateOf("") } |
| 27 | + var errorMessage by remember { mutableStateOf("") } |
| 28 | + val apiKeys = remember { mutableStateListOf<String>() } |
| 29 | + var selectedKeyIndex by remember { mutableStateOf(apiKeyManager.getCurrentKeyIndex()) } |
| 30 | + val context = LocalContext.current |
| 31 | + |
| 32 | + // Load existing keys |
| 33 | + LaunchedEffect(Unit) { |
| 34 | + apiKeys.clear() |
| 35 | + apiKeys.addAll(apiKeyManager.getApiKeys()) |
| 36 | + } |
| 37 | + |
| 38 | + Dialog(onDismissRequest = { |
| 39 | + // Only allow dismissal if not first launch or if keys exist |
| 40 | + if (!isFirstLaunch || apiKeys.isNotEmpty()) { |
| 41 | + onDismiss() |
| 42 | + } |
| 43 | + }) { |
| 44 | + Surface( |
| 45 | + modifier = Modifier |
| 46 | + .fillMaxWidth() |
| 47 | + .wrapContentHeight(), |
| 48 | + shape = MaterialTheme.shapes.medium, |
| 49 | + color = MaterialTheme.colorScheme.surface |
| 50 | + ) { |
| 51 | + Column( |
| 52 | + modifier = Modifier |
| 53 | + .padding(16.dp) |
| 54 | + .fillMaxWidth() |
| 55 | + ) { |
| 56 | + Text( |
| 57 | + text = if (isFirstLaunch) "API Key Required" else "Manage API Keys", |
| 58 | + style = MaterialTheme.typography.headlineSmall, |
| 59 | + modifier = Modifier.padding(bottom = 16.dp) |
| 60 | + ) |
| 61 | + |
| 62 | + if (isFirstLaunch && apiKeys.isEmpty()) { |
| 63 | + Text( |
| 64 | + text = "Please enter a Gemini API key to use this application.", |
| 65 | + style = MaterialTheme.typography.bodyMedium, |
| 66 | + modifier = Modifier.padding(bottom = 16.dp) |
| 67 | + ) |
| 68 | + } |
| 69 | + |
| 70 | + // Get API Key button |
| 71 | + Button( |
| 72 | + onClick = { |
| 73 | + val intent = Intent(Intent.ACTION_VIEW, Uri.parse("https://makersuite.google.com/app/apikey")) |
| 74 | + context.startActivity(intent) |
| 75 | + }, |
| 76 | + modifier = Modifier |
| 77 | + .fillMaxWidth() |
| 78 | + .padding(bottom = 16.dp) |
| 79 | + ) { |
| 80 | + Text("Get API Key") |
| 81 | + } |
| 82 | + |
| 83 | + // Input field for new API key |
| 84 | + OutlinedTextField( |
| 85 | + value = apiKeyInput, |
| 86 | + onValueChange = { |
| 87 | + apiKeyInput = it |
| 88 | + errorMessage = "" |
| 89 | + }, |
| 90 | + label = { Text("Enter API Key") }, |
| 91 | + modifier = Modifier |
| 92 | + .fillMaxWidth() |
| 93 | + .padding(bottom = 8.dp), |
| 94 | + singleLine = true |
| 95 | + ) |
| 96 | + |
| 97 | + // Error message |
| 98 | + if (errorMessage.isNotEmpty()) { |
| 99 | + Text( |
| 100 | + text = errorMessage, |
| 101 | + color = MaterialTheme.colorScheme.error, |
| 102 | + style = MaterialTheme.typography.bodySmall, |
| 103 | + modifier = Modifier.padding(bottom = 8.dp) |
| 104 | + ) |
| 105 | + } |
| 106 | + |
| 107 | + // Add button |
| 108 | + Button( |
| 109 | + onClick = { |
| 110 | + if (apiKeyInput.isBlank()) { |
| 111 | + errorMessage = "API key cannot be empty" |
| 112 | + return@Button |
| 113 | + } |
| 114 | + |
| 115 | + val added = apiKeyManager.addApiKey(apiKeyInput) |
| 116 | + if (added) { |
| 117 | + apiKeys.clear() |
| 118 | + apiKeys.addAll(apiKeyManager.getApiKeys()) |
| 119 | + apiKeyInput = "" |
| 120 | + selectedKeyIndex = apiKeyManager.getCurrentKeyIndex() |
| 121 | + } else { |
| 122 | + errorMessage = "API key already exists" |
| 123 | + } |
| 124 | + }, |
| 125 | + modifier = Modifier |
| 126 | + .align(Alignment.End) |
| 127 | + .padding(bottom = 16.dp) |
| 128 | + ) { |
| 129 | + Text("Add Key") |
| 130 | + } |
| 131 | + |
| 132 | + // List of existing keys |
| 133 | + if (apiKeys.isNotEmpty()) { |
| 134 | + Text( |
| 135 | + text = "Saved API Keys", |
| 136 | + style = MaterialTheme.typography.titleMedium, |
| 137 | + modifier = Modifier.padding(bottom = 8.dp) |
| 138 | + ) |
| 139 | + |
| 140 | + LazyColumn( |
| 141 | + modifier = Modifier |
| 142 | + .fillMaxWidth() |
| 143 | + .heightIn(max = 200.dp) |
| 144 | + ) { |
| 145 | + itemsIndexed(apiKeys) { index, key -> |
| 146 | + Row( |
| 147 | + modifier = Modifier |
| 148 | + .fillMaxWidth() |
| 149 | + .padding(vertical = 4.dp), |
| 150 | + verticalAlignment = Alignment.CenterVertically |
| 151 | + ) { |
| 152 | + RadioButton( |
| 153 | + selected = index == selectedKeyIndex, |
| 154 | + onClick = { |
| 155 | + apiKeyManager.setCurrentKeyIndex(index) |
| 156 | + selectedKeyIndex = index |
| 157 | + } |
| 158 | + ) |
| 159 | + |
| 160 | + Text( |
| 161 | + text = key.take(10) + "..." + key.takeLast(5), |
| 162 | + modifier = Modifier |
| 163 | + .weight(1f) |
| 164 | + .padding(start = 8.dp) |
| 165 | + ) |
| 166 | + |
| 167 | + IconButton( |
| 168 | + onClick = { |
| 169 | + apiKeyManager.removeApiKey(key) |
| 170 | + apiKeys.clear() |
| 171 | + apiKeys.addAll(apiKeyManager.getApiKeys()) |
| 172 | + selectedKeyIndex = apiKeyManager.getCurrentKeyIndex() |
| 173 | + } |
| 174 | + ) { |
| 175 | + Text("✕", textAlign = TextAlign.Center) |
| 176 | + } |
| 177 | + } |
| 178 | + } |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + // Buttons |
| 183 | + Row( |
| 184 | + modifier = Modifier |
| 185 | + .fillMaxWidth() |
| 186 | + .padding(top = 16.dp), |
| 187 | + horizontalArrangement = Arrangement.End |
| 188 | + ) { |
| 189 | + if (!isFirstLaunch || apiKeys.isNotEmpty()) { |
| 190 | + TextButton( |
| 191 | + onClick = onDismiss |
| 192 | + ) { |
| 193 | + Text("Close") |
| 194 | + } |
| 195 | + } |
| 196 | + } |
| 197 | + } |
| 198 | + } |
| 199 | + } |
| 200 | +} |
0 commit comments