Repository Public License
1// ORIX Alarm2// File: App/Features/Editor/ScenarioTimelineView.swift3// Description: Live visual timeline of a wake scenario (gentle wake, main alarm, snooze branch, missions, confirmation) and its compact strip variant.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import AlarmCore8import SwiftUI910extension SnoozePolicy {11 /// Actual snooze durations in minutes, in order.12 var durationsInMinutes: [Int] {13 guard isAllowed else { return [] }14 return (1...maxCount).compactMap { duration(forSnooze: $0).map { Int(($0 / 60).rounded()) } }15 }16 /// "9 min, 6 min, 4 min".17 var durationsText: String { durationsInMinutes.map { String(localized: "\($0) min") }.joined(separator: ", ") }18}1920extension MissionStep {21 /// Short settings summary: "Hard · 5 rounds", "40 steps", "Tag: Bathroom".22 func settingsSummary(qrLabel: String?) -> String {23 switch kind {24 case .steps: return String(localized: "\(stepGoal) steps")25 case .qrEscape: return qrLabel.map { String(localized: "Tag: \($0)") } ?? String(localized: "No QR tag chosen")26 case .wordScramble:27 return "\(difficulty.title) · \(roundsText) · \(language == .english ? String(localized: "English") : String(localized: "French"))"28 default: return "\(difficulty.title) · \(roundsText)"29 }30 }31 private var roundsText: String { rounds == 1 ? String(localized: "1 round") : String(localized: "\(rounds) rounds") }32}3334extension WakeScenario {35 /// One-line description used by lists and VoiceOver.36 var summaryText: String {37 var parts: [String] = []38 if progressive.isEnabled { parts.append(String(localized: "Gentle wake \(progressive.leadMinutes) min")) }39 parts.append(missions.isEmpty ? String(localized: "No mission") : (missions.count == 1 ? String(localized: "1 mission") : String(localized: "\(missions.count) missions")))40 parts.append(snooze.isAllowed ? String(localized: "Snooze ×\(snooze.maxCount)") : String(localized: "No snooze"))41 if requiresConfirmation { parts.append(String(localized: "Confirmation")) }42 return parts.joined(separator: " · ")43 }44}4546/// Full vertical timeline. `time` is the alarm time when known (alarm editor); without it the labels are relative.47struct ScenarioTimelineView: View {48 let scenario: WakeScenario49 let soundID: String50 var time: ClockTime? = nil51 var use24h: Bool? = nil52 var qrLabel: (UUID?) -> String? = { _ in nil }5354 private struct Item: Identifiable {55 enum Style { case stage, alarm, branch, mission, finish, muted }56 var id: String57 var style: Style58 var label: String59 var title: String60 var detail: String61 var symbol: String62 var tint: Color63 var badge: String? = nil64 var curve: IntensityCurve? = nil65 }6667 private var items: [Item] {68 var out: [Item] = []69 let p = scenario.progressive70 func clock(_ offset: Int, fallback: String) -> String { time.map { Fmt.time($0.adding(minutes: offset), use24h: use24h).digits } ?? fallback }71 if p.isEnabled {72 out.append(Item(id: "prep", style: .stage, label: clock(-p.leadMinutes, fallback: "−\(p.leadMinutes) min"), title: String(localized: "Preparation"),73 detail: String(localized: "Nightstand mode starts the pre-alarm stage, at zero intensity."), symbol: "moon.stars.fill", tint: Theme.violet,74 badge: String(localized: "Nightstand mode only")))75 var what = [SoundCatalog.soundscape(p.soundscapeID).title]76 if p.sunrise { what.append(String(localized: "sunrise light")) }77 out.append(Item(id: "gentle", style: .stage, label: String(localized: "\(p.leadMinutes) min"), title: String(localized: "Gentle wake"),78 detail: String(localized: "\(what.joined(separator: " + ")) rise along the \(p.curve.title.lowercased()) curve."),79 symbol: "sunrise.fill", tint: Theme.sunrise, badge: String(localized: "Nightstand mode only"), curve: p.curve))80 } else {81 out.append(Item(id: "gentle-off", style: .muted, label: "", title: String(localized: "No gentle wake"),82 detail: String(localized: "The morning starts directly with the alarm."), symbol: "sunrise", tint: Theme.textTertiary))83 }84 out.append(Item(id: "alarm", style: .alarm, label: clock(0, fallback: String(localized: "Alarm")), title: String(localized: "Main alarm"),85 detail: String(localized: "\(SoundCatalog.alarmSound(soundID).title) · system alarm, rings even when the app is closed."),86 symbol: "alarm.waves.left.and.right.fill", tint: Theme.gold, badge: String(localized: "Always rings")))87 if scenario.snooze.isAllowed {88 let count = scenario.snooze.maxCount89 out.append(Item(id: "snooze", style: .branch, label: String(localized: "×\(count)"), title: String(localized: "Snooze branch"),90 detail: String(localized: "\(scenario.snooze.durationsText), then the alarm rings again.")91 + (scenario.snooze.escalatesDifficulty ? " " + String(localized: "Missions get one level harder per snooze.") : ""),92 symbol: "arrow.uturn.backward", tint: Theme.azure))93 } else {94 out.append(Item(id: "snooze-off", style: .muted, label: "", title: String(localized: "No snooze"),95 detail: String(localized: "The alarm offers no snooze button."), symbol: "zzz", tint: Theme.textTertiary))96 }97 if scenario.missions.isEmpty {98 out.append(Item(id: "missions-off", style: .muted, label: "", title: String(localized: "No wake mission"),99 detail: String(localized: "Stopping the alarm ends the sequence."), symbol: "puzzlepiece.extension", tint: Theme.textTertiary))100 }101 for (index, step) in scenario.missions.enumerated() {102 out.append(Item(id: step.id.uuidString, style: .mission, label: String(localized: "Step \(index + 1)"), title: step.kind.title,103 detail: step.settingsSummary(qrLabel: qrLabel(step.qrTagID)), symbol: step.kind.symbol, tint: step.kind.tint))104 }105 if scenario.requiresConfirmation {106 out.append(Item(id: "confirm", style: .finish, label: String(localized: "End"), title: String(localized: "Wake confirmation"),107 detail: String(localized: "You tap “I'm up”. Only this is counted as a wake-up."), symbol: "checkmark.seal.fill", tint: Theme.success))108 } else {109 out.append(Item(id: "confirm-off", style: .muted, label: String(localized: "End"), title: String(localized: "No confirmation"),110 detail: String(localized: "No wake-up is recorded in statistics for this alarm."), symbol: "checkmark.seal", tint: Theme.textTertiary))111 }112 return out113 }114115 var body: some View {116 let items = items117 VStack(spacing: 0) {118 ForEach(Array(items.enumerated()), id: \.element.id) { index, item in119 row(item, isFirst: index == 0, isLast: index == items.count - 1)120 .transition(.opacity.combined(with: .move(edge: .leading)))121 }122 }123 .animation(.smooth(duration: 0.3), value: items.map(\.id))124 .accessibilityElement(children: .contain).accessibilityIdentifier("timeline.full")125 }126127 private func row(_ item: Item, isFirst: Bool, isLast: Bool) -> some View {128 let muted = item.style == .muted129 let branch = item.style == .branch130 return HStack(alignment: .top, spacing: 10) {131 Text(item.label).font(.system(size: 12, weight: .semibold, design: .rounded)).monospacedDigit()132 .foregroundStyle(item.style == .alarm ? Theme.gold : Theme.textSecondary)133 .lineLimit(1).minimumScaleFactor(0.7)134 .frame(width: 50, alignment: .trailing).padding(.top, 9)135 ZStack(alignment: .top) {136 // Rail.137 VStack(spacing: 0) {138 Rectangle().fill(isFirst ? AnyShapeStyle(.clear) : AnyShapeStyle(Theme.hairline.opacity(2))).frame(width: 2, height: 16)139 Rectangle().fill(isLast ? AnyShapeStyle(.clear) : AnyShapeStyle(Theme.hairline.opacity(2))).frame(width: 2).frame(maxHeight: .infinity)140 }141 node(item, muted: muted, branch: branch)142 }143 .frame(width: 34)144 VStack(alignment: .leading, spacing: 5) {145 HStack(alignment: .firstTextBaseline, spacing: 8) {146 Text(item.title).font(.system(.subheadline, design: .rounded, weight: .semibold))147 .foregroundStyle(muted ? Theme.textSecondary : Theme.textPrimary)148 if branch { Image(systemName: "arrow.triangle.branch").font(.caption2).foregroundStyle(item.tint).accessibilityHidden(true) }149 }150 Text(item.detail).font(.orixCaption).foregroundStyle(muted ? Theme.textTertiary : Theme.textSecondary).fixedSize(horizontal: false, vertical: true)151 if let curve = item.curve {152 CurveSparkline(curve: curve, lineWidth: 2.5).frame(height: 46)153 .padding(8)154 .background(Theme.midnight.opacity(0.4), in: RoundedRectangle(cornerRadius: 12, style: .continuous))155 .overlay(RoundedRectangle(cornerRadius: 12, style: .continuous).strokeBorder(Theme.hairline))156 }157 if let badge = item.badge {158 Label(badge, systemImage: item.style == .alarm ? "checkmark.shield.fill" : "iphone.gen3.radiowaves.left.and.right")159 .font(.system(size: 10, weight: .bold, design: .rounded)).foregroundStyle(item.tint)160 .padding(.horizontal, 8).padding(.vertical, 4)161 .background(item.tint.opacity(0.14), in: Capsule())162 }163 }164 .padding(.top, 7).padding(.bottom, 16)165 .padding(.leading, branch ? 10 : 0)166 .overlay(alignment: .leading) {167 if branch { Rectangle().fill(item.tint.opacity(0.5)).frame(width: 2).padding(.top, 8).padding(.bottom, 16).offset(x: -1) }168 }169 Spacer(minLength: 0)170 }171 .accessibilityElement(children: .ignore)172 .accessibilityLabel(Text([item.label, item.title, item.detail, item.badge ?? ""].filter { !$0.isEmpty }.joined(separator: ", ")))173 }174175 private func node(_ item: Item, muted: Bool, branch: Bool) -> some View {176 let size: CGFloat = item.style == .alarm ? 34 : (branch || muted ? 24 : 30)177 return Image(systemName: item.symbol).font(.system(size: size * 0.42, weight: .bold))178 .foregroundStyle(item.style == .alarm ? Theme.midnight : (muted ? Theme.textTertiary : item.tint))179 .frame(width: size, height: size)180 .background {181 if item.style == .alarm { Circle().fill(Theme.sunriseGradient) }182 else { Circle().fill(Theme.midnight).overlay(Circle().fill(item.tint.opacity(muted ? 0.05 : 0.18))) }183 }184 .overlay(Circle().strokeBorder(item.style == .alarm ? .clear : item.tint.opacity(muted ? 0.3 : 0.7),185 style: StrokeStyle(lineWidth: 1.5, dash: branch || muted ? [3, 3] : [])))186 .shadow(color: item.style == .alarm ? Theme.sunrise.opacity(0.6) : .clear, radius: 8)187 .padding(.top, (34 - size) / 2)188 .accessibilityHidden(true)189 }190}191192/// Compact horizontal strip: curve, alarm bell, mission symbols, snooze and confirmation markers.193struct ScenarioMiniTimeline: View {194 let scenario: WakeScenario195196 var body: some View {197 HStack(spacing: 0) {198 if scenario.progressive.isEnabled {199 CurveSparkline(curve: scenario.progressive.curve, lineWidth: 2).frame(width: 46, height: 24)200 connector201 }202 dot("alarm.fill", tint: Theme.gold, filled: true)203 if scenario.snooze.isAllowed {204 connector205 dot("zzz", tint: Theme.azure, dashed: true)206 }207 ForEach(scenario.missions) { step in208 connector209 dot(step.kind.symbol, tint: step.kind.tint)210 }211 if scenario.requiresConfirmation {212 connector213 dot("checkmark", tint: Theme.success)214 }215 Spacer(minLength: 0)216 }217 .frame(height: 28)218 .accessibilityElement(children: .ignore)219 .accessibilityLabel(Text(scenario.summaryText))220 }221222 private var connector: some View {223 Rectangle().fill(Theme.hairline.opacity(2.2)).frame(height: 2).frame(minWidth: 5, maxWidth: 14)224 }225226 private func dot(_ symbol: String, tint: Color, filled: Bool = false, dashed: Bool = false) -> some View {227 Image(systemName: symbol).font(.system(size: 11, weight: .bold))228 .foregroundStyle(filled ? Theme.midnight : tint)229 .frame(width: 26, height: 26)230 .background(filled ? AnyShapeStyle(Theme.sunriseGradient) : AnyShapeStyle(tint.opacity(0.16)), in: Circle())231 .overlay(Circle().strokeBorder(filled ? .clear : tint.opacity(0.6), style: StrokeStyle(lineWidth: 1.2, dash: dashed ? [3, 2.5] : [])))232 }233}234