AlarmSettingsView.swift 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // LoopFollow
  2. // AlarmSettingsView.swift
  3. import SwiftUI
  4. struct AlarmSettingsView: View {
  5. @ObservedObject private var cfgStore = Storage.shared.alarmConfiguration
  6. /// Helper to bind an optional Date? into a non‑optional Date for DatePicker
  7. private func optDateBinding(_ b: Binding<Date?>) -> Binding<Date> {
  8. Binding(
  9. get: { b.wrappedValue ?? Date() },
  10. set: { b.wrappedValue = $0 }
  11. )
  12. }
  13. private var dayBinding: Binding<Date> {
  14. Binding(
  15. get: {
  16. var c = Calendar.current.dateComponents([.year, .month, .day], from: Date())
  17. c.hour = cfgStore.value.dayStart.hour
  18. c.minute = cfgStore.value.dayStart.minute
  19. return Calendar.current.date(from: c)!
  20. },
  21. set: { d in
  22. let hc = Calendar.current.dateComponents([.hour, .minute], from: d)
  23. cfgStore.value.dayStart = TimeOfDay(hour: hc.hour!, minute: hc.minute!)
  24. }
  25. )
  26. }
  27. private var nightBinding: Binding<Date> {
  28. Binding(
  29. get: {
  30. var c = Calendar.current.dateComponents([.year, .month, .day], from: Date())
  31. c.hour = cfgStore.value.nightStart.hour
  32. c.minute = cfgStore.value.nightStart.minute
  33. return Calendar.current.date(from: c)!
  34. },
  35. set: { d in
  36. let hc = Calendar.current.dateComponents([.hour, .minute], from: d)
  37. cfgStore.value.nightStart = TimeOfDay(hour: hc.hour!, minute: hc.minute!)
  38. }
  39. )
  40. }
  41. var body: some View {
  42. NavigationView {
  43. Form {
  44. Section(
  45. header: Text("Snooze & Mute Options"),
  46. footer: Text("""
  47. “Snooze All” disables every alarm. \
  48. “Mute All” silences phone sounds but still vibrates \
  49. and shows iOS notifications.
  50. """)
  51. ) {
  52. Toggle("All Alerts Snoozed", isOn: Binding(
  53. get: {
  54. if let until = cfgStore.value.snoozeUntil { return until > Date() }
  55. return false
  56. },
  57. set: { on in
  58. if on {
  59. let target = cfgStore.value.snoozeUntil ?? Date()
  60. if target <= Date() {
  61. cfgStore.value.snoozeUntil = Date().addingTimeInterval(3600)
  62. }
  63. } else {
  64. cfgStore.value.snoozeUntil = nil
  65. }
  66. }
  67. ))
  68. if let until = cfgStore.value.snoozeUntil, until > Date() {
  69. DatePicker(
  70. "Until",
  71. selection: optDateBinding(
  72. Binding(
  73. get: { cfgStore.value.snoozeUntil },
  74. set: { cfgStore.value.snoozeUntil = $0 }
  75. )
  76. ),
  77. displayedComponents: [.date, .hourAndMinute]
  78. )
  79. .datePickerStyle(.compact)
  80. }
  81. Toggle("All Sounds Muted", isOn: Binding(
  82. get: {
  83. if let until = cfgStore.value.muteUntil { return until > Date() }
  84. return false
  85. },
  86. set: { on in
  87. if on {
  88. let target = cfgStore.value.muteUntil ?? Date()
  89. if target <= Date() {
  90. cfgStore.value.muteUntil = Date().addingTimeInterval(3600)
  91. }
  92. } else {
  93. cfgStore.value.muteUntil = nil
  94. }
  95. }
  96. ))
  97. if let until = cfgStore.value.muteUntil, until > Date() {
  98. DatePicker(
  99. "Until",
  100. selection: optDateBinding(
  101. Binding(
  102. get: { cfgStore.value.muteUntil },
  103. set: { cfgStore.value.muteUntil = $0 }
  104. )
  105. ),
  106. displayedComponents: [.date, .hourAndMinute]
  107. )
  108. .datePickerStyle(.compact)
  109. }
  110. }
  111. Section(
  112. header: Text("Day / Night Schedule"),
  113. footer: Text("Pick when your day period begins and when your night period begins. " +
  114. "Any time from your Day-starts time up until your Night-starts time will count as day; " +
  115. "from Night-starts until the next Day-starts will count as night.")
  116. ) {
  117. DatePicker(
  118. "Day starts",
  119. selection: dayBinding,
  120. displayedComponents: [.hourAndMinute]
  121. )
  122. .datePickerStyle(.compact)
  123. DatePicker(
  124. "Night starts",
  125. selection: nightBinding,
  126. displayedComponents: [.hourAndMinute]
  127. )
  128. .datePickerStyle(.compact)
  129. }
  130. Section(header: Text("Alarm Settings")) {
  131. Toggle(
  132. "Override System Volume",
  133. isOn: Binding(
  134. get: { cfgStore.value.overrideSystemOutputVolume },
  135. set: { cfgStore.value.overrideSystemOutputVolume = $0 }
  136. )
  137. )
  138. if cfgStore.value.overrideSystemOutputVolume {
  139. Stepper(
  140. "Volume Level: \(Int(cfgStore.value.forcedOutputVolume * 100))%",
  141. value: Binding(
  142. get: { Double(cfgStore.value.forcedOutputVolume) },
  143. set: { cfgStore.value.forcedOutputVolume = Float($0) }
  144. ),
  145. in: 0 ... 1,
  146. step: 0.05
  147. )
  148. }
  149. Toggle(
  150. "Audio During Calls",
  151. isOn: Binding(
  152. get: { cfgStore.value.audioDuringCalls },
  153. set: { cfgStore.value.audioDuringCalls = $0 }
  154. )
  155. )
  156. Toggle(
  157. "Ignore Zero BG",
  158. isOn: Binding(
  159. get: { cfgStore.value.ignoreZeroBG },
  160. set: { cfgStore.value.ignoreZeroBG = $0 }
  161. )
  162. )
  163. Toggle(
  164. "Auto‑Snooze CGM Start",
  165. isOn: Binding(
  166. get: { cfgStore.value.autoSnoozeCGMStart },
  167. set: { cfgStore.value.autoSnoozeCGMStart = $0 }
  168. )
  169. )
  170. Toggle(
  171. "Volume Buttons Snooze Alarms",
  172. isOn: Binding(
  173. get: { cfgStore.value.enableVolumeButtonSnooze },
  174. set: { cfgStore.value.enableVolumeButtonSnooze = $0 }
  175. )
  176. )
  177. }
  178. }
  179. }
  180. .preferredColorScheme(Storage.shared.appearanceMode.value.colorScheme)
  181. .navigationBarTitle("Alarm Settings", displayMode: .inline)
  182. }
  183. }