/ViewModel.kt Secret
Last active
November 16, 2021 14:17
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
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