DeliveryUncertaintyRecoveryView.swift 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //
  2. // DeliveryUncertaintyRecoveryView.swift
  3. // OmniKit
  4. //
  5. // Created by Pete Schwamb on 8/17/20.
  6. // Copyright © 2020 LoopKit Authors. All rights reserved.
  7. //
  8. import SwiftUI
  9. import LoopKitUI
  10. import RileyLinkBLEKit
  11. struct DeliveryUncertaintyRecoveryView: View {
  12. let model: DeliveryUncertaintyRecoveryViewModel
  13. @ObservedObject var rileyLinkListDataSource: RileyLinkListDataSource
  14. var handleRileyLinkSelection: (RileyLinkDevice) -> Void
  15. @Environment(\.guidanceColors) var guidanceColors
  16. var body: some View {
  17. GuidePage(content: {
  18. Text(String(format: LocalizedString("%1$@ has been unable to communicate with the pod on your body since %2$@.\n\nWithout communication with the pod, the app cannot continue to send commands for insulin delivery or display accurate, recent information about your active insulin or the insulin being delivered by the Pod.\n\nMonitor your glucose closely for the next 6 or more hours, as there may or may not be insulin actively working in your body that %3$@ cannot display.", comment: "Format string for main text of delivery uncertainty recovery page. (1: app name)(2: date of command)(3: app name)"), self.model.appName, self.uncertaintyDateLocalizedString, self.model.appName))
  19. .padding([.top, .bottom])
  20. Section(header: HStack {
  21. FrameworkLocalText("Devices", comment: "Header for devices section of RileyLinkSetupView")
  22. Spacer()
  23. ProgressView()
  24. }) {
  25. ForEach(rileyLinkListDataSource.devices, id: \.peripheralIdentifier) { device in
  26. Toggle(isOn: rileyLinkListDataSource.autoconnectBinding(for: device)) {
  27. HStack {
  28. Text(device.name ?? "Unknown")
  29. Spacer()
  30. if rileyLinkListDataSource.autoconnectBinding(for: device).wrappedValue {
  31. if device.isConnected {
  32. Text(formatRSSI(rssi:device.rssi)).foregroundColor(.secondary)
  33. } else {
  34. Image(systemName: "wifi.exclamationmark")
  35. .imageScale(.large)
  36. .foregroundColor(guidanceColors.warning)
  37. }
  38. }
  39. }
  40. .contentShape(Rectangle())
  41. .onTapGesture {
  42. handleRileyLinkSelection(device)
  43. }
  44. }
  45. }
  46. }
  47. .onAppear {
  48. rileyLinkListDataSource.isScanningEnabled = true
  49. model.respondToRecovery = true
  50. }
  51. .onDisappear {
  52. rileyLinkListDataSource.isScanningEnabled = false
  53. model.respondToRecovery = false
  54. }
  55. }) {
  56. VStack {
  57. Text(LocalizedString("Attemping to re-establish communication", comment: "Description string above progress indicator while attempting to re-establish communication from an unacknowledged command")).padding(.top)
  58. ProgressIndicatorView(state: .indeterminantProgress)
  59. Button(action: {
  60. self.model.podDeactivationChosen()
  61. }) {
  62. Text(LocalizedString("Deactivate Pod", comment: "Button title to deactive pod on uncertain program"))
  63. .actionButtonStyle(.destructive)
  64. .padding()
  65. }
  66. }
  67. }
  68. .navigationBarTitle(Text(LocalizedString("Unable to Reach Pod", comment: "Title of delivery uncertainty recovery page")), displayMode: .large)
  69. .navigationBarItems(leading: backButton)
  70. }
  71. private var uncertaintyDateLocalizedString: String {
  72. DateFormatter.localizedString(from: model.uncertaintyStartedAt, dateStyle: .none, timeStyle: .short)
  73. }
  74. var decimalFormatter: NumberFormatter = {
  75. let formatter = NumberFormatter()
  76. formatter.numberStyle = .decimal
  77. formatter.minimumFractionDigits = 0
  78. formatter.maximumFractionDigits = 2
  79. return formatter
  80. }()
  81. private func formatRSSI(rssi: Int?) -> String {
  82. if let rssi = rssi, let rssiStr = decimalFormatter.decibleString(from: rssi) {
  83. return rssiStr
  84. } else {
  85. return ""
  86. }
  87. }
  88. private var backButton: some View {
  89. Button(LocalizedString("Back", comment: "Back button text on DeliveryUncertaintyRecoveryView"), action: {
  90. self.model.onDismiss?()
  91. })
  92. }
  93. }