From 2981af3c7e2913fec4416f93b924a4a05692e910 Mon Sep 17 00:00:00 2001 From: Pete Schwamb Date: Sun, 25 Jun 2017 00:11:21 -0500 Subject: [PATCH] Fixing an issue where prediction text didn't display due to a race condition --- .../PredictionTableViewController.swift | 100 ++++++++++-------- .../PredictionInputEffectTableViewCell.swift | 7 ++ 2 files changed, 64 insertions(+), 43 deletions(-) diff --git a/Loop/View Controllers/PredictionTableViewController.swift b/Loop/View Controllers/PredictionTableViewController.swift index 94d83eea1f..d1d870b52d 100644 --- a/Loop/View Controllers/PredictionTableViewController.swift +++ b/Loop/View Controllers/PredictionTableViewController.swift @@ -154,13 +154,24 @@ class PredictionTableViewController: ChartsTableViewController, IdentifiableClas reloadGroup.notify(queue: .main) { self.charts.prerender() - for case let cell as ChartTableViewCell in self.tableView.visibleCells { - cell.reloadChart() - - if let indexPath = self.tableView.indexPath(for: cell) { - self.tableView(self.tableView, updateTitleFor: cell, at: indexPath) + self.tableView.beginUpdates() + for cell in self.tableView.visibleCells { + switch cell { + case let cell as ChartTableViewCell: + cell.reloadChart() + + if let indexPath = self.tableView.indexPath(for: cell) { + self.tableView(self.tableView, updateTitleFor: cell, at: indexPath) + } + case let cell as PredictionInputEffectTableViewCell: + if let indexPath = self.tableView.indexPath(for: cell) { + self.tableView(self.tableView, updateTextFor: cell, at: indexPath) + } + default: + break } } + self.tableView.endUpdates() } } @@ -214,35 +225,7 @@ class PredictionTableViewController: ChartsTableViewController, IdentifiableClas return cell case .inputs: let cell = tableView.dequeueReusableCell(withIdentifier: PredictionInputEffectTableViewCell.className, for: indexPath) as! PredictionInputEffectTableViewCell - - let input = availableInputs[indexPath.row] - - cell.titleLabel?.text = input.localizedTitle - cell.accessoryType = selectedInputs.contains(input) ? .checkmark : .none - cell.enabled = input != .retrospection || deviceManager.loopManager.settings.retrospectiveCorrectionEnabled - - var subtitleText = input.localizedDescription(forGlucoseUnit: charts.glucoseUnit) ?? "" - - if input == .retrospection, - let startGlucose = retrospectivePredictedGlucose?.first, - let endGlucose = retrospectivePredictedGlucose?.last, - let currentGlucose = self.deviceManager.loopManager.glucoseStore.latestGlucose - { - let formatter = NumberFormatter.glucoseFormatter(for: charts.glucoseUnit) - let values = [startGlucose, endGlucose, currentGlucose].map { formatter.string(from: NSNumber(value: $0.quantity.doubleValue(for: charts.glucoseUnit))) ?? "?" } - - let retro = String( - 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)"), - values[0], values[1], values[2] - ) - - subtitleText = String(format: "%@\n%@", subtitleText, retro) - } - - cell.subtitleLabel?.text = subtitleText - - cell.contentView.layoutMargins.left = tableView.separatorInset.left - + self.tableView(tableView, updateTextFor: cell, at: indexPath) return cell case .settings: let cell = tableView.dequeueReusableCell(withIdentifier: SwitchTableViewCell.className, for: indexPath) as! SwitchTableViewCell @@ -259,16 +242,47 @@ class PredictionTableViewController: ChartsTableViewController, IdentifiableClas } private func tableView(_ tableView: UITableView, updateTitleFor cell: ChartTableViewCell, at indexPath: IndexPath) { - switch Section(rawValue: indexPath.section)! { - case .charts: - if let eventualGlucose = eventualGlucoseDescription { - cell.titleLabel?.text = String(format: NSLocalizedString("Eventually %@", comment: "The subtitle format describing eventual glucose. (1: localized glucose value description)"), eventualGlucose) - } else { - cell.titleLabel?.text = "–" - } - default: - break + guard case .charts? = Section(rawValue: indexPath.section) else { + return + } + + if let eventualGlucose = eventualGlucoseDescription { + cell.titleLabel?.text = String(format: NSLocalizedString("Eventually %@", comment: "The subtitle format describing eventual glucose. (1: localized glucose value description)"), eventualGlucose) + } else { + cell.titleLabel?.text = "–" + } + } + + private func tableView(_ tableView: UITableView, updateTextFor cell: PredictionInputEffectTableViewCell, at indexPath: IndexPath) { + guard case .inputs? = Section(rawValue: indexPath.section) else { + return + } + + let input = availableInputs[indexPath.row] + + cell.titleLabel?.text = input.localizedTitle + cell.accessoryType = selectedInputs.contains(input) ? .checkmark : .none + cell.enabled = input != .retrospection || deviceManager.loopManager.settings.retrospectiveCorrectionEnabled + + var subtitleText = input.localizedDescription(forGlucoseUnit: charts.glucoseUnit) ?? "" + + if input == .retrospection, + let startGlucose = retrospectivePredictedGlucose?.first, + let endGlucose = retrospectivePredictedGlucose?.last, + let currentGlucose = self.deviceManager.loopManager.glucoseStore.latestGlucose + { + let formatter = NumberFormatter.glucoseFormatter(for: charts.glucoseUnit) + let values = [startGlucose, endGlucose, currentGlucose].map { formatter.string(from: NSNumber(value: $0.quantity.doubleValue(for: charts.glucoseUnit))) ?? "?" } + + let retro = String( + 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)"), + values[0], values[1], values[2] + ) + + subtitleText = String(format: "%@\n%@", subtitleText, retro) } + + cell.subtitleLabel?.text = subtitleText } override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { diff --git a/Loop/Views/PredictionInputEffectTableViewCell.swift b/Loop/Views/PredictionInputEffectTableViewCell.swift index e9736b4708..567a043f8c 100644 --- a/Loop/Views/PredictionInputEffectTableViewCell.swift +++ b/Loop/Views/PredictionInputEffectTableViewCell.swift @@ -14,6 +14,13 @@ class PredictionInputEffectTableViewCell: UITableViewCell { @IBOutlet weak var subtitleLabel: UILabel! + override func layoutSubviews() { + super.layoutSubviews() + + contentView.layoutMargins.left = separatorInset.left + contentView.layoutMargins.right = separatorInset.left + } + var enabled: Bool = true { didSet { if enabled {