HomeRootView.swift 15 KB

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