SnoozeRootView.swift 5.6 KB

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