Skip to content

Commit

Permalink
Define frequency UI
Browse files Browse the repository at this point in the history
  • Loading branch information
Supereg committed Dec 23, 2024
1 parent d3360a3 commit 5c7b36f
Show file tree
Hide file tree
Showing 14 changed files with 302 additions and 313 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,42 +24,53 @@ struct AddMedicationSchedule: View {

@State private var viewModel = CreateScheduleViewModel() // TODO: integrate this new model!

@FocusState private var hasFocus: Bool

var body: some View {
VStack(spacing: 0) {
Form {
titleSection
.onTapGesture {
// TODO: we can do that with focus states!
UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
.onTapGesture { // TODO: should we use that?
hasFocus = false
}

Section {
EditFrequencyButton(model: $viewModel)

if case .interval = viewModel.selection {
ScheduleIntervalPicker(model: $viewModel)
}
EditFrequency(frequency: $frequency, startDate: $startDate, model: $viewModel)
} header: {
Text("When will you take this?")
}
.headerProminence(.increased) // TODO: use that always?

EditScheduleTime(times: $times, model: $viewModel)
}
.focused($hasFocus)

VStack(alignment: .center) {
AsyncButton(
action: {
// TODO: restore!
/*
viewModel.medicationInstances.append(
viewModel.createMedicationInstance(
medicationOption,
dosage,
Schedule(frequency: frequency, times: times, startDate: startDate)
)
)*/
// TODO: viewModel.medicationInstances.sort()
dismiss()
},
label: {
Text("Add Medication", bundle: .module)
.frame(maxWidth: .infinity, minHeight: 38)
}
)
AsyncButton {
// TODO: restore!
/*
viewModel.medicationInstances.append(
viewModel.createMedicationInstance(
medicationOption,
dosage,
Schedule(frequency: frequency, times: times, startDate: startDate)
)
)*/
// TODO: viewModel.medicationInstances.sort()
dismiss()
} label: {
Text("Add Medication", bundle: .module)
.frame(maxWidth: .infinity, minHeight: 38)
}
.buttonStyle(.borderedProminent)
}
.padding()
.background {
// TODO: weird?
Color(uiColor: .systemGroupedBackground)
.edgesIgnoringSafeArea(.bottom)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,78 +11,36 @@ import SpeziViews
import SwiftUI


struct EditFrequency: View {
@Binding private var frequency: Frequency
@Binding private var startDate: Date
@State private var showFrequencySheet = false
struct EditFrequency: View { // TODO: remove it!
@Binding private var model: CreateScheduleViewModel


var body: some View {
Section {
Button {
showFrequencySheet.toggle()
} label: {
LabeledContent {
Text("Change")
.foregroundStyle(Color.accentColor)
} label: {
Text(frequency.description)
.foregroundStyle(Color.primary)
}
}
EditFrequencyButton(model: $model)

if case .interval = model.selection {
Picker("Interval", selection: $model.dayInterval) {
ForEach(2..<366) { day in
Group {
if day == 2 {
Text("Every Other Day", bundle: .module)
} else {
Text("Every \(day) Days", bundle: .module)
}
}
.tag(day)
}
}
.tint(Color.accentColor)
ScheduleIntervalPicker(model: $model)
}
} header: {
Text("When will you take this?")
}
.headerProminence(.increased) // TODO: bit weird?
.sheet(isPresented: $showFrequencySheet) { // TODO: does this sheet need to be placed outside the list?
ScheduleFrequencyView(frequency: $frequency, startDate: $startDate, model: $model)
}
}


init(frequency: Binding<Frequency>, startDate: Binding<Date>, model: Binding<CreateScheduleViewModel>) {
self._frequency = frequency
self._startDate = startDate
init(model: Binding<CreateScheduleViewModel>) {
self._model = model
}
}


#if DEBUG
#Preview {
@Previewable @State var frequency: Frequency = .regularDayIntervals(1)
@Previewable @State var date: Date = .now
@Previewable @State var model = CreateScheduleViewModel()

List {
EditFrequency(frequency: $frequency, startDate: $date, model: $model)
}
}

#Preview {
@Previewable @State var frequency: Frequency = .regularDayIntervals(1)
@Previewable @State var date: Date = .now
@Previewable @State var model = CreateScheduleViewModel(selection: .interval)

List {
EditFrequency(frequency: $frequency, startDate: $date, model: $model)
EditFrequency(model: $model)
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ struct EditMedication<MI: MedicationInstance>: View {

@Binding private var medicationInstance: MI

@State var model = CreateScheduleViewModel() // TODO: these needs to come from somewhere!
// TODO: (e.g., $medicationInstance.schedule.frequency)
@State var model = CreateScheduleViewModel() // TODO: these needs to come from somewhere! and update it back


var body: some View {
Expand All @@ -27,12 +28,8 @@ struct EditMedication<MI: MedicationInstance>: View {
// TODO: EditDosage(dosage: $medicationInstance.dosage, medication: medicationInstance.type, initialDosage: medicationInstance.dosage)
// TODO: .labelsHidden()
}
Section(String(localized: "Schedule", bundle: .module)) {
EditFrequency(
frequency: $medicationInstance.schedule.frequency,
startDate: $medicationInstance.schedule.startDate,
model: $model
)
Section(String(localized: "Schedule", bundle: .module)) { // TODO: e.g., double section!
EditFrequency(model: $model)
}
Section(String(localized: "Schedule Times", bundle: .module)) {
EditScheduleTime(times: $medicationInstance.schedule.times, model: $model)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ struct EditScheduleTime: View {
var body: some View {
Section {
if case .weekdayBased = model.selection {
Section {
WeekdaysPicker(selection: $model.weekdays)
}
// TODO: disable days, if other day selection is already done.
WeekdaysPicker(selection: $model.weekdays)
}
if !model.times.isEmpty {
/*ForEach($model.times) { time in
Expand All @@ -42,19 +41,15 @@ struct EditScheduleTime: View {
}

private var addTimeButton: some View {
Button(
action: {
addNewTime()
},
label: {
HStack {
Image(systemName: "plus.circle.fill")
.accessibilityHidden(true)
.foregroundStyle(Color.green)
Text("Add a time")
}
Button(action: addNewTime) {
Label {
Text("Add a time", bundle: .module)
} icon: {
Image(systemName: "plus.circle.fill")
.accessibilityHidden(true)
.foregroundStyle(Color.green)
}
)
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,13 @@ struct EditScheduleTimeRow: View {

var body: some View {
HStack {
Button(
action: {
// TODO: times.removeAll(where: { $0.id == time.id })
},
label: {
Image(systemName: "minus.circle.fill")
.accessibilityLabel(Text("Delete", bundle: .module))
.foregroundStyle(Color.red)
}
)
Button {
// TODO: times.removeAll(where: { $0.id == time.id })
} label: {
Image(systemName: "minus.circle.fill")
.accessibilityLabel(Text("Delete", bundle: .module))
.foregroundStyle(Color.red)
}
.buttonStyle(.borderless)

ScheduledTimeDatePicker(
Expand Down
Loading

0 comments on commit 5c7b36f

Please sign in to comment.