HomeRootView.swift 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import SwiftDate
  2. import SwiftUI
  3. extension Home {
  4. struct RootView: BaseView {
  5. @EnvironmentObject var viewModel: ViewModel<Provider>
  6. @State var isStatusPopupPresented = false
  7. private var numberFormatter: NumberFormatter {
  8. let formatter = NumberFormatter()
  9. formatter.numberStyle = .decimal
  10. return formatter
  11. }
  12. var header: some View {
  13. HStack {
  14. VStack(alignment: .leading) {
  15. Text("PUMP").font(.caption)
  16. }.frame(minWidth: 0, maxWidth: .infinity)
  17. Spacer()
  18. CurrentGlucoseView(
  19. recentGlucose: $viewModel.recentGlucose,
  20. delta: $viewModel.glucoseDelta,
  21. units: viewModel.units
  22. ).frame(minWidth: 0, maxWidth: .infinity)
  23. Spacer()
  24. LoopView(
  25. suggestion: $viewModel.suggestion,
  26. enactedSuggestion: $viewModel.enactedSuggestion,
  27. closedLoop: $viewModel.closedLoop,
  28. timerDate: $viewModel.timerDate,
  29. isLooping: $viewModel.isLooping,
  30. lastLoopDate: $viewModel.lastLoopDate
  31. ).onTapGesture {
  32. isStatusPopupPresented = true
  33. }.onLongPressGesture {
  34. viewModel.runLoop()
  35. }.frame(minWidth: 0, maxWidth: .infinity)
  36. }.frame(maxWidth: .infinity)
  37. }
  38. var infoPanal: some View {
  39. HStack(alignment: .firstTextBaseline) {
  40. Text("IOB").font(.caption)
  41. .padding(.leading)
  42. Text((numberFormatter.string(from: (viewModel.suggestion?.iob ?? 0) as NSNumber) ?? "0") + " U")
  43. .font(.caption)
  44. Text("COB").font(.caption)
  45. Text((numberFormatter.string(from: (viewModel.suggestion?.cob ?? 0) as NSNumber) ?? "0") + " g")
  46. .font(.caption)
  47. if let tempRate = viewModel.tempRate {
  48. Text("Temp basal").font(.caption).foregroundColor(.blue)
  49. Text((numberFormatter.string(from: tempRate as NSNumber) ?? "0") + " U/hr")
  50. .font(.caption).foregroundColor(.blue)
  51. }
  52. Spacer()
  53. }.frame(maxWidth: .infinity, maxHeight: 30)
  54. .background(Rectangle().fill(Color.gray.opacity(0.2)))
  55. }
  56. var body: some View {
  57. viewModel.setFilteredGlucoseHours(hours: 24)
  58. return GeometryReader { geo in
  59. VStack(spacing: 0) {
  60. header.padding(.vertical).frame(maxHeight: 70)
  61. infoPanal
  62. MainChartView(
  63. glucose: $viewModel.glucose,
  64. suggestion: $viewModel.suggestion,
  65. tempBasals: $viewModel.tempBasals,
  66. boluses: $viewModel.boluses,
  67. hours: .constant(viewModel.filteredHours),
  68. maxBasal: $viewModel.maxBasal,
  69. basalProfile: $viewModel.basalProfile,
  70. tempTargets: $viewModel.tempTargets,
  71. carbs: $viewModel.carbs,
  72. units: viewModel.units
  73. )
  74. .padding(.bottom)
  75. ZStack {
  76. Rectangle().fill(Color.gray.opacity(0.2)).frame(height: 50 + geo.safeAreaInsets.bottom)
  77. HStack {
  78. Button { viewModel.showModal(for: .addCarbs) }
  79. label: {
  80. Image("carbs")
  81. .renderingMode(.template)
  82. .resizable()
  83. .frame(width: 24, height: 24)
  84. }.foregroundColor(.orange)
  85. Spacer()
  86. Button { viewModel.showModal(for: .addTempTarget) }
  87. label: {
  88. Image("target")
  89. .renderingMode(.template)
  90. .resizable()
  91. .frame(width: 24, height: 24)
  92. }.foregroundColor(.primary)
  93. Spacer()
  94. Button { viewModel.showModal(for: .bolus) }
  95. label: {
  96. Image("bolus")
  97. .renderingMode(.template)
  98. .resizable()
  99. .frame(width: 24, height: 24)
  100. }.foregroundColor(.blue)
  101. Spacer()
  102. if viewModel.allowManualTemp {
  103. Button { viewModel.showModal(for: .manualTempBasal) }
  104. label: {
  105. Image("bolus1")
  106. .renderingMode(.template)
  107. .resizable()
  108. .frame(width: 24, height: 24)
  109. }.foregroundColor(.blue)
  110. Spacer()
  111. }
  112. Button { viewModel.showModal(for: .settings) }
  113. label: {
  114. Image("settings1")
  115. .renderingMode(.template)
  116. .resizable()
  117. .frame(width: 24, height: 24)
  118. }.foregroundColor(.gray)
  119. }
  120. .padding(.horizontal, 24)
  121. .padding(.bottom, geo.safeAreaInsets.bottom)
  122. }
  123. }
  124. .edgesIgnoringSafeArea(.bottom)
  125. }
  126. .navigationTitle("Home")
  127. .navigationBarHidden(true)
  128. .ignoresSafeArea(.keyboard)
  129. .popup(isPresented: isStatusPopupPresented, alignment: .top, direction: .top) {
  130. VStack(alignment: .leading) {
  131. Text(viewModel.statusTitle).foregroundColor(.white)
  132. .padding(.bottom, 4)
  133. Text(viewModel.suggestion?.reason ?? "No sugestion found").font(.caption).foregroundColor(.white)
  134. }
  135. .padding()
  136. .background(
  137. RoundedRectangle(cornerRadius: 8, style: .continuous)
  138. .fill(Color(UIColor.darkGray))
  139. )
  140. .onTapGesture {
  141. isStatusPopupPresented = false
  142. }
  143. }
  144. }
  145. }
  146. }