Last active
March 21, 2022 22:26
-
-
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/
This file contains 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
// 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) | |
} | |
} | |
}) | |
} |
This file contains 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
// 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