@@ -65,7 +65,8 @@ import androidx.compose.ui.unit.sp
6565import io.branch.referral.PrefHelper
6666import java.util.UUID
6767
68- var customerEventAlias = " alias"
68+ var customerEventAlias = " "
69+ var sessionID = " "
6970
7071class MainActivity : ComponentActivity () {
7172 private var navController: NavHostController ? = null
@@ -155,14 +156,20 @@ fun MainContent(navController: NavController) {
155156 var showSessionIdDialog by remember { mutableStateOf(false ) }
156157
157158 var textFieldValue by remember { mutableStateOf(PrefHelper .getInstance(context).apiBaseUrl) }
158- var aliasValue by remember { mutableStateOf(" " ) }
159159
160160 val sharedPreferences = context.getSharedPreferences(" branch_session_prefs" , Context .MODE_PRIVATE )
161161 val blsSessionId = sharedPreferences.getString(" bls_session_id" , null ) ? : UUID .randomUUID().toString().also {
162162 sharedPreferences.edit().putString(" bls_session_id" , it).apply ()
163163 }
164+ sessionID = blsSessionId
164165 var sessionIdValue by remember { mutableStateOf(blsSessionId) }
165166
167+ val savedAlias = sharedPreferences.getString(" customer_event_alias" , null ) ? : " " .also {
168+ sharedPreferences.edit().putString(" customer_event_alias" , it).apply ()
169+ }
170+ customerEventAlias = savedAlias
171+ var aliasValue by remember { mutableStateOf(savedAlias) }
172+
166173 Column (modifier = Modifier .padding(16 .dp)) {
167174 Row (verticalAlignment = Alignment .CenterVertically , modifier = Modifier .fillMaxWidth()) {
168175 Image (
@@ -232,6 +239,7 @@ fun MainContent(navController: NavController) {
232239 },
233240 confirmButton = {
234241 TextButton (onClick = {
242+ sharedPreferences.edit().putString(" customer_event_alias" , aliasValue).apply ()
235243 customerEventAlias = aliasValue
236244 Toast .makeText(context, " Set Customer Event Alias to $aliasValue " , Toast .LENGTH_SHORT ).show()
237245 showAliasDialog = false
@@ -301,12 +309,46 @@ fun ButtonRow(navController: NavController, modifier: Modifier = Modifier) {
301309
302310@Composable
303311fun FunctionButtonRow (modifier : Modifier = Modifier , context : android.content.Context ) {
312+ val showDialog = remember { mutableStateOf(false ) }
313+
314+ fun sendEvent (eventType : String ) {
315+ when (eventType) {
316+ " Purchase" -> sendStandardEvent(context, BRANCH_STANDARD_EVENT .PURCHASE )
317+ " Add to Cart" -> sendStandardEvent(context, BRANCH_STANDARD_EVENT .ADD_TO_CART )
318+ " Login" -> sendStandardEvent(context, BRANCH_STANDARD_EVENT .LOGIN )
319+ " Search" -> sendStandardEvent(context, BRANCH_STANDARD_EVENT .SEARCH )
320+ " Share" -> sendStandardEvent(context, BRANCH_STANDARD_EVENT .SHARE )
321+ }
322+ showDialog.value = false
323+ }
324+
304325 Column (modifier = modifier) {
305326 RoundedButton (
306327 title = " Send Standard Event" ,
307328 icon = R .drawable.send
308329 )
309- { sendStandardEvent(context) }
330+ { showDialog.value = true }
331+ if (showDialog.value) {
332+ // Dialog with event options
333+ AlertDialog (
334+ onDismissRequest = { showDialog.value = false },
335+ title = { Text (" Choose Event Type" ) },
336+ text = {
337+ Column {
338+ Button (onClick = { sendEvent(" Purchase" ) }) { Text (" Purchase" ) }
339+ Button (onClick = { sendEvent(" Add to Cart" ) }) { Text (" Add to Cart" ) }
340+ Button (onClick = { sendEvent(" Login" ) }) { Text (" Login" ) }
341+ Button (onClick = { sendEvent(" Search" ) }) { Text (" Search" ) }
342+ Button (onClick = { sendEvent(" Share" ) }) { Text (" Share" ) }
343+ }
344+ },
345+ confirmButton = {
346+ Button (onClick = { showDialog.value = false }) {
347+ Text (" Cancel" )
348+ }
349+ }
350+ )
351+ }
310352
311353 RoundedButton (
312354 title = " Send Custom Event" ,
@@ -316,10 +358,13 @@ fun FunctionButtonRow(modifier: Modifier = Modifier, context: android.content.Co
316358 }
317359}
318360
319- fun sendStandardEvent (context : android.content.Context ) {
320- BranchEvent (BRANCH_STANDARD_EVENT .SEARCH ).setCustomerEventAlias(customerEventAlias).logEvent(context, object : BranchLogEventCallback {
361+ fun sendStandardEvent (context : Context , event : BRANCH_STANDARD_EVENT ) {
362+ BranchEvent (event)
363+ .setCustomerEventAlias(customerEventAlias)
364+ .addCustomDataProperty(" bls_session_id" , sessionID)
365+ .logEvent(context, object : BranchLogEventCallback {
321366 override fun onSuccess (responseCode : Int ) {
322- Toast .makeText(context, " Sent Standard Event!" , Toast .LENGTH_SHORT ).show()
367+ Toast .makeText(context, " Sent ${event.getName()} Event!" , Toast .LENGTH_SHORT ).show()
323368 }
324369
325370 override fun onFailure (e : Exception ) {
@@ -328,7 +373,10 @@ fun sendStandardEvent(context: android.content.Context) {
328373 })
329374}
330375fun sendCustomEvent (context : android.content.Context ) {
331- BranchEvent (" My Custom Event" ).setCustomerEventAlias(customerEventAlias).logEvent(context, object : BranchLogEventCallback {
376+ BranchEvent (" My Custom Event" )
377+ .setCustomerEventAlias(customerEventAlias)
378+ .addCustomDataProperty(" bls_session_id" , sessionID)
379+ .logEvent(context, object : BranchLogEventCallback {
332380 override fun onSuccess (responseCode : Int ) {
333381 Toast .makeText(context, " Sent Custom Event!" , Toast .LENGTH_SHORT ).show()
334382 }
0 commit comments