30 lines
844 B
Swift
30 lines
844 B
Swift
//
|
|
// CategoryDataStore.swift
|
|
// Twigs
|
|
//
|
|
// Created by William Brawner on 1/2/22.
|
|
// Copyright © 2022 William Brawner. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
import TwigsCore
|
|
|
|
@MainActor
|
|
class CategoryDataStore: ObservableObject {
|
|
@Published var sum: AsyncData<Int> = .empty
|
|
let transactionRepository: TransactionRepository
|
|
|
|
init(transactionRepository: TransactionRepository) {
|
|
self.transactionRepository = transactionRepository
|
|
}
|
|
|
|
func sum(categoryId: String, from: Date? = nil, to: Date? = nil) async {
|
|
self.sum = .loading
|
|
do {
|
|
let sum = try await self.transactionRepository.sumTransactions(budgetId: nil, categoryId: categoryId, from: from, to: to).balance
|
|
self.sum = .success(sum)
|
|
} catch {
|
|
self.sum = .error(error)
|
|
}
|
|
}
|
|
}
|