Эх сурвалжийг харах

Reduce code duplication in NotificationUtils

Signed-off-by: Dariusz Olszewski <starypatyk@users.noreply.github.com>
Dariusz Olszewski 3 жил өмнө
parent
commit
2f73433170

+ 29 - 58
app/src/main/java/com/nextcloud/talk/utils/NotificationUtils.kt

@@ -181,7 +181,15 @@ object NotificationUtils {
         return null
     }
 
-    fun cancelAllNotificationsForAccount(context: Context?, conversationUser: UserEntity) {
+    private inline fun scanNotifications(
+        context: Context?,
+        conversationUser: UserEntity,
+        callback: (
+            notificationManager: NotificationManager,
+            statusBarNotification: StatusBarNotification,
+            notification: Notification
+        ) -> Unit
+    ) {
         if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M || conversationUser.id == -1L || context == null) {
             return
         }
@@ -193,33 +201,26 @@ object NotificationUtils {
         for (statusBarNotification in statusBarNotifications) {
             notification = statusBarNotification.notification
 
-            if (notification != null && !notification.extras.isEmpty) {
-                if (conversationUser.id == notification.extras.getLong(BundleKeys.KEY_INTERNAL_USER_ID)) {
-                    notificationManager.cancel(statusBarNotification.id)
-                }
+            if (
+                notification != null &&
+                !notification.extras.isEmpty &&
+                conversationUser.id == notification.extras.getLong(BundleKeys.KEY_INTERNAL_USER_ID)
+            ) {
+                callback(notificationManager, statusBarNotification, notification)
             }
         }
     }
 
-    fun cancelExistingNotificationWithId(context: Context?, conversationUser: UserEntity, notificationId: Long?) {
-        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M || conversationUser.id == -1L || context == null) {
-            return
+    fun cancelAllNotificationsForAccount(context: Context?, conversationUser: UserEntity) {
+        scanNotifications(context, conversationUser) { notificationManager, statusBarNotification, _ ->
+            notificationManager.cancel(statusBarNotification.id)
         }
+    }
 
-        val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
-
-        val statusBarNotifications = notificationManager.activeNotifications
-        var notification: Notification?
-        for (statusBarNotification in statusBarNotifications) {
-            notification = statusBarNotification.notification
-
-            if (notification != null && !notification.extras.isEmpty) {
-                if (
-                    conversationUser.id == notification.extras.getLong(BundleKeys.KEY_INTERNAL_USER_ID) &&
-                    notificationId == notification.extras.getLong(BundleKeys.KEY_NOTIFICATION_ID)
-                ) {
-                    notificationManager.cancel(statusBarNotification.id)
-                }
+    fun cancelExistingNotificationWithId(context: Context?, conversationUser: UserEntity, notificationId: Long?) {
+        scanNotifications(context, conversationUser) { notificationManager, statusBarNotification, notification ->
+            if (notificationId == notification.extras.getLong(BundleKeys.KEY_NOTIFICATION_ID)) {
+                notificationManager.cancel(statusBarNotification.id)
             }
         }
     }
@@ -229,27 +230,11 @@ object NotificationUtils {
         conversationUser: UserEntity,
         roomTokenOrId: String
     ): StatusBarNotification? {
-        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M || conversationUser.id == -1L || context == null) {
-            return null
-        }
-
-        val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
-
-        val statusBarNotifications = notificationManager.activeNotifications
-        var notification: Notification?
-        for (statusBarNotification in statusBarNotifications) {
-            notification = statusBarNotification.notification
-
-            if (notification != null && !notification.extras.isEmpty) {
-                if (
-                    conversationUser.id == notification.extras.getLong(BundleKeys.KEY_INTERNAL_USER_ID) &&
-                    roomTokenOrId == statusBarNotification.notification.extras.getString(BundleKeys.KEY_ROOM_TOKEN)
-                ) {
-                    return statusBarNotification
-                }
+        scanNotifications(context, conversationUser) { _, statusBarNotification, notification ->
+            if (roomTokenOrId == notification.extras.getString(BundleKeys.KEY_ROOM_TOKEN)) {
+                return statusBarNotification
             }
         }
-
         return null
     }
 
@@ -258,23 +243,9 @@ object NotificationUtils {
         conversationUser: UserEntity,
         roomTokenOrId: String
     ) {
-        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M || conversationUser.id == -1L || context == null) {
-            return
-        }
-
-        val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
-
-        val statusBarNotifications = notificationManager.activeNotifications
-        var notification: Notification?
-        for (statusBarNotification in statusBarNotifications) {
-            notification = statusBarNotification.notification
-
-            if (notification != null && !notification.extras.isEmpty) {
-                if (conversationUser.id == notification.extras.getLong(BundleKeys.KEY_INTERNAL_USER_ID) &&
-                    roomTokenOrId == statusBarNotification.notification.extras.getString(BundleKeys.KEY_ROOM_TOKEN)
-                ) {
-                    notificationManager.cancel(statusBarNotification.id)
-                }
+        scanNotifications(context, conversationUser) { notificationManager, statusBarNotification, notification ->
+            if (roomTokenOrId == notification.extras.getString(BundleKeys.KEY_ROOM_TOKEN)) {
+                notificationManager.cancel(statusBarNotification.id)
             }
         }
     }