Skip to content

Instantly share code, notes, and snippets.

@hichamboushaba
Last active November 16, 2021 14:17
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