Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 57 additions & 43 deletions Loop/View Controllers/PredictionTableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

Expand Down Expand Up @@ -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
Expand All @@ -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? {
Expand Down
7 changes: 7 additions & 0 deletions Loop/Views/PredictionInputEffectTableViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down