Skip to content

Instantly share code, notes, and snippets.

@markchristopherng
Last active August 22, 2022 02:30
Show Gist options
  • Save markchristopherng/f932143f97c99363c883de3fa9df203c to your computer and use it in GitHub Desktop.
Save markchristopherng/f932143f97c99363c883de3fa9df203c to your computer and use it in GitHub Desktop.
composable(
// Route information
route = WearScreen.DETAILS.name + "/{trackNumber}",
arguments = listOf(navArgument("trackNumber") { type = NavType.StringType })
) { backStackEntry ->
val trackNumber = backStackEntry.arguments?.getString("trackNumber") ?: ""
// Retrieve View model
val trackDetailsViewModel = viewModel<TrackDetailsViewModel>()
// Call API to retrieve data
LaunchedEffect(
key1 = true,
block = {
trackDetailsViewModel.initialise(trackNumber)
trackDetailsViewModel.getTrackDetails()
trackDetailsViewModel.sendAnalytics()
baseViewModel.refreshLoginStatus()
}
)
// State of LiveData
val trackDetailsState = trackDetailsViewModel.trackWearItem.observeAsState()
val dataState = trackDetailsViewModel.trackWearItem.state.observeAsState()
val authenticatedState = baseViewModel.loggedInStatus.observeAsState()
val trackOnPhoneOpenedState = trackDetailsViewModel.openPhoneRequest.observeAsState()
val authenticated = authenticatedState.value?.getContentIfNotHandled() ?: true
val trackOnPhoneOpened = trackOnPhoneOpenedState.value?.getContentIfNotHandled() ?: false
// Render screen
trackDetailsState.value?.let {
DetailsScreen(
trackDetails = it,
dataState = dataState.value,
authenticated = authenticated,
trackOnPhoneOpened = trackOnPhoneOpened,
onQrCodeClick = { qrCodes ->
navController.navigate(WearScreen.QRCODE.name + "/${trackDetailsViewModel.getSafeEncodedQrCodeNavParam(qrCodes)}")
},
updateAlias = { alias ->
trackDetailsViewModel.updateAlias(alias)
},
requestTrackOnPhone = {
trackDetailsViewModel.requestTrackOnPhone()
},
onError = { error: Throwable, retry: Boolean ->
val errorType = WearErrorType.from(error)
navController.navigate(WearScreen.ERROR.name + "?error=$errorType&retry=$retry")
},
onUnAuthenticated = {
navController.navigate(WearScreen.LOGIN.name)
}
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment