IceCubesApp/Packages/Lists/Sources/Lists/Edit/ListEditViewModel.swift

39 lines
907 B
Swift
Raw Normal View History

import Models
import Network
2023-01-17 10:36:01 +00:00
import SwiftUI
@MainActor
public class ListEditViewModel: ObservableObject {
let list: Models.List
2023-01-17 10:36:01 +00:00
var client: Client?
2023-01-17 10:36:01 +00:00
@Published var isLoadingAccounts: Bool = true
@Published var accounts: [Account] = []
2023-01-17 10:36:01 +00:00
init(list: Models.List) {
self.list = list
}
2023-01-17 10:36:01 +00:00
func fetchAccounts() async {
guard let client else { return }
isLoadingAccounts = true
do {
accounts = try await client.get(endpoint: Lists.accounts(listId: list.id))
isLoadingAccounts = false
} catch {
isLoadingAccounts = false
}
}
2023-01-17 10:36:01 +00:00
func delete(account: Account) async {
guard let client else { return }
do {
2023-01-17 10:36:01 +00:00
let response = try await client.delete(endpoint: Lists.updateAccounts(listId: list.id, accounts: [account.id]))
if response?.statusCode == 200 {
accounts.removeAll(where: { $0.id == account.id })
}
2023-01-17 10:36:01 +00:00
} catch {}
}
}