Skip to content

Commit

Permalink
[other] ENGMT-1882: update android app for staging usage
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 committed Nov 27, 2024
1 parent e98d011 commit 14cdba2
Show file tree
Hide file tree
Showing 11 changed files with 623 additions and 102 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/
2 changes: 2 additions & 0 deletions 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 @@ -72,4 +73,5 @@ dependencies {
implementation("io.branch.sdk.android:library:5.9.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) 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))
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,22 @@ import io.branch.referral.Branch
import java.util.UUID

class BranchLinkSimulatorApplication: Application() {
private lateinit var currentConfig: ApiConfiguration
lateinit var roundTripStore: RoundTripStore
private set

override fun onCreate() {
super.onCreate()

// Branch logging for debugging
Branch.enableLogging()
Branch.setAPIUrl("https://protected-api.branch.io/")
val preferences = getSharedPreferences(PREFERENCES_KEY, Context.MODE_PRIVATE)
currentConfig = ApiConfigManager.loadConfigOrDefault(preferences)

Branch.setAPIUrl(currentConfig.apiUrl)

roundTripStore = RoundTripStore(this)
Branch.enableLogging(roundTripStore)
// Branch object initialization
Branch.getAutoInstance(this)
Branch.getAutoInstance(this, currentConfig.branchKey)

// Retrieve or create the bls_session_id
val sharedPreferences = getSharedPreferences("branch_session_prefs", Context.MODE_PRIVATE)
Expand All @@ -26,6 +33,5 @@ class BranchLinkSimulatorApplication: Application() {

// Set the bls_session_id in Branch request metadata
Branch.getInstance().setRequestMetadata("bls_session_id", blsSessionId)

}
}
Loading

0 comments on commit 14cdba2

Please sign in to comment.