-
-
Save hichamboushaba/b529828509e44d867fdb6d4d1aa65952 to your computer and use it in GitHub Desktop.
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