MainView.swift 4.5 KB

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