HomeRootView.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. import SwiftDate
  2. import SwiftUI
  3. extension Home {
  4. struct RootView: BaseView {
  5. @EnvironmentObject var viewModel: ViewModel<Provider>
  6. @State var isStatusPopupPresented = false
  7. private var numberFormatter: NumberFormatter {
  8. let formatter = NumberFormatter()
  9. formatter.numberStyle = .decimal
  10. formatter.maximumFractionDigits = 2
  11. return formatter
  12. }
  13. private var targetFormatter: NumberFormatter {
  14. let formatter = NumberFormatter()
  15. formatter.numberStyle = .decimal
  16. formatter.maximumFractionDigits = 1
  17. return formatter
  18. }
  19. private var dateFormatter: DateFormatter {
  20. let dateFormatter = DateFormatter()
  21. dateFormatter.timeStyle = .short
  22. return dateFormatter
  23. }
  24. var header: some View {
  25. HStack(alignment: .bottom) {
  26. Spacer()
  27. VStack(alignment: .leading, spacing: 12) {
  28. HStack {
  29. Text("IOB").font(.caption2).foregroundColor(.secondary)
  30. Text((numberFormatter.string(from: (viewModel.suggestion?.iob ?? 0) as NSNumber) ?? "0") + " U")
  31. .font(.system(size: 12, weight: .bold))
  32. }
  33. HStack {
  34. Text("COB").font(.caption2).foregroundColor(.secondary)
  35. Text((numberFormatter.string(from: (viewModel.suggestion?.cob ?? 0) as NSNumber) ?? "0") + " g")
  36. .font(.system(size: 12, weight: .bold))
  37. }
  38. }
  39. Spacer()
  40. CurrentGlucoseView(
  41. recentGlucose: $viewModel.recentGlucose,
  42. delta: $viewModel.glucoseDelta,
  43. units: viewModel.units
  44. )
  45. .onTapGesture {
  46. viewModel.openCGM()
  47. }
  48. Spacer()
  49. PumpView(
  50. reservoir: $viewModel.reservoir,
  51. battery: $viewModel.battery,
  52. name: $viewModel.pumpName,
  53. expiresAtDate: $viewModel.pumpExpiresAtDate,
  54. timerDate: $viewModel.timerDate
  55. )
  56. .onTapGesture {
  57. viewModel.setupPump = true
  58. }
  59. .popover(isPresented: $viewModel.setupPump) {
  60. if let pumpManager = viewModel.provider.apsManager.pumpManager {
  61. PumpConfig.PumpSettingsView(pumpManager: pumpManager, completionDelegate: viewModel)
  62. }
  63. }
  64. Spacer()
  65. LoopView(
  66. suggestion: $viewModel.suggestion,
  67. enactedSuggestion: $viewModel.enactedSuggestion,
  68. closedLoop: $viewModel.closedLoop,
  69. timerDate: $viewModel.timerDate,
  70. isLooping: $viewModel.isLooping,
  71. lastLoopDate: $viewModel.lastLoopDate
  72. ).onTapGesture {
  73. isStatusPopupPresented = true
  74. }.onLongPressGesture {
  75. viewModel.runLoop()
  76. }
  77. Spacer()
  78. }.frame(maxWidth: .infinity)
  79. }
  80. var infoPanal: some View {
  81. HStack(alignment: .center) {
  82. if let tempRate = viewModel.tempRate {
  83. Text((numberFormatter.string(from: tempRate as NSNumber) ?? "0") + " U/hr")
  84. .font(.system(size: 12, weight: .bold)).foregroundColor(.insulin)
  85. .padding(.leading, 8)
  86. }
  87. if let tempTarget = viewModel.tempTarget {
  88. Text(tempTarget.displayName).font(.caption).foregroundColor(.secondary)
  89. if viewModel.units == .mmolL {
  90. Text(
  91. targetFormatter
  92. .string(from: (tempTarget.targetBottom?.asMmolL ?? 0) as NSNumber)!
  93. )
  94. .font(.caption)
  95. .foregroundColor(.secondary)
  96. if tempTarget.targetBottom != tempTarget.targetTop {
  97. Text("-").font(.caption)
  98. .foregroundColor(.secondary)
  99. Text(
  100. targetFormatter
  101. .string(from: (tempTarget.targetTop?.asMmolL ?? 0) as NSNumber)! +
  102. " \(viewModel.units.rawValue)"
  103. )
  104. .font(.caption)
  105. .foregroundColor(.secondary)
  106. } else {
  107. Text(viewModel.units.rawValue).font(.caption)
  108. .foregroundColor(.secondary)
  109. }
  110. } else {
  111. Text(targetFormatter.string(from: (tempTarget.targetBottom ?? 0) as NSNumber)!)
  112. .font(.caption)
  113. .foregroundColor(.secondary)
  114. if tempTarget.targetBottom != tempTarget.targetTop {
  115. Text("-").font(.caption)
  116. .foregroundColor(.secondary)
  117. Text(
  118. targetFormatter
  119. .string(from: (tempTarget.targetTop ?? 0) as NSNumber)! + " \(viewModel.units.rawValue)"
  120. )
  121. .font(.caption)
  122. .foregroundColor(.secondary)
  123. } else {
  124. Text(viewModel.units.rawValue).font(.caption)
  125. .foregroundColor(.secondary)
  126. }
  127. }
  128. }
  129. Spacer()
  130. if let progress = viewModel.bolusProgress {
  131. Text("Bolusing")
  132. .font(.system(size: 12, weight: .bold)).foregroundColor(.insulin)
  133. ProgressView(value: Double(progress))
  134. .progressViewStyle(BolusProgressViewStyle())
  135. .padding(.trailing, 8)
  136. .onTapGesture {
  137. viewModel.cancelBolus()
  138. }
  139. }
  140. }
  141. .frame(maxWidth: .infinity, maxHeight: 30)
  142. }
  143. var legendPanal: some View {
  144. HStack(alignment: .firstTextBaseline) {
  145. Circle().fill(Color.loopGreen).frame(width: 8, height: 8)
  146. .padding(.leading, 8)
  147. Text("BG")
  148. .font(.system(size: 12, weight: .bold)).foregroundColor(.loopGreen)
  149. Circle().fill(Color.insulin).frame(width: 8, height: 8)
  150. .padding(.leading, 8)
  151. Text("IOB")
  152. .font(.system(size: 12, weight: .bold)).foregroundColor(.insulin)
  153. Circle().fill(Color.zt).frame(width: 8, height: 8)
  154. .padding(.leading, 8)
  155. Text("ZT")
  156. .font(.system(size: 12, weight: .bold)).foregroundColor(.zt)
  157. Circle().fill(Color.loopYellow).frame(width: 8, height: 8)
  158. .padding(.leading, 8)
  159. Text("COB")
  160. .font(.system(size: 12, weight: .bold)).foregroundColor(.loopYellow)
  161. Circle().fill(Color.uam).frame(width: 8, height: 8)
  162. .padding(.leading, 8)
  163. Text("UAM")
  164. .font(.system(size: 12, weight: .bold)).foregroundColor(.uam)
  165. }
  166. .frame(maxWidth: .infinity, maxHeight: 30)
  167. }
  168. var body: some View {
  169. GeometryReader { geo in
  170. VStack(spacing: 0) {
  171. header
  172. .frame(maxHeight: 70)
  173. .padding(.top, geo.safeAreaInsets.top)
  174. .background(Color.gray.opacity(0.2))
  175. infoPanal
  176. MainChartView(
  177. glucose: $viewModel.glucose,
  178. suggestion: $viewModel.suggestion,
  179. tempBasals: $viewModel.tempBasals,
  180. boluses: $viewModel.boluses,
  181. hours: .constant(viewModel.filteredHours),
  182. maxBasal: $viewModel.maxBasal,
  183. basalProfile: $viewModel.basalProfile,
  184. tempTargets: $viewModel.tempTargets,
  185. carbs: $viewModel.carbs,
  186. timerDate: $viewModel.timerDate,
  187. units: viewModel.units
  188. )
  189. .padding(.bottom)
  190. .modal(for: .dataTable, from: self)
  191. legendPanal
  192. ZStack {
  193. Rectangle().fill(Color.gray.opacity(0.2)).frame(height: 50 + geo.safeAreaInsets.bottom)
  194. HStack {
  195. Button { viewModel.showModal(for: .addCarbs) }
  196. label: {
  197. Image("carbs")
  198. .renderingMode(.template)
  199. .resizable()
  200. .frame(width: 24, height: 24)
  201. }.foregroundColor(.loopGreen)
  202. Spacer()
  203. Button { viewModel.showModal(for: .addTempTarget) }
  204. label: {
  205. Image("target")
  206. .renderingMode(.template)
  207. .resizable()
  208. .frame(width: 24, height: 24)
  209. }.foregroundColor(.loopYellow)
  210. Spacer()
  211. Button { viewModel.showModal(for: .bolus(waitForDuggestion: false)) }
  212. label: {
  213. Image("bolus")
  214. .renderingMode(.template)
  215. .resizable()
  216. .frame(width: 24, height: 24)
  217. }.foregroundColor(.insulin)
  218. Spacer()
  219. if viewModel.allowManualTemp {
  220. Button { viewModel.showModal(for: .manualTempBasal) }
  221. label: {
  222. Image("bolus1")
  223. .renderingMode(.template)
  224. .resizable()
  225. .frame(width: 24, height: 24)
  226. }.foregroundColor(.insulin)
  227. Spacer()
  228. }
  229. Button { viewModel.showModal(for: .settings) }
  230. label: {
  231. Image("settings1")
  232. .renderingMode(.template)
  233. .resizable()
  234. .frame(width: 24, height: 24)
  235. }.foregroundColor(.loopGray)
  236. }
  237. .padding(.horizontal, 24)
  238. .padding(.bottom, geo.safeAreaInsets.bottom)
  239. }
  240. }
  241. .edgesIgnoringSafeArea(.vertical)
  242. }
  243. .navigationTitle("Home")
  244. .navigationBarHidden(true)
  245. .ignoresSafeArea(.keyboard)
  246. .popup(isPresented: isStatusPopupPresented, alignment: .top, direction: .top) {
  247. VStack(alignment: .leading) {
  248. Text(viewModel.statusTitle).foregroundColor(.white)
  249. .padding(.bottom, 4)
  250. Text(viewModel.suggestion?.reason ?? "No sugestion found").font(.caption).foregroundColor(.white)
  251. if let errorMessage = viewModel.errorMessage, let date = viewModel.errorDate {
  252. Text("Error at \(dateFormatter.string(from: date))").foregroundColor(.white)
  253. .padding(.bottom, 4)
  254. .padding(.top, 8)
  255. Text(errorMessage).font(.caption).foregroundColor(.white)
  256. }
  257. }
  258. .padding()
  259. .background(
  260. RoundedRectangle(cornerRadius: 8, style: .continuous)
  261. .fill(Color(UIColor.darkGray))
  262. )
  263. .onTapGesture {
  264. isStatusPopupPresented = false
  265. }
  266. .gesture(
  267. DragGesture(minimumDistance: 10, coordinateSpace: .local)
  268. .onEnded { value in
  269. if value.translation.height < 0 {
  270. isStatusPopupPresented = false
  271. }
  272. }
  273. )
  274. }
  275. }
  276. }
  277. }