AlarmSnoozeSection.swift 2.4 KB

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