DateInlineRow.swift 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // DateInliuneRow.swift
  2. // Eureka ( https://github.com/xmartlabs/Eureka )
  3. //
  4. // Copyright (c) 2016 Xmartlabs SRL ( http://xmartlabs.com )
  5. //
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in
  15. // all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. // THE SOFTWARE.
  24. import Foundation
  25. import UIKit
  26. extension DatePickerRowProtocol {
  27. func configureInlineRow(_ inlineRow: DatePickerRowProtocol) {
  28. inlineRow.minimumDate = minimumDate
  29. inlineRow.maximumDate = maximumDate
  30. inlineRow.minuteInterval = minuteInterval
  31. }
  32. func configurePickerStyle(_ cell: DatePickerCell, _ mode: UIDatePicker.Mode = .dateAndTime) {
  33. cell.datePicker.datePickerMode = mode
  34. // For Xcode 11.4 and above
  35. #if swift(>=5.2)
  36. if #available(iOS 14.0, *) {
  37. #if swift(>=5.3) && !(os(OSX) || (os(iOS) && targetEnvironment(macCatalyst)))
  38. cell.datePicker.preferredDatePickerStyle = .inline
  39. #else
  40. cell.datePicker.preferredDatePickerStyle = .wheels
  41. #endif
  42. }
  43. else if #available(iOS 13.4, *) {
  44. cell.datePicker.preferredDatePickerStyle = .wheels
  45. }
  46. #endif
  47. }
  48. }
  49. open class _DateInlineRow: _DateInlineFieldRow {
  50. public typealias InlineRow = DatePickerRow
  51. public required init(tag: String?) {
  52. super.init(tag: tag)
  53. dateFormatter?.timeStyle = .none
  54. dateFormatter?.dateStyle = .medium
  55. }
  56. open func setupInlineRow(_ inlineRow: DatePickerRow) {
  57. configureInlineRow(inlineRow)
  58. configurePickerStyle(inlineRow.cell, .date)
  59. }
  60. }
  61. open class _TimeInlineRow: _DateInlineFieldRow {
  62. public typealias InlineRow = TimePickerRow
  63. public required init(tag: String?) {
  64. super.init(tag: tag)
  65. dateFormatter?.timeStyle = .short
  66. dateFormatter?.dateStyle = .none
  67. }
  68. open func setupInlineRow(_ inlineRow: TimePickerRow) {
  69. configureInlineRow(inlineRow)
  70. configurePickerStyle(inlineRow.cell, .time)
  71. }
  72. }
  73. open class _DateTimeInlineRow: _DateInlineFieldRow {
  74. public typealias InlineRow = DateTimePickerRow
  75. public required init(tag: String?) {
  76. super.init(tag: tag)
  77. dateFormatter?.timeStyle = .short
  78. dateFormatter?.dateStyle = .short
  79. }
  80. open func setupInlineRow(_ inlineRow: DateTimePickerRow) {
  81. configureInlineRow(inlineRow)
  82. configurePickerStyle(inlineRow.cell)
  83. }
  84. }
  85. open class _CountDownInlineRow: _DateInlineFieldRow {
  86. public typealias InlineRow = CountDownPickerRow
  87. public required init(tag: String?) {
  88. super.init(tag: tag)
  89. displayValueFor = {
  90. guard let date = $0 else {
  91. return nil
  92. }
  93. let dateComponents = Calendar.current.dateComponents([.hour, .minute], from: date)
  94. return DateComponentsFormatter.localizedString(from: dateComponents, unitsStyle: .full)?.replacingOccurrences(of: ",", with: "")
  95. }
  96. }
  97. public func setupInlineRow(_ inlineRow: CountDownPickerRow) {
  98. configureInlineRow(inlineRow)
  99. }
  100. }
  101. /// A row with an Date as value where the user can select a date from an inline picker view.
  102. public final class DateInlineRow_<T>: _DateInlineRow, RowType, InlineRowType {
  103. required public init(tag: String?) {
  104. super.init(tag: tag)
  105. onExpandInlineRow { cell, row, _ in
  106. let color = cell.detailTextLabel?.textColor
  107. row.onCollapseInlineRow { cell, _, _ in
  108. cell.detailTextLabel?.textColor = color
  109. }
  110. cell.detailTextLabel?.textColor = cell.tintColor
  111. }
  112. }
  113. public override func customDidSelect() {
  114. super.customDidSelect()
  115. if !isDisabled {
  116. toggleInlineRow()
  117. }
  118. }
  119. }
  120. public typealias DateInlineRow = DateInlineRow_<Date>
  121. /// A row with an Date as value where the user can select date and time from an inline picker view.
  122. public final class DateTimeInlineRow_<T>: _DateTimeInlineRow, RowType, InlineRowType {
  123. required public init(tag: String?) {
  124. super.init(tag: tag)
  125. onExpandInlineRow { cell, row, _ in
  126. let color = cell.detailTextLabel?.textColor
  127. row.onCollapseInlineRow { cell, _, _ in
  128. cell.detailTextLabel?.textColor = color
  129. }
  130. cell.detailTextLabel?.textColor = cell.tintColor
  131. }
  132. }
  133. public override func customDidSelect() {
  134. super.customDidSelect()
  135. if !isDisabled {
  136. toggleInlineRow()
  137. }
  138. }
  139. }
  140. public typealias DateTimeInlineRow = DateTimeInlineRow_<Date>
  141. /// A row with an Date as value where the user can select a time from an inline picker view.
  142. public final class TimeInlineRow_<T>: _TimeInlineRow, RowType, InlineRowType {
  143. required public init(tag: String?) {
  144. super.init(tag: tag)
  145. onExpandInlineRow { cell, row, _ in
  146. let color = cell.detailTextLabel?.textColor
  147. row.onCollapseInlineRow { cell, _, _ in
  148. cell.detailTextLabel?.textColor = color
  149. }
  150. cell.detailTextLabel?.textColor = cell.tintColor
  151. }
  152. }
  153. public override func customDidSelect() {
  154. super.customDidSelect()
  155. if !isDisabled {
  156. toggleInlineRow()
  157. }
  158. }
  159. }
  160. public typealias TimeInlineRow = TimeInlineRow_<Date>
  161. ///// A row with an Date as value where the user can select hour and minute as a countdown timer in an inline picker view.
  162. public final class CountDownInlineRow_<T>: _CountDownInlineRow, RowType, InlineRowType {
  163. required public init(tag: String?) {
  164. super.init(tag: tag)
  165. onExpandInlineRow { cell, row, _ in
  166. let color = cell.detailTextLabel?.textColor
  167. row.onCollapseInlineRow { cell, _, _ in
  168. cell.detailTextLabel?.textColor = color
  169. }
  170. cell.detailTextLabel?.textColor = cell.tintColor
  171. }
  172. }
  173. public override func customDidSelect() {
  174. super.customDidSelect()
  175. if !isDisabled {
  176. toggleInlineRow()
  177. }
  178. }
  179. }
  180. public typealias CountDownInlineRow = CountDownInlineRow_<Date>