HomeRootView.swift 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. @State var currentISF: Int?
  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. var header: some View {
  28. HStack(alignment: .bottom) {
  29. Spacer()
  30. VStack(alignment: .leading, spacing: 12) {
  31. HStack {
  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(
  40. (numberFormatter.string(from: (state.suggestion?.cob ?? 0) as NSNumber) ?? "0") +
  41. NSLocalizedString(" g", comment: "gram of carbs")
  42. )
  43. .font(.system(size: 12, weight: .bold))
  44. }
  45. }
  46. Spacer()
  47. CurrentGlucoseView(
  48. recentGlucose: $state.recentGlucose,
  49. delta: $state.glucoseDelta,
  50. units: $state.units
  51. )
  52. .onTapGesture {
  53. state.openCGM()
  54. }
  55. Spacer()
  56. PumpView(
  57. reservoir: $state.reservoir,
  58. battery: $state.battery,
  59. name: $state.pumpName,
  60. expiresAtDate: $state.pumpExpiresAtDate,
  61. timerDate: $state.timerDate
  62. )
  63. .onTapGesture {
  64. state.setupPump = true
  65. }
  66. .popover(isPresented: $state.setupPump) {
  67. if let pumpManager = state.provider.apsManager.pumpManager {
  68. PumpConfig.PumpSettingsView(pumpManager: pumpManager, completionDelegate: state)
  69. }
  70. }
  71. Spacer()
  72. LoopView(
  73. suggestion: $state.suggestion,
  74. enactedSuggestion: $state.enactedSuggestion,
  75. closedLoop: $state.closedLoop,
  76. timerDate: $state.timerDate,
  77. isLooping: $state.isLooping,
  78. lastLoopDate: $state.lastLoopDate
  79. ).onTapGesture {
  80. isStatusPopupPresented = true
  81. }.onLongPressGesture {
  82. state.runLoop()
  83. }
  84. Spacer()
  85. }.frame(maxWidth: .infinity)
  86. }
  87. var infoPanal: some View {
  88. HStack(alignment: .center) {
  89. if state.pumpSuspended {
  90. Text("Pump suspended")
  91. .font(.system(size: 12, weight: .bold)).foregroundColor(.loopGray)
  92. .padding(.leading, 8)
  93. } else if let tempRate = state.tempRate {
  94. Text(
  95. (numberFormatter.string(from: tempRate as NSNumber) ?? "0") +
  96. NSLocalizedString(" U/hr", comment: "Unit per hour with space")
  97. )
  98. .font(.system(size: 12, weight: .bold)).foregroundColor(.insulin)
  99. .padding(.leading, 8)
  100. }
  101. if let tempTarget = state.tempTarget {
  102. Text(tempTarget.displayName).font(.caption).foregroundColor(.secondary)
  103. if state.units == .mmolL {
  104. Text(
  105. targetFormatter
  106. .string(from: (tempTarget.targetBottom?.asMmolL ?? 0) as NSNumber)!
  107. )
  108. .font(.caption)
  109. .foregroundColor(.secondary)
  110. if tempTarget.targetBottom != tempTarget.targetTop {
  111. Text("-").font(.caption)
  112. .foregroundColor(.secondary)
  113. Text(
  114. targetFormatter
  115. .string(from: (tempTarget.targetTop?.asMmolL ?? 0) as NSNumber)! +
  116. " \(state.units.rawValue)"
  117. )
  118. .font(.caption)
  119. .foregroundColor(.secondary)
  120. } else {
  121. Text(state.units.rawValue).font(.caption)
  122. .foregroundColor(.secondary)
  123. }
  124. } else {
  125. Text(targetFormatter.string(from: (tempTarget.targetBottom ?? 0) as NSNumber)!)
  126. .font(.caption)
  127. .foregroundColor(.secondary)
  128. if tempTarget.targetBottom != tempTarget.targetTop {
  129. Text("-").font(.caption)
  130. .foregroundColor(.secondary)
  131. Text(
  132. targetFormatter
  133. .string(from: (tempTarget.targetTop ?? 0) as NSNumber)! + " \(state.units.rawValue)"
  134. )
  135. .font(.caption)
  136. .foregroundColor(.secondary)
  137. } else {
  138. Text(state.units.rawValue).font(.caption)
  139. .foregroundColor(.secondary)
  140. }
  141. }
  142. }
  143. Spacer()
  144. if let progress = state.bolusProgress {
  145. Text("Bolusing")
  146. .font(.system(size: 12, weight: .bold)).foregroundColor(.insulin)
  147. ProgressView(value: Double(progress))
  148. .progressViewStyle(BolusProgressViewStyle())
  149. .padding(.trailing, 8)
  150. .onTapGesture {
  151. state.cancelBolus()
  152. }
  153. }
  154. Spacer()
  155. Text(
  156. NSLocalizedString("ISF", comment: "current ISF") + ":"
  157. )
  158. .font(.system(size: 12))
  159. .padding(.leading, 6)
  160. .fixedSize()
  161. Text(
  162. numberFormatter.string(from: (currentISF ?? 0) as NSNumber) ?? "0"
  163. )
  164. .font(.system(size: 12, weight: .bold)).foregroundColor(.secondary)
  165. .fixedSize()
  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.05))
  218. // .background(Color.gray.opacity(0.2))
  219. Divider().background(Color.gray) // Added 29/4
  220. infoPanal
  221. .background(Color.backgroundColor)
  222. MainChartView(
  223. glucose: $state.glucose,
  224. suggestion: $state.suggestion,
  225. tempBasals: $state.tempBasals,
  226. boluses: $state.boluses,
  227. suspensions: $state.suspensions,
  228. hours: .constant(state.filteredHours),
  229. maxBasal: $state.maxBasal,
  230. autotunedBasalProfile: $state.autotunedBasalProfile,
  231. basalProfile: $state.basalProfile,
  232. tempTargets: $state.tempTargets,
  233. carbs: $state.carbs,
  234. timerDate: $state.timerDate,
  235. units: $state.units
  236. )
  237. .background(Color.gray.opacity(0.05))
  238. // .padding(.bottom)
  239. .modal(for: .dataTable, from: self)
  240. Divider().background(Color.gray) // Added 29/4
  241. legendPanal
  242. .background(Color.backgroundColor)
  243. Divider().background(Color.gray)
  244. ZStack {
  245. Rectangle().fill(Color.gray.opacity(0.05)).frame(height: 50 + geo.safeAreaInsets.bottom)
  246. HStack {
  247. Button { state.showModal(for: .addCarbs) }
  248. label: {
  249. ZStack(alignment: Alignment(horizontal: .trailing, vertical: .bottom)) {
  250. Image("carbs")
  251. .renderingMode(.template)
  252. .resizable()
  253. .frame(width: 24, height: 24)
  254. .foregroundColor(.loopGreen)
  255. .padding(8)
  256. if let carbsReq = state.carbsRequired {
  257. Text(numberFormatter.string(from: carbsReq as NSNumber)!)
  258. .font(.caption)
  259. .foregroundColor(.white)
  260. .padding(4)
  261. .background(Capsule().fill(Color.red))
  262. }
  263. }
  264. }
  265. Spacer()
  266. Button { state.showModal(for: .addTempTarget) }
  267. label: {
  268. Image("target")
  269. .renderingMode(.template)
  270. .resizable()
  271. .frame(width: 24, height: 24)
  272. .padding(8)
  273. }.foregroundColor(.loopYellow)
  274. Spacer()
  275. Button { state.showModal(for: .bolus(waitForDuggestion: false)) }
  276. label: {
  277. Image("bolus")
  278. .renderingMode(.template)
  279. .resizable()
  280. .frame(width: 24, height: 24)
  281. .padding(8)
  282. }.foregroundColor(.insulin)
  283. Spacer()
  284. if state.allowManualTemp {
  285. Button { state.showModal(for: .manualTempBasal) }
  286. label: {
  287. Image("bolus1")
  288. .renderingMode(.template)
  289. .resizable()
  290. .frame(width: 24, height: 24)
  291. .padding(8)
  292. }.foregroundColor(.insulin)
  293. Spacer()
  294. }
  295. Button { state.showModal(for: .settings) }
  296. label: {
  297. Image("settings1")
  298. .renderingMode(.template)
  299. .resizable()
  300. .frame(width: 24, height: 24)
  301. .padding(8)
  302. }.foregroundColor(.loopGray)
  303. }
  304. .padding(.horizontal, 24)
  305. .padding(.bottom, geo.safeAreaInsets.bottom)
  306. }
  307. }
  308. .edgesIgnoringSafeArea(.vertical)
  309. }
  310. .onAppear(perform: configureView)
  311. .navigationTitle("Home")
  312. .navigationBarHidden(true)
  313. .ignoresSafeArea(.keyboard)
  314. .popup(isPresented: isStatusPopupPresented, alignment: .top, direction: .top) {
  315. VStack(alignment: .leading) {
  316. Text(state.statusTitle).foregroundColor(.white)
  317. .padding(.bottom, 4)
  318. Text(state.suggestion?.reason ?? "No sugestion found").font(.caption).foregroundColor(.white)
  319. if let errorMessage = state.errorMessage, let date = state.errorDate {
  320. Text("Error at \(dateFormatter.string(from: date))").foregroundColor(.white)
  321. .padding(.bottom, 4)
  322. .padding(.top, 8)
  323. Text(errorMessage).font(.caption).foregroundColor(.white)
  324. }
  325. }
  326. .padding()
  327. .background(
  328. RoundedRectangle(cornerRadius: 8, style: .continuous)
  329. .fill(Color(UIColor.darkGray))
  330. )
  331. .onTapGesture {
  332. isStatusPopupPresented = false
  333. }
  334. .gesture(
  335. DragGesture(minimumDistance: 10, coordinateSpace: .local)
  336. .onEnded { value in
  337. if value.translation.height < 0 {
  338. isStatusPopupPresented = false
  339. }
  340. }
  341. )
  342. }
  343. }
  344. }
  345. }