Skip to content

Commit

Permalink
Merge pull request #127 from moneymanagerex/txn
Browse files Browse the repository at this point in the history
TxnListView with year filter for performance
  • Loading branch information
guanlisheng authored Oct 7, 2024
2 parents 32efddf + 782ad05 commit 8154ef3
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
40 changes: 33 additions & 7 deletions MMEX/View/Transaction/TransactionListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ struct TransactionListView: View {
@State private var newTxn = TransactionData()
@State private var isPresentingTransactionAddView = false

// State variables for date filtering
@State private var selectedYear = Calendar.current.component(.year, from: Date())

var body: some View {
Group {
List($viewModel.txns) { $txn in
Expand Down Expand Up @@ -57,19 +60,34 @@ struct TransactionListView: View {
}
}
.toolbar {
Button(action: {
isPresentingTransactionAddView = true
}, label: {
Image(systemName: "plus")
})
.accessibilityLabel("New Transaction")
// Year Picker
ToolbarItem(placement: .navigation) {
Picker("Year", selection: $selectedYear) {
ForEach((2010...Calendar.current.component(.year, from: Date())).reversed(), id: \.self) { year in
Text(String(format: "%d", year)).tag(year) // Correct year format
}
}
.pickerStyle(MenuPickerStyle()) // Show as a menu
.onChange(of: selectedYear) {
filterTransactions()
}
}

ToolbarItem(placement: .navigation) {
Button(action: {
isPresentingTransactionAddView = true
}, label: {
Image(systemName: "plus")
})
.accessibilityLabel("New Transaction")
}
}
}
.onAppear {
viewModel.loadAccounts()
viewModel.loadCategories()
viewModel.loadPayees()
viewModel.loadTransactions()
filterTransactions()

// database level setting
let repository = env.infotableRepository
Expand Down Expand Up @@ -116,6 +134,14 @@ struct TransactionListView: View {
return isoDate
*/
}

// Filter transactions based on selected year
func filterTransactions() {
let startDate = Calendar.current.date(from: DateComponents(year: selectedYear, month: 1, day: 1)) ?? Date()
let endDate = Calendar.current.date(from: DateComponents(year: selectedYear + 1, month: 1, day: 1))?.addingTimeInterval(-1) ?? Date()

viewModel.loadTransactions(for: nil, startDate: startDate, endDate: endDate)
}
}

#Preview {
Expand Down
4 changes: 2 additions & 2 deletions MMEX/ViewModel/TransactionViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,9 @@ class TransactionViewModel: ObservableObject {
}
}

func loadTransactions(for accountId: Int64? = nil) {
func loadTransactions(for accountId: Int64? = nil, startDate: Date? = nil, endDate: Date? = nil) {
DispatchQueue.global(qos: .background).async {
var loadedTransactions = self.transactionRepo?.loadRecent(accountId: accountId) ?? []
var loadedTransactions = self.transactionRepo?.loadRecent(accountId: accountId, startDate: startDate, endDate: endDate) ?? []
for i in loadedTransactions.indices {
// TODO other better indicator
if loadedTransactions[i].categId <= 0 {
Expand Down

0 comments on commit 8154ef3

Please sign in to comment.