Skip to content

Commit

Permalink
[LOOP-4908] Bolus Status Banner UI Updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Camji55 authored Jun 21, 2024
2 parents f05851f + fd8d86d commit 4938d26
Show file tree
Hide file tree
Showing 3 changed files with 173 additions and 147 deletions.
50 changes: 16 additions & 34 deletions Loop/View Controllers/StatusTableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,9 @@ final class StatusTableViewController: LoopChartsTableViewController {

private func updateBolusProgress() {
if let cell = tableView.cellForRow(at: IndexPath(row: StatusRow.status.rawValue, section: Section.status.rawValue)) as? BolusProgressTableViewCell {
cell.deliveredUnits = bolusProgressReporter?.progress.deliveredUnits
if case let .bolusing(_, total) = cell.configuration {
cell.configuration = .bolusing(delivered: bolusProgressReporter?.progress.deliveredUnits, ofTotalVolume: total)
}
}
}

Expand Down Expand Up @@ -1001,7 +1003,6 @@ final class StatusTableViewController: LoopChartsTableViewController {

return cell
case .status:

func getTitleSubtitleCell() -> TitleSubtitleTableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: TitleSubtitleTableViewCell.className, for: indexPath) as! TitleSubtitleTableViewCell
cell.selectionStyle = .none
Expand Down Expand Up @@ -1056,45 +1057,26 @@ final class StatusTableViewController: LoopChartsTableViewController {

return cell
case .enactingBolus:
let cell = getTitleSubtitleCell()
cell.titleLabel.text = NSLocalizedString("Starting Bolus", comment: "The title of the cell indicating a bolus is being sent")

let indicatorView = UIActivityIndicatorView(style: .default)
indicatorView.startAnimating()
cell.accessoryView = indicatorView
return cell
let progressCell = tableView.dequeueReusableCell(withIdentifier: BolusProgressTableViewCell.className, for: indexPath) as! BolusProgressTableViewCell
progressCell.selectionStyle = .none
progressCell.configuration = .starting
return progressCell
case .bolusing(let dose):
let progressCell = tableView.dequeueReusableCell(withIdentifier: BolusProgressTableViewCell.className, for: indexPath) as! BolusProgressTableViewCell
progressCell.selectionStyle = .none
progressCell.totalUnits = dose.programmedUnits
progressCell.configuration = .bolusing(delivered: bolusProgressReporter?.progress.deliveredUnits, ofTotalVolume: dose.programmedUnits)
progressCell.tintColor = .insulinTintColor
progressCell.deliveredUnits = bolusProgressReporter?.progress.deliveredUnits
progressCell.backgroundColor = .secondarySystemBackground
return progressCell
case .cancelingBolus:
let cell = getTitleSubtitleCell()
cell.titleLabel.text = NSLocalizedString("Canceling Bolus", comment: "The title of the cell indicating a bolus is being canceled")

let indicatorView = UIActivityIndicatorView(style: .default)
indicatorView.startAnimating()
cell.accessoryView = indicatorView
return cell
let progressCell = tableView.dequeueReusableCell(withIdentifier: BolusProgressTableViewCell.className, for: indexPath) as! BolusProgressTableViewCell
progressCell.selectionStyle = .none
progressCell.configuration = .canceling
return progressCell
case .canceledBolus(let dose):
let cell = getTitleSubtitleCell()

lazy var insulinFormatter: QuantityFormatter = {
let formatter = QuantityFormatter(for: .internationalUnit())
formatter.numberFormatter.minimumFractionDigits = 2
return formatter
}()

let totalUnitsQuantity = HKQuantity(unit: .internationalUnit(), doubleValue: dose.programmedUnits)
let totalUnitsString = insulinFormatter.string(from: totalUnitsQuantity) ?? ""

let deliveredUnitsQuantity = HKQuantity(unit: .internationalUnit(), doubleValue: dose.deliveredUnits ?? 0)
let deliveredUnitsString = insulinFormatter.string(from: deliveredUnitsQuantity, includeUnit: false) ?? ""
cell.titleLabel.text = String(format: NSLocalizedString("Bolus Canceled: Delivered %1$@ of %2$@", comment: "The title of the cell indicating a bolus has been canceled. (1: delivered volume)(2: total volume)"), deliveredUnitsString, totalUnitsString)
return cell
let progressCell = tableView.dequeueReusableCell(withIdentifier: BolusProgressTableViewCell.className, for: indexPath) as! BolusProgressTableViewCell
progressCell.selectionStyle = .none
progressCell.configuration = .canceled(delivered: dose.deliveredUnits ?? 0, ofTotalVolume: dose.programmedUnits)
return progressCell
case .pumpSuspended(let resuming):
let cell = getTitleSubtitleCell()
cell.titleLabel.text = NSLocalizedString("Insulin Suspended", comment: "The title of the cell indicating the pump is suspended")
Expand Down
115 changes: 69 additions & 46 deletions Loop/Views/BolusProgressTableViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ import MKRingProgressView


public class BolusProgressTableViewCell: UITableViewCell {

public enum Configuration {
case starting
case bolusing(delivered: Double?, ofTotalVolume: Double)
case canceling
case canceled(delivered: Double, ofTotalVolume: Double)
}

@IBOutlet weak var activityIndicator: UIActivityIndicatorView!
@IBOutlet weak var paddedView: UIView!
@IBOutlet weak var progressIndicator: RingProgressView!
@IBOutlet weak var progressLabel: UILabel!

@IBOutlet weak var tapToStopLabel: UILabel! {
Expand All @@ -28,26 +39,12 @@ public class BolusProgressTableViewCell: UITableViewCell {
}
}

@IBOutlet weak var progressIndicator: RingProgressView!

public var totalUnits: Double? {
didSet {
updateProgress()
}
}

public var deliveredUnits: Double? {
public var configuration: Configuration? {
didSet {
updateProgress()
}
}

private lazy var gradient = CAGradientLayer()

private var doseTotalUnits: Double?

private var disableUpdates: Bool = false

lazy var insulinFormatter: QuantityFormatter = {
let formatter = QuantityFormatter(for: .internationalUnit())
formatter.numberFormatter.minimumFractionDigits = 2
Expand All @@ -57,17 +54,14 @@ public class BolusProgressTableViewCell: UITableViewCell {
override public func awakeFromNib() {
super.awakeFromNib()

gradient.frame = bounds
backgroundView?.layer.insertSublayer(gradient, at: 0)
paddedView.layer.masksToBounds = true
paddedView.layer.cornerRadius = 10
paddedView.layer.borderWidth = 1
paddedView.layer.borderColor = UIColor.systemGray5.cgColor

updateColors()
}

override public func layoutSubviews() {
super.layoutSubviews()

gradient.frame = bounds
}

public override func tintColorDidChange() {
super.tintColorDidChange()
updateColors()
Expand All @@ -83,40 +77,69 @@ public class BolusProgressTableViewCell: UITableViewCell {
progressIndicator.startColor = tintColor
progressIndicator.endColor = tintColor
stopSquare.backgroundColor = tintColor
gradient.colors = [
UIColor.cellBackgroundColor.withAlphaComponent(0).cgColor,
UIColor.cellBackgroundColor.cgColor
]
}

private func updateProgress() {
guard !disableUpdates, let totalUnits = totalUnits else {
guard let configuration else {
progressIndicator.isHidden = true
activityIndicator.isHidden = true
tapToStopLabel.isHidden = true
return
}

let totalUnitsQuantity = HKQuantity(unit: .internationalUnit(), doubleValue: totalUnits)
let totalUnitsString = insulinFormatter.string(from: totalUnitsQuantity) ?? ""

if let deliveredUnits = deliveredUnits {
let deliveredUnitsQuantity = HKQuantity(unit: .internationalUnit(), doubleValue: deliveredUnits)
let deliveredUnitsString = insulinFormatter.string(from: deliveredUnitsQuantity, includeUnit: false) ?? ""

progressLabel.text = String(format: NSLocalizedString("Bolused %1$@ of %2$@", comment: "The format string for bolus progress. (1: delivered volume)(2: total volume)"), deliveredUnitsString, totalUnitsString)

let progress = deliveredUnits / totalUnits
UIView.animate(withDuration: 0.3) {
self.progressIndicator.progress = progress

switch configuration {
case .starting:
progressIndicator.isHidden = true
activityIndicator.isHidden = false
tapToStopLabel.isHidden = true

progressLabel.text = NSLocalizedString("Starting Bolus", comment: "The title of the cell indicating a bolus is being sent")
case let .bolusing(delivered, totalVolume):
progressIndicator.isHidden = false
activityIndicator.isHidden = true
tapToStopLabel.isHidden = false

let totalUnitsQuantity = HKQuantity(unit: .internationalUnit(), doubleValue: totalVolume)
let totalUnitsString = insulinFormatter.string(from: totalUnitsQuantity) ?? ""

if let delivered {
let deliveredUnitsQuantity = HKQuantity(unit: .internationalUnit(), doubleValue: delivered)
let deliveredUnitsString = insulinFormatter.string(from: deliveredUnitsQuantity, includeUnit: false) ?? ""

progressLabel.text = String(format: NSLocalizedString("Bolused %1$@ of %2$@", comment: "The format string for bolus progress. (1: delivered volume)(2: total volume)"), deliveredUnitsString, totalUnitsString)

let progress = delivered / totalVolume

UIView.animate(withDuration: 0.3) {
self.progressIndicator.progress = progress
}
} else {
progressLabel.text = String(format: NSLocalizedString("Bolusing %1$@", comment: "The format string for bolus in progress showing total volume. (1: total volume)"), totalUnitsString)
}
} else {
progressLabel.text = String(format: NSLocalizedString("Bolusing %1$@", comment: "The format string for bolus in progress showing total volume. (1: total volume)"), totalUnitsString)
case .canceling:
progressIndicator.isHidden = true
activityIndicator.isHidden = false
tapToStopLabel.isHidden = true

progressLabel.text = NSLocalizedString("Canceling Bolus", comment: "The title of the cell indicating a bolus is being canceled")
case let .canceled(delivered, totalVolume):
progressIndicator.isHidden = true
activityIndicator.isHidden = true
tapToStopLabel.isHidden = true

let totalUnitsQuantity = HKQuantity(unit: .internationalUnit(), doubleValue: totalVolume)
let totalUnitsString = insulinFormatter.string(from: totalUnitsQuantity) ?? ""

let deliveredUnitsQuantity = HKQuantity(unit: .internationalUnit(), doubleValue: delivered)
let deliveredUnitsString = insulinFormatter.string(from: deliveredUnitsQuantity, includeUnit: false) ?? ""

progressLabel.text = String(format: NSLocalizedString("Bolus Canceled: Delivered %1$@ of %2$@", comment: "The title of the cell indicating a bolus has been canceled. (1: delivered volume)(2: total volume)"), deliveredUnitsString, totalUnitsString)
}
}

override public func prepareForReuse() {
super.prepareForReuse()
disableUpdates = true
deliveredUnits = 0
disableUpdates = false
configuration = nil
progressIndicator.progress = 0
CATransaction.flush()
progressLabel.text = ""
Expand Down
Loading

0 comments on commit 4938d26

Please sign in to comment.