IceCubes/IceCubesApp/App/AppAccounts/AppAccountsManager.swift

35 lines
967 B
Swift

import SwiftUI
import Network
class AppAccountsManager: ObservableObject {
@Published var currentAccount: AppAccount {
didSet {
currentClient = .init(server: currentAccount.server,
oauthToken: currentAccount.oauthToken)
}
}
@Published var currentClient: Client
init() {
var defaultAccount = AppAccount(server: IceCubesApp.defaultServer, oauthToken: nil)
do {
let keychainAccounts = try AppAccount.retrieveAll()
defaultAccount = keychainAccounts.first ?? defaultAccount
} catch {}
currentAccount = defaultAccount
currentClient = .init(server: defaultAccount.server, oauthToken: defaultAccount.oauthToken)
}
func add(account: AppAccount) {
do {
try account.save()
currentAccount = account
} catch { }
}
func delete(account: AppAccount) {
account.delete()
currentAccount = AppAccount(server: IceCubesApp.defaultServer, oauthToken: nil)
}
}