Move category header into transaction list for style consistency

This commit is contained in:
William Brawner 2021-12-07 19:31:52 -07:00
parent 7d8ec85f05
commit 8aa5119e3e
3 changed files with 29 additions and 10 deletions

View file

@ -39,7 +39,7 @@ struct CategoryDetailsView: View {
}
var body: some View {
VStack {
TransactionListView(self.budget, category: category, header: VStack {
Text(verbatim: category.description ?? "")
.padding()
HStack {
@ -47,8 +47,8 @@ struct CategoryDetailsView: View {
LabeledCounter(title: middleLabel, amount: spent)
LabeledCounter(title: LocalizedStringKey("amount_remaining"), amount: remaining)
}
TransactionListView(self.budget, category: category)
}.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .top)
}.frame(maxWidth: .infinity, alignment: .center).eraseToAnyView())
.onAppear {
if sumRequest == "" || !sumRequest.contains(category.id) {
sumRequest = transactionDataStore.sum(budgetId: nil, categoryId: category.id, from: nil, to: nil)

View file

@ -7,6 +7,7 @@
//
import Foundation
import SwiftUI
extension Int {
func toDecimalString() -> String {
@ -22,3 +23,8 @@ extension Int {
}
}
extension View {
func eraseToAnyView() -> AnyView {
return AnyView(self)
}
}

View file

@ -13,12 +13,22 @@ struct TransactionListView: View {
@EnvironmentObject var transactionDataStore: TransactionDataStore
@State var requestId: String = ""
@State var isAddingTransaction = false
let header: AnyView?
@ViewBuilder
var body: some View {
switch transactionDataStore.transactions[requestId] {
case .success(let transactions):
Group {
if transactions.isEmpty {
Text("no_transactions")
} else {
List {
if let header = header {
Section {
header
}
}
ForEach(transactions.keys, id: \.self) { (key: String) in
Group {
Section(header: Text(key)) {
@ -29,6 +39,8 @@ struct TransactionListView: View {
}
}
}
}
}
.sheet(isPresented: $isAddingTransaction, content: {
AddTransactionView(showSheet: $isAddingTransaction, budgetId: self.budget.id)
.navigationBarTitle("add_transaction")
@ -62,9 +74,10 @@ struct TransactionListView: View {
let budget: Budget
let category: Category?
init(_ budget: Budget, category: Category? = nil) {
init(_ budget: Budget, category: Category? = nil, header: AnyView? = nil) {
self.budget = budget
self.category = category
self.header = header
}
}