MainView.swift 5.4 KB

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