DateFieldRow.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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: class {
  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. }
  48. deinit {
  49. datePicker.removeTarget(self, action: nil, for: .allEvents)
  50. }
  51. open override func update() {
  52. super.update()
  53. selectionStyle = row.isDisabled ? .none : .default
  54. datePicker.setDate(row.value ?? Date(), animated: row is CountDownPickerRow)
  55. datePicker.minimumDate = (row as? DatePickerRowProtocol)?.minimumDate
  56. datePicker.maximumDate = (row as? DatePickerRowProtocol)?.maximumDate
  57. if let minuteIntervalValue = (row as? DatePickerRowProtocol)?.minuteInterval {
  58. datePicker.minuteInterval = minuteIntervalValue
  59. }
  60. if row.isHighlighted {
  61. textLabel?.textColor = tintColor
  62. }
  63. }
  64. open override func didSelect() {
  65. super.didSelect()
  66. row.deselect()
  67. }
  68. override open var inputView: UIView? {
  69. if let v = row.value {
  70. datePicker.setDate(v, animated:row is CountDownRow)
  71. }
  72. return datePicker
  73. }
  74. @objc func datePickerValueChanged(_ sender: UIDatePicker) {
  75. row.value = sender.date
  76. detailTextLabel?.text = row.displayValueFor?(row.value)
  77. }
  78. private func datePickerMode() -> UIDatePicker.Mode {
  79. switch row {
  80. case is DateRow:
  81. return .date
  82. case is TimeRow:
  83. return .time
  84. case is DateTimeRow:
  85. return .dateAndTime
  86. case is CountDownRow:
  87. return .countDownTimer
  88. default:
  89. return .date
  90. }
  91. }
  92. open override func cellCanBecomeFirstResponder() -> Bool {
  93. return canBecomeFirstResponder
  94. }
  95. override open var canBecomeFirstResponder: Bool {
  96. return !row.isDisabled
  97. }
  98. }
  99. open class _DateFieldRow: Row<DateCell>, DatePickerRowProtocol, NoValueDisplayTextConformance {
  100. /// The minimum value for this row's UIDatePicker
  101. open var minimumDate: Date?
  102. /// The maximum value for this row's UIDatePicker
  103. open var maximumDate: Date?
  104. /// The interval between options for this row's UIDatePicker
  105. open var minuteInterval: Int?
  106. /// The formatter for the date picked by the user
  107. open var dateFormatter: DateFormatter?
  108. open var noValueDisplayText: String? = nil
  109. required public init(tag: String?) {
  110. super.init(tag: tag)
  111. displayValueFor = { [unowned self] value in
  112. guard let val = value, let formatter = self.dateFormatter else { return nil }
  113. return formatter.string(from: val)
  114. }
  115. }
  116. }