HomeRootView.swift 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import SwiftDate
  2. import SwiftUI
  3. extension Home {
  4. struct RootView: BaseView {
  5. @EnvironmentObject var viewModel: ViewModel<Provider>
  6. @State var isPopupPresented = 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. HStack {
  16. Text("IOB").font(.caption)
  17. Text((numberFormatter.string(from: (viewModel.suggestion?.iob ?? 0) as NSNumber) ?? "0") + " U")
  18. .font(.caption2)
  19. }.padding(.top, 16)
  20. Spacer()
  21. HStack {
  22. Text("COB").font(.caption)
  23. Text((numberFormatter.string(from: (viewModel.suggestion?.cob ?? 0) as NSNumber) ?? "0") + " g")
  24. .font(.caption2)
  25. }
  26. }
  27. Spacer()
  28. CurrentGlucoseView(
  29. recentGlucose: $viewModel.recentGlucose,
  30. delta: $viewModel.glucoseDelta,
  31. units: viewModel.units
  32. )
  33. .padding(.horizontal)
  34. LoopView(suggestion: $viewModel.suggestion).onTapGesture {
  35. isPopupPresented = true
  36. }.onLongPressGesture {
  37. viewModel.runLoop()
  38. }
  39. }.frame(maxWidth: .infinity)
  40. }
  41. var body: some View {
  42. viewModel.setFilteredGlucoseHours(hours: 24)
  43. return GeometryReader { geo in
  44. VStack {
  45. header.padding().frame(maxHeight: 70)
  46. GlucoseChartView(glucose: $viewModel.glucose, suggestion: $viewModel.suggestion, units: viewModel.units)
  47. .frame(maxHeight: .infinity)
  48. ZStack {
  49. Rectangle().fill(Color.gray.opacity(0.2)).frame(height: 50 + geo.safeAreaInsets.bottom)
  50. HStack {
  51. Button { viewModel.showModal(for: .addCarbs) }
  52. label: {
  53. Image(systemName: "circlebadge.2.fill")
  54. }.foregroundColor(.green)
  55. Spacer()
  56. Button { viewModel.showModal(for: .addTempTarget) }
  57. label: {
  58. Image(systemName: "target")
  59. }.foregroundColor(.green)
  60. Spacer()
  61. Button { viewModel.showModal(for: .bolus) }
  62. label: {
  63. Image(systemName: "drop.fill")
  64. }.foregroundColor(.orange)
  65. Spacer()
  66. if viewModel.allowManualTemp {
  67. Button { viewModel.showModal(for: .manualTempBasal) }
  68. label: {
  69. Image(systemName: "circle.bottomhalf.fill")
  70. }.foregroundColor(.blue)
  71. Spacer()
  72. }
  73. Button { viewModel.showModal(for: .settings) }
  74. label: {
  75. Image(systemName: "gearshape")
  76. }.foregroundColor(.gray)
  77. }
  78. .padding(.horizontal, 24)
  79. .padding(.bottom, geo.safeAreaInsets.bottom)
  80. }
  81. }
  82. .edgesIgnoringSafeArea(.bottom)
  83. }
  84. .navigationTitle("Home")
  85. .navigationBarHidden(true)
  86. .ignoresSafeArea(.keyboard)
  87. .popup(isPresented: isPopupPresented, alignment: .top, direction: .top) {
  88. Text(viewModel.suggestion?.reason ?? "No sugestion found").font(.caption).padding().foregroundColor(.white)
  89. .background(
  90. RoundedRectangle(cornerRadius: 8, style: .continuous)
  91. .fill(Color(UIColor.darkGray))
  92. )
  93. .onTapGesture {
  94. isPopupPresented = false
  95. }
  96. }
  97. }
  98. }
  99. }