HomeRootView.swift 12 KB

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