Change notification icon and add more logs

This commit is contained in:
Marcin Czachurski 2023-10-25 08:05:50 +02:00
parent 1292000620
commit 9be8a36b1c
3 changed files with 41 additions and 27 deletions

View File

@ -37,7 +37,6 @@
<key>UIBackgroundModes</key> <key>UIBackgroundModes</key>
<array> <array>
<string>fetch</string> <string>fetch</string>
<string>processing</string>
</array> </array>
</dict> </dict>
</plist> </plist>

View File

@ -12,6 +12,7 @@ import EnvironmentKit
import WidgetKit import WidgetKit
import SwiftData import SwiftData
import TipKit import TipKit
import OSLog
import BackgroundTasks import BackgroundTasks
@main @main
@ -63,17 +64,6 @@ struct VernissageApp: App {
.task { .task {
await self.onApplicationStart() 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 .onReceive(timer) { _ in
Task { Task {
// Refresh indicator of new photos and new notifications each two minutes (when application is in the foreground).. // 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 .onChange(of: phase) { oldValue, newValue in
switch newValue { 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 default: break
} }
} }
.backgroundTask(.appRefresh(AppConstants.backgroundFetcherName)) { .backgroundTask(.appRefresh(AppConstants.backgroundFetcherName)) {
Task { @MainActor in await self.setBadgeCount()
await self.setBadgeCount()
}
} }
} }
@ -238,24 +237,38 @@ struct VernissageApp: App {
} }
private func calculateNewNotificationsInBackground() async { private func calculateNewNotificationsInBackground() async {
Logger.main.info("Calculating new notifications started.")
let modelContext = self.modelContainer.mainContext let modelContext = self.modelContainer.mainContext
var amountOfNewNotifications = 0
if let account = self.applicationState.account { if let account = self.applicationState.account {
self.applicationState.amountOfNewNotifications = await NotificationsService.shared.amountOfNewNotifications(for: account, modelContext: modelContext) 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) 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() { private func scheduleAppRefresh() {
let request = BGAppRefreshTaskRequest(identifier: AppConstants.backgroundFetcherName) let request = BGAppRefreshTaskRequest(identifier: AppConstants.backgroundFetcherName)
request.earliestBeginDate = .now.addingTimeInterval(3600) request.earliestBeginDate = .now.addingTimeInterval(20 * 60)
try? BGTaskScheduler.shared.submit(request)
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 { private func setBadgeCount() async {
await self.calculateNewNotificationsInBackground()
scheduleAppRefresh() scheduleAppRefresh()
await self.calculateNewNotificationsInBackground()
} }
} }

View File

@ -58,15 +58,17 @@ struct NotificationRowView: View {
Spacer() 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) { if let createdAt = self.notification.createdAt.toDate(.isoDateTimeMilliSec) {
RelativeTime(date: createdAt) RelativeTime(date: createdAt)
.foregroundColor(.customGrayColor) .foregroundColor(.customGrayColor)
.font(.footnote) .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") Text(self.getTitle(), comment: "Notification type")