MainView.swift 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. .scaledToFill()
  50. .minimumScaleFactor(0.5)
  51. Text(state.trend)
  52. }
  53. Text(state.delta).font(.caption2)
  54. .scaledToFill()
  55. .minimumScaleFactor(0.5)
  56. .foregroundColor(.secondary)
  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)
  65. .scaledToFill()
  66. .minimumScaleFactor(0.5)
  67. .foregroundColor(.secondary)
  68. } else {
  69. Text("--").font(.caption2)
  70. }
  71. }
  72. }
  73. Spacer()
  74. HStack(alignment: .firstTextBaseline) {
  75. HStack {
  76. Text(iobFormatter.string(from: (state.iob ?? 0) as NSNumber)! + " U")
  77. .font(.caption2)
  78. .scaledToFill()
  79. .foregroundColor(.insulin)
  80. .minimumScaleFactor(0.5)
  81. }.minimumScaleFactor(0.5)
  82. Spacer()
  83. HStack {
  84. Text(iobFormatter.string(from: (state.cob ?? 0) as NSNumber)! + " g")
  85. .font(.caption2)
  86. .scaledToFill()
  87. .foregroundColor(.loopGreen)
  88. .minimumScaleFactor(0.5)
  89. }
  90. if let eventualBG = state.eventualBG.nonEmpty {
  91. Spacer()
  92. HStack {
  93. Text(eventualBG)
  94. .font(.caption2)
  95. .scaledToFill()
  96. .foregroundColor(.secondary)
  97. .minimumScaleFactor(0.5)
  98. }
  99. }
  100. }
  101. Spacer()
  102. }.padding()
  103. }
  104. var buttons: some View {
  105. HStack(alignment: .center) {
  106. NavigationLink(isActive: $state.isCarbsViewActive) {
  107. CarbsView()
  108. .environmentObject(state)
  109. } label: {
  110. Image("carbs", bundle: nil)
  111. .renderingMode(.template)
  112. .resizable()
  113. .frame(width: 24, height: 24)
  114. .foregroundColor(.loopGreen)
  115. }
  116. NavigationLink(isActive: $state.isTempTargetViewActive) {
  117. TempTargetsView()
  118. .environmentObject(state)
  119. } label: {
  120. VStack {
  121. Image("target", bundle: nil)
  122. .renderingMode(.template)
  123. .resizable()
  124. .frame(width: 24, height: 24)
  125. .foregroundColor(.loopYellow)
  126. if let until = state.tempTargets.compactMap(\.until).first, until > Date() {
  127. Text(until, style: .timer)
  128. .scaledToFill()
  129. .font(.system(size: 8))
  130. }
  131. }
  132. }
  133. NavigationLink(isActive: $state.isBolusViewActive) {
  134. BolusView()
  135. .environmentObject(state)
  136. } label: {
  137. Image("bolus", bundle: nil)
  138. .renderingMode(.template)
  139. .resizable()
  140. .frame(width: 24, height: 24)
  141. .foregroundColor(.insulin)
  142. }
  143. }
  144. }
  145. private var iobFormatter: NumberFormatter {
  146. let formatter = NumberFormatter()
  147. formatter.maximumFractionDigits = 2
  148. formatter.numberStyle = .decimal
  149. return formatter
  150. }
  151. private var timeString: String {
  152. let minAgo = Int((Date().timeIntervalSince(state.lastLoopDate ?? .distantPast) - Config.lag) / 60) + 1
  153. if minAgo > 1440 {
  154. return "--"
  155. }
  156. return "\(minAgo) " + NSLocalizedString("min", comment: "Minutes ago since last loop")
  157. }
  158. private var color: Color {
  159. guard let lastLoopDate = state.lastLoopDate else {
  160. return .loopGray
  161. }
  162. let delta = Date().timeIntervalSince(lastLoopDate) - Config.lag
  163. if delta <= 5.minutes.timeInterval {
  164. return .loopGreen
  165. } else if delta <= 10.minutes.timeInterval {
  166. return .loopYellow
  167. } else {
  168. return .loopRed
  169. }
  170. }
  171. }
  172. struct ContentView_Previews: PreviewProvider {
  173. static var previews: some View {
  174. let state = WatchStateModel()
  175. state.glucose = "888"
  176. state.delta = "+888"
  177. state.iob = 100.38
  178. state.cob = 112.123
  179. state.eventualBG = "⇢ 8,888"
  180. state.lastLoopDate = Date().addingTimeInterval(-200)
  181. state
  182. .tempTargets =
  183. [TempTargetWatchPreset(name: "Test", id: "test", description: "", until: Date().addingTimeInterval(3600 * 3))]
  184. return Group {
  185. MainView()
  186. MainView().previewDevice("Apple Watch Series 5 - 40mm")
  187. }.environmentObject(state)
  188. }
  189. }