Add search functionality to transaction lists

This commit is contained in:
William Brawner 2021-12-22 17:15:04 -07:00
parent 20b56fb05a
commit bcab5fa078

View file

@ -14,6 +14,7 @@ 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
@State var search: String = ""
let header: AnyView? let header: AnyView?
@ViewBuilder @ViewBuilder
@ -28,8 +29,13 @@ struct TransactionListView: View {
} }
ForEach(transactions.keys, id: \.self) { (key: String) in ForEach(transactions.keys, id: \.self) { (key: String) in
Group { Group {
let filtered = search.isEmpty ? transactions[key]! : transactions[key]!.filter { $0.title.lowercased().contains(search.lowercased())
|| $0.description?.lowercased().contains(search.lowercased()) ?? false
|| $0.amount.toCurrencyString().contains(search)
}
if !filtered.isEmpty {
Section(header: Text(key)) { Section(header: Text(key)) {
ForEach(transactions[key]!) { transaction in ForEach(filtered) { transaction in
TransactionListItemView(transaction) TransactionListItemView(transaction)
} }
} }
@ -37,6 +43,7 @@ struct TransactionListView: View {
} }
} }
} }
}
@ViewBuilder @ViewBuilder
var body: some View { var body: some View {
@ -45,6 +52,7 @@ struct TransactionListView: View {
List { List {
TransactionList(transactions) TransactionList(transactions)
} }
.searchable(text: $search)
.sheet(isPresented: $isAddingTransaction, content: { .sheet(isPresented: $isAddingTransaction, content: {
AddTransactionView(showSheet: $isAddingTransaction, budgetId: self.budget.id) AddTransactionView(showSheet: $isAddingTransaction, budgetId: self.budget.id)
.navigationBarTitle("add_transaction") .navigationBarTitle("add_transaction")