HomeRootView.swift 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. Text(
  132. (numberFormatter.string(from: tempRate as NSNumber) ?? "0") +
  133. NSLocalizedString(" U/hr", comment: "Unit per hour with space")
  134. )
  135. .font(.system(size: 12, weight: .bold)).foregroundColor(.insulin)
  136. .padding(.leading, 8)
  137. }
  138. if let tempTarget = state.tempTarget {
  139. Text(tempTarget.displayName).font(.caption).foregroundColor(.secondary)
  140. if state.units == .mmolL {
  141. Text(
  142. targetFormatter
  143. .string(from: (tempTarget.targetBottom?.asMmolL ?? 0) as NSNumber)!
  144. )
  145. .font(.caption)
  146. .foregroundColor(.secondary)
  147. if tempTarget.targetBottom != tempTarget.targetTop {
  148. Text("-").font(.caption)
  149. .foregroundColor(.secondary)
  150. Text(
  151. targetFormatter
  152. .string(from: (tempTarget.targetTop?.asMmolL ?? 0) as NSNumber)! +
  153. " \(state.units.rawValue)"
  154. )
  155. .font(.caption)
  156. .foregroundColor(.secondary)
  157. } else {
  158. Text(state.units.rawValue).font(.caption)
  159. .foregroundColor(.secondary)
  160. }
  161. } else {
  162. Text(targetFormatter.string(from: (tempTarget.targetBottom ?? 0) as NSNumber)!)
  163. .font(.caption)
  164. .foregroundColor(.secondary)
  165. if tempTarget.targetBottom != tempTarget.targetTop {
  166. Text("-").font(.caption)
  167. .foregroundColor(.secondary)
  168. Text(
  169. targetFormatter
  170. .string(from: (tempTarget.targetTop ?? 0) as NSNumber)! + " \(state.units.rawValue)"
  171. )
  172. .font(.caption)
  173. .foregroundColor(.secondary)
  174. } else {
  175. Text(state.units.rawValue).font(.caption)
  176. .foregroundColor(.secondary)
  177. }
  178. }
  179. }
  180. Spacer()
  181. if let progress = state.bolusProgress {
  182. Text("Bolusing")
  183. .font(.system(size: 12, weight: .bold)).foregroundColor(.insulin)
  184. ProgressView(value: Double(progress))
  185. .progressViewStyle(BolusProgressViewStyle())
  186. .padding(.trailing, 8)
  187. .onTapGesture {
  188. state.cancelBolus()
  189. }
  190. }
  191. }
  192. .frame(maxWidth: .infinity, maxHeight: 30)
  193. }
  194. var legendPanal: some View {
  195. HStack(alignment: .center) {
  196. Group {
  197. Circle().fill(Color.loopGreen).frame(width: 8, height: 8)
  198. Text("BG")
  199. .font(.system(size: 12, weight: .bold)).foregroundColor(.loopGreen)
  200. }
  201. Group {
  202. Circle().fill(Color.insulin).frame(width: 8, height: 8)
  203. .padding(.leading, 8)
  204. Text("IOB")
  205. .font(.system(size: 12, weight: .bold)).foregroundColor(.insulin)
  206. }
  207. Group {
  208. Circle().fill(Color.zt).frame(width: 8, height: 8)
  209. .padding(.leading, 8)
  210. Text("ZT")
  211. .font(.system(size: 12, weight: .bold)).foregroundColor(.zt)
  212. }
  213. Group {
  214. Circle().fill(Color.loopYellow).frame(width: 8, height: 8)
  215. .padding(.leading, 8)
  216. Text("COB")
  217. .font(.system(size: 12, weight: .bold)).foregroundColor(.loopYellow)
  218. }
  219. Group {
  220. Circle().fill(Color.uam).frame(width: 8, height: 8)
  221. .padding(.leading, 8)
  222. Text("UAM")
  223. .font(.system(size: 12, weight: .bold)).foregroundColor(.uam)
  224. }
  225. if let eventualBG = state.eventualBG {
  226. Text(
  227. "⇢ " + numberFormatter.string(
  228. from: (state.units == .mmolL ? eventualBG.asMmolL : Decimal(eventualBG)) as NSNumber
  229. )!
  230. )
  231. .font(.system(size: 12, weight: .bold)).foregroundColor(.secondary)
  232. }
  233. }
  234. .frame(maxWidth: .infinity, maxHeight: 30)
  235. }
  236. var mainChart: some View {
  237. ZStack {
  238. if state.animatedBackground {
  239. SpriteView(scene: spriteScene, options: [.allowsTransparency])
  240. .ignoresSafeArea()
  241. .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
  242. }
  243. MainChartView(
  244. glucose: $state.glucose,
  245. suggestion: $state.suggestion,
  246. tempBasals: $state.tempBasals,
  247. boluses: $state.boluses,
  248. suspensions: $state.suspensions,
  249. hours: .constant(state.filteredHours),
  250. maxBasal: $state.maxBasal,
  251. autotunedBasalProfile: $state.autotunedBasalProfile,
  252. basalProfile: $state.basalProfile,
  253. tempTargets: $state.tempTargets,
  254. carbs: $state.carbs,
  255. timerDate: $state.timerDate,
  256. units: $state.units
  257. )
  258. }
  259. .padding(.bottom)
  260. .modal(for: .dataTable, from: self)
  261. }
  262. @ViewBuilder private func bottomPanel(_ geo: GeometryProxy) -> some View {
  263. ZStack {
  264. Rectangle().fill(Color.gray.opacity(0.2)).frame(height: 50 + geo.safeAreaInsets.bottom)
  265. HStack {
  266. Button { state.showModal(for: .addCarbs) }
  267. label: {
  268. ZStack(alignment: Alignment(horizontal: .trailing, vertical: .bottom)) {
  269. Image("carbs")
  270. .renderingMode(.template)
  271. .resizable()
  272. .frame(width: 24, height: 24)
  273. .foregroundColor(.loopGreen)
  274. .padding(8)
  275. if let carbsReq = state.carbsRequired {
  276. Text(numberFormatter.string(from: carbsReq as NSNumber)!)
  277. .font(.caption)
  278. .foregroundColor(.white)
  279. .padding(4)
  280. .background(Capsule().fill(Color.red))
  281. }
  282. }
  283. }
  284. Spacer()
  285. Button { state.showModal(for: .addTempTarget) }
  286. label: {
  287. Image("target")
  288. .renderingMode(.template)
  289. .resizable()
  290. .frame(width: 24, height: 24)
  291. .padding(8)
  292. }.foregroundColor(.loopYellow)
  293. Spacer()
  294. Button { state.showModal(for: .bolus(waitForSuggestion: false)) }
  295. label: {
  296. Image("bolus")
  297. .renderingMode(.template)
  298. .resizable()
  299. .frame(width: 24, height: 24)
  300. .padding(8)
  301. }.foregroundColor(.insulin)
  302. Spacer()
  303. if state.allowManualTemp {
  304. Button { state.showModal(for: .manualTempBasal) }
  305. label: {
  306. Image("bolus1")
  307. .renderingMode(.template)
  308. .resizable()
  309. .frame(width: 24, height: 24)
  310. .padding(8)
  311. }.foregroundColor(.insulin)
  312. Spacer()
  313. }
  314. Button { state.showModal(for: .settings) }
  315. label: {
  316. Image("settings1")
  317. .renderingMode(.template)
  318. .resizable()
  319. .frame(width: 24, height: 24)
  320. .padding(8)
  321. }.foregroundColor(.loopGray)
  322. }
  323. .padding(.horizontal, 24)
  324. .padding(.bottom, geo.safeAreaInsets.bottom)
  325. }
  326. }
  327. var body: some View {
  328. GeometryReader { geo in
  329. VStack(spacing: 0) {
  330. header(geo)
  331. infoPanal
  332. mainChart
  333. legendPanal
  334. bottomPanel(geo)
  335. }
  336. .edgesIgnoringSafeArea(.vertical)
  337. }
  338. .onAppear(perform: configureView)
  339. .navigationTitle("Home")
  340. .navigationBarHidden(true)
  341. .ignoresSafeArea(.keyboard)
  342. .popup(isPresented: isStatusPopupPresented, alignment: .top, direction: .top) {
  343. popup
  344. .padding()
  345. .background(
  346. RoundedRectangle(cornerRadius: 8, style: .continuous)
  347. .fill(Color(UIColor.darkGray))
  348. )
  349. .onTapGesture {
  350. isStatusPopupPresented = false
  351. }
  352. .gesture(
  353. DragGesture(minimumDistance: 10, coordinateSpace: .local)
  354. .onEnded { value in
  355. if value.translation.height < 0 {
  356. isStatusPopupPresented = false
  357. }
  358. }
  359. )
  360. }
  361. }
  362. private var popup: some View {
  363. VStack(alignment: .leading, spacing: 4) {
  364. Text(state.statusTitle).font(.headline).foregroundColor(.white)
  365. .padding(.bottom, 4)
  366. if let suggestion = state.suggestion {
  367. TagCloudView(tags: suggestion.reasonParts).animation(.none, value: false)
  368. Text(suggestion.reasonConclusion.capitalizingFirstLetter()).font(.caption).foregroundColor(.white)
  369. } else {
  370. Text("No sugestion found").font(.body).foregroundColor(.white)
  371. }
  372. if let errorMessage = state.errorMessage, let date = state.errorDate {
  373. Text("Error at \(dateFormatter.string(from: date))")
  374. .foregroundColor(.white)
  375. .font(.headline)
  376. .padding(.bottom, 4)
  377. .padding(.top, 8)
  378. Text(errorMessage).font(.caption).foregroundColor(.loopRed)
  379. }
  380. }
  381. }
  382. }
  383. }