diff --git a/MMEX/View/Transaction/TransactionEditView.swift b/MMEX/View/Transaction/TransactionEditView.swift index 27502475..bd5a2fbf 100644 --- a/MMEX/View/Transaction/TransactionEditView.swift +++ b/MMEX/View/Transaction/TransactionEditView.swift @@ -18,6 +18,7 @@ struct TransactionEditView: View { @State private var selectedDate = Date() @State private var newSplit: TransactionSplitData = TransactionSplitData() // TODO: set default category ? + @State private var categoryUsed: Set = [] // app level setting @AppStorage("defaultPayeeSetting") private var defaultPayeeSetting: DefaultPayeeSetting = .none @@ -174,7 +175,7 @@ struct TransactionEditView: View { } .padding(.horizontal, 0) - ForEach(txn.splits) { split in + ForEach(txn.splits, id: \.self.categId) { split in HStack { Text(getCategoryName(for: split.categId)) .frame(maxWidth: .infinity, alignment: .leading) // Align to the left @@ -188,6 +189,10 @@ struct TransactionEditView: View { } .onDelete { indices in txn.splits.remove(atOffsets: indices) + categoryUsed = [] + for split in txn.splits { + categoryUsed.insert(split.categId) + } txn.transAmount = txn.splits.reduce(0.0) { $0 + $1.amount } } @@ -198,7 +203,9 @@ struct TransactionEditView: View { Text("Category").tag(DataId.void) } ForEach(categories) { category in - Text(category.fullName(with: viewModel.categDelimiter)).tag(category.id) + if !categoryUsed.contains(category.id) { + Text(category.fullName(with: viewModel.categDelimiter)).tag(category.id) + } } } .pickerStyle(MenuPickerStyle()) // Show a menu for the category picker @@ -218,6 +225,7 @@ struct TransactionEditView: View { Button(action: { withAnimation { txn.splits.append(newSplit) + categoryUsed.insert(newSplit.categId) txn.transAmount = txn.splits.reduce(0.0) { $0 + $1.amount } newSplit = TransactionSplitData() } @@ -261,6 +269,11 @@ struct TransactionEditView: View { if (txn.id.isVoid) { txn.status = defaultStatus } + + categoryUsed = [] + for split in txn.splits { + categoryUsed.insert(split.categId) + } } .onDisappear { // Resign the focus when the view disappears, hiding the keyboard