MainView.swift 5.6 KB

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