forked from LoopKit/LoopKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRepeatingScheduleValueTableViewCell.swift
More file actions
114 lines (88 loc) · 3.01 KB
/
RepeatingScheduleValueTableViewCell.swift
File metadata and controls
114 lines (88 loc) · 3.01 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
//
// RepeatingScheduleValueTableViewCell.swift
// Naterade
//
// Created by Nathan Racklyeft on 2/6/16.
// Copyright © 2016 Nathan Racklyeft. All rights reserved.
//
import UIKit
protocol RepeatingScheduleValueTableViewCellDelegate: DatePickerTableViewCellDelegate {
func repeatingScheduleValueTableViewCellDidUpdateValue(_ cell: RepeatingScheduleValueTableViewCell)
}
class RepeatingScheduleValueTableViewCell: DatePickerTableViewCell, UITextFieldDelegate {
weak var delegate: RepeatingScheduleValueTableViewCellDelegate?
var timeZone: TimeZone! {
didSet {
dateFormatter.timeZone = timeZone
datePicker.timeZone = timeZone
}
}
private lazy var dateFormatter: DateFormatter = {
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .none
dateFormatter.timeStyle = .short
return dateFormatter
}()
override func updateDateLabel() {
dateLabel.text = dateFormatter.string(from: date)
}
override func dateChanged(_ sender: UIDatePicker) {
super.dateChanged(sender)
delegate?.datePickerTableViewCellDidUpdateDate(self)
}
var value: Double = 0 {
didSet {
textField.text = valueNumberFormatter.string(from: value)
}
}
var datePickerInterval: TimeInterval {
return TimeInterval(minutes: Double(datePicker.minuteInterval))
}
var isReadOnly = false {
didSet {
if isReadOnly, textField.isFirstResponder {
textField.resignFirstResponder()
}
}
}
lazy var valueNumberFormatter: NumberFormatter = {
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
formatter.minimumFractionDigits = 1
return formatter
}()
@IBOutlet weak var dateLabel: UILabel!
@IBOutlet weak var unitLabel: UILabel! {
didSet {
// Setting this color in code because the nib isn't being applied correctly
unitLabel.textColor = .secondaryLabel
}
}
@IBOutlet weak var textField: UITextField! {
didSet {
// Setting this color in code because the nib isn't being applied correctly
textField.textColor = .label
}
}
var unitString: String? {
get {
return unitLabel.text
}
set {
unitLabel.text = newValue
}
}
// MARK: - UITextFieldDelegate
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
return !isReadOnly
}
func textFieldDidBeginEditing(_ textField: UITextField) {
DispatchQueue.main.async {
textField.selectedTextRange = textField.textRange(from: textField.beginningOfDocument, to: textField.endOfDocument)
}
}
func textFieldDidEndEditing(_ textField: UITextField) {
value = valueNumberFormatter.number(from: textField.text ?? "")?.doubleValue ?? 0
delegate?.repeatingScheduleValueTableViewCellDidUpdateValue(self)
}
}