DatePickerTableViewCell.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. //
  2. // DatePickerTableViewCell.swift
  3. // CarbKit
  4. //
  5. // Created by Nathan Racklyeft on 1/15/16.
  6. // Copyright © 2016 Nathan Racklyeft. All rights reserved.
  7. //
  8. import UIKit
  9. public protocol DatePickerTableViewCellDelegate: class {
  10. func datePickerTableViewCellDidUpdateDate(_ cell: DatePickerTableViewCell)
  11. }
  12. open class DatePickerTableViewCell: UITableViewCell {
  13. open var date: Date {
  14. get {
  15. return datePicker.date
  16. }
  17. set {
  18. if let maximumDate = datePicker.maximumDate,
  19. newValue >= maximumDate
  20. {
  21. datePicker.setDate(maximumDate, animated: true)
  22. } else if let minimumDate = datePicker.minimumDate,
  23. newValue <= minimumDate
  24. {
  25. datePicker.setDate(minimumDate, animated: true)
  26. } else {
  27. datePicker.setDate(newValue, animated: true)
  28. }
  29. dateChanged(datePicker)
  30. updateDateLabel()
  31. }
  32. }
  33. open var duration: TimeInterval {
  34. get {
  35. return datePicker.countDownDuration
  36. }
  37. set {
  38. datePicker.countDownDuration = newValue
  39. updateDateLabel()
  40. }
  41. }
  42. open var maximumDuration = TimeInterval(hours: 8) {
  43. didSet {
  44. if duration > maximumDuration {
  45. duration = maximumDuration
  46. }
  47. }
  48. }
  49. @IBOutlet open weak var datePicker: UIDatePicker!
  50. @IBOutlet open weak var datePickerHeightConstraint: NSLayoutConstraint!
  51. private var datePickerExpandedHeight: CGFloat = 0
  52. open var isDatePickerHidden: Bool {
  53. get {
  54. return datePicker.isHidden || !datePicker.isEnabled
  55. }
  56. set {
  57. if datePicker.isEnabled {
  58. datePicker.isHidden = newValue
  59. datePickerHeightConstraint.constant = newValue ? 0 : datePickerExpandedHeight
  60. if !newValue, case .countDownTimer = datePicker.datePickerMode {
  61. // Workaround for target-action change notifications not firing if initial value is set while view is hidden
  62. DispatchQueue.main.async {
  63. self.datePicker.date = self.datePicker.date
  64. self.datePicker.countDownDuration = self.datePicker.countDownDuration
  65. }
  66. }
  67. }
  68. }
  69. }
  70. open override func awakeFromNib() {
  71. super.awakeFromNib()
  72. datePickerExpandedHeight = datePickerHeightConstraint.constant
  73. setSelected(true, animated: false)
  74. updateDateLabel()
  75. }
  76. open override func setSelected(_ selected: Bool, animated: Bool) {
  77. super.setSelected(selected, animated: animated)
  78. if selected {
  79. isDatePickerHidden = !isDatePickerHidden
  80. }
  81. }
  82. open func updateDateLabel() {
  83. }
  84. @IBAction open func dateChanged(_ sender: UIDatePicker) {
  85. if case .countDownTimer = sender.datePickerMode, duration > maximumDuration {
  86. duration = maximumDuration
  87. } else {
  88. updateDateLabel()
  89. }
  90. }
  91. }
  92. /// UITableViewController extensions to aid working with DatePickerTableViewCell
  93. extension DatePickerTableViewCellDelegate where Self: UITableViewController {
  94. public func hideDatePickerCells(excluding indexPath: IndexPath? = nil) {
  95. guard isViewLoaded else {
  96. return
  97. }
  98. for case let cell as DatePickerTableViewCell in tableView.visibleCells where tableView.indexPath(for: cell) != indexPath && cell.isDatePickerHidden == false {
  99. cell.isDatePickerHidden = true
  100. }
  101. }
  102. }