|
| 1 | +package io.branch.branchlinksimulator |
| 2 | + |
| 3 | +import androidx.compose.ui.platform.ClipboardManager |
| 4 | +import android.content.Context |
| 5 | +import android.content.SharedPreferences |
| 6 | +import androidx.compose.foundation.background |
| 7 | +import androidx.compose.foundation.layout.* |
| 8 | +import androidx.compose.foundation.shape.RoundedCornerShape |
| 9 | +import androidx.compose.material3.* |
| 10 | +import androidx.compose.runtime.* |
| 11 | +import androidx.compose.ui.Alignment |
| 12 | +import androidx.compose.ui.Modifier |
| 13 | +import androidx.compose.ui.graphics.Color |
| 14 | +import androidx.compose.ui.platform.LocalClipboardManager |
| 15 | +import androidx.compose.ui.platform.LocalContext |
| 16 | +import androidx.compose.ui.text.AnnotatedString |
| 17 | +import androidx.compose.ui.text.style.TextAlign |
| 18 | +import androidx.compose.ui.text.style.TextOverflow |
| 19 | +import androidx.compose.ui.unit.dp |
| 20 | +import kotlin.system.exitProcess |
| 21 | + |
| 22 | +const val SELECTED_CONFIG_NAME = "selectedConfigName" |
| 23 | +const val PREFERENCES_KEY = "ApiPreferences" |
| 24 | + |
| 25 | +@Composable |
| 26 | +fun ApiSettingsPanel() { |
| 27 | + val context = LocalContext.current |
| 28 | + val preferences = remember { context.getSharedPreferences(PREFERENCES_KEY, Context.MODE_PRIVATE) } |
| 29 | + var selectedConfig by remember { mutableStateOf(ApiConfigManager.loadConfigOrDefault(preferences)) } |
| 30 | + |
| 31 | + Column( |
| 32 | + verticalArrangement = Arrangement.spacedBy(10.dp), |
| 33 | + modifier = Modifier.padding(16.dp) |
| 34 | + ) { |
| 35 | + ApiInfoRow(label = "Branch Key", value = selectedConfig.branchKey) |
| 36 | + ApiInfoRow(label = "API URL", value = selectedConfig.apiUrl) |
| 37 | + ApiInfoRow(label = "App ID", value = selectedConfig.appId) |
| 38 | + |
| 39 | + Column(horizontalAlignment = Alignment.CenterHorizontally) { |
| 40 | + Row(horizontalArrangement = Arrangement.spacedBy(5.dp)) { |
| 41 | + ApiButton(configName = STAGING, selectedConfig = selectedConfig, Modifier.weight(1f), onSelect = { |
| 42 | + selectedConfig = it |
| 43 | + saveConfig(preferences, it) |
| 44 | + }) |
| 45 | + ApiButton(configName = PRODUCTION, selectedConfig = selectedConfig, Modifier.weight(1f), onSelect = { |
| 46 | + selectedConfig = it |
| 47 | + saveConfig(preferences, it) |
| 48 | + }) |
| 49 | + } |
| 50 | + |
| 51 | + Row(horizontalArrangement = Arrangement.spacedBy(5.dp)) { |
| 52 | + ApiButton(configName = STAGING_AC, selectedConfig = selectedConfig, Modifier.weight(1f), onSelect = { |
| 53 | + selectedConfig = it |
| 54 | + saveConfig(preferences, it) |
| 55 | + }) |
| 56 | + ApiButton(configName = PRODUCTION_AC, selectedConfig = selectedConfig, Modifier.weight(1f), onSelect = { |
| 57 | + selectedConfig = it |
| 58 | + saveConfig(preferences, it) |
| 59 | + }) |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +@Composable |
| 66 | +fun ApiInfoRow(label: String, value: String) { |
| 67 | + val localClipboardManager = LocalClipboardManager.current |
| 68 | + |
| 69 | + Row( |
| 70 | + modifier = Modifier |
| 71 | + .background(Color.LightGray, RoundedCornerShape(10.dp)) |
| 72 | + .padding(horizontal = 10.dp, vertical = 5.dp), |
| 73 | + verticalAlignment = Alignment.CenterVertically |
| 74 | + ) { |
| 75 | + Column( |
| 76 | + modifier = Modifier.weight(1f) |
| 77 | + ) { |
| 78 | + Text( |
| 79 | + text = "$label:", |
| 80 | + style = MaterialTheme.typography.labelMedium, |
| 81 | + color = Color.Black |
| 82 | + ) |
| 83 | + Text( |
| 84 | + text = value, |
| 85 | + style = MaterialTheme.typography.bodySmall, |
| 86 | + color = Color.Black |
| 87 | + ) |
| 88 | + } |
| 89 | + |
| 90 | + Button( |
| 91 | + onClick = { copyToClipboard(localClipboardManager, value) }, |
| 92 | + ) { |
| 93 | + Text("Copy") |
| 94 | + } |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | + |
| 99 | +@Composable |
| 100 | +fun ApiButton( |
| 101 | + configName: String, |
| 102 | + selectedConfig: ApiConfiguration, |
| 103 | + modifier: Modifier = Modifier, |
| 104 | + onSelect: (ApiConfiguration) -> Unit |
| 105 | +) { |
| 106 | + val config = apiConfigurationsMap[configName] |
| 107 | + val isSelected = selectedConfig == config |
| 108 | + |
| 109 | + var showDialog by remember { mutableStateOf(false) } |
| 110 | + |
| 111 | + Button( |
| 112 | + onClick = { |
| 113 | + if (config != null) { |
| 114 | + onSelect(config) |
| 115 | + showDialog = true |
| 116 | + } |
| 117 | + }, |
| 118 | + modifier = modifier.padding(0.dp), |
| 119 | + colors = ButtonDefaults.buttonColors( |
| 120 | + containerColor = if (isSelected) Color.Blue else Color.Gray, |
| 121 | + contentColor = Color.White |
| 122 | + ) |
| 123 | + ) { |
| 124 | + Text( |
| 125 | + text = configName, |
| 126 | + maxLines = 1, |
| 127 | + overflow = TextOverflow.Ellipsis, |
| 128 | + textAlign = TextAlign.Center |
| 129 | + ) |
| 130 | + } |
| 131 | + |
| 132 | + if (showDialog) { |
| 133 | + AlertDialog( |
| 134 | + onDismissRequest = { showDialog = false }, |
| 135 | + title = { |
| 136 | + Text(text = "Configuration Changed") |
| 137 | + }, |
| 138 | + text = { |
| 139 | + Text(text = "You need to restart the app for the changes to take effect.") |
| 140 | + }, |
| 141 | + dismissButton = { |
| 142 | + Button(onClick = { showDialog = false }) { |
| 143 | + Text("Cancel") |
| 144 | + } |
| 145 | + }, |
| 146 | + confirmButton = { |
| 147 | + Button(onClick = { exitProcess(0) }) { |
| 148 | + Text("OK") |
| 149 | + } |
| 150 | + } |
| 151 | + ) |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +fun saveConfig(preferences: SharedPreferences, config: ApiConfiguration) { |
| 156 | + val configName = apiConfigurationsMap.entries.firstOrNull { it.value == config }?.key ?: STAGING |
| 157 | + preferences.edit().putString(SELECTED_CONFIG_NAME, configName).apply() |
| 158 | +} |
| 159 | + |
| 160 | +fun copyToClipboard(manager: ClipboardManager, text: String) { |
| 161 | + manager.setText(AnnotatedString(text)) |
| 162 | +} |
0 commit comments