Fix errors with saving yearly recurring transactions

This commit is contained in:
William Brawner 2022-05-28 23:41:19 -06:00
parent ec5b98943a
commit 60f1f516b3
2 changed files with 17 additions and 22 deletions

View file

@ -101,15 +101,15 @@ class RecurringTransactionForm: ObservableObject {
func save() async { func save() async {
let amount = Double(self.amount) ?? 0.0 let amount = Double(self.amount) ?? 0.0
var frequencyUnit: FrequencyUnit var frequencyUnit: FrequencyUnit
switch self.frequencyUnit { switch self.baseFrequencyUnit {
case .daily: case "week":
frequencyUnit = .daily
case .weekly(_):
frequencyUnit = .weekly(self.daysOfWeek) frequencyUnit = .weekly(self.daysOfWeek)
case .monthly(_): case "month":
frequencyUnit = .monthly(self.dayOfMonth) frequencyUnit = .monthly(self.dayOfMonth)
case .yearly(_): case "year":
frequencyUnit = .yearly(self.dayOfYear) frequencyUnit = .yearly(self.dayOfYear)
default:
frequencyUnit = .daily
} }
let components = Calendar.current.dateComponents([.hour, .minute, .second], from: self.start) let components = Calendar.current.dateComponents([.hour, .minute, .second], from: self.start)
let time = Time(hours: components.hour!, minutes: components.minute!, seconds: components.second!)! let time = Time(hours: components.hour!, minutes: components.minute!, seconds: components.second!)!

View file

@ -11,15 +11,18 @@ import TwigsCore
struct YearlyFrequencyPicker: UIViewRepresentable { struct YearlyFrequencyPicker: UIViewRepresentable {
@Binding var dayOfYear: DayOfYear @Binding var dayOfYear: DayOfYear
@State var selectedMonth: Int { var selectedMonth: Int {
didSet { didSet {
selectedDay = min(self.selectedDay, DayOfYear.maxDays(inMonth: self.selectedMonth + 1) - 1) selectedDay = min(self.selectedDay, DayOfYear.maxDays(inMonth: self.selectedMonth + 1) - 1)
} }
} }
@State var selectedDay: Int { var selectedDay: Int {
didSet { didSet {
print("Upading selectedDay")
if let dayOfYear = DayOfYear(month: selectedMonth + 1, day: selectedDay + 1) { if let dayOfYear = DayOfYear(month: selectedMonth + 1, day: selectedDay + 1) {
self.dayOfYear = dayOfYear self.dayOfYear = dayOfYear
} else {
print("WARNING: Failed to set dayOfYear with \(selectedMonth) and \(selectedDay)")
} }
} }
} }
@ -31,7 +34,7 @@ struct YearlyFrequencyPicker: UIViewRepresentable {
} }
func makeCoordinator() -> YearlyFrequencyPicker.Coordinator { func makeCoordinator() -> YearlyFrequencyPicker.Coordinator {
Coordinator(self, selectedMonth: $selectedMonth, selectedDay: $selectedDay) Coordinator(self)
} }
func makeUIView(context: UIViewRepresentableContext<YearlyFrequencyPicker>) -> UIPickerView { func makeUIView(context: UIViewRepresentableContext<YearlyFrequencyPicker>) -> UIPickerView {
@ -64,20 +67,10 @@ struct YearlyFrequencyPicker: UIViewRepresentable {
] ]
var parent: YearlyFrequencyPicker var parent: YearlyFrequencyPicker
@Binding var selectedMonth: Int {
didSet {
// This is a workaround for the pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) function not getting the correct values for selectedMonth
month = self.selectedMonth
}
}
@Binding var selectedDay: Int
private var month = 0 private var month = 0
init(_ pickerView: YearlyFrequencyPicker, selectedMonth: Binding<Int>, selectedDay: Binding<Int> ) { init(_ pickerView: YearlyFrequencyPicker) {
self.parent = pickerView self.parent = pickerView
self._selectedMonth = selectedMonth
self._selectedDay = selectedDay
} }
func numberOfComponents(in pickerView: UIPickerView) -> Int { func numberOfComponents(in pickerView: UIPickerView) -> Int {
@ -101,9 +94,11 @@ struct YearlyFrequencyPicker: 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 {
selectedMonth = row parent.selectedMonth = row
month = row
} else { } else {
selectedDay = row parent.selectedDay = row
print("update selectedDay")
} }
} }
} }