generated from StanfordBDHG/SwiftPackageTemplate
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updates Medication Schedule & Dosage Selection (#5)
# Updates Medication Schedule & Dosage Selection ## ⚙️ Release Notes - Associates a dosage with a medication - Updates the API surface including convenience methods for getting dates from the schedule - Improve the date picker logic to only submit the date once the picker is dismissed - Improve encoding format - Improve Schedule and ScheduledTime with additional conformances - Improve UI and accessibility setup ## 📝 Code of Conduct & Contributing Guidelines By submitting creating this pull request, you agree to follow our [Code of Conduct](https://github.com/StanfordSpezi/.github/blob/main/CODE_OF_CONDUCT.md) and [Contributing Guidelines](https://github.com/StanfordSpezi/.github/blob/main/CONTRIBUTING.md): - [x] I agree to follow the [Code of Conduct](https://github.com/StanfordSpezi/.github/blob/main/CODE_OF_CONDUCT.md) and [Contributing Guidelines](https://github.com/StanfordSpezi/.github/blob/main/CONTRIBUTING.md).
- Loading branch information
1 parent
c386e66
commit 8aa11bd
Showing
16 changed files
with
455 additions
and
129 deletions.
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
Sources/SpeziMedication/Extensions/UIDatePicker+SwiftUI.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// | ||
// This source file is part of the Stanford Spezi open-source project | ||
// | ||
// SPDX-FileCopyrightText: 2022 Stanford University and the project authors (see CONTRIBUTORS.md) | ||
// | ||
// SPDX-License-Identifier: MIT | ||
// | ||
|
||
import SwiftUI | ||
|
||
|
||
struct ScheduledTimeDatePicker: UIViewRepresentable { | ||
class Coordinator: NSObject { | ||
private var lastDate: Date | ||
private let date: Binding<Date> | ||
private let excludedDates: [Date] | ||
|
||
|
||
fileprivate init(date: Binding<Date>, excludedDates: [Date]) { | ||
self.date = date | ||
self.lastDate = date.wrappedValue | ||
self.excludedDates = excludedDates | ||
} | ||
|
||
|
||
@objc | ||
fileprivate func valueChanged(datePicker: UIDatePicker, forEvent event: UIEvent) { | ||
guard !excludedDates.contains(datePicker.date) else { | ||
datePicker.date = lastDate | ||
return | ||
} | ||
|
||
lastDate = datePicker.date | ||
} | ||
|
||
@objc | ||
fileprivate func editingDidEnd(datePicker: UIDatePicker, forEvent event: UIEvent) { | ||
self.date.wrappedValue = datePicker.date | ||
} | ||
} | ||
|
||
|
||
static let minuteInterval = 5 | ||
|
||
|
||
@Binding private var date: Date | ||
private let excludedDates: [Date] | ||
|
||
|
||
init(date: Binding<Date>, excludedDates: [Date]) { | ||
self._date = date | ||
self.excludedDates = excludedDates | ||
} | ||
|
||
|
||
func makeCoordinator() -> Self.Coordinator { | ||
Coordinator(date: $date, excludedDates: excludedDates) | ||
} | ||
|
||
func makeUIView(context: Context) -> UIDatePicker { | ||
let datePicker = UIDatePicker() | ||
datePicker.datePickerMode = .time | ||
datePicker.preferredDatePickerStyle = .compact | ||
datePicker.minuteInterval = Self.minuteInterval | ||
datePicker.addTarget(context.coordinator, action: #selector(Coordinator.valueChanged), for: .valueChanged) | ||
datePicker.addTarget(context.coordinator, action: #selector(Coordinator.editingDidEnd), for: .editingDidEnd) | ||
return datePicker | ||
} | ||
|
||
func updateUIView(_ datePicker: UIDatePicker, context: Context) { | ||
datePicker.date = date | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// | ||
// This source file is part of the Stanford Spezi open-source project | ||
// | ||
// SPDX-FileCopyrightText: 2022 Stanford University and the project authors (see CONTRIBUTORS.md) | ||
// | ||
// SPDX-License-Identifier: MIT | ||
// | ||
|
||
import Foundation | ||
import SwiftUI | ||
|
||
|
||
@Observable | ||
public class ScheduledTime: Codable, Identifiable, Hashable, Equatable, Comparable { | ||
enum CodingKeys: CodingKey { | ||
case time | ||
case dosage | ||
} | ||
|
||
|
||
public var time: DateComponents | ||
public var dosage: Double | ||
|
||
|
||
public var id: String { | ||
"\(time.hour ?? 0):\(time.minute ?? 0)" | ||
} | ||
|
||
var date: Date { | ||
Calendar.current.date(bySettingHour: self.time.hour ?? 0, minute: self.time.minute ?? 0, second: 0, of: .now) ?? .now | ||
} | ||
|
||
var dateBinding: Binding<Date> { | ||
Binding( | ||
get: { | ||
self.date | ||
}, | ||
set: { newValue in | ||
self.time = Calendar.current.dateComponents([.hour, .minute], from: newValue) | ||
} | ||
) | ||
} | ||
|
||
|
||
public init(time: DateComponents, dosage: Double = 1.0) { | ||
precondition(time.hour != nil && time.minute != nil) | ||
|
||
self.time = time | ||
self.dosage = dosage | ||
} | ||
|
||
public convenience init(date: Date, dosage: Double = 1.0) { | ||
self.init(time: Calendar.current.dateComponents([.hour, .minute], from: date), dosage: dosage) | ||
} | ||
|
||
public required init(from decoder: Decoder) throws { | ||
let container = try decoder.container(keyedBy: CodingKeys.self) | ||
self.time = try container.decode(DateComponents.self, forKey: .time) | ||
self.dosage = try container.decode(Double.self, forKey: .dosage) | ||
} | ||
|
||
|
||
public static func == (lhs: ScheduledTime, rhs: ScheduledTime) -> Bool { | ||
lhs.time.hour == rhs.time.hour && lhs.time.minute == rhs.time.minute | ||
} | ||
|
||
public static func < (lhs: ScheduledTime, rhs: ScheduledTime) -> Bool { | ||
if lhs.time.hour == rhs.time.hour { | ||
return lhs.time.minute ?? 0 < rhs.time.minute ?? 0 | ||
} | ||
|
||
return lhs.time.hour ?? 0 < rhs.time.hour ?? 0 | ||
} | ||
|
||
public func hash(into hasher: inout Hasher) { | ||
hasher.combine(id) | ||
} | ||
|
||
public func encode(to encoder: Encoder) throws { | ||
var container = encoder.container(keyedBy: CodingKeys.self) | ||
try container.encode(self.time, forKey: .time) | ||
try container.encode(self.dosage, forKey: .dosage) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.