Fix errors with saving monthly recurring transactions

This commit is contained in:
William Brawner 2022-05-28 23:49:11 -06:00
parent 60f1f516b3
commit a61cc567fe

View file

@ -11,9 +11,25 @@ import TwigsCore
struct MonthlyFrequencyPicker: UIViewRepresentable { struct MonthlyFrequencyPicker: UIViewRepresentable {
@Binding var dayOfMonth: DayOfMonth @Binding var dayOfMonth: DayOfMonth
@State var dayOfWeek: Int var dayOfWeek: Int {
@State var intDay: Int didSet {
@State var ordinalDay: Int dayOfMonth = .ordinal(Ordinal.allCases[ordinalDay], DayOfWeek.allCases[dayOfWeek])
}
}
var intDay: Int {
didSet {
dayOfMonth = .fixed(intDay + 1)
}
}
var ordinalDay: Int {
didSet {
if ordinalDay == 0 {
dayOfMonth = .fixed(intDay + 1)
} else {
dayOfMonth = .ordinal(Ordinal.allCases[ordinalDay], DayOfWeek.allCases[dayOfWeek])
}
}
}
init(dayOfMonth: Binding<DayOfMonth>) { init(dayOfMonth: Binding<DayOfMonth>) {
self._dayOfMonth = dayOfMonth self._dayOfMonth = dayOfMonth
@ -33,7 +49,7 @@ struct MonthlyFrequencyPicker: UIViewRepresentable {
} }
func makeCoordinator() -> MonthlyFrequencyPicker.Coordinator { func makeCoordinator() -> MonthlyFrequencyPicker.Coordinator {
Coordinator(self, selectedOrdinal: $ordinalDay, selectedDay: $intDay, selectedDayOfWeek: $dayOfWeek) Coordinator(self)
} }
func makeUIView(context: UIViewRepresentableContext<MonthlyFrequencyPicker>) -> UIPickerView { func makeUIView(context: UIViewRepresentableContext<MonthlyFrequencyPicker>) -> UIPickerView {
@ -56,22 +72,10 @@ struct MonthlyFrequencyPicker: UIViewRepresentable {
} }
var parent: MonthlyFrequencyPicker var parent: MonthlyFrequencyPicker
@Binding var selectedOrdinal: Int {
didSet {
// This is a workaround for the pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) function not getting the correct values for the selectedOrdinal
ordinal = self.selectedOrdinal
}
}
@Binding var selectedDay: Int
@Binding var selectedDayOfWeek: Int
private var ordinal = 0 private var ordinal = 0
init(_ pickerView: MonthlyFrequencyPicker, selectedOrdinal: Binding<Int>, selectedDay: Binding<Int>, selectedDayOfWeek: Binding<Int>) { init(_ pickerView: MonthlyFrequencyPicker) {
self.parent = pickerView self.parent = pickerView
self._selectedOrdinal = selectedOrdinal
self._selectedDay = selectedDay
self._selectedDayOfWeek = selectedDayOfWeek
} }
func numberOfComponents(in pickerView: UIPickerView) -> Int { func numberOfComponents(in pickerView: UIPickerView) -> Int {
@ -103,13 +107,14 @@ struct MonthlyFrequencyPicker: UIViewRepresentable {
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if component == 0 { if component == 0 {
selectedOrdinal = row parent.ordinalDay = row
ordinal = row
return return
} }
if ordinal == 0 { if ordinal == 0 {
selectedDay = row parent.intDay = row
} else { } else {
selectedDayOfWeek = row parent.dayOfWeek = row
} }
} }
} }