Skip to content

Instantly share code, notes, and snippets.

@chenzhang2006
Last active December 9, 2022 17:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chenzhang2006/fc5474c6acb437c33a2d4a81a3dc923a to your computer and use it in GitHub Desktop.
Save chenzhang2006/fc5474c6acb437c33a2d4a81a3dc923a to your computer and use it in GitHub Desktop.
StateFlow to observe LoginState
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