AlarmSnoozeSection.swift 2.4 KB

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