Skip to content

Commit

Permalink
Merge pull request #65 from moneymanagerex/txn
Browse files Browse the repository at this point in the history
Honor last used payee to pop payee & category
  • Loading branch information
guanlisheng authored Sep 29, 2024
2 parents 46ea193 + 63d9523 commit 691201f
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
3 changes: 1 addition & 2 deletions MMEX/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ struct ContentView: View {
@EnvironmentObject var dataManager: DataManager

var body: some View {
print("DEBUG: ContentView.body")
return ZStack {
ZStack {
if dataManager.isDatabaseConnected {
connectedView
} else {
Expand Down
15 changes: 15 additions & 0 deletions MMEX/Repositories/TransactionRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,19 @@ extension TransactionRepository {

return select(from: table)
}

// TODO: update payee's category mapping after insert & update ?

// Fetch the latest record, filtered by account (optional)
func latest(accountID: Int64? = nil) -> TransactionData? {
var query = Self.table.order(Self.col_id.desc) // Order by descending ID

// If accountID is provided, add it to the filter
if let accountID = accountID {
query = query.filter(Self.col_accountId == accountID)
}

// Pluck the latest row
return pluck(key: "latests", from: query)
}
}
1 change: 1 addition & 0 deletions MMEX/Views/Transactions/TransactionDetailView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,5 @@ struct TransactionDetailView: View {
categories: .constant(CategoryData.sampleData),
accounts: .constant(AccountData.sampleData)
)
.environmentObject(DataManager())
}
16 changes: 15 additions & 1 deletion MMEX/Views/Transactions/TransactionEditView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import SwiftUI

struct TransactionEditView: View {
@Binding var txn: TransactionData
@EnvironmentObject var dataManager: DataManager // Access DataManager from environment
@State private var amountString: String = "0" // Temporary storage for numeric input as a string
@State private var selectedDate = Date()

Expand Down Expand Up @@ -147,7 +148,7 @@ struct TransactionEditView: View {
if self.payees.count == 1 {
txn.payeeId = self.payees.first!.id
} else if (defaultPayeeSetting == DefaultPayeeSetting.lastUsed) {
// TODO
loadLatestTxn()
}

if self.accounts.count == 1 {
Expand All @@ -167,6 +168,18 @@ struct TransactionEditView: View {
isAmountFocused = false
}
}

func loadLatestTxn() {
if let latestTxn = dataManager.transactionRepository?.latest(accountID: txn.accountId) ?? dataManager.transactionRepository?.latest() {
// Update UI on the main thread
DispatchQueue.main.async {
if (defaultPayeeSetting == DefaultPayeeSetting.lastUsed && txn.payeeId == 0) {
txn.payeeId = latestTxn.payeeId
txn.categId = latestTxn.categId
}
}
}
}
}

#Preview {
Expand All @@ -176,4 +189,5 @@ struct TransactionEditView: View {
categories: .constant(CategoryData.sampleData),
accounts: .constant(AccountData.sampleData)
)
.environmentObject(DataManager())
}

0 comments on commit 691201f

Please sign in to comment.