| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- // LoopFollow
- // AlarmSnoozeSection.swift
- // Created by Jonas Björkert on 2025-05-12.
- import SwiftUI
- struct AlarmSnoozeSection: View {
- @Binding var alarm: Alarm
- let range: ClosedRange<Int>
- let step: Int
- private var unitLabel: String { alarm.type.snoozeTimeUnit.label }
- private var defaultSnoozeBinding: Binding<Int> {
- Binding(
- get: { alarm.snoozeDuration },
- set: { alarm.snoozeDuration = $0 }
- )
- }
- private var isSnoozed: Binding<Bool> {
- Binding(
- get: {
- if let until = alarm.snoozedUntil, until > Date() { return true }
- return false
- },
- set: { on in
- if on {
- if alarm.snoozedUntil == nil || alarm.snoozedUntil! < Date() {
- let secs = alarm.type.snoozeTimeUnit.seconds
- alarm.snoozedUntil = Date()
- .addingTimeInterval(Double(alarm.snoozeDuration) * secs)
- }
- } else {
- alarm.snoozedUntil = nil
- }
- }
- )
- }
- var body: some View {
- Section(
- header: Text("SNOOZE"),
- footer: Text(
- """
- “Default Snooze” controls the default value for how long the alert stays quiet after you press Snooze. \
- \(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. " : "")\
- Toggle “Snoozed” to mute this alarm right now.
- """
- )
- ) {
- Stepper(
- value: defaultSnoozeBinding,
- in: range,
- step: step
- ) {
- HStack {
- Text("Default Snooze:")
- Spacer()
- Text("\(alarm.snoozeDuration) \(unitLabel)")
- .foregroundColor(.secondary)
- }
- }
- Toggle("Snoozed", isOn: isSnoozed)
- if isSnoozed.wrappedValue, let until = alarm.snoozedUntil {
- DatePicker(
- "Until",
- selection: Binding(
- get: { until },
- set: { alarm.snoozedUntil = $0 }
- ),
- displayedComponents: [.date, .hourAndMinute]
- )
- }
- }
- }
- }
|