SnoozeRootView.swift 4.9 KB

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