Bladeren bron

delete notifications if corresponding chat gets deleted

cyberta 4 jaren geleden
bovenliggende
commit
a71d9e1036
2 gewijzigde bestanden met toevoegingen van 14 en 5 verwijderingen
  1. 13 5
      deltachat-ios/Helper/NotificationManager.swift
  2. 1 0
      deltachat-ios/ViewModel/ChatListViewModel.swift

+ 13 - 5
deltachat-ios/Helper/NotificationManager.swift

@@ -29,6 +29,14 @@ public class NotificationManager {
         nc.removeAllDeliveredNotifications()
         nc.removeAllPendingNotificationRequests()
     }
+    
+    public static func removeNotificationsForChat(chatId: Int) {
+        DispatchQueue.global(qos: .background).async {
+            NotificationManager.removePendingNotificationsFor(chatId: chatId)
+            NotificationManager.removeDeliveredNotificationsFor(chatId: chatId)
+            NotificationManager.updateApplicationIconBadge(reset: false)
+        }
+    }
 
     private func initIncomingMsgsObserver() {
         incomingMsgObserver = NotificationCenter.default.addObserver(
@@ -92,20 +100,20 @@ public class NotificationManager {
             forName: dcMsgsNoticed,
             object: nil, queue: OperationQueue.main
         ) { notification in
-            DispatchQueue.global(qos: .background).async { [weak self] in
+            DispatchQueue.global(qos: .background).async {
                 if !UserDefaults.standard.bool(forKey: "notifications_disabled") {
                     NotificationManager.updateApplicationIconBadge(reset: false)
                     if let ui = notification.userInfo,
                        let chatId = ui["chat_id"] as? Int {
-                        self?.removePendingNotificationsFor(chatId: chatId)
-                        self?.removeDeliveredNotificationsFor(chatId: chatId)
+                        NotificationManager.removePendingNotificationsFor(chatId: chatId)
+                        NotificationManager.removeDeliveredNotificationsFor(chatId: chatId)
                     }
                 }
             }
         }
     }
 
-    private func removeDeliveredNotificationsFor(chatId: Int) {
+    private static func removeDeliveredNotificationsFor(chatId: Int) {
         var identifiers = [String]()
         let nc = UNUserNotificationCenter.current()
         nc.getDeliveredNotifications { notifications in
@@ -119,7 +127,7 @@ public class NotificationManager {
         }
     }
 
-    private func removePendingNotificationsFor(chatId: Int) {
+    private static func removePendingNotificationsFor(chatId: Int) {
         var identifiers = [String]()
         let nc = UNUserNotificationCenter.current()
         nc.getPendingNotificationRequests { notificationRequests in

+ 1 - 0
deltachat-ios/ViewModel/ChatListViewModel.swift

@@ -202,6 +202,7 @@ class ChatListViewModel: NSObject, ChatListViewModelProtocol {
 
     func deleteChat(chatId: Int) {
         dcContext.deleteChat(chatId: chatId)
+        NotificationManager.removeNotificationsForChat(chatId: chatId)
     }
 
     func archiveChatToggle(chatId: Int) {