Skip to content

Commit 5ea3f2f

Browse files
authored
[other] ENGMT-xx: improvements to api connectivity (#2)
Now we will show an alert dialogue if branch initialization fails, helping users realize they need to be on VPN Also, defaults to production since more people will be using that
1 parent 6116867 commit 5ea3f2f

File tree

4 files changed

+118
-83
lines changed

4 files changed

+118
-83
lines changed

app/src/main/java/io/branch/branchlinksimulator/ApiConfigManager.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import android.content.SharedPreferences
44

55
object ApiConfigManager {
66
fun loadConfigOrDefault(preferences: SharedPreferences): ApiConfiguration {
7-
return loadConfig(preferences) ?: apiConfigurationsMap[STAGING] ?: ApiConfiguration("N/A", "N/A", "N/A", false)
7+
return loadConfig(preferences) ?: apiConfigurationsMap[PRODUCTION] ?: ApiConfiguration("N/A", "N/A", "N/A", false)
88
}
99

1010
private fun loadConfig(preferences: SharedPreferences): ApiConfiguration? {

app/src/main/java/io/branch/branchlinksimulator/ApiSettingsPanel.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@ import androidx.compose.ui.text.AnnotatedString
1717
import androidx.compose.ui.text.style.TextAlign
1818
import androidx.compose.ui.text.style.TextOverflow
1919
import androidx.compose.ui.unit.dp
20+
import androidx.navigation.NavController
2021
import kotlin.system.exitProcess
2122

2223
const val SELECTED_CONFIG_NAME = "selectedConfigName"
2324
const val PREFERENCES_KEY = "ApiPreferences"
2425

2526
@Composable
26-
fun ApiSettingsPanel() {
27+
fun ApiSettingsPanel(navController: NavController) {
2728
val context = LocalContext.current
2829
val preferences = remember { context.getSharedPreferences(PREFERENCES_KEY, Context.MODE_PRIVATE) }
2930
var selectedConfig by remember { mutableStateOf(ApiConfigManager.loadConfigOrDefault(preferences)) }
@@ -58,6 +59,9 @@ fun ApiSettingsPanel() {
5859
saveConfig(preferences, it)
5960
})
6061
}
62+
RoundedButton(title = "See Requests", icon = R.drawable.branch_badge_all_white) {
63+
navController.navigate("request")
64+
}
6165
}
6266
}
6367
}
@@ -153,7 +157,7 @@ fun ApiButton(
153157
}
154158

155159
fun saveConfig(preferences: SharedPreferences, config: ApiConfiguration) {
156-
val configName = apiConfigurationsMap.entries.firstOrNull { it.value == config }?.key ?: STAGING
160+
val configName = apiConfigurationsMap.entries.firstOrNull { it.value == config }?.key ?: PRODUCTION
157161
preferences.edit().putString(SELECTED_CONFIG_NAME, configName).apply()
158162
}
159163

app/src/main/java/io/branch/branchlinksimulator/BranchLinkSimulatorApplication.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import io.branch.referral.Branch
66
import java.util.UUID
77

