@@ -58,9 +58,14 @@ import androidx.compose.material3.ButtonDefaults
5858import androidx.compose.material3.Card
5959import androidx.compose.material3.CardDefaults
6060import androidx.compose.material3.Icon
61+ import androidx.compose.runtime.State
6162import androidx.compose.ui.graphics.ColorFilter
6263import androidx.compose.ui.unit.sp
64+ import androidx.lifecycle.lifecycleScope
65+ import kotlinx.coroutines.launch
66+ import kotlinx.coroutines.suspendCancellableCoroutine
6367import java.util.UUID
68+ import kotlin.coroutines.resume
6469
6570var customerEventAlias = " "
6671var 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