Adding SwiftUI wrapper for DurationPicker#5
Adding SwiftUI wrapper for DurationPicker#5GabRoyer wants to merge 7 commits intomac-gallagher:mainfrom
DurationPicker#5Conversation
…n an environemnt variable to better align with
mac-gallagher
left a comment
There was a problem hiding this comment.
Appreciate the contribution @GabRoyer! This is a great addition. Gave an initial pass
| import SwiftUI | ||
| import DurationPicker |
| 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. |
There was a problem hiding this comment.
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 { | ||
| public init(_ duration: Binding<TimeInterval>, components: Components = .hourMinuteSecond, hourInterval: Int = 1, minuteInterval: Int = 1, secondInterval: Int = 1, minumumDuration: TimeInterval? = nil, maximumDuration: TimeInterval? = nil) { |
| /// 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. | ||
| /// | ||
| /// You can use a duration picker to allow a user to enter a time interval between 0 and 24 hours. | ||
| public struct DurationPickerView: UIViewRepresentable { |
There was a problem hiding this comment.
Would prefer simply DurationPicker. This more closely resembles other SwiftUI components provided by Apple (DatePicker, TextField, etc.)
Edit: Ah strike this. This would introduce a naming conflict in the project with the UIKit version
| /// | ||
| /// You can use a duration picker to allow a user to enter a time interval between 0 and 24 hours. | ||
| public struct DurationPickerView: UIViewRepresentable { | ||
| public init(_ duration: Binding<TimeInterval>, components: Components = .hourMinuteSecond, hourInterval: Int = 1, minuteInterval: Int = 1, secondInterval: Int = 1, minumumDuration: TimeInterval? = nil, maximumDuration: TimeInterval? = nil) { |
There was a problem hiding this comment.
Everything besides duration and components would be better suited as a view modifier instead of passed in the initializer. This would also allow these properties to be mutable (they currently cannot be changed after initializing)
Also I think displayedComponents is just fine (actually would prefer this)
| timeDurationPicker.minimumDuration = minumumDuration | ||
| timeDurationPicker.maximumDuration = maximumDuration | ||
|
|
||
| timeDurationPicker.addTarget(context.coordinator, action: #selector(Coordinator.changed(_:)), for: .primaryActionTriggered) |
There was a problem hiding this comment.
Can you use UIActions instead of the old target-selector pattern?
Also here and throughout, parameters should be on their own lines
| // 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) |
There was a problem hiding this comment.
Nit: would prefer to drop return since it's not necessary
| return Coordinator(duration: $duration) | |
| Coordinator(duration: $duration) |
| // 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> |
There was a problem hiding this comment.
Nit add vertical space
| private var duration: Binding<TimeInterval> | |
| private var duration: Binding<TimeInterval> |
|
|
||
| /// 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. |
There was a problem hiding this comment.
This comment is not accurate
| @Previewable @State var minuteInterval: Int = 1 | ||
| @Previewable @State var secondInterval: Int = 1 | ||
|
|
||
| // Can't make it case iterable since its defined as an extension. |
| ) | ||
|
|
||
| LabeledContent { | ||
| Text(Date()..<Date().addingTimeInterval(duration), format: .timeDuration) |
There was a problem hiding this comment.
This should be a textfield since we can also set the duration programmatically and have it reflect on the picker
|
@GabRoyer Bump :) |
Description
Added a SwiftUI wrapper for
DurationPickercalledDurationPickerView. I added it under a new library since very rarely do UIKit users want to use a SwiftUI component (and vice-versa) if there is a component that works natively with their framework.I've renamed
ModetoComponentsas it better aligns with SwiftUI'sDatePickerterminology. Though I thought "displayedComponents" would be a misnomer as it's a bit more than what's being display since changing, say, the Hour component in.hourmode also sets the minutes and seconds to 0.Usage
See preview for usage example, but it behaves pretty much like any SwiftUI view. For example, one can simply use it as such: