Skip to content

Commit

Permalink
[other] ENGMT-1882: update android app for staging usage (#1)
Browse files Browse the repository at this point in the history
Adds the ability to switch between apis

Adds a request log
  • Loading branch information
bredmond5 authored Dec 2, 2024
1 parent e98d011 commit 6116867
Show file tree
Hide file tree
Showing 21 changed files with 624 additions and 214 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,9 @@
.cxx
local.properties
*.aab
.idea/
app/src/main/res/drawable/
.kotlin/errors/
app/release
app/src/main/ic_launcher-playstore.png
app/src/main/res/
3 changes: 0 additions & 3 deletions .idea/.gitignore

This file was deleted.

1 change: 0 additions & 1 deletion .idea/.name

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/compiler.xml

This file was deleted.

10 changes: 0 additions & 10 deletions .idea/deploymentTargetDropDown.xml

This file was deleted.

19 changes: 0 additions & 19 deletions .idea/gradle.xml

This file was deleted.

41 changes: 0 additions & 41 deletions .idea/inspectionProfiles/Project_Default.xml

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/kotlinc.xml

This file was deleted.

10 changes: 0 additions & 10 deletions .idea/migrations.xml

This file was deleted.

9 changes: 0 additions & 9 deletions .idea/misc.xml

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/vcs.xml

This file was deleted.

4 changes: 3 additions & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
plugins {
id("com.android.application")
id("org.jetbrains.kotlin.android")
id("kotlinx-serialization")
}

android {
Expand Down Expand Up @@ -69,7 +70,8 @@ dependencies {
debugImplementation("androidx.compose.ui:ui-test-manifest")
implementation("androidx.navigation:navigation-compose:2.7.7")

implementation("io.branch.sdk.android:library:5.9.0")
implementation("io.branch.sdk.android:library:5.14.0")
implementation("com.google.android.gms:play-services-ads-identifier:18.0.1")
implementation("com.android.installreferrer:installreferrer:2.2")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.3")
}
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] }
}
}
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 app/src/main/java/io/branch/branchlinksimulator/ApiSettingsPanel.kt
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) MaterialTheme.colorScheme.primary 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))
}
Loading

0 comments on commit 6116867

Please sign in to comment.