AlarmSnoozeSection.swift 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // LoopFollow
  2. // AlarmSnoozeSection.swift
  3. // Created by Jonas Björkert on 2025-05-12.
  4. import SwiftUI
  5. struct AlarmSnoozeSection: View {
  6. @Binding var alarm: Alarm
  7. let range: ClosedRange<Int>
  8. let step: Int
  9. private var unitLabel: String { alarm.type.snoozeTimeUnit.label }
  10. private var defaultSnoozeBinding: Binding<Int> {
  11. Binding(
  12. get: { alarm.snoozeDuration },
  13. set: { alarm.snoozeDuration = $0 }
  14. )
  15. }
  16. private var isSnoozed: Binding<Bool> {
  17. Binding(
  18. get: {
  19. if let until = alarm.snoozedUntil, until > Date() { return true }
  20. return false
  21. },
  22. set: { on in
  23. if on {
  24. if alarm.snoozedUntil == nil || alarm.snoozedUntil! < Date() {
  25. let secs = alarm.type.snoozeTimeUnit.seconds
  26. alarm.snoozedUntil = Date()
  27. .addingTimeInterval(Double(alarm.snoozeDuration) * secs)
  28. }
  29. } else {
  30. alarm.snoozedUntil = nil
  31. }
  32. }
  33. )
  34. }
  35. var body: some View {
  36. Section(
  37. header: Text("SNOOZE"),
  38. footer: Text(
  39. """
  40. “Default Snooze” controls the default value for how long the alert stays quiet after you press Snooze. \
  41. \(range.contains(0) ? "A snooze duration of 0 means the alarm is acknowledged (silenced), and will alert again next time the condition applies, without time limitation. " : "")\
  42. Toggle “Snoozed” to mute this alarm right now.
  43. """
  44. )
  45. ) {
  46. Stepper(
  47. value: defaultSnoozeBinding,
  48. in: range,
  49. step: step
  50. ) {
  51. HStack {
  52. Text("Default Snooze:")
  53. Spacer()
  54. Text("\(alarm.snoozeDuration) \(unitLabel)")
  55. .foregroundColor(.secondary)
  56. }
  57. }
  58. Toggle("Snoozed", isOn: isSnoozed)
  59. if isSnoozed.wrappedValue, let until = alarm.snoozedUntil {
  60. DatePicker(
  61. "Until",
  62. selection: Binding(
  63. get: { until },
  64. set: { alarm.snoozedUntil = $0 }
  65. ),
  66. displayedComponents: [.date, .hourAndMinute]
  67. )
  68. }
  69. }
  70. }
  71. }