HomeRootView.swift 16 KB

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