HomeRootView.swift 15 KB

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