Skip to content

Commit

Permalink
[#64] Add support for getting state as events
Browse files Browse the repository at this point in the history
  • Loading branch information
PatilShreyas committed Mar 3, 2024
1 parent 07a8e6c commit 41960f8
Showing 1 changed file with 24 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@ import kotlinx.coroutines.FlowPreview
import kotlinx.coroutines.Job
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.cancelChildren
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -77,6 +80,24 @@ internal class PermissionWatchmen(
return combineStates(*permissionStates) { MultiplePermissionState(it.toList()) }
}

fun watchStateEvents(permission: String): Flow<PermissionState> {
// Wakeup watchmen if sleeping
wakeUp()
return getPermissionEvent(permission)
}

fun watchMultipleStateEvents(permissions: Array<String>): Flow<MultiplePermissionState> {
// Wakeup watchmen if sleeping
wakeUp()

val permissionStates = permissions
.distinct()
.map { permission -> getPermissionEvent(permission) }
.toTypedArray()

return combine(*permissionStates) { MultiplePermissionState(it.toList()) }
}

fun notifyPermissionsChanged(permissions: Array<String>) {
watchmenScope.launch {
permissions.forEach { permission ->
Expand Down Expand Up @@ -108,6 +129,9 @@ internal class PermissionWatchmen(
}.state
}

private fun getPermissionEvent(permission: String) =
permissionEvents.filter { it.permission == permission }

/**
* Watches for the permission events and updates appropriate state holders of permission
*/
Expand Down

0 comments on commit 41960f8

Please sign in to comment.