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 {
|
2023-01-24 13:38:26 +01:00
|
|
|
var appAccount: AppAccount
|
2022-12-30 08:36:22 +01:00
|
|
|
let client: Client
|
2023-01-17 19:41:46 +01:00
|
|
|
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
|
|
|
|
2023-01-17 19:41:46 +01:00
|
|
|
public init(appAccount: AppAccount, isCompact: Bool = false) {
|
2022-12-30 08:36:22 +01:00
|
|
|
self.appAccount = appAccount
|
2023-01-17 19:41:46 +01:00
|
|
|
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)
|
2023-01-24 13:38:26 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|