forked from LoopKit/LoopKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOverridePresetCollectionViewCell.swift
More file actions
192 lines (157 loc) · 5.91 KB
/
OverridePresetCollectionViewCell.swift
File metadata and controls
192 lines (157 loc) · 5.91 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
//
// OverridePresetCollectionViewCell.swift
// Loop
//
// Created by Michael Pangburn on 1/2/19.
// Copyright © 2019 LoopKit Authors. All rights reserved.
//
import UIKit
protocol OverridePresetCollectionViewCellDelegate: AnyObject {
func overridePresetCollectionViewCellDidScheduleOverride(_ cell: OverridePresetCollectionViewCell)
func overridePresetCollectionViewCellDidPerformFirstDeletionStep(_ cell: OverridePresetCollectionViewCell)
func overridePresetCollectionViewCellDidDeletePreset(_ cell: OverridePresetCollectionViewCell)
}
final class OverridePresetCollectionViewCell: UICollectionViewCell, IdentifiableClass {
@IBOutlet weak var symbolLabel: UILabel!
@IBOutlet weak var startTimeLabel: UILabel! {
didSet {
startTimeLabel.text?.removeAll()
}
}
@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var targetRangeLabel: UILabel! {
didSet {
targetRangeLabel.text?.removeAll()
}
}
@IBOutlet weak var insulinNeedsBar: SegmentedGaugeBarView! {
didSet {
insulinNeedsBar.backgroundColor = .systemGray6
insulinNeedsBar.isUserInteractionEnabled = false
}
}
@IBOutlet private weak var durationStackView: UIStackView!
@IBOutlet weak var durationLabel: UILabel!
@IBOutlet weak var scheduleButton: UIButton!
@IBOutlet private weak var editingIndicator: UIImageView! {
didSet {
editingIndicator.alpha = 0
}
}
@IBOutlet private weak var deleteButton: UIButton! {
didSet {
deleteButton.layer.cornerRadius = 4
}
}
@IBOutlet private weak var deleteButtonWidthConstraint: NSLayoutConstraint! {
didSet {
deleteButtonWidthConstraint.constant = 0
}
}
weak var delegate: OverridePresetCollectionViewCellDelegate?
private lazy var overlayDimmerView: UIView = {
let view = UIView()
view.backgroundColor = .systemBackground
view.alpha = 0
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
override func awakeFromNib() {
super.awakeFromNib()
let selectedBackgroundView = UIView()
self.selectedBackgroundView = selectedBackgroundView
selectedBackgroundView.backgroundColor = .tertiarySystemFill
backgroundColor = .secondarySystemGroupedBackground
layer.cornerCurve = .continuous
layer.cornerRadius = 16
scheduleButton.addTarget(self, action: #selector(scheduleButtonTapped), for: .touchUpInside)
addSubview(overlayDimmerView)
NSLayoutConstraint.activate([
overlayDimmerView.leadingAnchor.constraint(equalTo: leadingAnchor),
overlayDimmerView.trailingAnchor.constraint(equalTo: trailingAnchor),
overlayDimmerView.topAnchor.constraint(equalTo: topAnchor),
overlayDimmerView.bottomAnchor.constraint(equalTo: bottomAnchor)
])
}
override func prepareForReuse() {
startTimeLabel.text?.removeAll()
targetRangeLabel.isHidden = false
insulinNeedsBar.isHidden = false
configureForStandard(animated: false)
removeOverlay(animated: false)
}
func configureForEditing(animated: Bool) {
func makeVisualChanges() {
durationStackView.alpha = 0
scheduleButton.alpha = 0
editingIndicator.alpha = 1
deleteButtonWidthConstraint.constant = 32
deleteButton.setImage(UIImage(systemName: "xmark")!, for: .normal)
deleteButton.setTitle(nil, for: .normal)
}
if animated {
UIView.animate(withDuration: 0.3, animations: {
makeVisualChanges()
self.layoutIfNeeded()
})
} else {
makeVisualChanges()
}
isShowingFinalDeleteConfirmation = false
}
func configureForStandard(animated: Bool) {
func makeVisualChanges() {
durationStackView.alpha = 1
scheduleButton.alpha = 1
editingIndicator.alpha = 0
deleteButtonWidthConstraint.constant = 0
deleteButton.setImage(UIImage(systemName: "xmark")!, for: .normal)
deleteButton.setTitle(nil, for: .normal)
}
if animated {
UIView.animate(withDuration: 0.3, animations: {
makeVisualChanges()
self.layoutIfNeeded()
})
} else {
makeVisualChanges()
}
isShowingFinalDeleteConfirmation = false
}
func applyOverlayToFade(animated: Bool) {
if animated {
UIView.animate(withDuration: 0.3, animations: {
self.overlayDimmerView.alpha = 0.5
})
} else {
self.overlayDimmerView.alpha = 0.5
}
}
func removeOverlay(animated: Bool) {
if animated {
UIView.animate(withDuration: 0.3, animations: {
self.overlayDimmerView.alpha = 0
})
} else {
self.overlayDimmerView.alpha = 0
}
}
@objc private func scheduleButtonTapped() {
delegate?.overridePresetCollectionViewCellDidScheduleOverride(self)
}
private(set) var isShowingFinalDeleteConfirmation = false
@IBAction private func deleteButtonTapped(_ sender: UIButton) {
if isShowingFinalDeleteConfirmation {
delegate?.overridePresetCollectionViewCellDidDeletePreset(self)
} else {
UIView.animate(withDuration: 0.3, animations: {
self.deleteButton.setImage(nil, for: .normal)
self.deleteButton.setTitle("Delete", for: .normal)
self.deleteButtonWidthConstraint.constant = 72
self.layoutIfNeeded()
})
isShowingFinalDeleteConfirmation = true
delegate?.overridePresetCollectionViewCellDidPerformFirstDeletionStep(self)
}
}
}