IceCubes/Packages/AppAccount/Sources/AppAccount/AppAccountViewModel.swift

35 lines
836 B
Swift
Raw Normal View History

2022-12-30 08:36:22 +01:00
import Models
import Network
2023-01-17 11:36:01 +01:00
import SwiftUI
2022-12-30 08:36:22 +01:00
@MainActor
public class AppAccountViewModel: ObservableObject {
var appAccount: AppAccount
2022-12-30 08:36:22 +01:00
let client: Client
let isCompact: Bool
2023-01-17 11:36:01 +01:00
2022-12-30 08:36:22 +01:00
@Published var account: Account?
2023-01-17 11:36:01 +01:00
2023-01-10 06:58:50 +01:00
var acct: String {
"@\(account?.acct ?? "...")@\(appAccount.server)"
}
2023-01-17 11:36:01 +01:00
public init(appAccount: AppAccount, isCompact: Bool = false) {
2022-12-30 08:36:22 +01:00
self.appAccount = appAccount
self.isCompact = isCompact
2023-01-17 11:36:01 +01:00
client = .init(server: appAccount.server, oauthToken: appAccount.oauthToken)
2022-12-30 08:36:22 +01:00
}
2023-01-17 11:36:01 +01:00
2022-12-30 08:36:22 +01:00
func fetchAccount() async {
do {
account = try await client.get(endpoint: Accounts.verifyCredentials)
if appAccount.accountName == nil, let account {
appAccount.accountName = "\(account.acct)@\(appAccount.server)"
try appAccount.save()
}
2022-12-30 08:36:22 +01:00
} catch {
print(error)
}
}
}