ios: notification badge count (#822)

This commit is contained in:
Evgeny Poberezkin
2022-07-20 08:58:53 +01:00
committed by GitHub
parent add82d73fa
commit 252897d0ff
6 changed files with 58 additions and 14 deletions

View File

@@ -127,6 +127,7 @@ final class ChatModel: ObservableObject {
addChat(Chat(c), at: i)
}
}
NtfManager.shared.setNtfBadgeCount(totalUnreadCount())
}
// func addGroup(_ group: SimpleXChat.Group) {
@@ -139,6 +140,7 @@ final class ChatModel: ObservableObject {
chats[i].chatItems = [cItem]
if case .rcvNew = cItem.meta.itemStatus {
chats[i].chatStats.unreadCount = chats[i].chatStats.unreadCount + 1
NtfManager.shared.incNtfBadgeCount()
}
if i > 0 {
if chatId == nil {
@@ -203,6 +205,9 @@ final class ChatModel: ObservableObject {
// remove from current chat
if chatId == cInfo.id {
if let i = chatItems.firstIndex(where: { $0.id == cItem.id }) {
if chatItems[i].isRcvNew() == true {
NtfManager.shared.decNtfBadgeCount()
}
_ = withAnimation {
self.chatItems.remove(at: i)
}
@@ -213,6 +218,7 @@ final class ChatModel: ObservableObject {
func markChatItemsRead(_ cInfo: ChatInfo) {
// update preview
if let chat = getChat(cInfo.id) {
NtfManager.shared.decNtfBadgeCount(by: chat.chatStats.unreadCount)
chat.chatStats = ChatStats()
}
// update current chat
@@ -230,6 +236,7 @@ final class ChatModel: ObservableObject {
func clearChat(_ cInfo: ChatInfo) {
// clear preview
if let chat = getChat(cInfo.id) {
NtfManager.shared.decNtfBadgeCount(by: chat.chatStats.unreadCount)
chat.chatItems = []
chat.chatStats = ChatStats()
chat.chatInfo = cInfo
@@ -251,6 +258,10 @@ final class ChatModel: ObservableObject {
}
}
func totalUnreadCount() -> Int {
chats.reduce(0, { count, chat in count + chat.chatStats.unreadCount })
}
func getPrevChatItem(_ ci: ChatItem) -> ChatItem? {
if let i = chatItems.firstIndex(where: { $0.id == ci.id }), i > 0 {
return chatItems[i - 1]

View File

@@ -188,6 +188,19 @@ class NtfManager: NSObject, UNUserNotificationCenterDelegate, ObservableObject {
addNotification(createCallInvitationNtf(invitation))
}
func setNtfBadgeCount(_ count: Int) {
UIApplication.shared.applicationIconBadgeNumber = count
ntfBadgeCountGroupDefault.set(count)
}
func decNtfBadgeCount(by count: Int = 1) {
setNtfBadgeCount(max(0, UIApplication.shared.applicationIconBadgeNumber - count))
}
func incNtfBadgeCount(by count: Int = 1) {
setNtfBadgeCount(UIApplication.shared.applicationIconBadgeNumber + count)
}
private func addNotification(_ content: UNMutableNotificationContent) {
if !granted { return }
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: ntfTimeInterval, repeats: false)

View File

@@ -594,6 +594,7 @@ func startChat() throws {
m.userSMPServers = try getUserSMPServers()
let chats = try apiGetChats()
m.chats = chats.map { Chat.init($0) }
NtfManager.shared.setNtfBadgeCount(m.totalUnreadCount())
try refreshCallInvitations()
(m.savedToken, m.tokenStatus, m.notificationMode) = apiGetNtfToken()
if let token = m.deviceToken {