2021-10-21 22:13:32 +00:00
|
|
|
//
|
|
|
|
// CategoryDetailsView.swift
|
|
|
|
// Twigs
|
|
|
|
//
|
|
|
|
// Created by William Brawner on 10/19/21.
|
|
|
|
// Copyright © 2021 William Brawner. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import SwiftUI
|
2022-01-03 17:56:43 +00:00
|
|
|
import TwigsCore
|
2021-10-21 22:13:32 +00:00
|
|
|
|
|
|
|
struct CategoryDetailsView: View {
|
2022-05-18 01:04:27 +00:00
|
|
|
@EnvironmentObject var dataStore: DataStore
|
2022-01-05 02:51:38 +00:00
|
|
|
@EnvironmentObject var categoryDataStore: CategoryDataStore
|
|
|
|
@EnvironmentObject var apiService: TwigsApiService
|
2021-10-21 22:13:32 +00:00
|
|
|
let budget: Budget
|
2022-01-03 17:56:43 +00:00
|
|
|
@State var sum: Int? = 0
|
2021-10-21 22:13:32 +00:00
|
|
|
var spent: Int {
|
|
|
|
get {
|
2022-01-05 02:51:38 +00:00
|
|
|
if case let .success(sum) = categoryDataStore.sum {
|
2022-01-03 17:56:43 +00:00
|
|
|
return abs(sum)
|
2021-10-21 22:13:32 +00:00
|
|
|
} else {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-01-05 02:51:38 +00:00
|
|
|
func middleLabel(_ category: TwigsCore.Category) -> LocalizedStringKey {
|
|
|
|
if category.expense {
|
|
|
|
return LocalizedStringKey("amount_spent")
|
|
|
|
} else {
|
|
|
|
return LocalizedStringKey("amount_earned")
|
2021-10-21 22:13:32 +00:00
|
|
|
}
|
|
|
|
}
|
2022-07-23 21:57:31 +00:00
|
|
|
|
2021-10-21 22:13:32 +00:00
|
|
|
var body: some View {
|
2022-05-18 01:04:27 +00:00
|
|
|
if let category = dataStore.selectedCategory {
|
|
|
|
TransactionListView() {
|
2022-01-05 02:51:38 +00:00
|
|
|
VStack {
|
|
|
|
Text(verbatim: category.description ?? "")
|
|
|
|
.padding()
|
|
|
|
HStack {
|
|
|
|
LabeledCounter(title: LocalizedStringKey("amount_budgeted"), amount: category.amount)
|
|
|
|
LabeledCounter(title: middleLabel(category), amount: spent)
|
|
|
|
LabeledCounter(title: LocalizedStringKey("amount_remaining"), amount: category.amount - spent)
|
|
|
|
}
|
|
|
|
}.frame(maxWidth: .infinity, alignment: .center)
|
|
|
|
}.task {
|
|
|
|
await categoryDataStore.sum(categoryId: category.id)
|
2021-10-21 22:13:32 +00:00
|
|
|
}
|
2022-01-05 02:51:38 +00:00
|
|
|
.navigationBarItems(trailing: Button(action: {
|
2022-05-18 03:40:08 +00:00
|
|
|
Task {
|
|
|
|
await dataStore.edit(category)
|
|
|
|
}
|
2021-10-22 02:43:01 +00:00
|
|
|
}) {
|
|
|
|
Text("edit")
|
2022-01-05 02:51:38 +00:00
|
|
|
})
|
2022-05-18 03:40:08 +00:00
|
|
|
.sheet(isPresented: self.$dataStore.editingCategory, onDismiss: {
|
|
|
|
self.dataStore.cancelEditCategory()
|
2022-01-05 02:51:38 +00:00
|
|
|
}, content: {
|
|
|
|
CategoryFormSheet(categoryForm: CategoryForm(
|
|
|
|
category: category,
|
2022-05-18 01:04:27 +00:00
|
|
|
dataStore: dataStore,
|
2022-01-05 02:51:38 +00:00
|
|
|
budgetId: category.budgetId
|
|
|
|
))
|
|
|
|
})
|
|
|
|
}
|
2021-10-21 22:13:32 +00:00
|
|
|
}
|
2022-07-23 21:57:31 +00:00
|
|
|
|
2022-01-05 02:51:38 +00:00
|
|
|
init (_ budget: Budget) {
|
2021-10-21 22:13:32 +00:00
|
|
|
self.budget = budget
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct LabeledCounter: View {
|
|
|
|
let title: LocalizedStringKey
|
|
|
|
let amount: Int
|
|
|
|
var body: some View {
|
|
|
|
VStack {
|
|
|
|
Text(title)
|
|
|
|
Text(verbatim: amount.toCurrencyString())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if DEBUG
|
|
|
|
struct CategoryDetailsView_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
2022-01-05 02:51:38 +00:00
|
|
|
CategoryDetailsView(MockBudgetRepository.budget)
|
2022-05-18 01:04:27 +00:00
|
|
|
.environmentObject(TwigsInMemoryCacheService())
|
2021-10-21 22:13:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|