HomeRootView.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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: .firstTextBaseline) {
  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("Bolus " + (numberFormatter.string(from: progress * 100 as NSNumber)!) + "%")
  132. .font(.system(size: 12, weight: .bold)).foregroundColor(.insulin)
  133. .padding(.trailing, 8)
  134. }
  135. }
  136. .frame(maxWidth: .infinity, maxHeight: 30)
  137. }
  138. var legendPanal: some View {
  139. HStack(alignment: .firstTextBaseline) {
  140. Circle().fill(Color.loopGreen).frame(width: 8, height: 8)
  141. .padding(.leading, 8)
  142. Text("BG")
  143. .font(.system(size: 12, weight: .bold)).foregroundColor(.loopGreen)
  144. Circle().fill(Color.insulin).frame(width: 8, height: 8)
  145. .padding(.leading, 8)
  146. Text("IOB")
  147. .font(.system(size: 12, weight: .bold)).foregroundColor(.insulin)
  148. Circle().fill(Color.zt).frame(width: 8, height: 8)
  149. .padding(.leading, 8)
  150. Text("ZT")
  151. .font(.system(size: 12, weight: .bold)).foregroundColor(.zt)
  152. Circle().fill(Color.loopYellow).frame(width: 8, height: 8)
  153. .padding(.leading, 8)
  154. Text("COB")
  155. .font(.system(size: 12, weight: .bold)).foregroundColor(.loopYellow)
  156. Circle().fill(Color.uam).frame(width: 8, height: 8)
  157. .padding(.leading, 8)
  158. Text("UAM")
  159. .font(.system(size: 12, weight: .bold)).foregroundColor(.uam)
  160. }
  161. .frame(maxWidth: .infinity, maxHeight: 30)
  162. }
  163. var body: some View {
  164. GeometryReader { geo in
  165. VStack(spacing: 0) {
  166. header
  167. .frame(maxHeight: 70)
  168. .padding(.top, geo.safeAreaInsets.top)
  169. .background(Color.gray.opacity(0.2))
  170. infoPanal
  171. MainChartView(
  172. glucose: $viewModel.glucose,
  173. suggestion: $viewModel.suggestion,
  174. tempBasals: $viewModel.tempBasals,
  175. boluses: $viewModel.boluses,
  176. hours: .constant(viewModel.filteredHours),
  177. maxBasal: $viewModel.maxBasal,
  178. basalProfile: $viewModel.basalProfile,
  179. tempTargets: $viewModel.tempTargets,
  180. carbs: $viewModel.carbs,
  181. timerDate: $viewModel.timerDate,
  182. units: viewModel.units
  183. )
  184. .padding(.bottom)
  185. .modal(for: .dataTable, from: self)
  186. legendPanal
  187. ZStack {
  188. Rectangle().fill(Color.gray.opacity(0.2)).frame(height: 50 + geo.safeAreaInsets.bottom)
  189. HStack {
  190. Button { viewModel.showModal(for: .addCarbs) }
  191. label: {
  192. Image("carbs")
  193. .renderingMode(.template)
  194. .resizable()
  195. .frame(width: 24, height: 24)
  196. }.foregroundColor(.loopGreen)
  197. Spacer()
  198. Button { viewModel.showModal(for: .addTempTarget) }
  199. label: {
  200. Image("target")
  201. .renderingMode(.template)
  202. .resizable()
  203. .frame(width: 24, height: 24)
  204. }.foregroundColor(.loopYellow)
  205. Spacer()
  206. Button { viewModel.showModal(for: .bolus(waitForDuggestion: false)) }
  207. label: {
  208. Image("bolus")
  209. .renderingMode(.template)
  210. .resizable()
  211. .frame(width: 24, height: 24)
  212. }.foregroundColor(.insulin)
  213. Spacer()
  214. if viewModel.allowManualTemp {
  215. Button { viewModel.showModal(for: .manualTempBasal) }
  216. label: {
  217. Image("bolus1")
  218. .renderingMode(.template)
  219. .resizable()
  220. .frame(width: 24, height: 24)
  221. }.foregroundColor(.insulin)
  222. Spacer()
  223. }
  224. Button { viewModel.showModal(for: .settings) }
  225. label: {
  226. Image("settings1")
  227. .renderingMode(.template)
  228. .resizable()
  229. .frame(width: 24, height: 24)
  230. }.foregroundColor(.loopGray)
  231. }
  232. .padding(.horizontal, 24)
  233. .padding(.bottom, geo.safeAreaInsets.bottom)
  234. }
  235. }
  236. .edgesIgnoringSafeArea(.vertical)
  237. }
  238. .navigationTitle("Home")
  239. .navigationBarHidden(true)
  240. .ignoresSafeArea(.keyboard)
  241. .popup(isPresented: isStatusPopupPresented, alignment: .top, direction: .top) {
  242. VStack(alignment: .leading) {
  243. Text(viewModel.statusTitle).foregroundColor(.white)
  244. .padding(.bottom, 4)
  245. Text(viewModel.suggestion?.reason ?? "No sugestion found").font(.caption).foregroundColor(.white)
  246. if let errorMessage = viewModel.errorMessage, let date = viewModel.errorDate {
  247. Text("Error at \(dateFormatter.string(from: date))").foregroundColor(.white)
  248. .padding(.bottom, 4)
  249. .padding(.top, 8)
  250. Text(errorMessage).font(.caption).foregroundColor(.white)
  251. }
  252. }
  253. .padding()
  254. .background(
  255. RoundedRectangle(cornerRadius: 8, style: .continuous)
  256. .fill(Color(UIColor.darkGray))
  257. )
  258. .onTapGesture {
  259. isStatusPopupPresented = false
  260. }
  261. .gesture(
  262. DragGesture(minimumDistance: 10, coordinateSpace: .local)
  263. .onEnded { value in
  264. if value.translation.height < 0 {
  265. isStatusPopupPresented = false
  266. }
  267. }
  268. )
  269. }
  270. }
  271. }
  272. }