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-01-11 13:16:43 +01:00
|
|
|
import CoreData
|
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-04-01 12:10:59 +02:00
|
|
|
|
2023-01-11 13:16:43 +01:00
|
|
|
func getAccountsData(viewContext: NSManagedObjectContext? = nil) -> [AccountData] {
|
|
|
|
let context = viewContext ?? CoreDataHandler.shared.container.viewContext
|
2022-12-31 16:31:05 +01:00
|
|
|
let fetchRequest = AccountData.fetchRequest()
|
|
|
|
do {
|
|
|
|
return try context.fetch(fetchRequest)
|
|
|
|
} 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-04-01 12:10:59 +02:00
|
|
|
|
2023-01-11 13:16:43 +01:00
|
|
|
func getCurrentAccountData(viewContext: NSManagedObjectContext? = nil) -> AccountData? {
|
|
|
|
let accounts = self.getAccountsData(viewContext: viewContext)
|
2023-03-15 14:27:59 +01:00
|
|
|
let defaultSettings = ApplicationSettingsHandler.shared.get()
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-01-01 18:13:36 +01:00
|
|
|
let currentAccount = accounts.first { accountData in
|
|
|
|
accountData.id == defaultSettings.currentAccount
|
|
|
|
}
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-01-01 18:13:36 +01:00
|
|
|
if let currentAccount {
|
|
|
|
return currentAccount
|
|
|
|
}
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-01-01 18:13:36 +01:00
|
|
|
return accounts.first
|
|
|
|
}
|
2022-12-31 16:31:05 +01:00
|
|
|
|
2023-01-11 13:16:43 +01:00
|
|
|
func getAccountData(accountId: String, viewContext: NSManagedObjectContext? = nil) -> AccountData? {
|
|
|
|
let context = viewContext ?? CoreDataHandler.shared.container.viewContext
|
|
|
|
let fetchRequest = AccountData.fetchRequest()
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-01-11 13:16:43 +01:00
|
|
|
fetchRequest.fetchLimit = 1
|
|
|
|
fetchRequest.predicate = NSPredicate(format: "id = %@", accountId)
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-01-11 13:16:43 +01:00
|
|
|
do {
|
|
|
|
return try context.fetch(fetchRequest).first
|
|
|
|
} 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-04-01 12:10:59 +02:00
|
|
|
|
2023-01-27 17:31:28 +01:00
|
|
|
func remove(accountData: AccountData) {
|
|
|
|
let context = CoreDataHandler.shared.container.viewContext
|
|
|
|
context.delete(accountData)
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-01-27 17:31:28 +01:00
|
|
|
do {
|
|
|
|
try context.save()
|
|
|
|
} 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-04-01 12:10:59 +02:00
|
|
|
|
2023-03-29 09:35:01 +02:00
|
|
|
func createAccountDataEntity(viewContext: NSManagedObjectContext? = nil) -> AccountData {
|
|
|
|
let context = viewContext ?? CoreDataHandler.shared.container.viewContext
|
2022-12-31 16:31:05 +01:00
|
|
|
return AccountData(context: context)
|
|
|
|
}
|
|
|
|
}
|