SnoozeRootView.swift 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 = NSLocalizedString("High Glucose Alarm active", comment: "High Glucose Alarm active")
  53. case .low:
  54. celltext = NSLocalizedString("Low Glucose Alarm active", comment: "Low Glucose Alarm active")
  55. case .none:
  56. celltext = NSLocalizedString("No Glucose Alarm active", comment: "No Glucose Alarm active")
  57. }
  58. if state.snoozeUntilDate > Date() {
  59. snoozeDescription = String(
  60. format: NSLocalizedString("snoozing until %@", comment: "snoozing until %@"),
  61. dateFormatter.string(from: state.snoozeUntilDate)
  62. )
  63. } else {
  64. snoozeDescription = NSLocalizedString("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. BaseUserNotificationsManager.stopSound()
  78. state.hideModal()
  79. } label: {
  80. Text("Click to Snooze Alerts")
  81. .padding()
  82. }
  83. }
  84. }
  85. private var snoozePicker: some View {
  86. VStack {
  87. Picker(selection: $selectedInterval, label: Text("Strength")) {
  88. ForEach(0 ..< pickerTimes.count) {
  89. Text(formatInterval(self.pickerTimes[$0]))
  90. }
  91. }
  92. .pickerStyle(.wheel)
  93. }
  94. }
  95. var body: some View {
  96. Form {
  97. Section {
  98. Text(snoozeDescription).lineLimit(nil)
  99. snoozePicker
  100. snoozeButton
  101. }
  102. }
  103. .scrollContentBackground(.hidden).background(appState.trioBackgroundColor(for: colorScheme))
  104. .navigationBarTitle("Snooze Alerts")
  105. .navigationBarTitleDisplayMode(.automatic)
  106. .navigationBarItems(trailing: Button("Close", action: state.hideModal))
  107. .onAppear {
  108. configureView()
  109. snoozeDescription = getSnoozeDescription()
  110. }
  111. }
  112. }
  113. }
  114. extension TimeInterval {
  115. static func seconds(_ seconds: Double) -> TimeInterval {
  116. seconds
  117. }
  118. static func minutes(_ minutes: Double) -> TimeInterval {
  119. TimeInterval(minutes: minutes)
  120. }
  121. static func hours(_ hours: Double) -> TimeInterval {
  122. TimeInterval(hours: hours)
  123. }
  124. init(minutes: Double) {
  125. // self.init(minutes * 60)
  126. let m = minutes * 60
  127. self.init(m)
  128. }
  129. init(hours: Double) {
  130. self.init(minutes: hours * 60)
  131. }
  132. var minutes: Double {
  133. self / 60.0
  134. }
  135. var hours: Double {
  136. minutes / 60.0
  137. }
  138. }