MainView.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import SwiftDate
  2. import SwiftUI
  3. struct MainView: View {
  4. private enum Config {
  5. static let lag: TimeInterval = 30
  6. }
  7. @EnvironmentObject var state: WatchStateModel
  8. @State var isCarbsActive = false
  9. @State var isTargetsActive = false
  10. @State var isBolusActive = false
  11. var body: some View {
  12. ZStack {
  13. VStack {
  14. header
  15. Spacer()
  16. buttons
  17. }
  18. if state.isConfirmationViewActive {
  19. ConfirmationView(success: $state.confirmationSuccess)
  20. .background(Rectangle().fill(.black))
  21. }
  22. }
  23. .frame(maxHeight: .infinity)
  24. .padding()
  25. .onAppear {
  26. state.requestState()
  27. }
  28. }
  29. var header: some View {
  30. VStack {
  31. HStack(alignment: .top) {
  32. VStack(alignment: .leading) {
  33. HStack {
  34. Text(state.glucose).font(.largeTitle)
  35. Text(state.trend)
  36. }
  37. Text(state.delta).font(.caption2)
  38. }
  39. Spacer()
  40. VStack(spacing: 0) {
  41. HStack {
  42. Circle().stroke(color, lineWidth: 6).frame(width: 30, height: 30).padding(10)
  43. }
  44. if state.lastLoopDate != nil {
  45. Text(timeString).font(.caption2)
  46. } else {
  47. Text("--").font(.caption2)
  48. }
  49. }
  50. }
  51. Spacer()
  52. HStack {
  53. Text("IOB: " + iobFormatter.string(from: (state.iob ?? 0) as NSNumber)! + " U").font(.caption2)
  54. Spacer()
  55. Text("COB: " + iobFormatter.string(from: (state.cob ?? 0) as NSNumber)! + " g").font(.caption2)
  56. }
  57. Spacer()
  58. }.padding()
  59. }
  60. var buttons: some View {
  61. HStack {
  62. NavigationLink(isActive: $state.isCarbsViewActive) {
  63. CarbsView()
  64. .environmentObject(state)
  65. } label: {
  66. Image("carbs", bundle: nil)
  67. .renderingMode(.template)
  68. .resizable()
  69. .frame(width: 24, height: 24)
  70. .foregroundColor(.loopGreen)
  71. }
  72. NavigationLink(isActive: $state.isTempTargetViewActive) {
  73. TempTargetsView()
  74. .environmentObject(state)
  75. } label: {
  76. VStack {
  77. Image("target", bundle: nil)
  78. .renderingMode(.template)
  79. .resizable()
  80. .frame(width: 24, height: 24)
  81. .foregroundColor(.loopYellow)
  82. if let until = state.tempTargets.compactMap(\.until).first, until > Date() {
  83. Text(until, style: .timer).font(.system(size: 8))
  84. }
  85. }
  86. }
  87. NavigationLink(isActive: $state.isBolusViewActive) {
  88. EmptyView()
  89. } label: {
  90. Image("bolus", bundle: nil)
  91. .renderingMode(.template)
  92. .resizable()
  93. .frame(width: 24, height: 24)
  94. .foregroundColor(.insulin)
  95. }
  96. }
  97. }
  98. private var iobFormatter: NumberFormatter {
  99. let formatter = NumberFormatter()
  100. formatter.maximumFractionDigits = 2
  101. formatter.numberStyle = .decimal
  102. return formatter
  103. }
  104. private var timeString: String {
  105. let minAgo = Int((Date().timeIntervalSince(state.lastLoopDate ?? .distantPast) - Config.lag) / 60) + 1
  106. if minAgo > 1440 {
  107. return "--"
  108. }
  109. return "\(minAgo) " + NSLocalizedString("min ago", comment: "Minutes ago since last loop")
  110. }
  111. private var color: Color {
  112. guard let lastLoopDate = state.lastLoopDate else {
  113. return .loopGray
  114. }
  115. let delta = Date().timeIntervalSince(lastLoopDate) - Config.lag
  116. if delta <= 5.minutes.timeInterval {
  117. return .loopGreen
  118. } else if delta <= 10.minutes.timeInterval {
  119. return .loopYellow
  120. } else {
  121. return .loopRed
  122. }
  123. }
  124. }
  125. struct ContentView_Previews: PreviewProvider {
  126. static var previews: some View {
  127. Group {
  128. MainView().environmentObject(WatchStateModel())
  129. MainView().previewDevice("Apple Watch Series 5 - 40mm").environmentObject(WatchStateModel())
  130. }
  131. }
  132. }