Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Only set badge for mailboxes with notification enabled #742

Merged
merged 1 commit into from
May 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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