mirror of
https://github.com/Dimillian/IceCubesApp.git
synced 2024-12-25 06:22:06 +01:00
36 lines
993 B
Swift
36 lines
993 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.last ?? 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()
|
|
AppAccount.deleteAll()
|
|
currentAccount = AppAccount(server: IceCubesApp.defaultServer, oauthToken: nil)
|
|
}
|
|
}
|