57 lines
1.7 KiB
Swift
57 lines
1.7 KiB
Swift
import Foundation
|
|
|
|
@Observable
|
|
@MainActor
|
|
final class AccountStore {
|
|
private(set) var accounts: [Account] = []
|
|
|
|
private static let storageKey = "qstatus.accounts"
|
|
|
|
init() {
|
|
loadAccounts()
|
|
}
|
|
|
|
func addAccount(_ account: Account, token: String) throws {
|
|
if accounts.contains(where: {
|
|
$0.serviceType == account.serviceType
|
|
&& $0.instanceURL.caseInsensitiveCompare(account.instanceURL) == .orderedSame
|
|
&& $0.username.caseInsensitiveCompare(account.username) == .orderedSame
|
|
}) {
|
|
throw PostingError(message: "This account is already added.")
|
|
}
|
|
|
|
try KeychainManager.saveToken(token, forAccount: account.keychainKey)
|
|
accounts.append(account)
|
|
saveAccounts()
|
|
}
|
|
|
|
func removeAccount(_ account: Account) {
|
|
try? KeychainManager.delete(account: account.keychainKey)
|
|
accounts.removeAll { $0.id == account.id }
|
|
saveAccounts()
|
|
}
|
|
|
|
func token(for account: Account) -> String? {
|
|
try? KeychainManager.loadToken(forAccount: account.keychainKey)
|
|
}
|
|
|
|
func updateAccount(_ account: Account) {
|
|
if let index = accounts.firstIndex(where: { $0.id == account.id }) {
|
|
accounts[index] = account
|
|
saveAccounts()
|
|
}
|
|
}
|
|
|
|
private func saveAccounts() {
|
|
if let data = try? JSONEncoder().encode(accounts) {
|
|
UserDefaults.standard.set(data, forKey: Self.storageKey)
|
|
}
|
|
}
|
|
|
|
private func loadAccounts() {
|
|
guard let data = UserDefaults.standard.data(forKey: Self.storageKey),
|
|
let decoded = try? JSONDecoder().decode([Account].self, from: data)
|
|
else { return }
|
|
accounts = decoded
|
|
}
|
|
}
|