IceCubesApp/Packages/Account/Sources/Account/Edit/EditAccountViewModel.swift

61 lines
1.9 KiB
Swift
Raw Normal View History

2023-01-10 07:24:05 +00:00
import Models
import Network
2023-01-17 10:36:01 +00:00
import SwiftUI
2023-01-10 07:24:05 +00:00
@MainActor
class EditAccountViewModel: ObservableObject {
public var client: Client?
2023-01-17 10:36:01 +00:00
2023-01-10 07:24:05 +00:00
@Published var displayName: String = ""
@Published var note: String = ""
@Published var postPrivacy = Models.Visibility.pub
@Published var isSensitive: Bool = false
@Published var isBot: Bool = false
@Published var isLocked: Bool = false
@Published var isDiscoverable: Bool = false
2023-01-17 10:36:01 +00:00
2023-01-10 07:24:05 +00:00
@Published var isLoading: Bool = true
@Published var isSaving: Bool = false
@Published var saveError: Bool = false
2023-01-17 10:36:01 +00:00
init() {}
2023-01-10 07:24:05 +00:00
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
withAnimation {
isLoading = false
}
2023-01-17 10:36:01 +00:00
} catch {}
2023-01-10 07:24:05 +00:00
}
2023-01-17 10:36:01 +00:00
2023-01-10 07:24:05 +00:00
func save() async {
isSaving = true
do {
let response =
2023-01-17 10:36:01 +00:00
try await client?.patch(endpoint: Accounts.updateCredentials(displayName: displayName,
note: note,
privacy: postPrivacy,
isSensitive: isSensitive,
isBot: isBot,
isLocked: isLocked,
isDiscoverable: isDiscoverable))
2023-01-10 07:24:05 +00:00
if response?.statusCode != 200 {
saveError = true
}
isSaving = false
} catch {
isSaving = false
saveError = true
}
}
}