Skip to content

Commit 8825eb9

Browse files
authored
Track changing units in charts (#749)
1 parent d7e3dba commit 8825eb9

6 files changed

+33
-6
lines changed

Loop/View Controllers/CarbAbsorptionViewController.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ final class CarbAbsorptionViewController: ChartsTableViewController, Identifiabl
9090

9191
// MARK: - Data loading
9292

93+
override func glucoseUnitDidChange() {
94+
refreshContext = RefreshContext.all
95+
}
96+
9397
override func reloadData(animated: Bool = false) {
9498
guard active && !reloading && !self.refreshContext.isEmpty else { return }
9599
var currentContext = self.refreshContext

Loop/View Controllers/ChartsTableViewController.swift

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ class ChartsTableViewController: UITableViewController, UIGestureRecognizerDeleg
8080
override func viewDidLoad() {
8181
super.viewDidLoad()
8282

83+
if let unit = self.deviceManager.loopManager.glucoseStore.preferredUnit {
84+
self.charts.glucoseUnit = unit
85+
}
86+
8387
let notificationCenter = NotificationCenter.default
8488
notificationObservers += [
8589
notificationCenter.addObserver(forName: .UIApplicationWillResignActive, object: UIApplication.shared, queue: .main) { [weak self] _ in
@@ -138,17 +142,26 @@ class ChartsTableViewController: UITableViewController, UIGestureRecognizerDeleg
138142
}
139143
}
140144

141-
@objc func unitPreferencesDidChange(_ note: Notification) {
145+
@objc private func unitPreferencesDidChange(_ note: Notification) {
142146
DispatchQueue.main.async {
143147
if let unit = self.deviceManager.loopManager.glucoseStore.preferredUnit {
148+
let didChange = unit != self.charts.glucoseUnit
144149
self.charts.glucoseUnit = unit
150+
151+
if didChange {
152+
self.glucoseUnitDidChange()
153+
}
145154
}
146155
self.log.debug("[reloadData] for HealthKit unit preference change")
147156
self.reloadData()
148157
}
149158
}
150159

151-
var charts = StatusChartsManager(colors: .default, settings: .default)
160+
func glucoseUnitDidChange() {
161+
// To override.
162+
}
163+
164+
let charts = StatusChartsManager(colors: .default, settings: .default)
152165

153166
// References to registered notification center observers
154167
var notificationObservers: [Any] = []

Loop/View Controllers/InsulinModelSettingsViewController.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ class InsulinModelSettingsViewController: ChartsTableViewController, Identifiabl
4747
}
4848
}
4949

50+
override func glucoseUnitDidChange() {
51+
refreshContext = true
52+
}
53+
5054
/// The sensitivity (in glucose units) to use for demonstrating the model
5155
var insulinSensitivitySchedule = InsulinSensitivitySchedule(unit: .milligramsPerDeciliter, dailyItems: [RepeatingScheduleValue<Double>(startTime: 0, value: 40)])!
5256

Loop/View Controllers/PredictionTableViewController.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ class PredictionTableViewController: ChartsTableViewController, IdentifiableClas
8484
}
8585
}
8686

87+
override func glucoseUnitDidChange() {
88+
refreshContext = RefreshContext.all
89+
}
90+
8791
override func reloadData(animated: Bool = false) {
8892
guard active && visible && !refreshContext.isEmpty else { return }
8993

Loop/View Controllers/SettingsTableViewController.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,13 @@ final class SettingsTableViewController: UITableViewController, DailyValueSchedu
127127
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
128128
switch segue.destination {
129129
case let vc as InsulinModelSettingsViewController:
130+
vc.deviceManager = dataManager
130131
vc.insulinModel = dataManager.loopManager.insulinModelSettings?.model
131132

132133
if let insulinSensitivitySchedule = dataManager.loopManager.insulinSensitivitySchedule {
133134
vc.insulinSensitivitySchedule = insulinSensitivitySchedule
134135
}
135136

136-
if let unit = dataManager.loopManager.glucoseStore.preferredUnit {
137-
vc.glucoseUnit = unit
138-
}
139137
vc.delegate = self
140138
default:
141139
break

Loop/View Controllers/StatusTableViewController.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ final class StatusTableViewController: ChartsTableViewController {
100100
}
101101
}
102102

103-
var appearedOnce = false
103+
private var appearedOnce = false
104104

105105
override func viewWillAppear(_ animated: Bool) {
106106
super.viewWillAppear(animated)
@@ -183,6 +183,10 @@ final class StatusTableViewController: ChartsTableViewController {
183183
return !landscapeMode && statusRowMode.hasRow
184184
}
185185

186+
override func glucoseUnitDidChange() {
187+
refreshContext = RefreshContext.all
188+
}
189+
186190
private func updateChartDateRange() {
187191
let settings = deviceManager.loopManager.settings
188192

0 commit comments

Comments
 (0)