SnoozeRootView.swift 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. state.hideModal()
  77. } label: {
  78. Text("Click to Snooze Alerts")
  79. .padding()
  80. }
  81. }
  82. }
  83. private var snoozePicker: some View {
  84. VStack {
  85. Picker(selection: $selectedInterval, label: Text("Strength")) {
  86. ForEach(0 ..< pickerTimes.count) {
  87. Text(formatInterval(self.pickerTimes[$0]))
  88. }
  89. }
  90. .pickerStyle(.wheel)
  91. }
  92. }
  93. var body: some View {
  94. Form {
  95. Section {
  96. Text(snoozeDescription).lineLimit(nil)
  97. snoozePicker
  98. snoozeButton
  99. }
  100. }
  101. .navigationBarTitle("Snooze Alerts")
  102. .navigationBarTitleDisplayMode(.automatic)
  103. .navigationBarItems(leading: Button("Close", action: state.hideModal))
  104. .onAppear {
  105. configureView()
  106. snoozeDescription = getSnoozeDescription()
  107. }
  108. }
  109. }
  110. }
  111. extension TimeInterval {
  112. static func seconds(_ seconds: Double) -> TimeInterval {
  113. seconds
  114. }
  115. static func minutes(_ minutes: Double) -> TimeInterval {
  116. TimeInterval(minutes: minutes)
  117. }
  118. static func hours(_ hours: Double) -> TimeInterval {
  119. TimeInterval(hours: hours)
  120. }
  121. init(minutes: Double) {
  122. // self.init(minutes * 60)
  123. let m = minutes * 60
  124. self.init(m)
  125. }
  126. init(hours: Double) {
  127. self.init(minutes: hours * 60)
  128. }
  129. var minutes: Double {
  130. self / 60.0
  131. }
  132. var hours: Double {
  133. minutes / 60.0
  134. }
  135. }