SnoozeRootView.swift 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import AudioToolbox
  2. import SwiftUI
  3. import Swinject
  4. extension Snooze {
  5. struct RootView: BaseView {
  6. let resolver: Resolver
  7. @State var state = StateModel()
  8. @Environment(\.colorScheme) var colorScheme
  9. @Environment(AppState.self) var appState
  10. @State private var selectedInterval = 0
  11. @State private var snoozeDescription = "nothing to see here"
  12. private var pickerTimes: [TimeInterval] {
  13. var arr: [TimeInterval] = []
  14. let mins10 = 0.166_67
  15. let mins20 = mins10 * 2
  16. let mins30 = mins10 * 3
  17. // let mins40 = mins10 * 4
  18. for hr in 0 ..< 2 {
  19. for min in [0.0, mins20, mins20 * 2] {
  20. arr.append(TimeInterval(hours: Double(hr) + min))
  21. }
  22. }
  23. for hr in 2 ..< 4 {
  24. for min in [0.0, mins30] {
  25. arr.append(TimeInterval(hours: Double(hr) + min))
  26. }
  27. }
  28. for hr in 4 ... 8 {
  29. arr.append(TimeInterval(hours: Double(hr)))
  30. }
  31. return arr
  32. }
  33. private var formatter: DateComponentsFormatter {
  34. let formatter = DateComponentsFormatter()
  35. formatter.allowsFractionalUnits = false
  36. formatter.unitsStyle = .full
  37. return formatter
  38. }
  39. private var dateFormatter: DateFormatter {
  40. let formatter = DateFormatter()
  41. formatter.timeStyle = .short
  42. return formatter
  43. }
  44. private func formatInterval(_ interval: TimeInterval) -> String {
  45. formatter.string(from: interval)!
  46. }
  47. func getSnoozeDescription() -> String {
  48. var snoozeDescription = ""
  49. var celltext = ""
  50. switch state.alarm {
  51. case .high:
  52. celltext = String(localized: "High Glucose Alarm active", comment: "High Glucose Alarm active")
  53. case .low:
  54. celltext = String(localized: "Low Glucose Alarm active", comment: "Low Glucose Alarm active")
  55. case .none:
  56. celltext = String(localized: "No Glucose Alarm active", comment: "No Glucose Alarm active")
  57. }
  58. if state.snoozeUntilDate > Date() {
  59. snoozeDescription = String(
  60. format: String(localized: "snoozing until %@", comment: "snoozing until %@"),
  61. dateFormatter.string(from: state.snoozeUntilDate)
  62. )
  63. } else {
  64. snoozeDescription = String(localized: "not snoozing", comment: "not snoozing")
  65. }
  66. return [celltext, snoozeDescription].joined(separator: ", ")
  67. }
  68. private var snoozeButton: some View {
  69. VStack(alignment: .leading) {
  70. Button {
  71. let interval = pickerTimes[selectedInterval]
  72. let snoozeFor = formatter.string(from: interval)!
  73. let untilDate = Date() + interval
  74. state.snoozeUntilDate = untilDate < Date() ? .distantPast : untilDate
  75. debug(.default, "will snooze for \(snoozeFor) until \(dateFormatter.string(from: untilDate))")
  76. snoozeDescription = getSnoozeDescription()
  77. state.hideModal()
  78. } label: {
  79. Text("Click to Snooze Alerts")
  80. .padding()
  81. }
  82. }
  83. }
  84. private var snoozePicker: some View {
  85. VStack {
  86. Picker(selection: $selectedInterval, label: Text("Strength")) {
  87. ForEach(0 ..< pickerTimes.count) {
  88. Text(formatInterval(self.pickerTimes[$0]))
  89. }
  90. }
  91. .pickerStyle(.wheel)
  92. }
  93. }
  94. var body: some View {
  95. Form {
  96. Section {
  97. Text(snoozeDescription).lineLimit(nil)
  98. snoozePicker
  99. snoozeButton
  100. }
  101. }
  102. .scrollContentBackground(.hidden).background(appState.trioBackgroundColor(for: colorScheme))
  103. .navigationBarTitle("Snooze Alerts")
  104. .navigationBarTitleDisplayMode(.automatic)
  105. .navigationBarItems(trailing: Button("Close", action: state.hideModal))
  106. .onAppear {
  107. configureView()
  108. snoozeDescription = getSnoozeDescription()
  109. }
  110. }
  111. }
  112. }
  113. extension TimeInterval {
  114. static func seconds(_ seconds: Double) -> TimeInterval {
  115. seconds
  116. }
  117. static func minutes(_ minutes: Double) -> TimeInterval {
  118. TimeInterval(minutes: minutes)
  119. }
  120. static func hours(_ hours: Double) -> TimeInterval {
  121. TimeInterval(hours: hours)
  122. }
  123. init(minutes: Double) {
  124. // self.init(minutes * 60)
  125. let m = minutes * 60
  126. self.init(m)
  127. }
  128. init(hours: Double) {
  129. self.init(minutes: hours * 60)
  130. }
  131. var minutes: Double {
  132. self / 60.0
  133. }
  134. var hours: Double {
  135. minutes / 60.0
  136. }
  137. }