I'm using the new android Splash Screen with Jetpack Compose. I have a problem with my application logic. It has a presentation screen (onboarding), and on its last page, it has a button to confirm, in this confirmation I save a boolean variable as true in the DataStore.
The logic I did, my Splash screen is stuck and I don't know where I could be going wrong. follow the codes:
MainActivity
@AndroidEntryPoint
class MainActivity : ComponentActivity() {
private val viewModel: SplashViewModel by viewModels()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
var keepSplashOpened = viewModel.state.keepSplashOpened
installSplashScreen().setKeepOnScreenCondition {
keepSplashOpened
}
setContent {
val window = rememberWindowSizeClass()
KippTheme(window) {
val graph = viewModel.state.graphs
RootNavigationGraph(
graph = graph,
onDataLoaded = {
keepSplashOpened = false
}
)
}
}
}
}
SplashViewModel
@HiltViewModel
class SplashViewModel @Inject constructor(
private val readOnBoardingUseCase: ReadOnBoardingUseCase
) : ViewModel() {
var state by mutableStateOf(SplashState())
init {
viewModelScope.launch {
readOnBoardingUseCase().collect { completed ->
state = if (completed) {
state.copy(graphs = Graph.HOME, keepSplashOpened = false)
} else {
state.copy(graphs = Graph.WELCOME, keepSplashOpened = false)
}
}
}
}
}
SplashState
data class SplashState(
val graphs: String? = null,
var keepSplashOpened: Boolean = true
)
Any personal suggestions? I'm new to posting here.
the above code is an example and like this is how i tried to do so far.
The problem is here
var keepSplashOpened = viewModel.state.keepSplashOpened
installSplashScreen().setKeepOnScreenCondition {
keepSplashOpened
}
The value of keepSplashOpened
is captured only one-time and doesn't get updated. To fix it, change the code to
installSplashScreen().setKeepOnScreenCondition {
viewModel.state.keepSplashOpened
}