MainView.swift 7.2 KB

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