From 9be8a36b1c84b3afe0acc93300cc1c3db200c6ca Mon Sep 17 00:00:00 2001 From: Marcin Czachurski Date: Wed, 25 Oct 2023 08:05:50 +0200 Subject: [PATCH] Change notification icon and add more logs --- Vernissage/Info.plist | 1 - Vernissage/VernissageApp.swift | 57 ++++++++++++------- .../Subviews/NotificationRowView.swift | 10 ++-- 3 files changed, 41 insertions(+), 27 deletions(-) diff --git a/Vernissage/Info.plist b/Vernissage/Info.plist index e9452e4..88fdaad 100644 --- a/Vernissage/Info.plist +++ b/Vernissage/Info.plist @@ -37,7 +37,6 @@ UIBackgroundModes fetch - processing diff --git a/Vernissage/VernissageApp.swift b/Vernissage/VernissageApp.swift index 721bdce..96e3cf3 100644 --- a/Vernissage/VernissageApp.swift +++ b/Vernissage/VernissageApp.swift @@ -12,6 +12,7 @@ import EnvironmentKit import WidgetKit import SwiftData import TipKit +import OSLog import BackgroundTasks @main @@ -63,17 +64,6 @@ struct VernissageApp: App { .task { await self.onApplicationStart() } - .onReceive(NotificationCenter.default.publisher(for: UIApplication.didBecomeActiveNotification)) { _ in - DispatchQueue.main.asyncAfter(deadline: .now() + 5) { - Task { - // Refresh indicator of new photos and new statuses when application is become active. - _ = await (self.calculateNewPhotosInBackground(), self.calculateNewNotificationsInBackground()) - } - } - - // Reload widget content when application become active. - WidgetCenter.shared.reloadAllTimelines() - } .onReceive(timer) { _ in Task { // Refresh indicator of new photos and new notifications each two minutes (when application is in the foreground).. @@ -106,14 +96,23 @@ struct VernissageApp: App { } .onChange(of: phase) { oldValue, newValue in switch newValue { - case .background: scheduleAppRefresh() + case .background: + scheduleAppRefresh() + case .active: + DispatchQueue.main.asyncAfter(deadline: .now() + 5) { + Task { + // Refresh indicator of new photos and new statuses when application is become active. + _ = await (self.calculateNewPhotosInBackground(), self.calculateNewNotificationsInBackground()) + } + } + + // Reload widget content when application become active. + WidgetCenter.shared.reloadAllTimelines() default: break } } .backgroundTask(.appRefresh(AppConstants.backgroundFetcherName)) { - Task { @MainActor in - await self.setBadgeCount() - } + await self.setBadgeCount() } } @@ -238,24 +237,38 @@ struct VernissageApp: App { } private func calculateNewNotificationsInBackground() async { + Logger.main.info("Calculating new notifications started.") + let modelContext = self.modelContainer.mainContext + var amountOfNewNotifications = 0 if let account = self.applicationState.account { - self.applicationState.amountOfNewNotifications = await NotificationsService.shared.amountOfNewNotifications(for: account, modelContext: modelContext) - try? await NotificationsService.shared.setBadgeCount(self.applicationState.amountOfNewNotifications, modelContext: modelContext) - } else { - try? await NotificationsService.shared.setBadgeCount(0, modelContext: modelContext) + amountOfNewNotifications = await NotificationsService.shared.amountOfNewNotifications(for: account, modelContext: modelContext) + } + + do { + self.applicationState.amountOfNewNotifications = amountOfNewNotifications + try await NotificationsService.shared.setBadgeCount(amountOfNewNotifications, modelContext: modelContext) + Logger.main.info("New notifications (\(amountOfNewNotifications)) calculated successfully.") + } catch { + Logger.main.error("Error ['Set badge count failed']: \(error.localizedDescription)") } } private func scheduleAppRefresh() { let request = BGAppRefreshTaskRequest(identifier: AppConstants.backgroundFetcherName) - request.earliestBeginDate = .now.addingTimeInterval(3600) - try? BGTaskScheduler.shared.submit(request) + request.earliestBeginDate = .now.addingTimeInterval(20 * 60) + + do { + try BGTaskScheduler.shared.submit(request) + Logger.main.info("Background task scheduled successfully.") + } catch { + Logger.main.error("Error ['Registering background task failed']: \(error.localizedDescription)") + } } private func setBadgeCount() async { - await self.calculateNewNotificationsInBackground() scheduleAppRefresh() + await self.calculateNewNotificationsInBackground() } } diff --git a/Vernissage/Views/NotificationsView/Subviews/NotificationRowView.swift b/Vernissage/Views/NotificationsView/Subviews/NotificationRowView.swift index 751335b..a5172a5 100644 --- a/Vernissage/Views/NotificationsView/Subviews/NotificationRowView.swift +++ b/Vernissage/Views/NotificationsView/Subviews/NotificationRowView.swift @@ -58,15 +58,17 @@ struct NotificationRowView: View { Spacer() + if self.isNewNotification { + Circle() + .foregroundStyle(self.applicationState.tintColor.color()) + .frame(width: 8.0, height: 8.0) + } + if let createdAt = self.notification.createdAt.toDate(.isoDateTimeMilliSec) { RelativeTime(date: createdAt) .foregroundColor(.customGrayColor) .font(.footnote) } - - Circle() - .foregroundStyle(self.isNewNotification ? self.applicationState.tintColor.color() : Color.clear) - .frame(width: 8.0, height: 8.0) } Text(self.getTitle(), comment: "Notification type")