MainView.swift 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. .onReceive(state.timer) { _ in
  26. state.requestState()
  27. }
  28. .onAppear {
  29. state.requestState()
  30. }
  31. }
  32. var header: some View {
  33. VStack {
  34. HStack(alignment: .top) {
  35. VStack(alignment: .leading) {
  36. HStack {
  37. Text(state.glucose).font(.largeTitle)
  38. Text(state.trend)
  39. }
  40. Text(state.delta).font(.caption2).foregroundColor(.gray)
  41. }
  42. Spacer()
  43. VStack(spacing: 0) {
  44. HStack {
  45. Circle().stroke(color, lineWidth: 6).frame(width: 30, height: 30).padding(10)
  46. }
  47. if state.lastLoopDate != nil {
  48. Text(timeString).font(.caption2).foregroundColor(.gray)
  49. } else {
  50. Text("--").font(.caption2).foregroundColor(.gray)
  51. }
  52. }
  53. }
  54. Spacer()
  55. Spacer()
  56. HStack {
  57. Text(iobFormatter.string(from: (state.cob ?? 0) as NSNumber)!).font(.caption2)
  58. Text("g").foregroundColor(.loopGreen)
  59. Spacer()
  60. Text(iobFormatter.string(from: (state.iob ?? 0) as NSNumber)!).font(.caption2)
  61. Text("U").foregroundColor(.insulin)
  62. Spacer()
  63. Spacer()
  64. }
  65. // Spacer()
  66. }.padding()
  67. }
  68. var buttons: some View {
  69. HStack {
  70. NavigationLink(isActive: $state.isCarbsViewActive) {
  71. CarbsView()
  72. .environmentObject(state)
  73. } label: {
  74. Image("carbs", bundle: nil)
  75. .renderingMode(.template)
  76. .resizable()
  77. .frame(width: 24, height: 24)
  78. .foregroundColor(.loopGreen)
  79. }
  80. NavigationLink(isActive: $state.isBolusViewActive) {
  81. BolusView()
  82. .environmentObject(state)
  83. } label: {
  84. Image("bolus", bundle: nil)
  85. .renderingMode(.template)
  86. .resizable()
  87. .frame(width: 24, height: 24)
  88. .foregroundColor(.insulin)
  89. }
  90. NavigationLink(isActive: $state.isTempTargetViewActive) {
  91. TempTargetsView()
  92. .environmentObject(state)
  93. } label: {
  94. VStack {
  95. Image("target", bundle: nil)
  96. .renderingMode(.template)
  97. .resizable()
  98. .frame(width: 24, height: 24)
  99. .foregroundColor(.loopYellow)
  100. if let until = state.tempTargets.compactMap(\.until).first, until > Date() {
  101. Text(until, style: .timer).font(.system(size: 8))
  102. }
  103. }
  104. }
  105. }
  106. }
  107. private var iobFormatter: NumberFormatter {
  108. let formatter = NumberFormatter()
  109. formatter.maximumFractionDigits = 2
  110. formatter.numberStyle = .decimal
  111. return formatter
  112. }
  113. private var timeString: String {
  114. let minAgo = Int((Date().timeIntervalSince(state.lastLoopDate ?? .distantPast) - Config.lag) / 60) + 1
  115. if minAgo > 1440 {
  116. return "--"
  117. }
  118. return "\(minAgo) " + NSLocalizedString("min", comment: "Minutes ago since last loop")
  119. }
  120. private var color: Color {
  121. guard let lastLoopDate = state.lastLoopDate else {
  122. return .loopGray
  123. }
  124. let delta = Date().timeIntervalSince(lastLoopDate) - Config.lag
  125. if delta <= 5.minutes.timeInterval {
  126. return .loopGreen
  127. } else if delta <= 10.minutes.timeInterval {
  128. return .loopYellow
  129. } else {
  130. return .loopRed
  131. }
  132. }
  133. }
  134. struct ContentView_Previews: PreviewProvider {
  135. static var previews: some View {
  136. Group {
  137. MainView().environmentObject(WatchStateModel())
  138. MainView().previewDevice("Apple Watch Series 5 - 40mm").environmentObject(WatchStateModel())
  139. }
  140. }
  141. }