MainView.swift 4.7 KB

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