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 {
|
|
|
|
@EnvironmentObject var transactionDataStore: TransactionDataStore
|
|
|
|
let budget: Budget
|
2022-01-03 17:56:43 +00:00
|
|
|
let category: TwigsCore.Category
|
|
|
|
@State var sum: Int? = 0
|
2021-10-22 02:43:01 +00:00
|
|
|
@State var editingCategory: Bool = false
|
2021-10-21 22:13:32 +00:00
|
|
|
var spent: Int {
|
|
|
|
get {
|
2022-01-03 17:56:43 +00:00
|
|
|
if let sum = self.sum {
|
|
|
|
return abs(sum)
|
2021-10-21 22:13:32 +00:00
|
|
|
} else {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var remaining: Int {
|
|
|
|
get {
|
|
|
|
return category.amount - spent
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var middleLabel: LocalizedStringKey {
|
|
|
|
get {
|
|
|
|
if category.expense {
|
|
|
|
return LocalizedStringKey("amount_spent")
|
|
|
|
} else {
|
|
|
|
return LocalizedStringKey("amount_earned")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var body: some View {
|
2022-01-03 17:56:43 +00:00
|
|
|
TransactionListView(self.budget, category: category) {
|
|
|
|
VStack {
|
|
|
|
Text(verbatim: category.description ?? "")
|
|
|
|
.padding()
|
|
|
|
HStack {
|
|
|
|
LabeledCounter(title: LocalizedStringKey("amount_budgeted"), amount: category.amount)
|
|
|
|
LabeledCounter(title: middleLabel, amount: spent)
|
|
|
|
LabeledCounter(title: LocalizedStringKey("amount_remaining"), amount: remaining)
|
|
|
|
}
|
|
|
|
}.frame(maxWidth: .infinity, alignment: .center)
|
|
|
|
}
|
2021-12-08 02:31:52 +00:00
|
|
|
|
2021-10-21 22:13:32 +00:00
|
|
|
.onAppear {
|
2022-01-03 17:56:43 +00:00
|
|
|
Task {
|
|
|
|
try await self.sum = transactionDataStore.sum(budgetId: nil, categoryId: category.id, from: nil, to: nil)
|
2021-10-21 22:13:32 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-22 02:43:01 +00:00
|
|
|
.navigationBarItems(trailing: Button(action: {
|
|
|
|
self.editingCategory = true
|
|
|
|
}) {
|
|
|
|
Text("edit")
|
|
|
|
}
|
|
|
|
)
|
|
|
|
.sheet(isPresented: self.$editingCategory, onDismiss: {
|
|
|
|
self.editingCategory = false
|
|
|
|
}, content: {
|
|
|
|
CategoryFormSheet(showSheet: self.$editingCategory, category: self.category, budgetId: self.category.budgetId)
|
|
|
|
})
|
2021-10-21 22:13:32 +00:00
|
|
|
}
|
|
|
|
|
2022-01-03 17:56:43 +00:00
|
|
|
init (_ category: TwigsCore.Category, budget: Budget) {
|
2021-10-21 22:13:32 +00:00
|
|
|
self.category = category
|
|
|
|
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 {
|
|
|
|
CategoryDetailsView(MockCategoryRepository.category, budget: MockBudgetRepository.budget)
|
|
|
|
.environmentObject(TransactionDataStore(MockTransactionRepository()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|