IceCubesApp/Packages/AppAccount/Sources/AppAccount/AppAccountViewModel.swift
Thomas Ricouard 4189a59cf6
iOS 17+ only support + migrating to Observation framework (#1571)
* Initial iOS 17 + Observable migration

* More Observation

* More observation

* Checkpoint

* Checkpoint

* Bump version to 1.8.0

* SwiftFormat

* Fix home timeline switch on login

* Fix sidebar routerPath

* Fixes on detail view

* Remove print changes

* Simply detail view

* More opt

* Migrate DisplaySettingsLocalValues

* Better post detail transition

* Status detail animation finally right

* Cleanup
2023-09-18 07:01:23 +02:00

61 lines
1.4 KiB
Swift

import Combine
import DesignSystem
import Models
import Network
import Observation
import SwiftUI
@MainActor
@Observable public class AppAccountViewModel {
private static var avatarsCache: [String: UIImage] = [:]
private static var accountsCache: [String: Account] = [:]
var appAccount: AppAccount
let client: Client
let isCompact: Bool
let isInNavigation: Bool
let showBadge: Bool
var account: Account? {
didSet {
if let account {
refreshAcct(account: account)
}
}
}
var acct: String {
if let acct = appAccount.accountName {
acct
} else {
"@\(account?.acct ?? "...")@\(appAccount.server)"
}
}
public init(appAccount: AppAccount, isCompact: Bool = false, isInNavigation: Bool = true, showBadge: Bool = false) {
self.appAccount = appAccount
self.isCompact = isCompact
self.isInNavigation = isInNavigation
self.showBadge = showBadge
client = .init(server: appAccount.server, oauthToken: appAccount.oauthToken)
}
func fetchAccount() async {
do {
account = Self.accountsCache[appAccount.id]
account = try await client.get(endpoint: Accounts.verifyCredentials)
Self.accountsCache[appAccount.id] = account
} catch {}
}
private func refreshAcct(account: Account) {
do {
if appAccount.accountName == nil {
appAccount.accountName = "\(account.acct)@\(appAccount.server)"
try appAccount.save()
}
} catch {}
}
}