2019-10-02 04:57:27 +00:00
|
|
|
//
|
|
|
|
// CategoryDataStore.swift
|
2022-01-03 17:56:43 +00:00
|
|
|
// Twigs
|
2019-10-02 04:57:27 +00:00
|
|
|
//
|
2022-01-03 17:56:43 +00:00
|
|
|
// Created by William Brawner on 1/2/22.
|
|
|
|
// Copyright © 2022 William Brawner. All rights reserved.
|
2019-10-02 04:57:27 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
2022-01-03 17:56:43 +00:00
|
|
|
import TwigsCore
|
2019-10-02 04:57:27 +00:00
|
|
|
|
2022-01-03 17:56:43 +00:00
|
|
|
@MainActor
|
2019-10-02 04:57:27 +00:00
|
|
|
class CategoryDataStore: ObservableObject {
|
2022-01-03 17:56:43 +00:00
|
|
|
@Published var sum: AsyncData<Int> = .empty
|
2022-05-18 01:04:27 +00:00
|
|
|
let apiService: TwigsApiService
|
2019-10-13 01:22:09 +00:00
|
|
|
|
2022-05-18 01:04:27 +00:00
|
|
|
init(_ apiService: TwigsApiService) {
|
|
|
|
self.apiService = apiService
|
2019-10-02 04:57:27 +00:00
|
|
|
}
|
|
|
|
|
2022-01-03 17:56:43 +00:00
|
|
|
func sum(categoryId: String, from: Date? = nil, to: Date? = nil) async {
|
|
|
|
self.sum = .loading
|
|
|
|
do {
|
2022-05-18 01:04:27 +00:00
|
|
|
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)
|
2021-10-22 02:43:01 +00:00
|
|
|
}
|
|
|
|
}
|
2019-10-02 04:57:27 +00:00
|
|
|
}
|