From e8d72d8a8cb500ae3125d9164e942a2bbcb9c468 Mon Sep 17 00:00:00 2001 From: enzowritescode <1328683+enzowritescode@users.noreply.github.com> Date: Mon, 17 Feb 2025 08:23:19 -0700 Subject: [PATCH] Random Cleanup (#8592) * Consts and helper functions in NotificationHelper.kt * remove dead code in RandomId.kt --- .../java/com/mattermost/helpers/RandomId.kt | 4 -- .../rnutils/helpers/NotificationHelper.kt | 68 ++++++++++++------- 2 files changed, 43 insertions(+), 29 deletions(-) diff --git a/android/app/src/main/java/com/mattermost/helpers/RandomId.kt b/android/app/src/main/java/com/mattermost/helpers/RandomId.kt index 67489eff6..97e9667a3 100644 --- a/android/app/src/main/java/com/mattermost/helpers/RandomId.kt +++ b/android/app/src/main/java/com/mattermost/helpers/RandomId.kt @@ -4,10 +4,6 @@ import java.util.UUID class RandomId { companion object { - private const val alphabet = "0123456789abcdefghijklmnopqrstuvwxyz" - private const val alphabetLength = alphabet.length - private const val idLength = 16 - fun generate(): String { return UUID.randomUUID().toString() } diff --git a/libraries/@mattermost/rnutils/android/src/main/java/com/mattermost/rnutils/helpers/NotificationHelper.kt b/libraries/@mattermost/rnutils/android/src/main/java/com/mattermost/rnutils/helpers/NotificationHelper.kt index a8f345036..05ab745fb 100644 --- a/libraries/@mattermost/rnutils/android/src/main/java/com/mattermost/rnutils/helpers/NotificationHelper.kt +++ b/libraries/@mattermost/rnutils/android/src/main/java/com/mattermost/rnutils/helpers/NotificationHelper.kt @@ -14,6 +14,13 @@ object NotificationHelper { private const val NOTIFICATIONS_IN_GROUP: String = "notificationsInGroup" private const val VERSION_PREFERENCE = "VERSION_PREFERENCE" const val MESSAGE_NOTIFICATION_ID: Int = 435345 + private const val KEY_ROOT_ID = "root_id" + private const val KEY_CHANNEL_ID = "channel_id" + private const val KEY_POST_ID = "post_id" + private const val KEY_IS_CRT_ENABLED = "is_crt_enabled" + private const val KEY_SERVER_URL = "server_url" + + private const val PREF_VERSION = "Version" fun cleanNotificationPreferencesIfNeeded(context: Context) { try { @@ -22,13 +29,13 @@ object NotificationHelper { var storedVersion: String? = null val pSharedPref = context.getSharedPreferences(VERSION_PREFERENCE, Context.MODE_PRIVATE) if (pSharedPref != null) { - storedVersion = pSharedPref.getString("Version", "") + storedVersion = pSharedPref.getString(PREF_VERSION, "") } if (version != storedVersion) { if (pSharedPref != null) { val editor = pSharedPref.edit() - editor.putString("Version", version) + editor.putString(PREF_VERSION, version) editor.apply() } @@ -41,8 +48,8 @@ object NotificationHelper { } fun getNotificationId(notification: Bundle): Int { - val postId = notification.getString("post_id") - val channelId = notification.getString("channel_id") + val postId = getPostId(notification) + val channelId = getChannelId(notification) var notificationId: Int = MESSAGE_NOTIFICATION_ID if (postId != null) { @@ -62,10 +69,10 @@ object NotificationHelper { fun addNotificationToPreferences(context: Context, notificationId: Int, notification: Bundle): Boolean { try { var createSummary = true - val serverUrl = notification.getString("server_url") - val channelId = notification.getString("channel_id") - val rootId = notification.getString("root_id") - val isCRTEnabled = notification.containsKey("is_crt_enabled") && notification.getString("is_crt_enabled") == "true" + val serverUrl = getServerUrl(notification) + val channelId = getChannelId(notification) + val rootId = getRootId(notification) + val isCRTEnabled = isCRTEnabled(notification) val isThreadNotification = isCRTEnabled && !TextUtils.isEmpty(rootId) val groupId = if (isThreadNotification) rootId else channelId @@ -103,10 +110,10 @@ object NotificationHelper { } fun dismissNotification(context: Context, notification: Bundle) { - val isCRTEnabled = notification.containsKey("is_crt_enabled") && notification.getString("is_crt_enabled") == "true" - val serverUrl = notification.getString("server_url") - val channelId = notification.getString("channel_id") - val rootId = notification.getString("root_id") + val isCRTEnabled = isCRTEnabled(notification) + val serverUrl = getServerUrl(notification) + val channelId = getChannelId(notification) + val rootId = getRootId(notification) val notificationId = getNotificationId(notification) @@ -130,9 +137,9 @@ object NotificationHelper { for (status in statusNotifications) { val bundle = status.notification.extras hasMore = if (isThreadNotification) { - bundle.containsKey("root_id") && bundle.getString("root_id") == rootId + bundle.containsKey(KEY_ROOT_ID) && getRootId(bundle) == rootId } else { - bundle.containsKey("channel_id") && bundle.getString("channel_id") == channelId + bundle.containsKey(KEY_CHANNEL_ID) && getChannelId(bundle) == channelId } if (hasMore) break } @@ -167,9 +174,9 @@ object NotificationHelper { for (sbn in notifications) { val n = sbn.notification val bundle = n.extras - val cId = bundle.getString("channel_id") - val rootId = bundle.getString("root_id") - val isCRTEnabled = bundle.containsKey("is_crt_enabled") && bundle.getString("is_crt_enabled") == "true" + val cId = getChannelId(bundle) + val rootId = getRootId(bundle) + val isCRTEnabled = isCRTEnabled(bundle) val skipThreadNotification = isCRTEnabled && !TextUtils.isEmpty(rootId) if (cId == channelId && !skipThreadNotification) { notificationManager.cancel(sbn.id) @@ -186,14 +193,14 @@ object NotificationHelper { for (sbn in notifications) { val n = sbn.notification val bundle = n.extras - val rootId = bundle.getString("root_id") - val postId = bundle.getString("post_id") + val rootId = getRootId(bundle) + val postId = getPostId(bundle) if (rootId == threadId) { notificationManager.cancel(sbn.id) } if (postId == threadId) { - val channelId = bundle.getString("channel_id") + val channelId = getChannelId(bundle) val id = sbn.id if (notificationsInServer != null && channelId != null) { val notificationsInChannel = notificationsInServer.optJSONObject(channelId) @@ -217,6 +224,8 @@ object NotificationHelper { } } + private fun getPostId(notification: Bundle) = notification.getString(KEY_POST_ID) + fun removeServerNotifications(context: Context, serverUrl: String) { val notificationManager = NotificationManagerCompat.from(context) val notificationsPerServer = loadMap(context) @@ -226,7 +235,7 @@ object NotificationHelper { for (sbn in notifications) { val n = sbn.notification val bundle = n.extras - val url = bundle.getString("server_url") + val url = getServerUrl(bundle) if (url == serverUrl) { notificationManager.cancel(sbn.id) } @@ -234,11 +243,11 @@ object NotificationHelper { } fun clearChannelOrThreadNotifications(context: Context, notification: Bundle) { - val serverUrl = notification.getString("server_url") - val channelId = notification.getString("channel_id") - val rootId = notification.getString("root_id") + val serverUrl = getServerUrl(notification) + val channelId = getChannelId(notification) + val rootId = getRootId(notification) if (channelId != null) { - val isCRTEnabled = notification.containsKey("is_crt_enabled") && notification.getString("is_crt_enabled") == "true" + val isCRTEnabled = isCRTEnabled(notification) // rootId is available only when CRT is enabled & clearing the thread val isClearThread = isCRTEnabled && !TextUtils.isEmpty(rootId) @@ -250,6 +259,15 @@ object NotificationHelper { } } + private fun getRootId(notification: Bundle) = notification.getString(KEY_ROOT_ID) + + private fun getChannelId(notification: Bundle) = notification.getString(KEY_CHANNEL_ID) + + private fun getServerUrl(notification: Bundle) = notification.getString(KEY_SERVER_URL) + + private fun isCRTEnabled(bundle: Bundle) = + bundle.containsKey(KEY_IS_CRT_ENABLED) && bundle.getString(KEY_IS_CRT_ENABLED) == "true" + /** * Map Structure