Skip to content

Commit

Permalink
fix: Only set badge for mailboxes with notification enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippeWeidmann committed May 11, 2023
1 parent 0b7b657 commit 4e695ea
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 18 deletions.
4 changes: 3 additions & 1 deletion Mail/SceneDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate, AccountManagerDelegate
func sceneWillResignActive(_ scene: UIScene) {
// Called when the scene will move from an active state to an inactive state.
// This may occur due to temporary interruptions (ex. an incoming phone call).
NotificationsHelper.updateUnreadCountBadge()
Task {
await NotificationsHelper.updateUnreadCountBadge()
}
}

func sceneWillEnterForeground(_ scene: UIScene) {
Expand Down
39 changes: 25 additions & 14 deletions MailCore/Utils/NotificationsHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import CocoaLumberjackSwift
import Foundation
import InfomaniakCore
import InfomaniakCoreUI
import InfomaniakDI
import InfomaniakNotifications
import MailResources
import RealmSwift
import SwiftSoup
Expand Down Expand Up @@ -55,17 +57,26 @@ public enum NotificationsHelper {
}
}

public static func getUnreadCount() -> Int {
public static func getUnreadCount() async -> Int {
var totalUnreadCount = 0
for mailbox in MailboxInfosManager.instance.getMailboxes() {
if let mailboxManager = AccountManager.instance.getMailboxManager(for: mailbox) {
totalUnreadCount += mailboxManager.getFolder(with: .inbox)?.unreadCount ?? 0
@InjectService var notificationService: InfomaniakNotifications

for account in AccountManager.instance.accounts {
let currentSubscription = await notificationService.subscriptionForUser(id: account.userId)

for mailbox in MailboxInfosManager.instance.getMailboxes(for: account.userId)
where currentSubscription?.topics.contains(mailbox.notificationTopicName) == true {
if let mailboxManager = AccountManager.instance.getMailboxManager(for: mailbox) {
totalUnreadCount += mailboxManager.getFolder(with: .inbox)?.unreadCount ?? 0
}
}
}

return totalUnreadCount
}

public static func updateUnreadCountBadge() {
@MainActor
public static func updateUnreadCountBadge() async {
// Start a background task to update the app badge when going in the background
var backgroundTaskIdentifier: UIBackgroundTaskIdentifier = .invalid
backgroundTaskIdentifier = UIApplication.shared.beginBackgroundTask(withName: "updateUnreadCountBadge task") {
Expand All @@ -74,14 +85,12 @@ public enum NotificationsHelper {
backgroundTaskIdentifier = .invalid
}

let totalUnreadCount = getUnreadCount()
let totalUnreadCount = await getUnreadCount()

DispatchQueue.main.async {
UIApplication.shared.applicationIconBadgeNumber = totalUnreadCount
if backgroundTaskIdentifier != .invalid {
UIApplication.shared.endBackgroundTask(backgroundTaskIdentifier)
backgroundTaskIdentifier = .invalid
}
UIApplication.shared.applicationIconBadgeNumber = totalUnreadCount
if backgroundTaskIdentifier != .invalid {
UIApplication.shared.endBackgroundTask(backgroundTaskIdentifier)
backgroundTaskIdentifier = .invalid
}
}

Expand Down Expand Up @@ -114,7 +123,9 @@ public enum NotificationsHelper {
}
}

public static func generateNotificationFor(message: Message, mailboxId: Int, userId: Int) -> UNMutableNotificationContent {
public static func generateNotificationFor(message: Message,
mailboxId: Int,
userId: Int) async -> UNMutableNotificationContent {
let content = UNMutableNotificationContent()
if !message.from.isEmpty {
content.title = message.from.map { $0.name }.joined(separator: ",")
Expand All @@ -125,7 +136,7 @@ public enum NotificationsHelper {
content.body = getCleanBodyFrom(message: message)
content.threadIdentifier = "\(mailboxId)_\(userId)"
content.targetContentIdentifier = "\(userId)_\(mailboxId)_\(message.uid)"
content.badge = getUnreadCount() as NSNumber
content.badge = await getUnreadCount() as NSNumber
content.sound = .default
content.userInfo = [NotificationsHelper.UserInfoKeys.userId: userId,
NotificationsHelper.UserInfoKeys.mailboxId: mailboxId,
Expand Down
6 changes: 3 additions & 3 deletions MailNotificationServiceExtension/NotificationService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ class NotificationService: UNNotificationServiceExtension {
return contentHandler(bestAttemptContent)
}

let completeNotification = NotificationsHelper.generateNotificationFor(message: fetchedMessage,
mailboxId: mailboxId,
userId: userId)
let completeNotification = await NotificationsHelper.generateNotificationFor(message: fetchedMessage,
mailboxId: mailboxId,
userId: userId)
contentHandler(completeNotification)
}
}
Expand Down

0 comments on commit 4e695ea

Please sign in to comment.