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