DateFieldRow.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // DateFieldRow.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. public protocol DatePickerRowProtocol: AnyObject {
  27. var minimumDate: Date? { get set }
  28. var maximumDate: Date? { get set }
  29. var minuteInterval: Int? { get set }
  30. }
  31. open class DateCell: Cell<Date>, CellType {
  32. public var datePicker: UIDatePicker
  33. public required init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
  34. datePicker = UIDatePicker()
  35. super.init(style: style, reuseIdentifier: reuseIdentifier)
  36. }
  37. required public init?(coder aDecoder: NSCoder) {
  38. datePicker = UIDatePicker()
  39. super.init(coder: aDecoder)
  40. }
  41. open override func setup() {
  42. super.setup()
  43. accessoryType = .none
  44. editingAccessoryType = .none
  45. datePicker.datePickerMode = datePickerMode()
  46. datePicker.addTarget(self, action: #selector(DateCell.datePickerValueChanged(_:)), for: .valueChanged)
  47. #if swift(>=5.2)
  48. if #available(iOS 13.4, *) {
  49. datePicker.preferredDatePickerStyle = .wheels
  50. }
  51. #endif
  52. }
  53. deinit {
  54. datePicker.removeTarget(self, action: nil, for: .allEvents)
  55. }
  56. open override func update() {
  57. super.update()
  58. selectionStyle = row.isDisabled ? .none : .default
  59. datePicker.setDate(row.value ?? Date(), animated: row is CountDownPickerRow)
  60. datePicker.minimumDate = (row as? DatePickerRowProtocol)?.minimumDate
  61. datePicker.maximumDate = (row as? DatePickerRowProtocol)?.maximumDate
  62. if let minuteIntervalValue = (row as? DatePickerRowProtocol)?.minuteInterval {
  63. datePicker.minuteInterval = minuteIntervalValue
  64. }
  65. if row.isHighlighted {
  66. textLabel?.textColor = tintColor
  67. }
  68. }
  69. open override func didSelect() {
  70. super.didSelect()
  71. row.deselect()
  72. }
  73. override open var inputView: UIView? {
  74. if let v = row.value {
  75. datePicker.setDate(v, animated:row is CountDownRow)
  76. }
  77. return datePicker
  78. }
  79. @objc(pickerDateChanged:) func datePickerValueChanged(_ sender: UIDatePicker) {
  80. row.value = sender.date
  81. detailTextLabel?.text = row.displayValueFor?(row.value)
  82. }
  83. private func datePickerMode() -> UIDatePicker.Mode {
  84. switch row {
  85. case is DateRow:
  86. return .date
  87. case is TimeRow:
  88. return .time
  89. case is DateTimeRow:
  90. return .dateAndTime
  91. case is CountDownRow:
  92. return .countDownTimer
  93. default:
  94. return .date
  95. }
  96. }
  97. open override func cellCanBecomeFirstResponder() -> Bool {
  98. return canBecomeFirstResponder
  99. }
  100. override open var canBecomeFirstResponder: Bool {
  101. return !row.isDisabled
  102. }
  103. }
  104. open class _DateFieldRow: Row<DateCell>, DatePickerRowProtocol, NoValueDisplayTextConformance {
  105. /// The minimum value for this row's UIDatePicker
  106. open var minimumDate: Date?
  107. /// The maximum value for this row's UIDatePicker
  108. open var maximumDate: Date?
  109. /// The interval between options for this row's UIDatePicker
  110. open var minuteInterval: Int?
  111. /// The formatter for the date picked by the user
  112. open var dateFormatter: DateFormatter?
  113. open var noValueDisplayText: String? = nil
  114. required public init(tag: String?) {
  115. super.init(tag: tag)
  116. displayValueFor = { [unowned self] value in
  117. guard let val = value, let formatter = self.dateFormatter else { return nil }
  118. return formatter.string(from: val)
  119. }
  120. }
  121. }