SnoozeRootView.swift 5.7 KB

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