HomeRootView.swift 15 KB

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