MinimedPumpSettingsView.swift 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. //
  2. // MinimedPumpSettingsView.swift
  3. // MinimedKitUI
  4. //
  5. // Created by Pete Schwamb on 11/29/22.
  6. // Copyright © 2022 LoopKit Authors. All rights reserved.
  7. //
  8. import Foundation
  9. import SwiftUI
  10. import LoopKitUI
  11. import LoopKit
  12. import RileyLinkKit
  13. import RileyLinkBLEKit
  14. struct MinimedPumpSettingsView: View {
  15. @Environment(\.guidanceColors) private var guidanceColors
  16. @Environment(\.insulinTintColor) var insulinTintColor
  17. @ObservedObject var viewModel: MinimedPumpSettingsViewModel
  18. @ObservedObject var rileyLinkListDataSource: RileyLinkListDataSource
  19. var supportedInsulinTypes: [InsulinType]
  20. @State private var showingDeletionSheet = false
  21. @State private var showSyncTimeOptions = false;
  22. var handleRileyLinkSelection: (RileyLinkDevice) -> Void
  23. init(viewModel: MinimedPumpSettingsViewModel, supportedInsulinTypes: [InsulinType], handleRileyLinkSelection: @escaping (RileyLinkDevice) -> Void, rileyLinkListDataSource: RileyLinkListDataSource) {
  24. self.viewModel = viewModel
  25. self.supportedInsulinTypes = supportedInsulinTypes
  26. self.handleRileyLinkSelection = handleRileyLinkSelection
  27. self.rileyLinkListDataSource = rileyLinkListDataSource
  28. }
  29. var body: some View {
  30. List {
  31. Section {
  32. headerImage
  33. .padding(.vertical)
  34. HStack(alignment: .top) {
  35. deliveryStatus
  36. Spacer()
  37. reservoirStatus
  38. }
  39. .padding(.bottom, 5)
  40. }
  41. if let basalDeliveryState = viewModel.basalDeliveryState {
  42. Section {
  43. HStack {
  44. Button(basalDeliveryState.buttonLabelText) {
  45. viewModel.suspendResumeButtonPressed(action: basalDeliveryState.shownAction)
  46. }.disabled(viewModel.suspendResumeButtonEnabled)
  47. if viewModel.suspendResumeButtonEnabled {
  48. Spacer()
  49. ProgressView()
  50. }
  51. }
  52. }
  53. }
  54. Section(header: SectionHeader(label: LocalizedString("Configuration", comment: "The title of the configuration section in MinimedPumpManager settings")))
  55. {
  56. NavigationLink(destination: InsulinTypeSetting(initialValue: viewModel.pumpManager.state.insulinType, supportedInsulinTypes: supportedInsulinTypes, allowUnsetInsulinType: false, didChange: viewModel.didChangeInsulinType)) {
  57. HStack {
  58. Text(LocalizedString("Insulin Type", comment: "Text for confidence reminders navigation link")).foregroundColor(Color.primary)
  59. if let currentTitle = viewModel.pumpManager.state.insulinType?.brandName {
  60. Spacer()
  61. Text(currentTitle)
  62. .foregroundColor(.secondary)
  63. }
  64. }
  65. }
  66. NavigationLink(destination: BatteryTypeSelectionView(batteryType: $viewModel.batteryChemistryType)) {
  67. HStack {
  68. Text(LocalizedString("Pump Battery Type", comment: "Text for medtronic pump battery type")).foregroundColor(Color.primary)
  69. Spacer()
  70. Text(viewModel.batteryChemistryType.description)
  71. .foregroundColor(.secondary)
  72. }
  73. }
  74. NavigationLink(destination: DataSourceSelectionView(batteryType: $viewModel.preferredDataSource)) {
  75. HStack {
  76. Text(LocalizedString("Preferred Data Source", comment: "Text for medtronic pump preferred data source")).foregroundColor(Color.primary)
  77. Spacer()
  78. Text(viewModel.preferredDataSource.description)
  79. .foregroundColor(.secondary)
  80. }
  81. }
  82. if viewModel.pumpManager.state.pumpModel.hasMySentry {
  83. NavigationLink(destination: UseMySentrySelectionView(mySentryConfig: $viewModel.mySentryConfig)) {
  84. HStack {
  85. Text(LocalizedString("Use MySentry", comment: "Text for medtronic pump to use MySentry")).foregroundColor(Color.primary)
  86. Spacer()
  87. Text((viewModel.mySentryConfig == .useMySentry ?
  88. LocalizedString("Yes", comment: "Value string for MySentry config when MySentry is being used") :
  89. LocalizedString("No", comment: "Value string for MySentry config when MySentry is not being used"))
  90. )
  91. .foregroundColor(.secondary)
  92. }
  93. }
  94. }
  95. }
  96. Section(header: HStack {
  97. Text(LocalizedString("Devices", comment: "Header for devices section of RileyLinkSetupView"))
  98. Spacer()
  99. ProgressView()
  100. }) {
  101. ForEach(rileyLinkListDataSource.devices, id: \.peripheralIdentifier) { device in
  102. Toggle(isOn: rileyLinkListDataSource.autoconnectBinding(for: device)) {
  103. HStack {
  104. Text(device.name ?? "Unknown")
  105. Spacer()
  106. if rileyLinkListDataSource.autoconnectBinding(for: device).wrappedValue {
  107. if device.isConnected {
  108. Text(formatRSSI(rssi:device.rssi)).foregroundColor(.secondary)
  109. } else {
  110. Image(systemName: "wifi.exclamationmark")
  111. .imageScale(.large)
  112. .foregroundColor(guidanceColors.warning)
  113. }
  114. }
  115. }
  116. .contentShape(Rectangle())
  117. .onTapGesture {
  118. handleRileyLinkSelection(device)
  119. }
  120. }
  121. }
  122. }
  123. .onAppear {
  124. rileyLinkListDataSource.isScanningEnabled = true
  125. }
  126. .onDisappear {
  127. rileyLinkListDataSource.isScanningEnabled = false
  128. }
  129. Section() {
  130. HStack {
  131. Text(LocalizedString("Pump Battery Remaining", comment: "Text for medtronic pump battery percent remaining")).foregroundColor(Color.primary)
  132. Spacer()
  133. if let chargeRemaining = viewModel.pumpManager.status.pumpBatteryChargeRemaining {
  134. Text(String("\(Int(round(chargeRemaining * 100)))%"))
  135. } else {
  136. Text(String(LocalizedString("unknown", comment: "Text to indicate battery percentage is unknown")))
  137. }
  138. }
  139. HStack {
  140. Text(LocalizedString("Pump Time", comment: "The title of the command to change pump time zone"))
  141. Spacer()
  142. if viewModel.isClockOffset {
  143. Image(systemName: "clock.fill")
  144. .foregroundColor(guidanceColors.warning)
  145. }
  146. TimeView(timeZone: viewModel.pumpManager.status.timeZone)
  147. .foregroundColor( viewModel.isClockOffset ? guidanceColors.warning : nil)
  148. }
  149. if viewModel.synchronizingTime {
  150. HStack {
  151. Text(LocalizedString("Adjusting Pump Time...", comment: "Text indicating ongoing pump time synchronization"))
  152. .foregroundColor(.secondary)
  153. Spacer()
  154. ActivityIndicator(isAnimating: .constant(true), style: .medium)
  155. }
  156. } else if self.viewModel.pumpManager.status.timeZone != TimeZone.currentFixed {
  157. Button(action: {
  158. showSyncTimeOptions = true
  159. }) {
  160. Text(LocalizedString("Sync to Current Time", comment: "The title of the command to change pump time zone"))
  161. }
  162. .actionSheet(isPresented: $showSyncTimeOptions) {
  163. syncPumpTimeActionSheet
  164. }
  165. }
  166. }
  167. Section {
  168. LabeledValueView(label: LocalizedString("Pump ID", comment: "The title text for the pump ID config value"),
  169. value: viewModel.pumpManager.state.pumpID)
  170. LabeledValueView(label: LocalizedString("Firmware Version", comment: "The title of the cell showing the pump firmware version"),
  171. value: String(describing: viewModel.pumpManager.state.pumpFirmwareVersion))
  172. LabeledValueView(label: LocalizedString("Region", comment: "The title of the cell showing the pump region"),
  173. value: String(describing: viewModel.pumpManager.state.pumpRegion))
  174. }
  175. Section() {
  176. deletePumpButton
  177. }
  178. }
  179. .alert(item: $viewModel.activeAlert, content: { alert in
  180. switch alert {
  181. case .suspendError(let error):
  182. return Alert(title: Text(LocalizedString("Error Suspending", comment: "The alert title for a suspend error")),
  183. message: Text(errorText(error)))
  184. case .resumeError(let error):
  185. return Alert(title: Text(LocalizedString("Error Resuming", comment: "The alert title for a resume error")),
  186. message: Text(errorText(error)))
  187. case .syncTimeError(let error):
  188. return Alert(title: Text(LocalizedString("Error Syncing Time", comment: "The alert title for an error while synching time")),
  189. message: Text(errorText(error)))
  190. }
  191. })
  192. .insetGroupedListStyle()
  193. .navigationBarItems(trailing: doneButton)
  194. .navigationBarTitle(String(format: LocalizedString("Medtronic %1$@", comment: "Format string fof navigation bar title for MinimedPumpSettingsView (1: model number)"), viewModel.pumpManager.state.pumpModel.description))
  195. }
  196. var deliverySectionTitle: String {
  197. if self.viewModel.isScheduledBasal {
  198. return LocalizedString("Scheduled Basal", comment: "Title of insulin delivery section")
  199. } else {
  200. return LocalizedString("Insulin Delivery", comment: "Title of insulin delivery section")
  201. }
  202. }
  203. var deliveryStatus: some View {
  204. VStack(alignment: .leading, spacing: 5) {
  205. Text(deliverySectionTitle)
  206. .foregroundColor(Color(UIColor.secondaryLabel))
  207. if viewModel.isSuspendedOrResuming {
  208. HStack(alignment: .center) {
  209. Image(systemName: "pause.circle.fill")
  210. .font(.system(size: 34))
  211. .fixedSize()
  212. .foregroundColor(viewModel.suspendResumeButtonColor(guidanceColors: guidanceColors))
  213. Text(LocalizedString("Insulin\nSuspended", comment: "Text shown in insulin delivery space when insulin suspended"))
  214. .fontWeight(.bold)
  215. .fixedSize()
  216. }
  217. } else if let basalRate = self.viewModel.basalDeliveryRate {
  218. HStack(alignment: .center) {
  219. HStack(alignment: .lastTextBaseline, spacing: 3) {
  220. Text(viewModel.basalRateFormatter.string(from: basalRate) ?? "")
  221. .font(.system(size: 28))
  222. .fontWeight(.heavy)
  223. .fixedSize()
  224. Text(LocalizedString("U/hr", comment: "Units for showing temp basal rate"))
  225. .foregroundColor(.secondary)
  226. }
  227. }
  228. } else if viewModel.basalDeliveryState?.isTransitioning == true {
  229. HStack(alignment: .center) {
  230. Image(systemName: "arrow.clockwise.circle.fill")
  231. .font(.system(size: 34))
  232. .fixedSize()
  233. .foregroundColor(.secondary)
  234. Text(LocalizedString("Changing", comment: "Text shown in basal rate space when basal is changing"))
  235. .fontWeight(.bold)
  236. .fixedSize()
  237. .foregroundColor(.secondary)
  238. }
  239. } else {
  240. HStack(alignment: .center) {
  241. Image(systemName: "x.circle.fill")
  242. .font(.system(size: 34))
  243. .fixedSize()
  244. .foregroundColor(guidanceColors.warning)
  245. Text(LocalizedString("Unknown", comment: "Text shown in basal rate space when delivery status is unknown"))
  246. .fontWeight(.bold)
  247. .fixedSize()
  248. }
  249. }
  250. }
  251. }
  252. func reservoirColor(for reservoirLevelHighlightState: ReservoirLevelHighlightState) -> Color {
  253. switch reservoirLevelHighlightState {
  254. case .normal:
  255. return insulinTintColor
  256. case .warning:
  257. return guidanceColors.warning
  258. case .critical:
  259. return guidanceColors.critical
  260. }
  261. }
  262. var reservoirStatus: some View {
  263. VStack(alignment: .leading, spacing: 5) {
  264. Text(LocalizedString("Insulin Remaining", comment: "Header for insulin remaining on pod settings screen"))
  265. .foregroundColor(Color(UIColor.secondaryLabel))
  266. if let reservoirReading = viewModel.reservoirReading,
  267. let reservoirLevelHighlightState = viewModel.reservoirLevelHighlightState,
  268. let reservoirPercent = viewModel.reservoirPercentage
  269. {
  270. HStack {
  271. MinimedReservoirView(filledPercent: reservoirPercent, fillColor: reservoirColor(for: reservoirLevelHighlightState))
  272. .frame(width: 23, height: 32)
  273. Text(viewModel.reservoirText(for: reservoirReading.units))
  274. .font(.system(size: 28))
  275. .fontWeight(.heavy)
  276. .fixedSize()
  277. }
  278. }
  279. }
  280. }
  281. var syncPumpTimeActionSheet: ActionSheet {
  282. ActionSheet(
  283. title: Text(LocalizedString("Time Change Detected", comment: "Title for pod sync time action sheet.")),
  284. message: Text(LocalizedString("The time on your pump is different from the current time. Do you want to update the time on your pump to the current time?", comment: "Message for pod sync time action sheet")),
  285. buttons: [
  286. .default(Text(LocalizedString("Yes, Sync to Current Time", comment: "Button text to confirm pump time sync"))) {
  287. self.viewModel.changeTimeZoneTapped()
  288. },
  289. .cancel(Text(LocalizedString("No, Keep Pump As Is", comment: "Button text to cancel pump time sync")))
  290. ]
  291. )
  292. }
  293. private func errorText(_ error: Error) -> String {
  294. if let error = error as? LocalizedError {
  295. return [error.localizedDescription, error.recoverySuggestion].compactMap{$0}.joined(separator: ". ")
  296. } else {
  297. return error.localizedDescription
  298. }
  299. }
  300. var decimalFormatter: NumberFormatter = {
  301. let formatter = NumberFormatter()
  302. formatter.numberStyle = .decimal
  303. formatter.minimumFractionDigits = 0
  304. formatter.maximumFractionDigits = 2
  305. return formatter
  306. }()
  307. private func formatRSSI(rssi: Int?) -> String {
  308. if let rssi = rssi, let rssiStr = decimalFormatter.decibleString(from: rssi) {
  309. return rssiStr
  310. } else {
  311. return ""
  312. }
  313. }
  314. private var deletePumpButton: some View {
  315. Button(action: {
  316. showingDeletionSheet = true
  317. }, label: {
  318. Text(LocalizedString("Delete Pump", comment: "Button label for removing Pump"))
  319. .foregroundColor(.red)
  320. }).actionSheet(isPresented: $showingDeletionSheet) {
  321. ActionSheet(
  322. title: Text(LocalizedString("Are you sure you want to delete this Pump?", comment: "Text to confirm delete this pump")),
  323. buttons: [
  324. .destructive(Text(LocalizedString("Delete Pump", comment: "Text to delete pump"))) {
  325. viewModel.deletePump()
  326. },
  327. .cancel(),
  328. ]
  329. )
  330. }
  331. }
  332. private var headerImage: some View {
  333. VStack(alignment: .center) {
  334. Image(uiImage: viewModel.pumpImage)
  335. .resizable()
  336. .aspectRatio(contentMode: ContentMode.fit)
  337. .frame(height: 150)
  338. .padding(.horizontal)
  339. }
  340. .frame(maxWidth: .infinity)
  341. }
  342. private var doneButton: some View {
  343. Button("Done", action: {
  344. viewModel.doneButtonPressed()
  345. })
  346. }
  347. }