diff --git a/Twigs/Category/CategoryDetailsView.swift b/Twigs/Category/CategoryDetailsView.swift index 820e441..b70a282 100644 --- a/Twigs/Category/CategoryDetailsView.swift +++ b/Twigs/Category/CategoryDetailsView.swift @@ -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) diff --git a/Twigs/Extensions.swift b/Twigs/Extensions.swift index a183833..1c2c6b4 100644 --- a/Twigs/Extensions.swift +++ b/Twigs/Extensions.swift @@ -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) + } +} diff --git a/Twigs/Transaction/TransactionListView.swift b/Twigs/Transaction/TransactionListView.swift index b114a3a..bb0d002 100644 --- a/Twigs/Transaction/TransactionListView.swift +++ b/Twigs/Transaction/TransactionListView.swift @@ -13,17 +13,29 @@ 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): - List { - ForEach(transactions.keys, id: \.self) { (key: String) in - Group { - Section(header: Text(key)) { - ForEach(transactions[key]!) { transaction in - TransactionListItemView(transaction) + 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)) { + ForEach(transactions[key]!) { transaction in + TransactionListItemView(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 } }