HomeRootView.swift 15 KB

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