Skip to content

Commit

Permalink
duplicate app-icon badge counter to chat's tab-bar-item
Browse files Browse the repository at this point in the history
both badge counters will show the same number in the same layout.
if notifications are disabled, both counters are hidden
(the badges beside chats stay)

in practise, this feels totally normal and correctly,
this behavior is known from many other apps on iOS.

this also resets the counter on app start,
maybe fixing some rare bugs where the counter is wrong on the app icon
  • Loading branch information
r10s committed Apr 6, 2024
1 parent 71a4a27 commit e42140b
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
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(disable: !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(disable: 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(disable: Bool = false) {
DispatchQueue.main.async {
UIApplication.shared.applicationIconBadgeNumber = forceZero ? 0 : DcAccounts.shared.getFreshMessageCount()
let number = disable ? 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

0 comments on commit e42140b

Please sign in to comment.