Skip to content

Commit

Permalink
Get status on init
Browse files Browse the repository at this point in the history
We don't cache enabled. We could do so if we want to fire the changed handler if the user changed settings between app sessions
  • Loading branch information
emawby committed Jun 6, 2024
1 parent a1cd957 commit e8277d7
Showing 1 changed file with 3 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ internal class NotificationPermissionController(
INotificationPermissionController {
private val waiter = WaiterWithValue<Boolean>()
private val events = EventProducer<INotificationPermissionChangedHandler>()
private var _enabled = false // Should be a cached value
private var _enabled: Boolean
private val coroutineScope = CoroutineScope(newSingleThreadContext(name = "NotificationPermissionController"))

override val canRequestPermission: Boolean
Expand All @@ -70,6 +70,7 @@ internal class NotificationPermissionController(
)!!

init {
_enabled = notificationsEnabled()
_requestPermission.registerAsCallback(PERMISSION_TYPE, this)
coroutineScope.launch {// Should this be Android version restricted?
pollForPermission()
Expand All @@ -79,7 +80,7 @@ internal class NotificationPermissionController(
private suspend fun pollForPermission() {
while (true) {
val enabled = this.notificationsEnabled()
if (_enabled != enabled) { // The permission has changed without prompting through OneSignal
if (_enabled != enabled) { // If the permission has changed without prompting through OneSignal
_enabled = enabled
events.fire { it.onNotificationPermissionChanged(enabled) }
}
Expand Down

0 comments on commit e8277d7

Please sign in to comment.