MinimedPumpManager+UI.swift 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. //
  2. // MinimedPumpManager+UI.swift
  3. // Loop
  4. //
  5. // Copyright © 2018 LoopKit Authors. All rights reserved.
  6. //
  7. import SwiftUI
  8. import UIKit
  9. import LoopKit
  10. import LoopKitUI
  11. import MinimedKit
  12. import RileyLinkKitUI
  13. extension MinimedPumpManager: PumpManagerUI {
  14. static public func setupViewController(insulinTintColor: Color, guidanceColors: GuidanceColors, allowedInsulinTypes: [InsulinType]) -> (UIViewController & PumpManagerSetupViewController & CompletionNotifying) {
  15. let navVC = MinimedPumpManagerSetupViewController.instantiateFromStoryboard()
  16. let insulinSelectionView = InsulinTypeConfirmation(initialValue: .novolog, supportedInsulinTypes: allowedInsulinTypes) { (confirmedType) in
  17. navVC.insulinType = confirmedType
  18. let nextViewController = navVC.storyboard?.instantiateViewController(identifier: "RileyLinkSetup") as! RileyLinkSetupTableViewController
  19. navVC.pushViewController(nextViewController, animated: true)
  20. }
  21. let rootVC = UIHostingController(rootView: insulinSelectionView)
  22. rootVC.title = "Insulin Type"
  23. navVC.pushViewController(rootVC, animated: false)
  24. navVC.navigationBar.backgroundColor = .secondarySystemBackground
  25. return navVC
  26. }
  27. public func settingsViewController(insulinTintColor: Color, guidanceColors: GuidanceColors, allowedInsulinTypes: [InsulinType]) -> (UIViewController & CompletionNotifying) {
  28. let settings = MinimedPumpSettingsViewController(pumpManager: self)
  29. let nav = SettingsNavigationViewController(rootViewController: settings)
  30. return nav
  31. }
  32. public func deliveryUncertaintyRecoveryViewController(insulinTintColor: Color, guidanceColors: GuidanceColors) -> (UIViewController & CompletionNotifying) {
  33. // Return settings for now. No uncertainty handling atm.
  34. let settings = MinimedPumpSettingsViewController(pumpManager: self)
  35. let nav = SettingsNavigationViewController(rootViewController: settings)
  36. return nav
  37. }
  38. public var smallImage: UIImage? {
  39. return state.smallPumpImage
  40. }
  41. public func hudProvider(insulinTintColor: Color, guidanceColors: GuidanceColors, allowedInsulinTypes: [InsulinType]) -> HUDProvider? {
  42. return MinimedHUDProvider(pumpManager: self, insulinTintColor: insulinTintColor, guidanceColors: guidanceColors, allowedInsulinTypes: allowedInsulinTypes)
  43. }
  44. public static func createHUDView(rawValue: HUDProvider.HUDViewRawState) -> LevelHUDView? {
  45. return MinimedHUDProvider.createHUDView(rawValue: rawValue)
  46. }
  47. }
  48. // MARK: - DeliveryLimitSettingsTableViewControllerSyncSource
  49. extension MinimedPumpManager {
  50. public func syncDeliveryLimitSettings(for viewController: DeliveryLimitSettingsTableViewController, completion: @escaping (DeliveryLimitSettingsResult) -> Void) {
  51. pumpOps.runSession(withName: "Save Settings", using: rileyLinkDeviceProvider.firstConnectedDevice) { (session) in
  52. guard let session = session else {
  53. completion(.failure(PumpManagerError.connection(MinimedPumpManagerError.noRileyLink)))
  54. return
  55. }
  56. do {
  57. if let maxBasalRate = viewController.maximumBasalRatePerHour {
  58. try session.setMaxBasalRate(unitsPerHour: maxBasalRate)
  59. }
  60. if let maxBolus = viewController.maximumBolus {
  61. try session.setMaxBolus(units: maxBolus)
  62. }
  63. let settings = try session.getSettings()
  64. completion(.success(maximumBasalRatePerHour: settings.maxBasal, maximumBolus: settings.maxBolus))
  65. } catch let error {
  66. self.log.error("Save delivery limit settings failed: %{public}@", String(describing: error))
  67. completion(.failure(error))
  68. }
  69. }
  70. }
  71. public func syncButtonTitle(for viewController: DeliveryLimitSettingsTableViewController) -> String {
  72. return LocalizedString("Save to Pump…", comment: "Title of button to save delivery limit settings to pump")
  73. }
  74. public func syncButtonDetailText(for viewController: DeliveryLimitSettingsTableViewController) -> String? {
  75. return nil
  76. }
  77. public func deliveryLimitSettingsTableViewControllerIsReadOnly(_ viewController: DeliveryLimitSettingsTableViewController) -> Bool {
  78. return false
  79. }
  80. }
  81. // MARK: - BasalScheduleTableViewControllerSyncSource
  82. extension MinimedPumpManager {
  83. public func syncScheduleValues(for viewController: BasalScheduleTableViewController, completion: @escaping (SyncBasalScheduleResult<Double>) -> Void) {
  84. syncBasalRateSchedule(items: viewController.scheduleItems) { result in
  85. switch result {
  86. case .success(let schedule):
  87. completion(.success(scheduleItems: schedule.items, timeZone: schedule.timeZone))
  88. case .failure(let error):
  89. completion(.failure(error))
  90. }
  91. }
  92. }
  93. public func syncButtonTitle(for viewController: BasalScheduleTableViewController) -> String {
  94. return LocalizedString("Save to Pump…", comment: "Title of button to save basal profile to pump")
  95. }
  96. public func syncButtonDetailText(for viewController: BasalScheduleTableViewController) -> String? {
  97. return nil
  98. }
  99. public func basalScheduleTableViewControllerIsReadOnly(_ viewController: BasalScheduleTableViewController) -> Bool {
  100. return false
  101. }
  102. }