HomeRootView.swift 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. }
  154. .frame(maxWidth: .infinity, maxHeight: 30)
  155. }
  156. var legendPanal: some View {
  157. HStack(alignment: .center) {
  158. Group {
  159. Circle().fill(Color.loopGreen).frame(width: 8, height: 8)
  160. Text("BG")
  161. .font(.system(size: 12, weight: .bold)).foregroundColor(.loopGreen)
  162. }
  163. Group {
  164. Circle().fill(Color.insulin).frame(width: 8, height: 8)
  165. .padding(.leading, 8)
  166. Text("IOB")
  167. .font(.system(size: 12, weight: .bold)).foregroundColor(.insulin)
  168. }
  169. Group {
  170. Circle().fill(Color.zt).frame(width: 8, height: 8)
  171. .padding(.leading, 8)
  172. Text("ZT")
  173. .font(.system(size: 12, weight: .bold)).foregroundColor(.zt)
  174. }
  175. Group {
  176. Circle().fill(Color.loopYellow).frame(width: 8, height: 8)
  177. .padding(.leading, 8)
  178. Text("COB")
  179. .font(.system(size: 12, weight: .bold)).foregroundColor(.loopYellow)
  180. }
  181. Group {
  182. Circle().fill(Color.uam).frame(width: 8, height: 8)
  183. .padding(.leading, 8)
  184. Text("UAM")
  185. .font(.system(size: 12, weight: .bold)).foregroundColor(.uam)
  186. }
  187. if let eventualBG = state.eventualBG {
  188. Text(
  189. "⇢ " + numberFormatter.string(
  190. from: (state.units == .mmolL ? eventualBG.asMmolL : Decimal(eventualBG)) as NSNumber
  191. )!
  192. )
  193. .font(.system(size: 12, weight: .bold)).foregroundColor(.secondary)
  194. }
  195. }
  196. .frame(maxWidth: .infinity, maxHeight: 30)
  197. }
  198. var body: some View {
  199. GeometryReader { geo in
  200. VStack(spacing: 0) {
  201. header
  202. .frame(maxHeight: 70)
  203. .padding(.top, geo.safeAreaInsets.top)
  204. .background(Color.gray.opacity(0.05))
  205. // .background(Color.gray.opacity(0.2))
  206. Divider().background(Color.gray) // Added 29/4
  207. infoPanal
  208. .background(Color.backgroundColor)
  209. MainChartView(
  210. glucose: $state.glucose,
  211. suggestion: $state.suggestion,
  212. tempBasals: $state.tempBasals,
  213. boluses: $state.boluses,
  214. suspensions: $state.suspensions,
  215. hours: .constant(state.filteredHours),
  216. maxBasal: $state.maxBasal,
  217. autotunedBasalProfile: $state.autotunedBasalProfile,
  218. basalProfile: $state.basalProfile,
  219. tempTargets: $state.tempTargets,
  220. carbs: $state.carbs,
  221. timerDate: $state.timerDate,
  222. units: $state.units
  223. )
  224. .background(Color.gray.opacity(0.05))
  225. // .padding(.bottom)
  226. .modal(for: .dataTable, from: self)
  227. Divider().background(Color.gray) // Added 29/4
  228. legendPanal
  229. .background(Color.backgroundColor)
  230. Divider().background(Color.gray)
  231. ZStack {
  232. Rectangle().fill(Color.gray.opacity(0.05)).frame(height: 50 + geo.safeAreaInsets.bottom)
  233. HStack {
  234. Button { state.showModal(for: .addCarbs) }
  235. label: {
  236. ZStack(alignment: Alignment(horizontal: .trailing, vertical: .bottom)) {
  237. Image("carbs")
  238. .renderingMode(.template)
  239. .resizable()
  240. .frame(width: 24, height: 24)
  241. .foregroundColor(.loopGreen)
  242. .padding(8)
  243. if let carbsReq = state.carbsRequired {
  244. Text(numberFormatter.string(from: carbsReq as NSNumber)!)
  245. .font(.caption)
  246. .foregroundColor(.white)
  247. .padding(4)
  248. .background(Capsule().fill(Color.red))
  249. }
  250. }
  251. }
  252. Spacer()
  253. Button { state.showModal(for: .addTempTarget) }
  254. label: {
  255. Image("target")
  256. .renderingMode(.template)
  257. .resizable()
  258. .frame(width: 24, height: 24)
  259. .padding(8)
  260. }.foregroundColor(.loopYellow)
  261. Spacer()
  262. Button { state.showModal(for: .bolus(waitForDuggestion: false)) }
  263. label: {
  264. Image("bolus")
  265. .renderingMode(.template)
  266. .resizable()
  267. .frame(width: 24, height: 24)
  268. .padding(8)
  269. }.foregroundColor(.insulin)
  270. Spacer()
  271. if state.allowManualTemp {
  272. Button { state.showModal(for: .manualTempBasal) }
  273. label: {
  274. Image("bolus1")
  275. .renderingMode(.template)
  276. .resizable()
  277. .frame(width: 24, height: 24)
  278. .padding(8)
  279. }.foregroundColor(.insulin)
  280. Spacer()
  281. }
  282. Button { state.showModal(for: .settings) }
  283. label: {
  284. Image("settings1")
  285. .renderingMode(.template)
  286. .resizable()
  287. .frame(width: 24, height: 24)
  288. .padding(8)
  289. }.foregroundColor(.loopGray)
  290. }
  291. .padding(.horizontal, 24)
  292. .padding(.bottom, geo.safeAreaInsets.bottom)
  293. }
  294. }
  295. .edgesIgnoringSafeArea(.vertical)
  296. }
  297. .onAppear(perform: configureView)
  298. .navigationTitle("Home")
  299. .navigationBarHidden(true)
  300. .ignoresSafeArea(.keyboard)
  301. .popup(isPresented: isStatusPopupPresented, alignment: .top, direction: .top) {
  302. VStack(alignment: .leading) {
  303. Text(state.statusTitle).foregroundColor(.white)
  304. .padding(.bottom, 4)
  305. Text(state.suggestion?.reason ?? "No sugestion found").font(.caption).foregroundColor(.white)
  306. if let errorMessage = state.errorMessage, let date = state.errorDate {
  307. Text("Error at \(dateFormatter.string(from: date))").foregroundColor(.white)
  308. .padding(.bottom, 4)
  309. .padding(.top, 8)
  310. Text(errorMessage).font(.caption).foregroundColor(.white)
  311. }
  312. }
  313. .padding()
  314. .background(
  315. RoundedRectangle(cornerRadius: 8, style: .continuous)
  316. .fill(Color(UIColor.darkGray))
  317. )
  318. .onTapGesture {
  319. isStatusPopupPresented = false
  320. }
  321. .gesture(
  322. DragGesture(minimumDistance: 10, coordinateSpace: .local)
  323. .onEnded { value in
  324. if value.translation.height < 0 {
  325. isStatusPopupPresented = false
  326. }
  327. }
  328. )
  329. }
  330. }
  331. }
  332. }