IceCubesApp/Packages/Account/Sources/Account/Edit/EditAccountViewModel.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

75 lines
2.2 KiB
Swift

import Models
import Network
import Observation
import SwiftUI
@MainActor
@Observable class EditAccountViewModel {
@Observable class FieldEditViewModel: Identifiable {
let id = UUID().uuidString
var name: String = ""
var value: String = ""
init(name: String, value: String) {
self.name = name
self.value = value
}
}
public var client: Client?
var displayName: String = ""
var note: String = ""
var postPrivacy = Models.Visibility.pub
var isSensitive: Bool = false
var isBot: Bool = false
var isLocked: Bool = false
var isDiscoverable: Bool = false
var fields: [FieldEditViewModel] = []
var isLoading: Bool = true
var isSaving: Bool = false
var saveError: Bool = false
init() {}
func fetchAccount() async {
guard let client else { return }
do {
let account: Account = try await client.get(endpoint: Accounts.verifyCredentials)
displayName = account.displayName ?? ""
note = account.source?.note ?? ""
postPrivacy = account.source?.privacy ?? .pub
isSensitive = account.source?.sensitive ?? false
isBot = account.bot
isLocked = account.locked
isDiscoverable = account.discoverable ?? false
fields = account.source?.fields.map { .init(name: $0.name, value: $0.value.asRawText) } ?? []
withAnimation {
isLoading = false
}
} catch {}
}
func save() async {
isSaving = true
do {
let data = UpdateCredentialsData(displayName: displayName,
note: note,
source: .init(privacy: postPrivacy, sensitive: isSensitive),
bot: isBot,
locked: isLocked,
discoverable: isDiscoverable,
fieldsAttributes: fields.map { .init(name: $0.name, value: $0.value) })
let response = try await client?.patch(endpoint: Accounts.updateCredentials(json: data))
if response?.statusCode != 200 {
saveError = true
}
isSaving = false
} catch {
isSaving = false
saveError = true
}
}
}