Fix refreshing counter when it's disabled
This commit is contained in:
parent
bd56408ce8
commit
6ced647807
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue