HomeRootView.swift 16 KB

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