twigs-ios/Twigs/Category/CategoryDataStore.swift

31 lines
779 B
Swift
Raw Normal View History

//
// CategoryDataStore.swift
2022-01-03 17:56:43 +00:00
// Twigs
//
2022-01-03 17:56:43 +00:00
// Created by William Brawner on 1/2/22.
// Copyright © 2022 William Brawner. All rights reserved.
//
import Foundation
2022-01-03 17:56:43 +00:00
import TwigsCore
2022-01-03 17:56:43 +00:00
@MainActor
class CategoryDataStore: ObservableObject {
2022-01-03 17:56:43 +00:00
@Published var sum: AsyncData<Int> = .empty
let apiService: TwigsApiService
init(_ apiService: TwigsApiService) {
self.apiService = apiService
}
2022-01-03 17:56:43 +00:00
func sum(categoryId: String, from: Date? = nil, to: Date? = nil) async {
self.sum = .loading
do {
let sum = try await self.apiService.sumTransactions(budgetId: nil, categoryId: categoryId, from: from, to: to).balance
2022-01-03 17:56:43 +00:00
self.sum = .success(sum)
} catch {
self.sum = .error(error)
}
}
}