From 38c47e13c8a66cd4abc5024c57ae2a82e213fe4a Mon Sep 17 00:00:00 2001 From: Hafiz Date: Tue, 13 Dec 2022 15:32:32 -0500 Subject: [PATCH] Use channel to send notification as read and receive as a flow --- .../android/habitica/helpers/NotificationsManager.kt | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/Habitica/src/main/java/com/habitrpg/android/habitica/helpers/NotificationsManager.kt b/Habitica/src/main/java/com/habitrpg/android/habitica/helpers/NotificationsManager.kt index 803829294..cfbcb6c4b 100644 --- a/Habitica/src/main/java/com/habitrpg/android/habitica/helpers/NotificationsManager.kt +++ b/Habitica/src/main/java/com/habitrpg/android/habitica/helpers/NotificationsManager.kt @@ -6,9 +6,11 @@ import com.habitrpg.android.habitica.data.ApiClient import com.habitrpg.android.habitica.models.tasks.Task import com.habitrpg.common.habitica.models.Notification import kotlinx.coroutines.MainScope +import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.filterNotNull +import kotlinx.coroutines.flow.receiveAsFlow import java.lang.ref.WeakReference import java.util.Date @@ -29,8 +31,8 @@ class MainNotificationsManager: NotificationsManager { private var lastNotificationHandling: Date? = null val _notifications = MutableStateFlow?>(null) - val _displaynotificationEvents = MutableStateFlow(null) - override val displayNotificationEvents: Flow = _displaynotificationEvents.filterNotNull() + val _displaynotificationEvents = Channel() + override val displayNotificationEvents: Flow = _displaynotificationEvents.receiveAsFlow().filterNotNull() init { this.seenNotifications = HashMap() @@ -99,12 +101,11 @@ class MainNotificationsManager: NotificationsManager { Notification.Type.ACHIEVEMENT_GENERIC.type -> true Notification.Type.ACHIEVEMENT_ONBOARDING_COMPLETE.type -> true Notification.Type.LOGIN_INCENTIVE.type -> true + Notification.Type.NEW_MYSTERY_ITEMS.type -> true else -> false } if (notificationDisplayed) { - _displaynotificationEvents.value = it - this.seenNotifications[it.id] = true readNotification(it) } } @@ -114,6 +115,8 @@ class MainNotificationsManager: NotificationsManager { private fun readNotification(notification: Notification) { MainScope().launchCatching { + _displaynotificationEvents.send(notification) + seenNotifications[notification.id] = true apiClient?.get()?.readNotification(notification.id) } }