Skip to content

Commit a0d14cb

Browse files
authored
Merge pull request #500 from LoopKit/fixing-rc-text
Fixing an issue where prediction text didn't display due to a race co…
2 parents 76762ed + 2981af3 commit a0d14cb

File tree

2 files changed

+64
-43
lines changed

2 files changed

+64
-43
lines changed

Loop/View Controllers/PredictionTableViewController.swift

Lines changed: 57 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -154,13 +154,24 @@ class PredictionTableViewController: ChartsTableViewController, IdentifiableClas
154154
reloadGroup.notify(queue: .main) {
155155
self.charts.prerender()
156156

157-
for case let cell as ChartTableViewCell in self.tableView.visibleCells {
158-
cell.reloadChart()
159-
160-
if let indexPath = self.tableView.indexPath(for: cell) {
161-
self.tableView(self.tableView, updateTitleFor: cell, at: indexPath)
157+
self.tableView.beginUpdates()
158+
for cell in self.tableView.visibleCells {
159+
switch cell {
160+
case let cell as ChartTableViewCell:
161+
cell.reloadChart()
162+
163+
if let indexPath = self.tableView.indexPath(for: cell) {
164+
self.tableView(self.tableView, updateTitleFor: cell, at: indexPath)
165+
}
166+
case let cell as PredictionInputEffectTableViewCell:
167+
if let indexPath = self.tableView.indexPath(for: cell) {
168+
self.tableView(self.tableView, updateTextFor: cell, at: indexPath)
169+
}
170+
default:
171+
break
162172
}
163173
}
174+
self.tableView.endUpdates()
164175
}
165176
}
166177

@@ -214,35 +225,7 @@ class PredictionTableViewController: ChartsTableViewController, IdentifiableClas
214225
return cell
215226
case .inputs:
216227
let cell = tableView.dequeueReusableCell(withIdentifier: PredictionInputEffectTableViewCell.className, for: indexPath) as! PredictionInputEffectTableViewCell
217-
218-
let input = availableInputs[indexPath.row]
219-
220-
cell.titleLabel?.text = input.localizedTitle
221-
cell.accessoryType = selectedInputs.contains(input) ? .checkmark : .none
222-
cell.enabled = input != .retrospection || deviceManager.loopManager.settings.retrospectiveCorrectionEnabled
223-
224-
var subtitleText = input.localizedDescription(forGlucoseUnit: charts.glucoseUnit) ?? ""
225-
226-
if input == .retrospection,
227-
let startGlucose = retrospectivePredictedGlucose?.first,
228-
let endGlucose = retrospectivePredictedGlucose?.last,
229-
let currentGlucose = self.deviceManager.loopManager.glucoseStore.latestGlucose
230-
{
231-
let formatter = NumberFormatter.glucoseFormatter(for: charts.glucoseUnit)
232-
let values = [startGlucose, endGlucose, currentGlucose].map { formatter.string(from: NSNumber(value: $0.quantity.doubleValue(for: charts.glucoseUnit))) ?? "?" }
233-
234-
let retro = String(
235-
format: NSLocalizedString("Last comparison: %1$@ → %2$@ vs %3$@", comment: "Format string describing retrospective glucose prediction comparison. (1: Previous glucose)(2: Predicted glucose)(3: Actual glucose)"),
236-
values[0], values[1], values[2]
237-
)
238-
239-
subtitleText = String(format: "%@\n%@", subtitleText, retro)
240-
}
241-
242-
cell.subtitleLabel?.text = subtitleText
243-
244-
cell.contentView.layoutMargins.left = tableView.separatorInset.left
245-
228+
self.tableView(tableView, updateTextFor: cell, at: indexPath)
246229
return cell
247230
case .settings:
248231
let cell = tableView.dequeueReusableCell(withIdentifier: SwitchTableViewCell.className, for: indexPath) as! SwitchTableViewCell
@@ -259,16 +242,47 @@ class PredictionTableViewController: ChartsTableViewController, IdentifiableClas
259242
}
260243

261244
private func tableView(_ tableView: UITableView, updateTitleFor cell: ChartTableViewCell, at indexPath: IndexPath) {
262-
switch Section(rawValue: indexPath.section)! {
263-
case .charts:
264-
if let eventualGlucose = eventualGlucoseDescription {
265-
cell.titleLabel?.text = String(format: NSLocalizedString("Eventually %@", comment: "The subtitle format describing eventual glucose. (1: localized glucose value description)"), eventualGlucose)
266-
} else {
267-
cell.titleLabel?.text = ""
268-
}
269-
default:
270-
break
245+
guard case .charts? = Section(rawValue: indexPath.section) else {
246+
return
247+
}
248+
249+
if let eventualGlucose = eventualGlucoseDescription {
250+
cell.titleLabel?.text = String(format: NSLocalizedString("Eventually %@", comment: "The subtitle format describing eventual glucose. (1: localized glucose value description)"), eventualGlucose)
251+
} else {
252+
cell.titleLabel?.text = ""
253+
}
254+
}
255+
256+
private func tableView(_ tableView: UITableView, updateTextFor cell: PredictionInputEffectTableViewCell, at indexPath: IndexPath) {
257+
guard case .inputs? = Section(rawValue: indexPath.section) else {
258+
return
259+
}
260+
261+
let input = availableInputs[indexPath.row]
262+
263+
cell.titleLabel?.text = input.localizedTitle
264+
cell.accessoryType = selectedInputs.contains(input) ? .checkmark : .none
265+
cell.enabled = input != .retrospection || deviceManager.loopManager.settings.retrospectiveCorrectionEnabled
266+
267+
var subtitleText = input.localizedDescription(forGlucoseUnit: charts.glucoseUnit) ?? ""
268+
269+
if input == .retrospection,
270+
let startGlucose = retrospectivePredictedGlucose?.first,
271+
let endGlucose = retrospectivePredictedGlucose?.last,
272+
let currentGlucose = self.deviceManager.loopManager.glucoseStore.latestGlucose
273+
{
274+
let formatter = NumberFormatter.glucoseFormatter(for: charts.glucoseUnit)
275+
let values = [startGlucose, endGlucose, currentGlucose].map { formatter.string(from: NSNumber(value: $0.quantity.doubleValue(for: charts.glucoseUnit))) ?? "?" }
276+
277+
let retro = String(
278+
format: NSLocalizedString("Last comparison: %1$@ → %2$@ vs %3$@", comment: "Format string describing retrospective glucose prediction comparison. (1: Previous glucose)(2: Predicted glucose)(3: Actual glucose)"),
279+
values[0], values[1], values[2]
280+
)
281+
282+
subtitleText = String(format: "%@\n%@", subtitleText, retro)
271283
}
284+
285+
cell.subtitleLabel?.text = subtitleText
272286
}
273287

274288
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {

Loop/Views/PredictionInputEffectTableViewCell.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ class PredictionInputEffectTableViewCell: UITableViewCell {
1414

1515
@IBOutlet weak var subtitleLabel: UILabel!
1616

17+
override func layoutSubviews() {
18+
super.layoutSubviews()
19+
20+
contentView.layoutMargins.left = separatorInset.left
21+
contentView.layoutMargins.right = separatorInset.left
22+
}
23+
1724
var enabled: Bool = true {
1825
didSet {
1926
if enabled {

0 commit comments

Comments
 (0)