MockPumpManagerSettingsSetupViewController.swift 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. //
  2. // MockPumpManagerSettingsSetupViewController.swift
  3. // LoopKitUI
  4. //
  5. // Created by Michael Pangburn on 11/20/18.
  6. // Copyright © 2018 LoopKit Authors. All rights reserved.
  7. //
  8. import UIKit
  9. import HealthKit
  10. import LoopKit
  11. import LoopKitUI
  12. import MockKit
  13. final class MockPumpManagerSettingsSetupViewController: SetupTableViewController {
  14. var pumpManager: MockPumpManager?
  15. private var pumpManagerSetupViewController: MockPumpManagerSetupViewController? {
  16. return navigationController as? MockPumpManagerSetupViewController
  17. }
  18. override func viewDidLoad() {
  19. super.viewDidLoad()
  20. tableView.register(SettingsTableViewCell.self, forCellReuseIdentifier: SettingsTableViewCell.className)
  21. }
  22. private lazy var quantityFormatter: QuantityFormatter = {
  23. let quantityFormatter = QuantityFormatter()
  24. quantityFormatter.numberFormatter.minimumFractionDigits = 0
  25. quantityFormatter.numberFormatter.maximumFractionDigits = 3
  26. return quantityFormatter
  27. }()
  28. // MARK: - Table view data source
  29. private enum Section: Int, CaseIterable {
  30. case configuration
  31. }
  32. private enum ConfigurationRow: Int, CaseIterable {
  33. case basalRates
  34. case deliveryLimits
  35. }
  36. override func numberOfSections(in tableView: UITableView) -> Int {
  37. return Section.allCases.count
  38. }
  39. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  40. switch Section(rawValue: section)! {
  41. case .configuration:
  42. return ConfigurationRow.allCases.count
  43. }
  44. }
  45. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  46. switch Section(rawValue: indexPath.section)! {
  47. case .configuration:
  48. let cell = tableView.dequeueReusableCell(withIdentifier: SettingsTableViewCell.className, for: indexPath)
  49. switch ConfigurationRow(rawValue: indexPath.row)! {
  50. case .basalRates:
  51. cell.textLabel?.text = "Basal Rates"
  52. if let basalRateSchedule = pumpManagerSetupViewController?.basalSchedule {
  53. let unit = HKUnit.internationalUnit()
  54. let total = HKQuantity(unit: unit, doubleValue: basalRateSchedule.total())
  55. cell.detailTextLabel?.text = quantityFormatter.string(from: total, for: unit)
  56. } else {
  57. cell.detailTextLabel?.text = SettingsTableViewCell.TapToSetString
  58. }
  59. case .deliveryLimits:
  60. cell.textLabel?.text = "Delivery Limits"
  61. if pumpManagerSetupViewController?.maxBolusUnits == nil || pumpManagerSetupViewController?.maxBasalRateUnitsPerHour == nil {
  62. cell.detailTextLabel?.text = SettingsTableViewCell.TapToSetString
  63. } else {
  64. cell.detailTextLabel?.text = SettingsTableViewCell.EnabledString
  65. }
  66. }
  67. cell.accessoryType = .disclosureIndicator
  68. return cell
  69. }
  70. }
  71. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  72. let sender = tableView.cellForRow(at: indexPath)
  73. switch Section(rawValue: indexPath.section)! {
  74. case .configuration:
  75. switch ConfigurationRow(rawValue: indexPath.row)! {
  76. case .basalRates:
  77. guard let pumpManager = pumpManager else {
  78. return
  79. }
  80. let vc = BasalScheduleTableViewController(allowedBasalRates: pumpManager.supportedBasalRates, maximumScheduleItemCount: pumpManager.maximumBasalScheduleEntryCount, minimumTimeInterval: pumpManager.minimumBasalScheduleEntryDuration)
  81. if let profile = pumpManagerSetupViewController?.basalSchedule {
  82. vc.scheduleItems = profile.items
  83. vc.timeZone = profile.timeZone
  84. }
  85. vc.title = sender?.textLabel?.text
  86. vc.delegate = self
  87. show(vc, sender: sender)
  88. case .deliveryLimits:
  89. let vc = DeliveryLimitSettingsTableViewController(style: .grouped)
  90. vc.maximumBasalRatePerHour = pumpManagerSetupViewController?.maxBasalRateUnitsPerHour
  91. vc.maximumBolus = pumpManagerSetupViewController?.maxBolusUnits
  92. vc.title = sender?.textLabel?.text
  93. vc.delegate = self
  94. show(vc, sender: sender)
  95. }
  96. }
  97. }
  98. override func continueButtonPressed(_ sender: Any) {
  99. pumpManagerSetupViewController?.completeSetup()
  100. }
  101. }
  102. extension MockPumpManagerSettingsSetupViewController: DailyValueScheduleTableViewControllerDelegate {
  103. func dailyValueScheduleTableViewControllerWillFinishUpdating(_ controller: DailyValueScheduleTableViewController) {
  104. if let controller = controller as? BasalScheduleTableViewController {
  105. pumpManagerSetupViewController?.basalSchedule = BasalRateSchedule(dailyItems: controller.scheduleItems, timeZone: controller.timeZone)
  106. }
  107. tableView.reloadRows(at: [[Section.configuration.rawValue, ConfigurationRow.basalRates.rawValue]], with: .none)
  108. }
  109. }
  110. extension MockPumpManagerSettingsSetupViewController: DeliveryLimitSettingsTableViewControllerDelegate {
  111. func deliveryLimitSettingsTableViewControllerDidUpdateMaximumBasalRatePerHour(_ vc: DeliveryLimitSettingsTableViewController) {
  112. pumpManagerSetupViewController?.maxBasalRateUnitsPerHour = vc.maximumBasalRatePerHour
  113. tableView.reloadRows(at: [[Section.configuration.rawValue, ConfigurationRow.deliveryLimits.rawValue]], with: .none)
  114. }
  115. func deliveryLimitSettingsTableViewControllerDidUpdateMaximumBolus(_ vc: DeliveryLimitSettingsTableViewController) {
  116. pumpManagerSetupViewController?.maxBolusUnits = vc.maximumBolus
  117. tableView.reloadRows(at: [[Section.configuration.rawValue, ConfigurationRow.deliveryLimits.rawValue]], with: .none)
  118. }
  119. }