forked from LoopKit/LoopKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChartsTableViewController.swift
More file actions
180 lines (143 loc) · 5.47 KB
/
ChartsTableViewController.swift
File metadata and controls
180 lines (143 loc) · 5.47 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
//
// ChartsTableViewController.swift
// LoopKitUI
//
// Copyright © 2017 LoopKit Authors. All rights reserved.
//
import UIKit
import Combine
import HealthKit
/// Abstract class providing boilerplate setup for chart-based table view controllers
open class ChartsTableViewController: UITableViewController, UIGestureRecognizerDelegate {
public var displayGlucosePreference: DisplayGlucosePreference? {
didSet {
guard let displayGlucosePreference = displayGlucosePreference else { return }
displayGlucosePreference.$unit
.sink { [weak self] displayGlucoseUnit in self?.unitPreferencesDidChange(to: displayGlucoseUnit) }
.store(in: &cancellables)
}
}
private lazy var cancellables = Set<AnyCancellable>()
open override func viewDidLoad() {
super.viewDidLoad()
if let unit = displayGlucosePreference?.unit {
self.charts.setGlucoseUnit(unit)
}
let gestureRecognizer = UILongPressGestureRecognizer()
gestureRecognizer.delegate = self
gestureRecognizer.minimumPressDuration = 0.3
gestureRecognizer.addTarget(self, action: #selector(handlePan(_:)))
charts.gestureRecognizer = gestureRecognizer
NotificationCenter.default.publisher(for: UIApplication.didBecomeActiveNotification, object: nil)
.receive(on: RunLoop.main)
.sink { [weak self] _ in
self?.active = true
if self?.visible == true {
self?.reloadData()
}
}
.store(in: &cancellables)
NotificationCenter.default.publisher(for: UIApplication.willResignActiveNotification, object: nil)
.receive(on: RunLoop.main)
.sink { [weak self] _ in
self?.active = false
}
.store(in: &cancellables)
}
open override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
if !visible {
charts.didReceiveMemoryWarning()
}
}
open override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
visible = true
}
open override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
visible = false
}
open override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
reloadData(animated: false)
}
open override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
charts.traitCollection = traitCollection
}
// MARK: - State
// This function should only be called from the main thread
public func unitPreferencesDidChange(to unit: HKUnit?) {
if let unit = unit {
self.charts.setGlucoseUnit(unit)
self.glucoseUnitDidChange()
}
self.reloadData()
}
open func glucoseUnitDidChange() {
// To override.
}
open func createChartsManager() -> ChartsManager {
fatalError("Subclasses must implement \(#function)")
}
lazy public private(set) var charts = createChartsManager()
// References to registered notification center observers
public var notificationObservers: [Any] = []
open var active: Bool = true {
didSet {
reloadData()
}
}
public var visible = false {
didSet {
reloadData()
}
}
// MARK: - Data loading
/// Refetches all data and updates the views. Must be called on the main queue.
///
/// - Parameters:
/// - animated: Whether the updating should be animated if possible
open func reloadData(animated: Bool = false) {
}
// MARK: - UIGestureRecognizer
public func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
/// Only start the long-press recognition when it starts in a chart cell
let point = gestureRecognizer.location(in: tableView)
if let indexPath = tableView.indexPathForRow(at: point) {
if let cell = tableView.cellForRow(at: indexPath), cell is ChartTableViewCell {
return true
}
}
return false
}
public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
@objc func handlePan(_ gestureRecognizer: UIGestureRecognizer) {
switch gestureRecognizer.state {
case .possible, .changed:
// Follow your dreams!
break
case .began, .cancelled, .ended, .failed:
for case let row as ChartTableViewCell in self.tableView.visibleCells {
let forwards = gestureRecognizer.state == .began
UIView.animate(withDuration: forwards ? 0.2 : 0.5, delay: forwards ? 0 : 1, animations: {
let alpha: CGFloat = forwards ? 0 : 1
row.titleLabel?.alpha = alpha
row.subtitleLabel?.alpha = alpha
})
}
@unknown default:
break
}
}
}
fileprivate extension ChartsManager {
func setGlucoseUnit(_ unit: HKUnit) {
for case let chart as GlucoseChart in charts {
chart.glucoseUnit = unit
}
}
}