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

View file

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

View file

@ -13,17 +13,29 @@ struct TransactionListView: View {
@EnvironmentObject var transactionDataStore: TransactionDataStore @EnvironmentObject var transactionDataStore: TransactionDataStore
@State var requestId: String = "" @State var requestId: String = ""
@State var isAddingTransaction = false @State var isAddingTransaction = false
let header: AnyView?
@ViewBuilder @ViewBuilder
var body: some View { var body: some View {
switch transactionDataStore.transactions[requestId] { switch transactionDataStore.transactions[requestId] {
case .success(let transactions): case .success(let transactions):
List { Group {
ForEach(transactions.keys, id: \.self) { (key: String) in if transactions.isEmpty {
Group { Text("no_transactions")
Section(header: Text(key)) { } else {
ForEach(transactions[key]!) { transaction in List {
TransactionListItemView(transaction) 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 budget: Budget
let category: Category? let category: Category?
init(_ budget: Budget, category: Category? = nil) { init(_ budget: Budget, category: Category? = nil, header: AnyView? = nil) {
self.budget = budget self.budget = budget
self.category = category self.category = category
self.header = header
} }
} }