2022-11-29 11:18:06 +00:00
|
|
|
import SwiftUI
|
|
|
|
import Network
|
|
|
|
import Models
|
|
|
|
|
|
|
|
@MainActor
|
|
|
|
class AccountDetailViewModel: ObservableObject {
|
|
|
|
let accountId: String
|
|
|
|
var client: Client = .init(server: "")
|
|
|
|
|
|
|
|
enum State {
|
2022-12-01 08:05:26 +00:00
|
|
|
case loading, data(account: Account), error(error: Error)
|
2022-11-29 11:18:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Published var state: State = .loading
|
|
|
|
|
|
|
|
init(accountId: String) {
|
|
|
|
self.accountId = accountId
|
|
|
|
}
|
|
|
|
|
2022-12-17 12:37:46 +00:00
|
|
|
init(account: Account) {
|
|
|
|
self.accountId = account.id
|
|
|
|
self.state = .data(account: account)
|
|
|
|
}
|
|
|
|
|
2022-11-29 11:18:06 +00:00
|
|
|
func fetchAccount() async {
|
|
|
|
do {
|
2022-12-01 08:05:26 +00:00
|
|
|
state = .data(account: try await client.get(endpoint: Accounts.accounts(id: accountId)))
|
2022-11-29 11:18:06 +00:00
|
|
|
} catch {
|
|
|
|
state = .error(error: error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|