Skip to content

Commit

Permalink
Fix setPositions on schedule creation
Browse files Browse the repository at this point in the history
  • Loading branch information
Supereg committed Dec 23, 2024
1 parent 5c7b36f commit 29a6769
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import SwiftUI


// TODO: why was that needed?
// TODO: why was that needed? => 5 minute interval
struct ScheduledTimeDatePicker: UIViewRepresentable {
@MainActor
class Coordinator: NSObject {
Expand All @@ -28,7 +28,7 @@ struct ScheduledTimeDatePicker: UIViewRepresentable {
@objc
fileprivate func valueChanged(datePicker: UIDatePicker, forEvent event: UIEvent) {
guard !excludedDates.contains(datePicker.date) else {
datePicker.date = lastDate // TODO: we just add 5 every time!
datePicker.date = lastDate // TODO: we just add 5 every time! (or subtract!)
return
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,17 @@ struct AddMedicationSchedule: View {
}
.navigationTitle("Medication Schedule")
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItemGroup(placement: .keyboard) {
Spacer()
Button {
hasFocus = false
} label: {
Text("Done", bundle: .module)
}
.bold()
}
}
}

@ViewBuilder private var titleSection: some View {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ struct EditScheduleTimeRow: View {

@Binding private var scheduledDosage: ScheduleDosage

@FocusState private var dosageFieldIsFocused: Bool


private let numberOfDosageFormatter: NumberFormatter = {
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
Expand All @@ -42,21 +39,24 @@ struct EditScheduleTimeRow: View {
}
.buttonStyle(.borderless)

ScheduledTimeDatePicker(
date: $scheduledDosage.time.animation(),
excludedDates: [] // TODO: times.map(\.date)
)
.frame(width: 100)
ScheduledTimeDatePicker(date: $scheduledDosage.time.animation(), excludedDates: [])
.frame(maxWidth: 70)
// TODO: excluded: times.map(\.date)

Spacer()
dosageTextField

TextField(value: $scheduledDosage.quantity, formatter: numberOfDosageFormatter) {
Text("Quantity", bundle: .module)
}
.textFieldStyle(.roundedBorder)
.keyboardType(.decimalPad)
.frame(maxWidth: 90)
}
.background {
Color.clear
.frame(maxWidth: .infinity, maxHeight: .infinity)
.contentShape(Rectangle())
.onTapGesture {
dosageFieldIsFocused = false
}
// TODO: add somewhere? .onTapGesture { dosageFieldIsFocused = false }
.padding(-32)
}
.onChange(of: scheduledDosage.time) {
Expand All @@ -65,32 +65,6 @@ struct EditScheduleTimeRow: View {
}
}
}

private var dosageTextField: some View {
TextField(
String(localized: "Quantity", bundle: .module),
value: $scheduledDosage.quantity,
formatter: numberOfDosageFormatter
)
.focused($dosageFieldIsFocused)
.textFieldStyle(.roundedBorder)
.keyboardType(.decimalPad)
.toolbar {
ToolbarItemGroup(placement: .keyboard) {
Spacer()
Button(
action: {
dosageFieldIsFocused = false
},
label: {
Text("Done")
}
)
}
}
.frame(maxWidth: 90)
}


init(scheduledDosage: Binding<ScheduleDosage>, form: MedicationType?) {
self.form = form
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,19 @@ public struct CreateScheduleViewModel {
}


// TODO: days and minutes doesn't work right?
// TODO: unit tests the setPositions!
let recurrence = Calendar.RecurrenceRule.daily(
calendar: .current,
interval: interval,
end: end,
weekdays: weekdays,
hours: hours,
minutes: minutes
hours: hours, // e.g., 13, 15, 16
minutes: minutes, // e.g., 30, 40, 50
// Without set positions we get 13:30, 13:40, 13:50, 15:30, 15:40, 15:50, 16:30, 16:40 and 16:50 (all permutations)
// We would specify setPositions [1, 5, 9] to take the first, fifth and 9th element of all occurrences: 13:30, 15:40, 16:50.
setPositions: (0..<times.count).map {
($0 * times.count) + $0 + 1
}
)

return SpeziScheduler.Schedule(startingAt: start, recurrence: recurrence)
Expand Down

0 comments on commit 29a6769

Please sign in to comment.