Skip to content

Commit eda710b

Browse files
committed
[other] ENGMT-xx: improvements to api connectivity
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 eda710b

File tree

4 files changed

+65
-25
lines changed

4 files changed

+65
-25
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ fun ApiButton(
153153
}
154154

155155
fun saveConfig(preferences: SharedPreferences, config: ApiConfiguration) {
156-
val configName = apiConfigurationsMap.entries.firstOrNull { it.value == config }?.key ?: STAGING
156+
val configName = apiConfigurationsMap.entries.firstOrNull { it.value == config }?.key ?: PRODUCTION
157157
preferences.edit().putString(SELECTED_CONFIG_NAME, configName).apply()
158158
}
159159

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: 62 additions & 22 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 {
@@ -239,6 +260,25 @@ fun MainContent(navController: NavController) {
239260
}
240261
}
241262

263+
if (initErrorMessageState.value != null && !initErrorMessageDialogShown) {
264+
item {
265+
AlertDialog(
266+
onDismissRequest = { initErrorMessageDialogShown = true },
267+
title = { Text("Error initializing Branch") },
268+
text = {
269+
Text(initErrorMessageState.value ?: "")
270+
},
271+
confirmButton = {
272+
TextButton(onClick = {
273+
initErrorMessageDialogShown = true
274+
}) {
275+
Text("Ok")
276+
}
277+
}
278+
)
279+
}
280+
}
281+
242282
if (showSessionIdDialog) {
243283
item {
244284
AlertDialog(

0 commit comments

Comments
 (0)