-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[other] ENGMT-1882: update android app for staging usage
Adds the ability to switch between apis Adds a request log
- Loading branch information
Showing
11 changed files
with
623 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
app/src/main/java/io/branch/branchlinksimulator/ApiConfigManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package io.branch.branchlinksimulator | ||
|
||
import android.content.SharedPreferences | ||
|
||
object ApiConfigManager { | ||
fun loadConfigOrDefault(preferences: SharedPreferences): ApiConfiguration { | ||
return loadConfig(preferences) ?: apiConfigurationsMap[STAGING] ?: ApiConfiguration("N/A", "N/A", "N/A", false) | ||
} | ||
|
||
private fun loadConfig(preferences: SharedPreferences): ApiConfiguration? { | ||
val configName = preferences.getString(SELECTED_CONFIG_NAME, null) | ||
return configName?.let { apiConfigurationsMap[it] } | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
app/src/main/java/io/branch/branchlinksimulator/ApiConfiguration.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package io.branch.branchlinksimulator | ||
|
||
data class ApiConfiguration( | ||
val branchKey: String, | ||
val apiUrl: String, | ||
val appId: String, | ||
val staging: Boolean | ||
) | ||
|
||
const val STAGING = "Staging" | ||
const val PRODUCTION = "Production" | ||
const val STAGING_AC = "Staging AC" | ||
const val PRODUCTION_AC = "Production AC" | ||
|
||
val apiConfigurationsMap: Map<String, ApiConfiguration> = mapOf( | ||
STAGING_AC to ApiConfiguration( | ||
branchKey = "key_live_juoZrlpzQZvBQbwR33GO5hicszlTGnVT", | ||
apiUrl = "https://protected-api.stage.branch.io/", | ||
appId = "1387589751543976586", | ||
staging = true | ||
), | ||
STAGING to ApiConfiguration( | ||
branchKey = "key_live_plqOidX7fW71Gzt0LdCThkemDEjCbTgx", | ||
apiUrl = "https://api.stage.branch.io/", | ||
appId = "436637608899006753", | ||
staging = true | ||
), | ||
PRODUCTION_AC to ApiConfiguration( | ||
branchKey = "key_live_hshD4wiPK2sSxfkZqkH30ggmyBfmGmD7", | ||
apiUrl = "https://protected-api.branch.io/", | ||
appId = "1284289243903971463", | ||
staging = false | ||
), | ||
PRODUCTION to ApiConfiguration( | ||
branchKey = "key_live_iDiV7ZewvDm9GIYxUnwdFdmmvrc9m3Aw", | ||
apiUrl = "https://api2.branch.io/", | ||
appId = "1364964166783226677", | ||
staging = false | ||
) | ||
) |
162 changes: 162 additions & 0 deletions
162
app/src/main/java/io/branch/branchlinksimulator/ApiSettingsPanel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
package io.branch.branchlinksimulator | ||
|
||
import androidx.compose.ui.platform.ClipboardManager | ||
import android.content.Context | ||
import android.content.SharedPreferences | ||
import androidx.compose.foundation.background | ||
import androidx.compose.foundation.layout.* | ||
import androidx.compose.foundation.shape.RoundedCornerShape | ||
import androidx.compose.material3.* | ||
import androidx.compose.runtime.* | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.platform.LocalClipboardManager | ||
import androidx.compose.ui.platform.LocalContext | ||
import androidx.compose.ui.text.AnnotatedString | ||
import androidx.compose.ui.text.style.TextAlign | ||
import androidx.compose.ui.text.style.TextOverflow | ||
import androidx.compose.ui.unit.dp | ||
import kotlin.system.exitProcess | ||
|
||
const val SELECTED_CONFIG_NAME = "selectedConfigName" | ||
const val PREFERENCES_KEY = "ApiPreferences" | ||
|
||
@Composable | ||
fun ApiSettingsPanel() { | ||
val context = LocalContext.current | ||
val preferences = remember { context.getSharedPreferences(PREFERENCES_KEY, Context.MODE_PRIVATE) } | ||
var selectedConfig by remember { mutableStateOf(ApiConfigManager.loadConfigOrDefault(preferences)) } | ||
|
||
Column( | ||
verticalArrangement = Arrangement.spacedBy(10.dp), | ||
modifier = Modifier.padding(16.dp) | ||
) { | ||
ApiInfoRow(label = "Branch Key", value = selectedConfig.branchKey) | ||
ApiInfoRow(label = "API URL", value = selectedConfig.apiUrl) | ||
ApiInfoRow(label = "App ID", value = selectedConfig.appId) | ||
|
||
Column(horizontalAlignment = Alignment.CenterHorizontally) { | ||
Row(horizontalArrangement = Arrangement.spacedBy(5.dp)) { | ||
ApiButton(configName = STAGING, selectedConfig = selectedConfig, Modifier.weight(1f), onSelect = { | ||
selectedConfig = it | ||
saveConfig(preferences, it) | ||
}) | ||
ApiButton(configName = PRODUCTION, selectedConfig = selectedConfig, Modifier.weight(1f), onSelect = { | ||
selectedConfig = it | ||
saveConfig(preferences, it) | ||
}) | ||
} | ||
|
||
Row(horizontalArrangement = Arrangement.spacedBy(5.dp)) { | ||
ApiButton(configName = STAGING_AC, selectedConfig = selectedConfig, Modifier.weight(1f), onSelect = { | ||
selectedConfig = it | ||
saveConfig(preferences, it) | ||
}) | ||
ApiButton(configName = PRODUCTION_AC, selectedConfig = selectedConfig, Modifier.weight(1f), onSelect = { | ||
selectedConfig = it | ||
saveConfig(preferences, it) | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Composable | ||
fun ApiInfoRow(label: String, value: String) { | ||
val localClipboardManager = LocalClipboardManager.current | ||
|
||
Row( | ||
modifier = Modifier | ||
.background(Color.LightGray, RoundedCornerShape(10.dp)) | ||
.padding(horizontal = 10.dp, vertical = 5.dp), | ||
verticalAlignment = Alignment.CenterVertically | ||
) { | ||
Column( | ||
modifier = Modifier.weight(1f) | ||
) { | ||
Text( | ||
text = "$label:", | ||
style = MaterialTheme.typography.labelMedium, | ||
color = Color.Black | ||
) | ||
Text( | ||
text = value, | ||
style = MaterialTheme.typography.bodySmall, | ||
color = Color.Black | ||
) | ||
} | ||
|
||
Button( | ||
onClick = { copyToClipboard(localClipboardManager, value) }, | ||
) { | ||
Text("Copy") | ||
} | ||
} | ||
} | ||
|
||
|
||
@Composable | ||
fun ApiButton( | ||
configName: String, | ||
selectedConfig: ApiConfiguration, | ||
modifier: Modifier = Modifier, | ||
onSelect: (ApiConfiguration) -> Unit | ||
) { | ||
val config = apiConfigurationsMap[configName] | ||
val isSelected = selectedConfig == config | ||
|
||
var showDialog by remember { mutableStateOf(false) } | ||
|
||
Button( | ||
onClick = { | ||
if (config != null) { | ||
onSelect(config) | ||
showDialog = true | ||
} | ||
}, | ||
modifier = modifier.padding(0.dp), | ||
colors = ButtonDefaults.buttonColors( | ||
containerColor = if (isSelected) Color.Blue else Color.Gray, | ||
contentColor = Color.White | ||
) | ||
) { | ||
Text( | ||
text = configName, | ||
maxLines = 1, | ||
overflow = TextOverflow.Ellipsis, | ||
textAlign = TextAlign.Center | ||
) | ||
} | ||
|
||
if (showDialog) { | ||
AlertDialog( | ||
onDismissRequest = { showDialog = false }, | ||
title = { | ||
Text(text = "Configuration Changed") | ||
}, | ||
text = { | ||
Text(text = "You need to restart the app for the changes to take effect.") | ||
}, | ||
dismissButton = { | ||
Button(onClick = { showDialog = false }) { | ||
Text("Cancel") | ||
} | ||
}, | ||
confirmButton = { | ||
Button(onClick = { exitProcess(0) }) { | ||
Text("OK") | ||
} | ||
} | ||
) | ||
} | ||
} | ||
|
||
fun saveConfig(preferences: SharedPreferences, config: ApiConfiguration) { | ||
val configName = apiConfigurationsMap.entries.firstOrNull { it.value == config }?.key ?: STAGING | ||
preferences.edit().putString(SELECTED_CONFIG_NAME, configName).apply() | ||
} | ||
|
||
fun copyToClipboard(manager: ClipboardManager, text: String) { | ||
manager.setText(AnnotatedString(text)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.