WIP - Complete App refactoring
This commit is contained in:
@@ -6,3 +6,40 @@
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
@Observable
|
||||
class AccountManager {
|
||||
var accounts: [any Account] = []
|
||||
var authTokens: [String: String] = [:]
|
||||
|
||||
/// Save account as JSON.
|
||||
func save(account: any Account, authToken: String?) async throws {
|
||||
account.saveTokenToKeychain(authToken!)
|
||||
let data = try JSONEncoder().encode(account)
|
||||
|
||||
let accountDir = account.accountType.rawValue + "/" + account.id.uuidString + "/account.json"
|
||||
await DataStore.shared.save(data: data, toPath: accountDir)
|
||||
}
|
||||
|
||||
/// Load accounts from JSON files.
|
||||
func loadAccounts() async throws {
|
||||
// Read data from file or user defaults
|
||||
for accountType in AccountType.allCases {
|
||||
// List all account UUIDs under the /accountType directory
|
||||
let accountUUIDs = DataStore.shared.listAllFolders(dir: accountType.rawValue + "/")
|
||||
|
||||
// Decode each account and fetch the authToken
|
||||
for accountUUID in accountUUIDs {
|
||||
do {
|
||||
guard let account = try await DataStore.shared.loadDynamic(fromPath: accountType.rawValue + "/" + accountUUID + "/account.json", type: accountType.accountType) else {
|
||||
continue
|
||||
}
|
||||
authTokens[accountUUID] = (account as! any Account).getTokenFromKeychain() ?? ""
|
||||
self.accounts.append(account as! (any Account))
|
||||
} catch {
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user