Remove app state from background tasks
This commit is contained in:
parent
9be8a36b1c
commit
af0bb99ce3
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue