HomeRootView.swift 16 KB

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