Impressia/CoreData/AccountDataHandler.swift

81 lines
2.7 KiB
Swift
Raw Normal View History

2022-12-31 16:31:05 +01:00
//
// https://mczachurski.dev
2023-04-09 20:51:33 +02:00
// Copyright © 2023 Marcin Czachurski and the repository contributors.
2023-03-28 10:35:38 +02:00
// Licensed under the Apache License 2.0.
2022-12-31 16:31:05 +01:00
//
import Foundation
2023-10-20 07:45:18 +02:00
import SwiftData
2022-12-31 16:31:05 +01:00
class AccountDataHandler {
2023-01-05 11:55:20 +01:00
public static let shared = AccountDataHandler()
private init() { }
2023-10-20 07:45:18 +02:00
func getAccountsData(modelContext: ModelContext) -> [AccountData] {
2022-12-31 16:31:05 +01:00
do {
2023-10-20 07:45:18 +02:00
var fetchDescriptor = FetchDescriptor<AccountData>()
fetchDescriptor.includePendingChanges = true
return try modelContext.fetch(fetchDescriptor)
2022-12-31 16:31:05 +01:00
} catch {
2023-03-11 18:30:33 +01:00
CoreDataError.shared.handle(error, message: "Accounts cannot be retrieved (getAccountsData).")
2022-12-31 16:31:05 +01:00
return []
}
}
2023-10-20 07:45:18 +02:00
func getCurrentAccountData(modelContext: ModelContext) -> AccountData? {
let accounts = self.getAccountsData(modelContext: modelContext)
let defaultSettings = ApplicationSettingsHandler.shared.get(modelContext: modelContext)
2023-01-01 18:13:36 +01:00
let currentAccount = accounts.first { accountData in
accountData.id == defaultSettings.currentAccount
}
2023-01-01 18:13:36 +01:00
if let currentAccount {
return currentAccount
}
2023-01-01 18:13:36 +01:00
return accounts.first
}
2022-12-31 16:31:05 +01:00
2023-10-20 07:45:18 +02:00
func getAccountData(accountId: String, modelContext: ModelContext) -> AccountData? {
2023-01-11 13:16:43 +01:00
do {
2023-10-20 17:35:11 +02:00
var fetchDescriptor = FetchDescriptor<AccountData>(
predicate: #Predicate { $0.id == accountId}
)
2023-10-20 07:45:18 +02:00
fetchDescriptor.fetchLimit = 1
fetchDescriptor.includePendingChanges = true
return try modelContext.fetch(fetchDescriptor).first
2023-01-11 13:16:43 +01:00
} catch {
2023-03-11 18:30:33 +01:00
CoreDataError.shared.handle(error, message: "Error during fetching status (getAccountData).")
2023-01-11 13:16:43 +01:00
return nil
}
}
2023-10-20 07:45:18 +02:00
func remove(accountData: AccountData, modelContext: ModelContext) {
2023-01-27 17:31:28 +01:00
do {
2023-10-20 07:45:18 +02:00
modelContext.delete(accountData)
try modelContext.save()
2023-01-27 17:31:28 +01:00
} catch {
2023-03-11 18:30:33 +01:00
CoreDataError.shared.handle(error, message: "Error during deleting account data (remove).")
2023-01-27 17:31:28 +01:00
}
}
2023-10-20 17:35:11 +02:00
func update(lastSeenStatusId: String?, lastLoadedStatusId: String?, accountId: String, modelContext: ModelContext) throws {
guard let accountDataFromDb = self.getAccountData(accountId: accountId, modelContext: modelContext) else {
return
}
if (accountDataFromDb.lastSeenStatusId ?? "0") < (lastSeenStatusId ?? "0") {
accountDataFromDb.lastSeenStatusId = lastSeenStatusId
}
if (accountDataFromDb.lastLoadedStatusId ?? "0") < (lastLoadedStatusId ?? "0") {
accountDataFromDb.lastLoadedStatusId = lastLoadedStatusId
}
try modelContext.save()
}
2022-12-31 16:31:05 +01:00
}