88
class BranchLinkSimulatorApplication: Application() {
9-
private lateinit var currentConfig: ApiConfiguration
9+
lateinit var currentConfig: ApiConfiguration
1010
lateinit var roundTripStore: RoundTripStore
1111
private set
1212

app/src/main/java/io/branch/branchlinksimulator/MainActivity.kt

Lines changed: 110 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,14 @@ import androidx.compose.material3.ButtonDefaults
5858
import androidx.compose.material3.Card
5959
import androidx.compose.material3.CardDefaults
6060
import androidx.compose.material3.Icon
61+
import androidx.compose.runtime.State
6162
import androidx.compose.ui.graphics.ColorFilter
6263
import androidx.compose.ui.unit.sp
64+
import androidx.lifecycle.lifecycleScope
65+
import kotlinx.coroutines.launch
66+
import kotlinx.coroutines.suspendCancellableCoroutine
6367
import java.util.UUID
68+
import kotlin.coroutines.resume
6469

6570
var customerEventAlias = ""
6671
var sessionID = ""
@@ -71,9 +76,10 @@ class MainActivity : ComponentActivity() {
7176

7277
override fun onCreate(savedInstanceState: Bundle?) {
7378
super.onCreate(savedInstanceState)
74-
7579
this.roundTripStore = (application as BranchLinkSimulatorApplication).roundTripStore
7680

81+
val initErrorMessageState = mutableStateOf<String?>(null)
82+
7783
setContent {
7884
BranchLinkSimulatorTheme {
7985
Surface(
@@ -83,11 +89,14 @@ class MainActivity : ComponentActivity() {
8389
navController = rememberNavController()
8490
NavHost(navController = navController!!, startDestination = "main") {
8591
composable("main") {
86-
MainContent(navController!!)
92+
MainContent(navController!!, initErrorMessageState)
8793
}
8894

8995
composable("request") {
90-
RoundTripsNavHost(navController = rememberNavController(), roundTripStore = roundTripStore)
96+
RoundTripsNavHost(
97+
navController = rememberNavController(),
98+
roundTripStore = roundTripStore
99+
)
91100
}
92101

93102
composable(
@@ -105,36 +114,47 @@ class MainActivity : ComponentActivity() {
105114
val params = backStackEntry.arguments?.getString("params") ?: ""
106115
DetailScreen(title, parseQueryParams(params))
107116
}
108-
109117
}
110118
}
111119
}
112120
}
113-
}
114121

115-
override fun onStart() {
116-
super.onStart()
122+
lifecycleScope.launch {
123+
val initErrorMessage = initializeBranch()
124+
initErrorMessageState.value = initErrorMessage
125+
}
126+
}
117127

128+
private suspend fun initializeBranch(): String? = suspendCancellableCoroutine { continuation ->
118129
Branch.sessionBuilder(this).withCallback { branchUniversalObject, linkProperties, error ->
119130
if (error != null) {
120-
Log.e("BranchSDK_Tester", "branch init failed. Caused by -" + error.message)
121-
} else {
122-
Log.i("BranchSDK_Tester", "branch init complete!")
123-
if (branchUniversalObject != null && linkProperties != null) {
124-
val title = branchUniversalObject.title ?: "Default Title"
131+
Log.e("BranchSDK_Tester", "Branch init failed. Caused by - ${error.message}")
132+
if (error.message == null)
133+
continuation.resume("unknown error occurred")
134+
var msg = error.message
135+
if ((application as BranchLinkSimulatorApplication).currentConfig.staging)
136+
msg += " Are you connected to vpn?"
137+
continuation.resume(msg)
138+
return@withCallback
139+
}
140+
Log.i("BranchSDK_Tester", "Branch init complete!")
141+
if (branchUniversalObject == null || linkProperties == null) {
142+
continuation.resume(null)
143+
return@withCallback
144+
}
125145

146+
val title = branchUniversalObject.title ?: "Default Title"
126147

127-
val lpQueryString = convertLinkPropertiesToQueryString(linkProperties)
128-
val buoQueryString = convertBUOToQueryString(branchUniversalObject)
148+
val lpQueryString = convertLinkPropertiesToQueryString(linkProperties)
149+
val buoQueryString = convertBUOToQueryString(branchUniversalObject)
129150

130-
val combinedQueryString = listOf(buoQueryString, lpQueryString)
131-
.filterNot { it.isEmpty() }
132-
.joinToString("&")
151+
val combinedQueryString = listOf(buoQueryString, lpQueryString)
152+
.filterNot { it.isEmpty() }
153+
.joinToString("&")
133154

134-
Log.i("BranchSDK_Tester", "Navigating to details/$title/$combinedQueryString")
135-
navController?.navigate("details/$title/$combinedQueryString")
136-
}
137-
}
155+
Log.i("BranchSDK_Tester", "Navigating to details/$title/$combinedQueryString")
156+
navController?.navigate("details/$title/$combinedQueryString")
157+
continuation.resume(null)
138158
}.withData(this.intent.data).init()
139159
}
140160

@@ -154,10 +174,11 @@ class MainActivity : ComponentActivity() {
154174
}
155175

156176
@Composable
157-
fun MainContent(navController: NavController) {
177+
fun MainContent(navController: NavController, initErrorMessageState: State<String?>) {
158178
val context = LocalContext.current
159179
var showAliasDialog by remember { mutableStateOf(false) }
160180
var showSessionIdDialog by remember { mutableStateOf(false) }
181+
var initErrorMessageDialogShown by remember { mutableStateOf(false) }
161182

162183
val sharedPreferences = context.getSharedPreferences("branch_session_prefs", Context.MODE_PRIVATE)
163184
val blsSessionId = sharedPreferences.getString("bls_session_id", null) ?: UUID.randomUUID().toString().also {
@@ -172,6 +193,72 @@ fun MainContent(navController: NavController) {
172193
customerEventAlias = savedAlias
173194
var aliasValue by remember { mutableStateOf(savedAlias) }
174195

196+
if (showAliasDialog) {
197+
AlertDialog(
198+
onDismissRequest = { showAliasDialog = false },
199+
title = { Text("Enter Customer Event Alias") },
200+
text = {
201+
TextField(
202+
value = aliasValue,
203+
onValueChange = { aliasValue = it },
204+
label = { Text("Ex. mainAlias") },
205+
)
206+
},
207+
confirmButton = {
208+
TextButton(onClick = {
209+
sharedPreferences.edit().putString("customer_event_alias", aliasValue).apply()
210+
customerEventAlias = aliasValue
211+
Toast.makeText(context, "Set Customer Event Alias to $aliasValue", Toast.LENGTH_SHORT).show()
212+
showAliasDialog = false
213+
}) {
214+
Text("Save")
215+
}
216+
}
217+
)
218+
}
219+
220+
if (showSessionIdDialog) {
221+
AlertDialog(
222+
onDismissRequest = { showSessionIdDialog = false },
223+
title = { Text("Enter A Session ID") },
224+
text = {
225+
TextField(
226+
value = sessionIdValue,
227+
onValueChange = { sessionIdValue = it },
228+
label = { Text("Ex. testingSession02") },
229+
)
230+
},
231+
confirmButton = {
232+
TextButton(onClick = {
233+
Branch.getInstance().setRequestMetadata("bls_session_id", sessionIdValue)
234+
sharedPreferences.edit().putString("bls_session_id", sessionIdValue).apply()
235+
Toast.makeText(context, "Set App's Session ID to $sessionIdValue", Toast.LENGTH_SHORT).show()
236+
showSessionIdDialog = false
237+
}) {
238+
Text("Save")
239+
}
240+
}
241+
)
242+
}
243+
244+
if (initErrorMessageState.value != null && !initErrorMessageDialogShown) {
245+
AlertDialog(
246+
onDismissRequest = { initErrorMessageDialogShown = true },
247+
title = { Text("Error initializing Branch") },
248+
text = {
249+
Text(initErrorMessageState.value ?: "")
250+
},
251+
confirmButton = {
252+
TextButton(onClick = {
253+
initErrorMessageDialogShown = true
254+
}) {
255+
Text("Ok")
256+
}
257+
}
258+
)
259+
}
260+
261+
175262
LazyColumn(modifier = Modifier.padding(16.dp)) {
176263
item {
177264
Row(verticalAlignment = Alignment.CenterVertically, modifier = Modifier.fillMaxWidth()) {
@@ -199,7 +286,7 @@ fun MainContent(navController: NavController) {
199286
}
200287

201288
item { SectionHeader(title = "Api Settings")}
202-
item { ApiSettingsPanel() }
289+
item { ApiSettingsPanel(navController) }
203290

204291
item { SectionHeader(title = "Event Settings") }
205292
item {
@@ -212,58 +299,6 @@ fun MainContent(navController: NavController) {
212299
showSessionIdDialog = true
213300
}
214301
}
215-
216-
if (showAliasDialog) {
217-
item {
218-
AlertDialog(
219-
onDismissRequest = { showAliasDialog = false },
220-
title = { Text("Enter Customer Event Alias") },
221-
text = {
222-
TextField(
223-
value = aliasValue,
224-
onValueChange = { aliasValue = it },
225-
label = { Text("Ex. mainAlias") },
226-
)
227-
},
228-
confirmButton = {
229-
TextButton(onClick = {
230-
sharedPreferences.edit().putString("customer_event_alias", aliasValue).apply()
231-
customerEventAlias = aliasValue
232-
Toast.makeText(context, "Set Customer Event Alias to $aliasValue", Toast.LENGTH_SHORT).show()
233-
showAliasDialog = false
234-
}) {
235-
Text("Save")
236-
}
237-
}
238-
)
239-
}
240-
}
241-
242-
if (showSessionIdDialog) {
243-
item {
244-
AlertDialog(
245-
onDismissRequest = { showSessionIdDialog = false },
246-
title = { Text("Enter A Session ID") },
247-
text = {
248-
TextField(
249-
value = sessionIdValue,
250-
onValueChange = { sessionIdValue = it },
251-
label = { Text("Ex. testingSession02") },
252-
)
253-
},
254-
confirmButton = {
255-
TextButton(onClick = {
256-
Branch.getInstance().setRequestMetadata("bls_session_id", sessionIdValue)
257-
sharedPreferences.edit().putString("bls_session_id", sessionIdValue).apply()
258-
Toast.makeText(context, "Set App's Session ID to $sessionIdValue", Toast.LENGTH_SHORT).show()
259-
showSessionIdDialog = false
260-
}) {
261-
Text("Save")
262-
}
263-
}
264-
)
265-
}
266-
}
267302
}
268303
}
269304

@@ -344,10 +379,6 @@ fun EventsColumn(navController: NavController, modifier: Modifier = Modifier, co
344379
icon = R.drawable.send_custom
345380
)
346381
{ sendCustomEvent(context) }
347-
348-
RoundedButton(title = "See Requests", icon = R.drawable.branch_badge_all_white) {
349-
navController.navigate("request")
350-
}
351382
}
352383
}
353384

0 commit comments

Comments
 (0)