OmnipodReservoirView.swift 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //
  2. // OmnipodReservoirView.swift
  3. // OmniKit
  4. //
  5. // Created by Pete Schwamb on 10/22/18.
  6. // Copyright © 2018 Pete Schwamb. All rights reserved.
  7. //
  8. import UIKit
  9. import LoopKitUI
  10. import OmniKit
  11. public final class OmnipodReservoirView: LevelHUDView, NibLoadable {
  12. override public var orderPriority: HUDViewOrderPriority {
  13. return 11
  14. }
  15. @IBOutlet private weak var volumeLabel: UILabel!
  16. @IBOutlet private weak var alertLabel: UILabel! {
  17. didSet {
  18. alertLabel.alpha = 0
  19. alertLabel.textColor = UIColor.white
  20. alertLabel.layer.cornerRadius = 9
  21. alertLabel.clipsToBounds = true
  22. }
  23. }
  24. public class func instantiate() -> OmnipodReservoirView {
  25. return nib().instantiate(withOwner: nil, options: nil)[0] as! OmnipodReservoirView
  26. }
  27. override public func awakeFromNib() {
  28. super.awakeFromNib()
  29. volumeLabel.isHidden = true
  30. }
  31. private var reservoirLevel: Double? {
  32. didSet {
  33. level = reservoirLevel
  34. switch reservoirLevel {
  35. case .none:
  36. volumeLabel.isHidden = true
  37. case let x? where x > 0.25:
  38. volumeLabel.isHidden = true
  39. case let x? where x > 0.10:
  40. volumeLabel.textColor = tintColor
  41. volumeLabel.isHidden = false
  42. default:
  43. volumeLabel.textColor = tintColor
  44. volumeLabel.isHidden = false
  45. }
  46. }
  47. }
  48. override public func tintColorDidChange() {
  49. super.tintColorDidChange()
  50. volumeLabel.textColor = tintColor
  51. }
  52. private func updateColor() {
  53. switch reservoirAlertState {
  54. case .lowReservoir, .empty:
  55. alertLabel?.backgroundColor = stateColors?.warning
  56. case .ok:
  57. alertLabel?.backgroundColor = stateColors?.normal
  58. }
  59. }
  60. override public func stateColorsDidUpdate() {
  61. super.stateColorsDidUpdate()
  62. updateColor()
  63. }
  64. private var reservoirAlertState = ReservoirAlertState.ok {
  65. didSet {
  66. var alertLabelAlpha: CGFloat = 1
  67. switch reservoirAlertState {
  68. case .ok:
  69. alertLabelAlpha = 0
  70. case .lowReservoir, .empty:
  71. alertLabel?.text = "!"
  72. }
  73. updateColor()
  74. if self.superview == nil {
  75. self.alertLabel?.alpha = alertLabelAlpha
  76. } else {
  77. UIView.animate(withDuration: 0.25, animations: {
  78. self.alertLabel?.alpha = alertLabelAlpha
  79. })
  80. }
  81. }
  82. }
  83. private lazy var timeFormatter: DateFormatter = {
  84. let formatter = DateFormatter()
  85. formatter.dateStyle = .none
  86. formatter.timeStyle = .short
  87. return formatter
  88. }()
  89. private lazy var numberFormatter: NumberFormatter = {
  90. let formatter = NumberFormatter()
  91. formatter.numberStyle = .decimal
  92. formatter.maximumFractionDigits = 0
  93. return formatter
  94. }()
  95. private let insulinFormatter: NumberFormatter = {
  96. let formatter = NumberFormatter()
  97. formatter.numberStyle = .decimal
  98. formatter.maximumFractionDigits = 3
  99. return formatter
  100. }()
  101. public func update(volume: Double?, at date: Date, level: Double?, reservoirAlertState: ReservoirAlertState) {
  102. self.reservoirLevel = level
  103. let time = timeFormatter.string(from: date)
  104. caption?.text = time
  105. if let volume = volume {
  106. if let units = numberFormatter.string(from: volume) {
  107. volumeLabel.text = String(format: LocalizedString("%@U", comment: "Format string for reservoir volume. (1: The localized volume)"), units)
  108. accessibilityValue = String(format: LocalizedString("%1$@ units remaining at %2$@", comment: "Accessibility format string for (1: localized volume)(2: time)"), units, time)
  109. }
  110. } else if let maxReservoirReading = insulinFormatter.string(from: Pod.maximumReservoirReading) {
  111. accessibilityValue = String(format: LocalizedString("Greater than %1$@ units remaining at %2$@", comment: "Accessibility format string for (1: localized volume)(2: time)"), maxReservoirReading, time)
  112. }
  113. self.reservoirAlertState = reservoirAlertState
  114. }
  115. }