MainView.swift 4.8 KB

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