diff --git a/modules/expo-background-notification-handler/android/src/main/java/expo/modules/backgroundnotificationhandler/BackgroundNotificationHandler.kt b/modules/expo-background-notification-handler/android/src/main/java/expo/modules/backgroundnotificationhandler/BackgroundNotificationHandler.kt index 8ab0ce3dad..886c073b89 100644 --- a/modules/expo-background-notification-handler/android/src/main/java/expo/modules/backgroundnotificationhandler/BackgroundNotificationHandler.kt +++ b/modules/expo-background-notification-handler/android/src/main/java/expo/modules/backgroundnotificationhandler/BackgroundNotificationHandler.kt @@ -2,7 +2,7 @@ package expo.modules.backgroundnotificationhandler import android.content.Context import com.google.firebase.messaging.RemoteMessage -import expo.modules.blueskyswissarmy.sharedprefs.Preferences +import expo.modules.blueskyswissarmy.sharedprefs.SharedPrefs enum class NotificationType( val type: String, @@ -39,7 +39,7 @@ class BackgroundNotificationHandler( } private fun mutateWithChatMessage(remoteMessage: RemoteMessage) { - if (Preferences(context).getBoolean("playSoundChat") == true) { + if (SharedPrefs(context).getBoolean("playSoundChat") == true) { // If oreo or higher if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { remoteMessage.data["channelId"] = "chat-messages" diff --git a/modules/expo-background-notification-handler/android/src/main/java/expo/modules/backgroundnotificationhandler/ExpoBackgroundNotificationHandlerModule.kt b/modules/expo-background-notification-handler/android/src/main/java/expo/modules/backgroundnotificationhandler/ExpoBackgroundNotificationHandlerModule.kt index 2a8ce16913..5b471a0305 100644 --- a/modules/expo-background-notification-handler/android/src/main/java/expo/modules/backgroundnotificationhandler/ExpoBackgroundNotificationHandlerModule.kt +++ b/modules/expo-background-notification-handler/android/src/main/java/expo/modules/backgroundnotificationhandler/ExpoBackgroundNotificationHandlerModule.kt @@ -1,6 +1,7 @@ package expo.modules.backgroundnotificationhandler -import expo.modules.blueskyswissarmy.sharedprefs.Preferences +import android.content.Context +import expo.modules.blueskyswissarmy.sharedprefs.SharedPrefs import expo.modules.kotlin.modules.Module import expo.modules.kotlin.modules.ModuleDefinition @@ -16,11 +17,22 @@ val DEFAULTS = "mutedThreads" to mapOf>(), ) +enum class BadgeType(val rawValue: String) { + Generic("badgeCountGeneric"), + Messages("badgeCountMessages"), +} + +const val INCREMENTED_FOR_KEY = "incremented-for-convos" + class ExpoBackgroundNotificationHandlerModule : Module() { companion object { var isForegrounded = false } + fun getContext(): Context { + return appContext.reactContext ?: throw Error("Context is null") + } + override fun definition() = ModuleDefinition { Name("ExpoBackgroundNotificationHandler") @@ -28,10 +40,10 @@ class ExpoBackgroundNotificationHandlerModule : Module() { OnCreate { val context = appContext.reactContext ?: throw Error("Context is null") DEFAULTS.forEach { (key, value) -> - if (Preferences(context).hasValue(key)) { + if (SharedPrefs(context).hasValue(key)) { return@forEach } - Preferences(context)._setAnyValue(key, value) + SharedPrefs(context)._setAnyValue(key, value) } } @@ -44,21 +56,30 @@ class ExpoBackgroundNotificationHandlerModule : Module() { } AsyncFunction("getPrefsAsync") { - val context = appContext.reactContext ?: throw Error("Context is null") val keys = DEFAULTS.keys - return@AsyncFunction Preferences(context).getValues(keys) + return@AsyncFunction SharedPrefs(getContext()).getValues(keys) } AsyncFunction("resetGenericCountAsync") { - // Not implemented + SharedPrefs(getContext()).setValue(BadgeType.Generic.rawValue, 0f) } - AsyncFunction("maybeIncrementMessagesCountAsync") { - // Not implemented + AsyncFunction("maybeIncrementMessagesCountAsync") { convoId: String -> + val prefs = SharedPrefs(getContext()) + if (!prefs.setContains(INCREMENTED_FOR_KEY, convoId)) { + val curr = prefs.getFloat(BadgeType.Messages.rawValue) ?: 0f + prefs.setValue(BadgeType.Messages.rawValue, curr + 1) + } } - AsyncFunction("maybeDecrementMessagesCountAsync") { - // Not implemented + AsyncFunction("maybeDecrementMessagesCountAsync") { convoId: String -> + val prefs = SharedPrefs(getContext()) + if (prefs.setContains(INCREMENTED_FOR_KEY, convoId)) { + val curr = prefs.getFloat(BadgeType.Messages.rawValue) ?: 0f + if (curr != 0f) { + prefs.setValue(BadgeType.Messages.rawValue, curr - 1) + } + } } } }