Fix refreshing counter when it's disabled

This commit is contained in:
Marcin Czachurski 2023-10-24 16:09:00 +02:00
parent bd56408ce8
commit 6ced647807
4 changed files with 17 additions and 7 deletions

View File

@ -92,12 +92,22 @@ public class NotificationsService {
}
/// Function sets application badge counts when notifications (and badge) are enabled.
public func setBadgeCount(_ count: Int) async throws {
public func setBadgeCount(_ count: Int, modelContext: ModelContext) async throws {
// Badge have to enabled in system settings.
let applicationSettings = ApplicationSettingsHandler.shared.get(modelContext: modelContext)
guard applicationSettings.showApplicationBadge else {
return
}
// Notifications have to be enabled.
let center = UNUserNotificationCenter.current()
let settings = await center.notificationSettings()
guard (settings.authorizationStatus == .authorized) || (settings.authorizationStatus == .provisional) else { return }
guard (settings.authorizationStatus == .authorized) || (settings.authorizationStatus == .provisional) else {
return
}
// Badge notification have to be enabled.
if settings.badgeSetting == .enabled {
try await center.setBadgeCount(count)
}

View File

@ -242,9 +242,9 @@ struct VernissageApp: App {
if let account = self.applicationState.account {
self.applicationState.amountOfNewNotifications = await NotificationsService.shared.amountOfNewNotifications(for: account, modelContext: modelContext)
try? await NotificationsService.shared.setBadgeCount(self.applicationState.amountOfNewStatuses)
try? await NotificationsService.shared.setBadgeCount(self.applicationState.amountOfNewStatuses, modelContext: modelContext)
} else {
try? await NotificationsService.shared.setBadgeCount(0)
try? await NotificationsService.shared.setBadgeCount(0, modelContext: modelContext)
}
}

View File

@ -107,7 +107,7 @@ struct NotificationsView: View {
// Refresh infomation about viewed notifications.
self.applicationState.amountOfNewNotifications = 0
try? await NotificationsService.shared.setBadgeCount(0)
try? await NotificationsService.shared.setBadgeCount(0, modelContext: modelContext)
}
} catch {
if !Task.isCancelled {
@ -152,7 +152,7 @@ struct NotificationsView: View {
// Refresh infomation about viewed notifications.
self.applicationState.amountOfNewNotifications = 0
try? await NotificationsService.shared.setBadgeCount(0)
try? await NotificationsService.shared.setBadgeCount(0, modelContext: modelContext)
self.minId = linkable.link?.minId
self.notifications.insert(contentsOf: linkable.data, at: 0)

View File

@ -34,7 +34,7 @@ struct NotificationView: View {
let center = UNUserNotificationCenter.current()
_ = try await center.requestAuthorization(options: [.alert, .sound, .badge])
} else {
try await NotificationsService.shared.setBadgeCount(0)
try await NotificationsService.shared.setBadgeCount(0, modelContext: modelContext)
}
} catch {
ErrorService.shared.handle(error, message: "settings.error.notificationEnableFailed", showToastr: false)