Repository Public License
1// ORIX Alarm2// File: App/Features/Missions/PuzzleMissionSupport.swift3// Description: Shared pieces of the puzzle missions (math, memory, pattern, colours, words): feedback banner, round progress, shake effect, pauses.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import AlarmCore8import SwiftUI910/// Result of the last attempt, shown with a colour, a symbol and a text.11enum PuzzleFeedback: Equatable {12 case none, correct, wrong1314 var tint: Color {15 switch self { case .none: Theme.textSecondary; case .correct: Theme.success; case .wrong: Theme.danger }16 }17 var symbol: String? {18 switch self { case .none: nil; case .correct: "checkmark.circle.fill"; case .wrong: "xmark.circle.fill" }19 }2021 /// Haptic, sound effect and VoiceOver announcement for a result.22 func signal(announcement: String) {23 switch self {24 case .none: return25 case .correct: Haptics.play(.success); SoundFX.play(.correct)26 case .wrong: Haptics.play(.failure); SoundFX.play(.wrong)27 }28 AccessibilityNotification.Announcement(announcement).post()29 }30}3132/// One status line under a puzzle. Its height never changes, so the layout does not jump when feedback appears.33struct PuzzleStatusLine: View {34 let text: String35 var feedback: PuzzleFeedback = .none36 var body: some View {37 HStack(spacing: 8) {38 if let symbol = feedback.symbol { Image(systemName: symbol).imageScale(.large).transition(.scale.combined(with: .opacity)) }39 Text(text).font(.system(.headline, design: .rounded, weight: .semibold)).monospacedDigit()40 .multilineTextAlignment(.center).lineLimit(2).minimumScaleFactor(0.8)41 }42 .foregroundStyle(feedback == .none ? Theme.textPrimary.opacity(0.86) : feedback.tint)43 .frame(maxWidth: .infinity, minHeight: 44)44 .contentTransition(.opacity)45 .accessibilityElement(children: .combine)46 }47}4849/// Segmented progress across the rounds of one mission.50struct PuzzleRoundProgress: View {51 /// Zero-based index of the round being played.52 let current: Int53 let total: Int54 /// `true` once the current round has just been solved.55 var currentSolved = false56 var body: some View {57 HStack(spacing: 5) {58 ForEach(0..<max(total, 1), id: \.self) { index in59 Capsule()60 .fill(index < current || (index == current && currentSolved) ? AnyShapeStyle(Theme.success)61 : (index == current ? AnyShapeStyle(Theme.sunriseGradient) : AnyShapeStyle(Theme.surfaceRaised)))62 .frame(height: 6)63 }64 }65 .accessibilityElement()66 .accessibilityLabel("Progress")67 .accessibilityValue("\(min(current + (currentSolved ? 1 : 0), total)) of \(total) solved")68 }69}7071/// Horizontal shake used on a wrong answer (skipped when Reduce Motion is on).72nonisolated struct PuzzleShake: GeometryEffect {73 var travel: CGFloat = 974 var animatableData: CGFloat75 init(trigger: Int, enabled: Bool = true) { animatableData = enabled ? CGFloat(trigger) : 0 }76 func effectValue(size: CGSize) -> ProjectionTransform {77 ProjectionTransform(CGAffineTransform(translationX: travel * sin(animatableData * .pi * 4), y: 0))78 }79}8081/// Small secondary action under a puzzle (replay, clear, shuffle…): at least 52 pt tall.82struct PuzzleAuxButton: View {83 let title: LocalizedStringKey84 let symbol: String85 var isEnabled = true86 let action: () -> Void87 var body: some View {88 Button(action: action) {89 Label(title, systemImage: symbol).font(.system(.subheadline, design: .rounded, weight: .semibold))90 .lineLimit(1).minimumScaleFactor(0.75)91 .foregroundStyle(Theme.textPrimary)92 .padding(.horizontal, 12)93 .frame(maxWidth: .infinity, minHeight: 52)94 .background(Theme.surfaceRaised, in: Capsule())95 .overlay(Capsule().strokeBorder(Theme.hairline))96 }97 .buttonStyle(.plain)98 .disabled(!isEnabled)99 .opacity(isEnabled ? 1 : 0.4)100 }101}102103enum PuzzleTiming {104 /// Suspends for a while. Returns `false` when the surrounding task was cancelled (view gone, or playback restarted).105 static func pause(_ seconds: Double) async -> Bool {106 do { try await Task.sleep(for: .seconds(seconds)); return true } catch { return false }107 }108}109110extension ResolvedMission {111 /// Number of puzzles to solve, never less than one.112 var puzzleRounds: Int { max(rounds, 1) }113}114