Skip to content

Commit c30cf8f

Browse files
committed
Add support for specifying the minute interval (1, 5, 15, or 30)
1 parent 4f35596 commit c30cf8f

3 files changed

Lines changed: 36 additions & 6 deletions

File tree

Sample App/WatchKit Sample App Extension/InterfaceController.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ class InterfaceController: WKInterfaceController {
2222
timePickerDataSource = TimePickerDataSource(
2323
hoursPicker: hourTimePicker,
2424
minutesPicker: minuteTimePicker,
25-
amPmPicker: amPmTimePicker)
25+
amPmPicker: amPmTimePicker,
26+
interval: .fiveMinutes)
2627

2728
timePickerDataSource.selectedTimeDidUpdate = { [weak self] selectedTime in
2829
let timeFormatter = DateFormatter()

WatchKitTimePicker.xcodeproj/xcuserdata/cal.xcuserdatad/xcschemes/xcschememanagement.plist

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@
44
<dict>
55
<key>SchemeUserState</key>
66
<dict>
7+
<key>Sample App.xcscheme_^#shared#^_</key>
8+
<dict>
9+
<key>orderHint</key>
10+
<integer>1</integer>
11+
</dict>
12+
<key>WatchKit Sample App.xcscheme_^#shared#^_</key>
13+
<dict>
14+
<key>orderHint</key>
15+
<integer>2</integer>
16+
</dict>
717
<key>WatchKitTimePicker.xcscheme_^#shared#^_</key>
818
<dict>
919
<key>orderHint</key>

WatchKitTimePicker/TimePickerDataSource.swift

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,32 @@ public class TimePickerDataSource {
1414
private weak var minutesPicker: WKInterfacePicker?
1515
private weak var amPmPicker: WKInterfacePicker?
1616

17+
private let interval: SelectionInterval
1718
public var selectedTimeDidUpdate: ((Date) -> Void)?
1819

20+
public enum SelectionInterval {
21+
case minute
22+
case fiveMinutes
23+
case fifteenMinutes
24+
case halfHour
25+
26+
var minutesBetweenOptions: Int {
27+
switch self {
28+
case .minute: return 1
29+
case .fiveMinutes: return 5
30+
case .fifteenMinutes: return 15
31+
case .halfHour: return 30
32+
}
33+
}
34+
}
35+
1936
public init(
2037
hoursPicker: WKInterfacePicker,
2138
minutesPicker: WKInterfacePicker,
22-
amPmPicker: WKInterfacePicker?)
39+
amPmPicker: WKInterfacePicker?,
40+
interval: SelectionInterval = .fiveMinutes)
2341
{
42+
self.interval = interval
2443
self.hoursPicker = hoursPicker
2544
self.minutesPicker = minutesPicker
2645
self.amPmPicker = amPmPicker
@@ -48,10 +67,10 @@ public class TimePickerDataSource {
4867
}
4968
}()
5069

51-
private lazy var minutePickerOptions: [Int] = {
52-
// create 0-60 minute entries for all 24 hours in the day.
53-
return Array(repeating: Array(stride(from: 0, to: 60, by: 5)), count: 24).flatMap { $0 }
54-
}()
70+
/// 0-60 minute entries for all 24 hours in the day.
71+
private lazy var minutePickerOptions: [Int] = { (interval: SelectionInterval) in
72+
Array(repeating: Array(stride(from: 0, to: 60, by: interval.minutesBetweenOptions)), count: 24).flatMap { $0 }
73+
}(self.interval)
5574

5675
private lazy var amPmPickerOptions: [String]? = {
5776
if userHas24HourTimeEnabled {

0 commit comments

Comments
 (0)