|
| 1 | +// |
| 2 | +// TimePickerDataSource.swift |
| 3 | +// WatchKitTimePicker |
| 4 | +// |
| 5 | +// Created by Cal Stephens on 11/5/18. |
| 6 | +// Copyright © 2018 Cal Stephens. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import WatchKit |
| 10 | + |
| 11 | +public class TimePickerDataSource { |
| 12 | + |
| 13 | + private weak var hoursPicker: WKInterfacePicker? |
| 14 | + private weak var minutesPicker: WKInterfacePicker? |
| 15 | + private weak var amPmPicker: WKInterfacePicker? |
| 16 | + |
| 17 | + public var selectedTimeDidUpdate: ((Date) -> Void)? |
| 18 | + |
| 19 | + public init(hoursPicker: WKInterfacePicker, minutesPicker: WKInterfacePicker, amPmPicker: WKInterfacePicker?, initiallySelectedDate: Date?) { |
| 20 | + self.hoursPicker = hoursPicker |
| 21 | + self.minutesPicker = minutesPicker |
| 22 | + self.amPmPicker = amPmPicker |
| 23 | + |
| 24 | + setup(withInitiallySelectedDate: initiallySelectedDate) |
| 25 | + } |
| 26 | + |
| 27 | + |
| 28 | + // MARK: Setup |
| 29 | + |
| 30 | + private lazy var timeFormatter: DateFormatter = { |
| 31 | + let formatter = DateFormatter() |
| 32 | + formatter.dateStyle = .none |
| 33 | + formatter.timeStyle = .short |
| 34 | + return formatter |
| 35 | + }() |
| 36 | + |
| 37 | + private lazy var userHas24HourTimeEnabled: Bool = { |
| 38 | + let timeString = timeFormatter.string(from: Date()) |
| 39 | + return !(timeString.contains(Locale.current.calendar.amSymbol) |
| 40 | + || timeString.contains(Locale.current.calendar.pmSymbol)) |
| 41 | + }() |
| 42 | + |
| 43 | + private lazy var hourPickerOptions: [Int] = { |
| 44 | + if userHas24HourTimeEnabled { |
| 45 | + return Array(0...23) |
| 46 | + } else { |
| 47 | + return [/* am: */ 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, |
| 48 | + /* pm: */ 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] |
| 49 | + } |
| 50 | + }() |
| 51 | + |
| 52 | + private lazy var minutePickerOptions: [Int] = { |
| 53 | + // create 0-60 minute entries for all 24 hours in the day. |
| 54 | + return Array(repeating: Array(stride(from: 0, to: 60, by: 5)), count: 24).flatMap { $0 } |
| 55 | + }() |
| 56 | + |
| 57 | + private lazy var amPmPickerOptions: [String]? = { |
| 58 | + if userHas24HourTimeEnabled { |
| 59 | + return nil |
| 60 | + } else { |
| 61 | + return [ |
| 62 | + Locale.current.calendar.amSymbol, |
| 63 | + Locale.current.calendar.pmSymbol] |
| 64 | + } |
| 65 | + }() |
| 66 | + |
| 67 | + private func setup(withInitiallySelectedDate initiallySelectedDate: Date?) { |
| 68 | + hoursPicker?.setItems(hourPickerOptions.map { hourValue in |
| 69 | + let pickerItem = WKPickerItem() |
| 70 | + pickerItem.title = "\(hourValue)" |
| 71 | + return pickerItem |
| 72 | + }) |
| 73 | + |
| 74 | + minutesPicker?.setItems(minutePickerOptions.map { minuteValue in |
| 75 | + let pickerItem = WKPickerItem() |
| 76 | + if "\(minuteValue)".count == 1 { |
| 77 | + pickerItem.title = "0\(minuteValue)" |
| 78 | + } else { |
| 79 | + pickerItem.title = "\(minuteValue)" |
| 80 | + } |
| 81 | + return pickerItem |
| 82 | + }) |
| 83 | + |
| 84 | + if let amPmPickerOptions = amPmPickerOptions { |
| 85 | + amPmPicker?.setItems(amPmPickerOptions.map { value in |
| 86 | + let pickerItem = WKPickerItem() |
| 87 | + pickerItem.title = value |
| 88 | + return pickerItem |
| 89 | + }) |
| 90 | + |
| 91 | + amPmPicker?.setHidden(false) |
| 92 | + hoursPicker?.setRelativeWidth(0.333, withAdjustment: 0) |
| 93 | + minutesPicker?.setRelativeWidth(0.333, withAdjustment: 0) |
| 94 | + amPmPicker?.setRelativeWidth(0.333, withAdjustment: 0) |
| 95 | + |
| 96 | + } else { |
| 97 | + amPmPicker?.setHidden(true) |
| 98 | + hoursPicker?.setRelativeWidth(0.5, withAdjustment: 0) |
| 99 | + minutesPicker?.setRelativeWidth(0.5, withAdjustment: 0) |
| 100 | + } |
| 101 | + |
| 102 | + // set the initial values of the pickers |
| 103 | + if let initiallySelectedDate = initiallySelectedDate { |
| 104 | + let dateComponents = Calendar.current.dateComponents(Set(arrayLiteral: .hour, .minute), from: initiallySelectedDate) |
| 105 | + let hours = dateComponents.hour! |
| 106 | + let minutes = dateComponents.minute! |
| 107 | + |
| 108 | + hoursPicker?.setSelectedItemIndex(hours) |
| 109 | + selectedHour = hourPickerOptions[hours] |
| 110 | + |
| 111 | + let displayedMinutesPerHour = minutePickerOptions.count / 24 |
| 112 | + let corresponsingMinuteIndex = (minutePickerOptions.firstIndex(where: { $0 >= minutes }) ?? 0) + (displayedMinutesPerHour * hours) |
| 113 | + minutesPicker?.setSelectedItemIndex(corresponsingMinuteIndex) |
| 114 | + selectedMinute = minutePickerOptions[corresponsingMinuteIndex] |
| 115 | + |
| 116 | + if !userHas24HourTimeEnabled { |
| 117 | + if hours < 12 { |
| 118 | + amPmPicker?.setSelectedItemIndex(0) |
| 119 | + amPm = .am |
| 120 | + } else { |
| 121 | + amPmPicker?.setSelectedItemIndex(1) |
| 122 | + amPm = .pm |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + |
| 129 | + // MARK: User Interaction |
| 130 | + |
| 131 | + private enum AMPM { |
| 132 | + case am, pm |
| 133 | + } |
| 134 | + |
| 135 | + private var selectedHour: Int! |
| 136 | + private var selectedMinute: Int! |
| 137 | + private var amPm: AMPM? |
| 138 | + |
| 139 | + public func hourPickerUpdated(to index: Int) { |
| 140 | + selectedHour = hourPickerOptions[index] |
| 141 | + |
| 142 | + // if using 12-hour time, switching between the first half and the last half swaps between AM and PM |
| 143 | + if !userHas24HourTimeEnabled { |
| 144 | + let selectedHourIsInTheAM = (index < 12) |
| 145 | + |
| 146 | + if selectedHourIsInTheAM, amPm == .pm { |
| 147 | + amPmPicker?.setSelectedItemIndex(0) |
| 148 | + } |
| 149 | + |
| 150 | + else if !selectedHourIsInTheAM, amPm == .am { |
| 151 | + amPmPicker?.setSelectedItemIndex(1) |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + // there are minute values for each of the 24 hours in the day, |
| 156 | + // so make sure the selected minute corresponds with the selected hour |
| 157 | + let displayedMinutesPerHour = minutePickerOptions.count / 24 |
| 158 | + let corresponsingMinuteIndex = (minutePickerOptions.firstIndex(of: selectedMinute) ?? 0) + (displayedMinutesPerHour * index) |
| 159 | + minutesPicker?.setSelectedItemIndex(corresponsingMinuteIndex) |
| 160 | + |
| 161 | + selectedTimeDidUpdate?(selectedTime()) |
| 162 | + } |
| 163 | + |
| 164 | + public func minutePickerUpdated(to index: Int) { |
| 165 | + selectedMinute = minutePickerOptions[index] |
| 166 | + |
| 167 | + // there are minute values for each of the 24 hours in the day, |
| 168 | + // so make sure the selected hour corresponds with the selected minute |
| 169 | + let displayedMinutesPerHour = minutePickerOptions.count / 24 |
| 170 | + let expectedHourIndex = index / (displayedMinutesPerHour) |
| 171 | + let expectedHour = hourPickerOptions[expectedHourIndex] |
| 172 | + if selectedHour != expectedHour { |
| 173 | + hoursPicker?.setSelectedItemIndex(expectedHourIndex) |
| 174 | + } |
| 175 | + |
| 176 | + selectedTimeDidUpdate?(selectedTime()) |
| 177 | + } |
| 178 | + |
| 179 | + public func amPmPickerUpdated(to index: Int) { |
| 180 | + guard !userHas24HourTimeEnabled else { return } |
| 181 | + |
| 182 | + if index == 0 { amPm = .am } |
| 183 | + else { amPm = .pm } |
| 184 | + |
| 185 | + // update the hour picker to inhabit the correct half of the 24 picker options |
| 186 | + var selectedIndex = hourPickerOptions.firstIndex(of: selectedHour) ?? 0 |
| 187 | + if amPm == .pm { |
| 188 | + // the PM times inhabit the second half |
| 189 | + selectedIndex += 12 |
| 190 | + } |
| 191 | + |
| 192 | + hoursPicker?.setSelectedItemIndex(selectedIndex) |
| 193 | + hourPickerUpdated(to: selectedIndex) |
| 194 | + |
| 195 | + selectedTimeDidUpdate?(selectedTime()) |
| 196 | + } |
| 197 | + |
| 198 | + public func selectedTime() -> Date { |
| 199 | + var hourIn24HourTime = selectedHour ?? 0 |
| 200 | + |
| 201 | + // if there's an option for am/pm, the user's in 12 hour time. |
| 202 | + // Need to do the necessary corrections. |
| 203 | + if let amPm = amPm { |
| 204 | + if hourIn24HourTime == 12 && amPm == .am { |
| 205 | + hourIn24HourTime = 0 |
| 206 | + } else if hourIn24HourTime == 12 && amPm == .pm { |
| 207 | + hourIn24HourTime = 12 |
| 208 | + } else if amPm == .pm { |
| 209 | + hourIn24HourTime += 12 |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | + return Calendar.current.date( |
| 214 | + bySettingHour: hourIn24HourTime, |
| 215 | + minute: selectedMinute, |
| 216 | + second: 0, |
| 217 | + of: Date(), |
| 218 | + matchingPolicy: .nextTime, |
| 219 | + repeatedTimePolicy: .first, |
| 220 | + direction: .forward)! |
| 221 | + } |
| 222 | + |
| 223 | +} |
0 commit comments