HomeRootView.swift 17 KB

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