@@ -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 {
@@ -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