Skip to content

Instantly share code, notes, and snippets.

@hichamboushaba
Last active November 16, 2021 14:17
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 hichamboushaba/b529828509e44d867fdb6d4d1aa65952 to your computer and use it in GitHub Desktop.
Save hichamboushaba/b529828509e44d867fdb6d4d1aa65952 to your computer and use it in GitHub Desktop.
class ViewModel {
private val _events = Channel<Event>(capacity = Channel.BUFFERED)
val events = _events.receiveAsFlow()
.whenAtLeast(Lifecycle.Started)
private val locationPermissionGranted = MutableStateFlow(false)
fun onPermissionGranted(granted: Boolean, shouldShowRationale: Boolean) {
if (!granted) {
if (shouldShowRationale) {
_events.trySend(Event.ShowPermissionRationale)
} else {
_events.trySend(Event.ShowPermissionSnackBar)
}
}
if (granted || !shouldShowRationale) {
locationPermissionGranted.value = granted
}
}
sealed class Event {
object RequestLocationPermission : Event()
object ShowPermissionRationale : Event()
object ShowPermissionSnackBar : Event()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment