hello , Am trying to inject a viewmodel with koin ...
# compose
d
hello , Am trying to inject a viewmodel with koin on the Main Activity and then retrieve the splashscreen condition so that I can install splash screen. however , I know I cannot inject viemodel on main actvity because it is not a composable. let me share the impl.
Copy code
Main 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
Copy code
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 issue
đź§µ 7
stackoverflow 2
z
Please keep code snippets more than a few lines to the thread.
I don’t know koin specifics but I know it was built before compose existed, and so it definitely doesn’t require compose to do injection. Whatever reason you’re having trouble injecting into your main activity shouldn’t have anything to do with compose.
d
@Zach Klippenstein (he/him) [MOD] well noted. thanks for the feedback