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

duplicate app-icon badge counter to chat's tab-bar-item #2150

Merged
merged 1 commit into from
May 9, 2024
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
1 change: 1 addition & 0 deletions deltachat-ios/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
logger.info("➡️ applicationDidBecomeActive")
UserDefaults.setMainIoRunning()
applicationInForeground = true
NotificationManager.updateBadgeCounters()
}

func applicationWillResignActive(_: UIApplication) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ internal final class SettingsViewController: UITableViewController {
NotificationManager.removeAllNotifications()
}
UserDefaults.standard.synchronize()
NotificationManager.updateApplicationIconBadge(forceZero: !sender.isOn)
NotificationManager.updateBadgeCounters(forceZero: !sender.isOn)
}

// MARK: - updates
Expand Down
3 changes: 2 additions & 1 deletion deltachat-ios/Coordinator/AppCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ class AppCoordinator: NSObject {
// the applicationIconBadgeNumber is remembered by the system even on reinstalls (just tested on ios 13.3.1),
// to avoid appearing an old number of a previous installation, we reset the counter manually.
// but even when this changes in ios, we need the reset as we allow account-deletion also in-app.
NotificationManager.updateApplicationIconBadge(forceZero: true)
NotificationManager.updateBadgeCounters(forceZero: true)
}

func presentTabBarController() {
Expand Down Expand Up @@ -271,6 +271,7 @@ class AppCoordinator: NSObject {
createChatsNavigationController(),
createSettingsNavigationController()], animated: false)
presentTabBarController()
NotificationManager.updateBadgeCounters()
}
}

Expand Down
18 changes: 14 additions & 4 deletions deltachat-ios/Helper/NotificationManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,19 @@ public class NotificationManager {
dcContext = dcAccounts.getSelected()
}

public static func updateApplicationIconBadge(forceZero: Bool = false) {
public static func updateBadgeCounters(forceZero: Bool = false) {
DispatchQueue.main.async {
UIApplication.shared.applicationIconBadgeNumber = forceZero ? 0 : DcAccounts.shared.getFreshMessageCount()
let number = forceZero ? 0 : DcAccounts.shared.getFreshMessageCount()

// update badge counter on iOS homescreen
UIApplication.shared.applicationIconBadgeNumber = number

// update badge counter on our tabbar
if let appDelegate = UIApplication.shared.delegate as? AppDelegate,
let appCoordinator = appDelegate.appCoordinator,
let chatsNavigationController = appCoordinator.tabBarController.viewControllers?[appCoordinator.chatsTab] {
chatsNavigationController.tabBarItem.badgeValue = number > 0 ? "\(number)" : nil
}
}
}

Expand Down Expand Up @@ -55,7 +65,7 @@ public class NotificationManager {
nc.removeDeliveredNotifications(withIdentifiers: toRemove)
}

NotificationManager.updateApplicationIconBadge()
NotificationManager.updateBadgeCounters()
}
}

Expand All @@ -65,7 +75,7 @@ public class NotificationManager {
object: nil, queue: OperationQueue.main
) { _ in
if !UserDefaults.standard.bool(forKey: "notifications_disabled") {
NotificationManager.updateApplicationIconBadge()
NotificationManager.updateBadgeCounters()
}
}

Expand Down
Loading