Skip to content

Commit

Permalink
Merge pull request #192 from moneymanagerex/fix_split-1
Browse files Browse the repository at this point in the history
fix(#191): split categories
  • Loading branch information
georgeef authored Oct 30, 2024
2 parents 9c5944e + 3440a9b commit f716245
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions MMEX/View/Transaction/TransactionEditView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<DataId> = []

// app level setting
@AppStorage("defaultPayeeSetting") private var defaultPayeeSetting: DefaultPayeeSetting = .none
Expand Down Expand Up @@ -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
Expand All @@ -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 }
}

Expand All @@ -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
Expand All @@ -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()
}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit f716245

Please sign in to comment.