HomeRootView.swift 16 KB

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