DateInlineRow.swift 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. }
  33. open class _DateInlineRow: _DateInlineFieldRow {
  34. public typealias InlineRow = DatePickerRow
  35. public required init(tag: String?) {
  36. super.init(tag: tag)
  37. dateFormatter?.timeStyle = .none
  38. dateFormatter?.dateStyle = .medium
  39. }
  40. open func setupInlineRow(_ inlineRow: DatePickerRow) {
  41. configureInlineRow(inlineRow)
  42. }
  43. }
  44. open class _TimeInlineRow: _DateInlineFieldRow {
  45. public typealias InlineRow = TimePickerRow
  46. public required init(tag: String?) {
  47. super.init(tag: tag)
  48. dateFormatter?.timeStyle = .short
  49. dateFormatter?.dateStyle = .none
  50. }
  51. open func setupInlineRow(_ inlineRow: TimePickerRow) {
  52. configureInlineRow(inlineRow)
  53. }
  54. }
  55. open class _DateTimeInlineRow: _DateInlineFieldRow {
  56. public typealias InlineRow = DateTimePickerRow
  57. public required init(tag: String?) {
  58. super.init(tag: tag)
  59. dateFormatter?.timeStyle = .short
  60. dateFormatter?.dateStyle = .short
  61. }
  62. open func setupInlineRow(_ inlineRow: DateTimePickerRow) {
  63. configureInlineRow(inlineRow)
  64. }
  65. }
  66. open class _CountDownInlineRow: _DateInlineFieldRow {
  67. public typealias InlineRow = CountDownPickerRow
  68. public required init(tag: String?) {
  69. super.init(tag: tag)
  70. displayValueFor = {
  71. guard let date = $0 else {
  72. return nil
  73. }
  74. let dateComponents = Calendar.current.dateComponents([.hour, .minute], from: date)
  75. return DateComponentsFormatter.localizedString(from: dateComponents, unitsStyle: .full)?.replacingOccurrences(of: ",", with: "")
  76. }
  77. }
  78. public func setupInlineRow(_ inlineRow: CountDownPickerRow) {
  79. configureInlineRow(inlineRow)
  80. }
  81. }
  82. /// A row with an Date as value where the user can select a date from an inline picker view.
  83. public final class DateInlineRow_<T>: _DateInlineRow, RowType, InlineRowType {
  84. required public init(tag: String?) {
  85. super.init(tag: tag)
  86. onExpandInlineRow { cell, row, _ in
  87. let color = cell.detailTextLabel?.textColor
  88. row.onCollapseInlineRow { cell, _, _ in
  89. cell.detailTextLabel?.textColor = color
  90. }
  91. cell.detailTextLabel?.textColor = cell.tintColor
  92. }
  93. }
  94. public override func customDidSelect() {
  95. super.customDidSelect()
  96. if !isDisabled {
  97. toggleInlineRow()
  98. }
  99. }
  100. }
  101. public typealias DateInlineRow = DateInlineRow_<Date>
  102. /// A row with an Date as value where the user can select date and time from an inline picker view.
  103. public final class DateTimeInlineRow_<T>: _DateTimeInlineRow, RowType, InlineRowType {
  104. required public init(tag: String?) {
  105. super.init(tag: tag)
  106. onExpandInlineRow { cell, row, _ in
  107. let color = cell.detailTextLabel?.textColor
  108. row.onCollapseInlineRow { cell, _, _ in
  109. cell.detailTextLabel?.textColor = color
  110. }
  111. cell.detailTextLabel?.textColor = cell.tintColor
  112. }
  113. }
  114. public override func customDidSelect() {
  115. super.customDidSelect()
  116. if !isDisabled {
  117. toggleInlineRow()
  118. }
  119. }
  120. }
  121. public typealias DateTimeInlineRow = DateTimeInlineRow_<Date>
  122. /// A row with an Date as value where the user can select a time from an inline picker view.
  123. public final class TimeInlineRow_<T>: _TimeInlineRow, RowType, InlineRowType {
  124. required public init(tag: String?) {
  125. super.init(tag: tag)
  126. onExpandInlineRow { cell, row, _ in
  127. let color = cell.detailTextLabel?.textColor
  128. row.onCollapseInlineRow { cell, _, _ in
  129. cell.detailTextLabel?.textColor = color
  130. }
  131. cell.detailTextLabel?.textColor = cell.tintColor
  132. }
  133. }
  134. public override func customDidSelect() {
  135. super.customDidSelect()
  136. if !isDisabled {
  137. toggleInlineRow()
  138. }
  139. }
  140. }
  141. public typealias TimeInlineRow = TimeInlineRow_<Date>
  142. ///// A row with an Date as value where the user can select hour and minute as a countdown timer in an inline picker view.
  143. public final class CountDownInlineRow_<T>: _CountDownInlineRow, RowType, InlineRowType {
  144. required public init(tag: String?) {
  145. super.init(tag: tag)
  146. onExpandInlineRow { cell, row, _ in
  147. let color = cell.detailTextLabel?.textColor
  148. row.onCollapseInlineRow { cell, _, _ in
  149. cell.detailTextLabel?.textColor = color
  150. }
  151. cell.detailTextLabel?.textColor = cell.tintColor
  152. }
  153. }
  154. public override func customDidSelect() {
  155. super.customDidSelect()
  156. if !isDisabled {
  157. toggleInlineRow()
  158. }
  159. }
  160. }
  161. public typealias CountDownInlineRow = CountDownInlineRow_<Date>