diff --git a/MMEX/ContentView.swift b/MMEX/ContentView.swift index 76dbb7e..fe52f9f 100644 --- a/MMEX/ContentView.swift +++ b/MMEX/ContentView.swift @@ -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 { diff --git a/MMEX/Repositories/TransactionRepository.swift b/MMEX/Repositories/TransactionRepository.swift index acb866e..c3b0433 100644 --- a/MMEX/Repositories/TransactionRepository.swift +++ b/MMEX/Repositories/TransactionRepository.swift @@ -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) + } } diff --git a/MMEX/Views/Transactions/TransactionDetailView.swift b/MMEX/Views/Transactions/TransactionDetailView.swift index 03ac594..1f0ad29 100644 --- a/MMEX/Views/Transactions/TransactionDetailView.swift +++ b/MMEX/Views/Transactions/TransactionDetailView.swift @@ -156,4 +156,5 @@ struct TransactionDetailView: View { categories: .constant(CategoryData.sampleData), accounts: .constant(AccountData.sampleData) ) + .environmentObject(DataManager()) } diff --git a/MMEX/Views/Transactions/TransactionEditView.swift b/MMEX/Views/Transactions/TransactionEditView.swift index 720a62c..ca5b0da 100644 --- a/MMEX/Views/Transactions/TransactionEditView.swift +++ b/MMEX/Views/Transactions/TransactionEditView.swift @@ -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() @@ -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 { @@ -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 { @@ -176,4 +189,5 @@ struct TransactionEditView: View { categories: .constant(CategoryData.sampleData), accounts: .constant(AccountData.sampleData) ) + .environmentObject(DataManager()) }