MainView.swift 7.0 KB

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