david dereba
01/18/2025, 3:27 AMMain Activity
class MainActivity : ComponentActivity() {
private val viewmodel1: MainViewmodel by inject()
private val splashScreen = viewmodel1.splashCondition
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
WindowCompat.setDecorFitsSystemWindows(window, false)
installSplashScreen().apply {
setKeepOnScreenCondition {
splashScreen
}
}
enableEdgeToEdge()
setContent {
EMarketplaceV2Theme {
val isSystemInDarkMode = isSystemInDarkTheme()
val systemController = rememberSystemUiController()
SideEffect {
systemController.setSystemBarsColor(
color = Color.Transparent,
darkIcons = !isSystemInDarkMode
)
}
Box(modifier = Modifier.background(color = MaterialTheme.colorScheme.background)) {
// Inject the ViewModel here using koinViewModel
val viewModel = koinViewModel<MainViewmodel>()
val startDestination = viewModel.startDestination
val tokenExpired by viewModel.tokenExpired.collectAsState()
LaunchedEffect(tokenExpired) {
if (tokenExpired) {
Toast.makeText(
this@MainActivity,
"Session Expired. Please login again.",
Toast.LENGTH_LONG
).show()
}
}
MainNavGraph(startDestination = startDestination, tokenExpired = tokenExpired)
}
}
}
}
}
my Main viewmodel where am retrieving my splashscreen condition
class MainViewmodel(
private val appEntryUseCases: AppEntryUseCases,
private val authEventBus: AuthEventBus
) : ViewModel() {
var splashCondition by mutableStateOf(true)
private set
var startDestination by mutableStateOf<Routes>(Routes.AppStartNavigationRoutes)
private set
// Add state flow to handle token expiration across the app
private val _tokenExpired = MutableStateFlow(false)
val tokenExpired = _tokenExpired.asStateFlow()
init {
observeAppEntry()
observeAuthEvents()
}
private fun observeAppEntry() {
appEntryUseCases.readAppEntry().onEach { shouldStartFromLoginScreen ->
startDestination = if (shouldStartFromLoginScreen) {
Routes.AuthNavigationRoutes
} else {
Routes.AppStartNavigationRoutes
}
delay(300)
splashCondition = false // Update the splash condition
}.launchIn(viewModelScope)
}
private fun observeAuthEvents() {
viewModelScope.launch {
authEventBus.authEvents.collect {
_tokenExpired.value = true
startDestination = Routes.AuthNavigationRoutes
}
}
}
}
kindly help me address the issueZach Klippenstein (he/him) [MOD]
01/18/2025, 4:38 PMZach Klippenstein (he/him) [MOD]
01/18/2025, 4:40 PMdavid dereba
01/18/2025, 4:42 PM