-
Notifications
You must be signed in to change notification settings - Fork 11
Adding SwiftUI wrapper for DurationPicker
#5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
7f9913f
d3e1019
7c59552
42aa2a8
bcd2363
ab8e38f
d1a8f96
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,188 @@ | ||||||||
| /// MIT License | ||||||||
| /// | ||||||||
| /// Copyright (c) 2025 Vis Fitness Inc. | ||||||||
| /// | ||||||||
| /// Permission is hereby granted, free of charge, to any person obtaining a copy | ||||||||
| /// of this software and associated documentation files (the "Software"), to deal | ||||||||
| /// in the Software without restriction, including without limitation the rights | ||||||||
| /// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||||||||
| /// copies of the Software, and to permit persons to whom the Software is | ||||||||
| /// furnished to do so, subject to the following conditions: | ||||||||
| /// | ||||||||
| /// The above copyright notice and this permission notice shall be included in all | ||||||||
| /// copies or substantial portions of the Software. | ||||||||
| /// | ||||||||
| /// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||||||||
| /// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||||||||
| /// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||||||||
| /// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||||||||
| /// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||||||||
| /// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||||||||
| /// SOFTWARE. | ||||||||
|
|
||||||||
| import SwiftUI | ||||||||
| import DurationPicker | ||||||||
|
|
||||||||
| /// A customizable control for inputting time values ranging between 0 and 24 hours. It serves as a drop-in replacement of [UIDatePicker](https://developer.apple.com/documentation/uikit/uidatepicker) with [countDownTimer](https://developer.apple.com/documentation/uikit/uidatepicker/mode/countdowntimer) mode with additional functionality for time input. | ||||||||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Update this to match the SwiftUI verbiage |
||||||||
| /// | ||||||||
| /// You can use a duration picker to allow a user to enter a time interval between 0 and 24 hours. | ||||||||
| public struct DurationPickerView: UIViewRepresentable { | ||||||||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would prefer simply Edit: Ah strike this. This would introduce a naming conflict in the project with the UIKit version |
||||||||
| public init(_ duration: Binding<TimeInterval>, components: Components = .hourMinuteSecond, hourInterval: Int = 1, minuteInterval: Int = 1, secondInterval: Int = 1, minumumDuration: TimeInterval? = nil, maximumDuration: TimeInterval? = nil) { | ||||||||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Add vertical space
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Everything besides Also I think |
||||||||
| self._duration = duration | ||||||||
| self.mode = components | ||||||||
| self.hourInterval = hourInterval | ||||||||
| self.minuteInterval = minuteInterval | ||||||||
| self.secondInterval = secondInterval | ||||||||
| self.minumumDuration = minumumDuration | ||||||||
| self.maximumDuration = maximumDuration | ||||||||
| } | ||||||||
|
|
||||||||
| // This has to be public to comply with UIViewRepresentable, but we don't actually want it to show up in the doc. | ||||||||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This comment is unnecessary here and elsewhere |
||||||||
| @_documentation(visibility: internal) | ||||||||
| public typealias UIViewType = DurationPicker | ||||||||
|
|
||||||||
| /// The components displayed by the duration picker. | ||||||||
| /// | ||||||||
| /// The mode determines which combination of hours, minutes, and seconds are displayed. You can set and retrieve the mode value through the ``DurationPickerView/mode`` property. | ||||||||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This comment is not accurate |
||||||||
| public typealias Components = DurationPicker.Mode | ||||||||
|
|
||||||||
| @Binding private var duration: TimeInterval | ||||||||
|
|
||||||||
| private var mode: Components | ||||||||
|
|
||||||||
| private var hourInterval: Int | ||||||||
| private var minuteInterval: Int | ||||||||
| private var secondInterval: Int | ||||||||
| private var minumumDuration: TimeInterval? | ||||||||
| private var maximumDuration: TimeInterval? | ||||||||
|
|
||||||||
| // This has to be public to comply with UIViewRepresentable, but we don't actually want it to show up in the doc. | ||||||||
| @_documentation(visibility: internal) | ||||||||
| public func makeUIView(context: Context) -> DurationPicker { | ||||||||
| let timeDurationPicker = DurationPicker() | ||||||||
|
|
||||||||
| timeDurationPicker.pickerMode = mode | ||||||||
| timeDurationPicker.hourInterval = hourInterval | ||||||||
| timeDurationPicker.minuteInterval = minuteInterval | ||||||||
| timeDurationPicker.secondInterval = secondInterval | ||||||||
| timeDurationPicker.minimumDuration = minumumDuration | ||||||||
| timeDurationPicker.maximumDuration = maximumDuration | ||||||||
|
|
||||||||
| timeDurationPicker.addTarget(context.coordinator, action: #selector(Coordinator.changed(_:)), for: .primaryActionTriggered) | ||||||||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you use UIActions instead of the old target-selector pattern? Also here and throughout, parameters should be on their own lines |
||||||||
| return timeDurationPicker | ||||||||
| } | ||||||||
|
|
||||||||
| // This has to be public to comply with UIViewRepresentable, but we don't actually want it to show up in the doc. | ||||||||
| @_documentation(visibility: internal) | ||||||||
| public func updateUIView(_ uiView: DurationPicker, context: Context) { | ||||||||
| if (uiView.duration != duration) { | ||||||||
| uiView.duration = duration | ||||||||
| } | ||||||||
| if (uiView.pickerMode != mode) { | ||||||||
| uiView.pickerMode = mode | ||||||||
| } | ||||||||
| if (uiView.hourInterval != hourInterval) { | ||||||||
| uiView.hourInterval = hourInterval | ||||||||
| } | ||||||||
| if (uiView.minuteInterval != minuteInterval) { | ||||||||
| uiView.minuteInterval = minuteInterval | ||||||||
| } | ||||||||
| if (uiView.secondInterval != secondInterval) { | ||||||||
| uiView.secondInterval = secondInterval | ||||||||
| } | ||||||||
| if (uiView.minimumDuration != minumumDuration) { | ||||||||
| uiView.minimumDuration = minumumDuration | ||||||||
| } | ||||||||
| if (uiView.maximumDuration != maximumDuration) { | ||||||||
| uiView.maximumDuration = maximumDuration | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| // This has to be public to comply with UIViewRepresentable, but we don't actually want it to show up in the doc. | ||||||||
| @_documentation(visibility: internal) | ||||||||
| public func makeCoordinator() -> DurationPickerView.Coordinator { | ||||||||
| return Coordinator(duration: $duration) | ||||||||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: would prefer to drop
Suggested change
|
||||||||
| } | ||||||||
|
|
||||||||
| // This has to be public to comply with UIViewRepresentable, but we don't actually want it to show up in the doc. | ||||||||
| @_documentation(visibility: internal) | ||||||||
| public class Coordinator: NSObject { | ||||||||
| private var duration: Binding<TimeInterval> | ||||||||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit add vertical space
Suggested change
|
||||||||
|
|
||||||||
| init(duration: Binding<TimeInterval>) { | ||||||||
| self.duration = duration | ||||||||
| } | ||||||||
|
|
||||||||
| @objc func changed(_ sender: DurationPicker) { | ||||||||
| self.duration.wrappedValue = sender.duration | ||||||||
| } | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| @available(iOS 17.0,*) | ||||||||
| #Preview { | ||||||||
| @Previewable @State var duration: TimeInterval = 60.0 * 30.0 | ||||||||
| @Previewable @State var components: DurationPickerView.Components = .hourMinuteSecond | ||||||||
| @Previewable @State var minimumDuration: TimeInterval? = nil | ||||||||
| @Previewable @State var maximumDuration: TimeInterval? = nil | ||||||||
| @Previewable @State var hourInterval: Int = 1 | ||||||||
| @Previewable @State var minuteInterval: Int = 1 | ||||||||
| @Previewable @State var secondInterval: Int = 1 | ||||||||
|
|
||||||||
| // Can't make it case iterable since its defined as an extension. | ||||||||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Comment is unnecessary. |
||||||||
| let modes: [DurationPickerView.Components] = [.hour, .hourMinute, .hourMinuteSecond, .minute, .minuteSecond, .second] | ||||||||
|
|
||||||||
| List { | ||||||||
| DurationPickerView( | ||||||||
| $duration, | ||||||||
| components: components, | ||||||||
| hourInterval: hourInterval, | ||||||||
| minuteInterval: minuteInterval, | ||||||||
| secondInterval: secondInterval, | ||||||||
| minumumDuration: minimumDuration, | ||||||||
| maximumDuration: maximumDuration | ||||||||
| ) | ||||||||
|
|
||||||||
| LabeledContent { | ||||||||
| Text(Date()..<Date().addingTimeInterval(duration), format: .timeDuration) | ||||||||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be a textfield since we can also set the duration programmatically and have it reflect on the picker |
||||||||
| } label: { | ||||||||
| Text("Value") | ||||||||
| } | ||||||||
|
|
||||||||
| Picker("Components", selection: $components) { | ||||||||
| ForEach(modes, id: \.self) { mode in | ||||||||
| Text(String(describing: mode)) | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| LabeledContent { | ||||||||
| TextField("Seconds", value: $minimumDuration, format: .number) | ||||||||
| } label: { | ||||||||
| Text("Minimum Duration") | ||||||||
| } | ||||||||
|
|
||||||||
| LabeledContent { | ||||||||
| TextField("Seconds", value: $maximumDuration, format: .number) | ||||||||
| } label: { | ||||||||
| Text("Maximum Duration") | ||||||||
| } | ||||||||
|
|
||||||||
| LabeledContent { | ||||||||
| TextField("Hour Interval", value: $hourInterval, format: .number) | ||||||||
| } label: { | ||||||||
| Text("Hour Interval") | ||||||||
| } | ||||||||
|
|
||||||||
| LabeledContent { | ||||||||
| TextField("Seconds", value: $minuteInterval, format: .number) | ||||||||
| } label: { | ||||||||
| Text("Minute Interval") | ||||||||
| } | ||||||||
|
|
||||||||
| LabeledContent { | ||||||||
| TextField("Seconds", value: $secondInterval, format: .number) | ||||||||
| } label: { | ||||||||
| Text("Second Interval") | ||||||||
| } | ||||||||
| } | ||||||||
| } | ||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: alphabetize