diff --git a/Vernissage/Services/HomeTimelineService.swift b/Vernissage/Services/HomeTimelineService.swift index 1c3236c..f323033 100644 --- a/Vernissage/Services/HomeTimelineService.swift +++ b/Vernissage/Services/HomeTimelineService.swift @@ -24,20 +24,24 @@ public class HomeTimelineService { private let imagePrefetcher = ImagePrefetcher(destination: .diskCache) private let semaphore = AsyncSemaphore(value: 1) - public func amountOfNewStatuses(for account: AccountModel, includeReblogs: Bool, hideStatusesWithoutAlt: Bool, modelContext: ModelContext) async -> Int { + public func amountOfNewStatuses(includeReblogs: Bool, hideStatusesWithoutAlt: Bool, modelContext: ModelContext) async -> Int { await semaphore.wait() defer { semaphore.signal() } - guard let accessToken = account.accessToken else { + guard let accountData = AccountDataHandler.shared.getCurrentAccountData(modelContext: modelContext) else { + return 0 + } + + guard let accessToken = accountData.accessToken else { return 0 } // Get maximimum downloaded stauts id. - guard let lastSeenStatusId = self.getLastLoadedStatusId(accountId: account.id, modelContext: modelContext) else { + guard let lastSeenStatusId = self.getLastLoadedStatusId(accountId: accountData.id, modelContext: modelContext) else { return 0 } - let client = PixelfedClient(baseURL: account.serverUrl).getAuthenticated(token: accessToken) + let client = PixelfedClient(baseURL: accountData.serverUrl).getAuthenticated(token: accessToken) var statuses: [Status] = [] var newestStatusId = lastSeenStatusId @@ -52,7 +56,7 @@ public class HomeTimelineService { break } - let visibleStatuses = self.getVisibleStatuses(accountId: account.id, + let visibleStatuses = self.getVisibleStatuses(accountId: accountData.id, statuses: downloadedStatuses, hideStatusesWithoutAlt: hideStatusesWithoutAlt, modelContext: modelContext) diff --git a/Vernissage/Services/NotificationsService.swift b/Vernissage/Services/NotificationsService.swift index 954391f..3edb931 100644 --- a/Vernissage/Services/NotificationsService.swift +++ b/Vernissage/Services/NotificationsService.swift @@ -47,20 +47,24 @@ public class NotificationsService { } } - public func amountOfNewNotifications(for account: AccountModel, modelContext: ModelContext) async -> Int { + public func amountOfNewNotifications(modelContext: ModelContext) async -> Int { await semaphore.wait() defer { semaphore.signal() } - guard let accessToken = account.accessToken else { + guard let accountData = AccountDataHandler.shared.getCurrentAccountData(modelContext: modelContext) else { + return 0 + } + + guard let accessToken = accountData.accessToken else { return 0 } // Get maximimum downloaded stauts id. - guard let lastSeenNotificationId = self.getLastSeenNotificationId(accountId: account.id, modelContext: modelContext) else { + guard let lastSeenNotificationId = self.getLastSeenNotificationId(accountId: accountData.id, modelContext: modelContext) else { return 0 } - let client = PixelfedClient(baseURL: account.serverUrl).getAuthenticated(token: accessToken) + let client = PixelfedClient(baseURL: accountData.serverUrl).getAuthenticated(token: accessToken) var notifications: [PixelfedKit.Notification] = [] var maxId: String? = nil var allItemsProcessed = false diff --git a/Vernissage/VernissageApp.swift b/Vernissage/VernissageApp.swift index 96e3cf3..7c3588b 100644 --- a/Vernissage/VernissageApp.swift +++ b/Vernissage/VernissageApp.swift @@ -226,28 +226,21 @@ struct VernissageApp: App { private func calculateNewPhotosInBackground() async { let modelContext = self.modelContainer.mainContext - if let account = self.applicationState.account { - self.applicationState.amountOfNewStatuses = await HomeTimelineService.shared.amountOfNewStatuses( - for: account, - includeReblogs: self.applicationState.showReboostedStatuses, - hideStatusesWithoutAlt: self.applicationState.hideStatusesWithoutAlt, - modelContext: modelContext - ) - } + self.applicationState.amountOfNewStatuses = await HomeTimelineService.shared.amountOfNewStatuses( + includeReblogs: self.applicationState.showReboostedStatuses, + hideStatusesWithoutAlt: self.applicationState.hideStatusesWithoutAlt, + modelContext: modelContext + ) } private func calculateNewNotificationsInBackground() async { Logger.main.info("Calculating new notifications started.") let modelContext = self.modelContainer.mainContext + let amountOfNewNotifications = await NotificationsService.shared.amountOfNewNotifications(modelContext: modelContext) + self.applicationState.amountOfNewNotifications = amountOfNewNotifications - var amountOfNewNotifications = 0 - if let account = self.applicationState.account { - 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 { diff --git a/Vernissage/Views/MainView.swift b/Vernissage/Views/MainView.swift index 578a388..ddf6831 100644 --- a/Vernissage/Views/MainView.swift +++ b/Vernissage/Views/MainView.swift @@ -337,19 +337,14 @@ struct MainView: View { } private func calculateNewPhotosInBackground() async { - if let account = self.applicationState.account { - self.applicationState.amountOfNewStatuses = await HomeTimelineService.shared.amountOfNewStatuses( - for: account, - includeReblogs: self.applicationState.showReboostedStatuses, - hideStatusesWithoutAlt: self.applicationState.hideStatusesWithoutAlt, - modelContext: modelContext - ) - } + self.applicationState.amountOfNewStatuses = await HomeTimelineService.shared.amountOfNewStatuses( + includeReblogs: self.applicationState.showReboostedStatuses, + hideStatusesWithoutAlt: self.applicationState.hideStatusesWithoutAlt, + modelContext: modelContext + ) } private func calculateNewNotificationsInBackground() async { - if let account = self.applicationState.account { - self.applicationState.amountOfNewNotifications = await NotificationsService.shared.amountOfNewNotifications(for: account, modelContext: modelContext) - } + self.applicationState.amountOfNewNotifications = await NotificationsService.shared.amountOfNewNotifications(modelContext: modelContext) } }