MainView.swift 4.6 KB

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