Last active
December 9, 2022 17:53
-
-
Save chenzhang2006/fc5474c6acb437c33a2d4a81a3dc923a to your computer and use it in GitHub Desktop.
StateFlow to observe LoginState
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private val _loginState = MutableStateFlow<Boolean>(false) | |
val loginStateFlow: StateFlow<Boolean> = _loginState.asStateFlow() | |
fun loggedIn() { | |
_loginState.value = true | |
} | |
fun loggedOut() { | |
_loginState.value = false | |
} | |
// Call-site | |
suspend fun observeLoginState() = coroutineScope { // #1 | |
repository.loginStateFlow.collect { ... } // #2, #3 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment