MainView.swift 6.3 KB

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