Skip to content

Instantly share code, notes, and snippets.

@marcelpallares
Last active March 21, 2022 22:26
Show Gist options
  • Save marcelpallares/4ad1e09339a1cf39dc8499fd9ec31947 to your computer and use it in GitHub Desktop.
Save marcelpallares/4ad1e09339a1cf39dc8499fd9ec31947 to your computer and use it in GitHub Desktop.
Emitting and collecting on a SharedFlow to answer on a Reddit thread: https://www.reddit.com/r/androiddev/comments/tirxqq/help_with_auth0/
// Add into class AuthCredentialsManager
private val mutableCredentialsFlow = MutableSharedFlow<Credentials>()
val credentialsFlow = mutableCredentialsFlow.asSharedFlow()
suspend fun emitCredentials(credentials: Credentials) {
mutableCredentialsFlow.emit(credentials)
}
fun getCredentials() {
manager.getCredentials(object : Callback<Credentials, CredentialsManagerException> {
override fun onFailure(exception: CredentialsManagerException) {
// Error
}
override fun onSuccess(credentials: Credentials) {
// Use here your coroutine scope
coroutineScope.launch {
emitCredentials(credentials)
}
}
})
}
// Place this where you want to collect from the flow
// Use here your coroutine scope (use viewModelScope if collecting from a ViewModel)
coroutineScope.launch {
// Collect from the flow
credentialsManager.credentialsFlow.collect {
// Do whatever needed with the credentials
println("New credentials received: $it")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment