Repository Public License
1// ORIX Alarm2// File: App/Features/Editor/ScenarioSectionsView.swift3// Description: Reusable scenario sections (sound, progressive wake, missions, snooze, confirmation, live timeline) shared by the alarm editor and Alarm Studio.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import AlarmCore8import SwiftUI910/// Problems that make a scenario impossible to run as configured.11enum ScenarioValidation {12 static func messages(for scenario: WakeScenario, qrTags: [QRTag]) -> [String] {13 var out: [String] = []14 if scenario.missions.count > WakeScenario.maxMissionSteps { out.append(message(.tooManyMissionSteps)) }15 if scenario.missions.contains(where: { step in step.kind == .qrEscape && !qrTags.contains { $0.id == step.qrTagID } }) { out.append(message(.qrTagMissing)) }16 return out17 }1819 static func message(_ issue: Recurrence.ValidationIssue) -> String {20 switch issue {21 case .tooManyMissionSteps: String(localized: "A Morning Combo holds at most \(WakeScenario.maxMissionSteps) missions. Remove a step.")22 case .qrTagMissing: String(localized: "QR Escape has no registered QR tag. Open that step and choose or register a tag, or remove the step.")23 case .noUpcomingOccurrence: String(localized: "No upcoming occurrence could be computed for this time and these days.")24 }25 }26}2728/// The scenario part of an alarm. Takes bindings so the same sections edit an alarm draft or a saved scenario.29struct ScenarioSectionsView: View {30 @Binding var scenario: WakeScenario31 @Binding var soundID: String32 /// Alarm time when editing an alarm; `nil` for a stand-alone scenario (relative labels in the timeline).33 var time: ClockTime? = nil34 var previewName: String = String(localized: "Preview")3536 @Environment(AppModel.self) private var model37 @State private var previewRun: MissionRun?3839 var body: some View {40 VStack(spacing: 18) {41 soundCard42 progressiveCard43 missionsCard44 snoozeCard45 confirmationCard46 timelineCard47 }48 .fullScreenCover(item: $previewRun) { run in49 MissionRunnerView(run: run, onClose: { previewRun = nil })50 }51 .onDisappear { SoundPlayer.shared.stopPreview() }52 }5354 // MARK: Sound55 private var soundCard: some View {56 EditorCard(title: "Alarm sound", symbol: "speaker.wave.2.fill", tint: Theme.gold) {57 SoundStrip(sounds: SoundCatalog.alarmSounds, selection: $soundID, idPrefix: "editor")58 Text("Played by the system alarm, at the iPhone's alarm volume.").font(.orixCaption).foregroundStyle(Theme.textSecondary)59 }60 }6162 // MARK: Progressive wake63 private var progressiveCard: some View {64 EditorCard(title: "Progressive wake", symbol: "sunrise.fill", tint: Theme.sunrise) {65 ToggleRow(title: "Gentle pre-alarm", detail: "Light and a soundscape rise slowly before the alarm.", isOn: $scenario.progressive.isEnabled.animation(.smooth))66 .accessibilityIdentifier("editor.progressive")67 if scenario.progressive.isEnabled {68 InfoNote(symbol: "iphone.gen3.radiowaves.left.and.right", tint: Theme.gold,69 text: "The pre-alarm stages run only while ORIX Alarm stays open in Nightstand mode. iOS does not allow an app to start sound or light the screen by itself before the system alarm. The system alarm itself always rings.")70 VStack(alignment: .leading, spacing: 4) {71 HStack {72 Text("Lead time").font(.orixBody)73 Spacer()74 Text("\(scenario.progressive.leadMinutes) min").font(.system(.body, design: .rounded, weight: .semibold)).monospacedDigit().foregroundStyle(Theme.gold)75 }76 Slider(value: Binding(get: { Double(scenario.progressive.leadMinutes) }, set: { scenario.progressive.leadMinutes = Int($0.rounded()) }), in: 5...60, step: 5) {77 Text("Lead time")78 } minimumValueLabel: {79 Text("5").font(.orixCaption).foregroundStyle(Theme.textTertiary)80 } maximumValueLabel: {81 Text("60").font(.orixCaption).foregroundStyle(Theme.textTertiary)82 }83 .tint(Theme.sunrise)84 .accessibilityValue("\(scenario.progressive.leadMinutes) minutes before the alarm")85 .accessibilityIdentifier("editor.leadTime")86 }87 Divider().overlay(Theme.hairline)88 VStack(alignment: .leading, spacing: 10) {89 Text("Soundscape").font(.orixBody)90 SoundStrip(sounds: SoundCatalog.soundscapes, selection: $scenario.progressive.soundscapeID, idPrefix: "editor.scape")91 }92 ToggleRow(title: "Sunrise light", detail: "The Nightstand screen warms from night blue to daylight.", isOn: $scenario.progressive.sunrise)93 .accessibilityIdentifier("editor.sunrise")94 Divider().overlay(Theme.hairline)95 VStack(alignment: .leading, spacing: 10) {96 Text("Intensity curve").font(.orixBody)97 IntensityCurveEditor(curve: $scenario.progressive.curve, leadMinutes: scenario.progressive.leadMinutes)98 }99 }100 }101 }102103 // MARK: Missions104 private var missionsCard: some View {105 EditorCard(title: scenario.missions.count > 1 ? "Morning Combo" : "Wake missions", symbol: "puzzlepiece.extension.fill", tint: Theme.violet) {106 MissionStepsEditor(steps: $scenario.missions)107 if !scenario.missions.isEmpty {108 Button {109 SoundPlayer.shared.stopPreview()110 previewRun = model.makePreviewRun(scenario.missions, name: previewName)111 } label: { Label("Preview missions", systemImage: "play.fill") }112 .buttonStyle(SecondaryButtonStyle())113 .accessibilityHint("Plays the sequence now. Nothing is recorded.")114 .accessibilityIdentifier("editor.previewMissions")115 }116 }117 }118119 // MARK: Snooze120 private var snoozeCard: some View {121 EditorCard(title: "Snooze rules", symbol: "zzz", tint: Theme.azure) {122 ToggleRow(title: "Allow snooze", isOn: $scenario.snooze.isAllowed.animation(.smooth))123 .accessibilityIdentifier("editor.snooze")124 if scenario.snooze.isAllowed {125 HStack(spacing: 8) {126 Chip(title: String(localized: "Fixed"), symbol: "equal", isSelected: scenario.snooze.mode == .fixed) { setMode(.fixed) }127 .accessibilityIdentifier("editor.snooze.fixed")128 Chip(title: String(localized: "Decreasing"), symbol: "chart.line.downtrend.xyaxis", isSelected: scenario.snooze.mode == .decreasing) { setMode(.decreasing) }129 .accessibilityIdentifier("editor.snooze.decreasing")130 Spacer(minLength: 0)131 }132 ValueStepper(title: scenario.snooze.mode == .fixed ? "Duration" : "First snooze", value: String(localized: "\(scenario.snooze.baseMinutes) min"),133 canDecrement: scenario.snooze.baseMinutes > 1, canIncrement: scenario.snooze.baseMinutes < 30,134 onDecrement: { scenario.snooze.baseMinutes -= 1 }, onIncrement: { scenario.snooze.baseMinutes += 1 })135 .accessibilityIdentifier("editor.snooze.base")136 ValueStepper(title: "Maximum snoozes", value: "\(scenario.snooze.maxCount)",137 canDecrement: scenario.snooze.maxCount > 1, canIncrement: scenario.snooze.maxCount < 10,138 onDecrement: { scenario.snooze.maxCount -= 1 }, onIncrement: { scenario.snooze.maxCount += 1 })139 .accessibilityIdentifier("editor.snooze.max")140 ToggleRow(title: "Raise mission difficulty after each snooze", detail: "One level harder per snooze, up to Expert.", isOn: $scenario.snooze.escalatesDifficulty)141 .accessibilityIdentifier("editor.snooze.escalate")142 snoozePreview143 }144 InfoNote(text: "iOS always lets you stop an alarm from the lock screen. Snooze rules and missions are a commitment you make with yourself, not a lock.")145 }146 }147148 private var snoozePreview: some View {149 let minutes = scenario.snooze.durationsInMinutes150 return VStack(alignment: .leading, spacing: 8) {151 Text("Actual snoozes").font(.system(size: 11, weight: .bold, design: .rounded)).kerning(0.8).foregroundStyle(Theme.textTertiary)152 FlowLayout(spacing: 6) {153 ForEach(Array(minutes.enumerated()), id: \.offset) { index, value in154 HStack(spacing: 4) {155 Text("\(index + 1)").font(.system(size: 9, weight: .heavy, design: .rounded)).foregroundStyle(Theme.midnight)156 .frame(width: 15, height: 15).background(Theme.azure, in: Circle())157 Text("\(value) min").font(.system(size: 13, weight: .semibold, design: .rounded)).monospacedDigit()158 }159 .padding(.leading, 5).padding(.trailing, 9).padding(.vertical, 5)160 .background(Theme.azure.opacity(0.14), in: Capsule())161 }162 }163 Text("\(scenario.snooze.durationsText) — up to \(minutes.reduce(0, +)) min after the alarm time.")164 .font(.orixCaption).foregroundStyle(Theme.textSecondary).fixedSize(horizontal: false, vertical: true)165 .accessibilityIdentifier("editor.snooze.preview")166 }167 .accessibilityElement(children: .ignore)168 .accessibilityLabel("Actual snoozes: \(scenario.snooze.durationsText). Up to \(minutes.reduce(0, +)) minutes after the alarm time.")169 }170171 private func setMode(_ mode: SnoozePolicy.Mode) {172 guard scenario.snooze.mode != mode else { return }173 Haptics.play(.selection)174 withAnimation(.smooth) { scenario.snooze.mode = mode }175 }176177 // MARK: Confirmation and haptics178 private var confirmationCard: some View {179 EditorCard(title: "After the alarm", symbol: "checkmark.seal.fill", tint: Theme.success) {180 ToggleRow(title: "Wake confirmation",181 detail: "Ask for an explicit “I'm up” at the end. Only this confirmation counts as a wake-up in your statistics; stopping the alarm does not.",182 isOn: $scenario.requiresConfirmation)183 .accessibilityIdentifier("editor.confirmation")184 Divider().overlay(Theme.hairline)185 ToggleRow(title: "Haptics", detail: "Vibrations during missions and the gentle wake.", isOn: $scenario.haptics)186 .accessibilityIdentifier("editor.haptics")187 }188 }189190 // MARK: Timeline191 private var timelineCard: some View {192 EditorCard(title: "Your morning", symbol: "timeline.selection", tint: Theme.gold) {193 ScenarioTimelineView(scenario: scenario, soundID: soundID, time: time, use24h: model.settings.use24HourClock,194 qrLabel: { model.qrTag($0)?.label })195 }196 }197}198