HomeRootView.swift 16 KB

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