Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Sources/Shared/Design/Components/StatusComponents.swift3// Description: Quiet status components — sync indicator (driven by the real engine state, never a blocking spinner), empty / loading (skeleton) /4// error states, and the toast with at most one action (Undo).5import SwiftUI67// MARK: - sync indicator89/// Nothing when synced; « Hors ligne · 3 en attente »; « Synchronisation » only when it lasts; an error with « Réessayer ».10struct SyncIndicator: View {11 let status: SyncStatusModel12 var retry: (() -> Void)?1314 var body: some View {15 let display = status.display16 Group {17 switch display {18 case .synced: EmptyView()19 case .syncing:20 Label { Text("Synchronisation") } icon: { Image(systemName: "arrow.triangle.2.circlepath").symbolEffect(.pulse, options: .repeating) }21 .foregroundStyle(SPColor.text3)22 case .offline(let pending):23 Label(pending > 0 ? L("Hors ligne · \(pending) en attente") : L("Hors ligne"), systemImage: "wifi.slash").foregroundStyle(SPColor.warn)24 case .error(let message):25 HStack(spacing: SPSpacing.s) {26 Label(message, systemImage: "exclamationmark.triangle").foregroundStyle(SPColor.danger).lineLimit(2)27 if let retry { Button("Réessayer", action: retry).buttonStyle(.borderless).foregroundStyle(SPColor.accent) }28 }29 }30 }31 .font(.footnote)32 .spAnimation(SPMotion.fade, value: display)33 .accessibilityElement(children: .combine)34 }35}3637// MARK: - empty, loading, error3839/// One glyph, one line, at most one action.40struct SPEmptyState: View {41 let icon: String42 let title: String43 var message: String?44 var actionTitle: String?45 var action: (() -> Void)?4647 var body: some View {48 VStack(spacing: SPSpacing.l) {49 Image(systemName: icon).font(.system(size: 34, weight: .light)).symbolRenderingMode(.hierarchical).foregroundStyle(SPColor.text3)50 Text(title).font(SPFont.headline).foregroundStyle(SPColor.text2).multilineTextAlignment(.center)51 if let message { Text(message).font(SPFont.callout).foregroundStyle(SPColor.text3).multilineTextAlignment(.center).frame(maxWidth: 360) }52 if let actionTitle, let action { Button(actionTitle, action: action).buttonStyle(.bordered) }53 }54 .padding(SPSpacing.x3)55 .frame(maxWidth: .infinity, maxHeight: .infinity)56 .accessibilityElement(children: .combine)57 }58}5960/// States what happened and offers to retry.61struct SPErrorState: View {62 let message: String63 var retry: (() -> Void)?64 var body: some View {65 VStack(spacing: SPSpacing.l) {66 Image(systemName: "exclamationmark.triangle").font(.system(size: 30, weight: .light)).foregroundStyle(SPColor.danger)67 Text(message).font(SPFont.callout).foregroundStyle(SPColor.text2).multilineTextAlignment(.center).frame(maxWidth: 380)68 if let retry { Button("Réessayer", action: retry).buttonStyle(.bordered) }69 }70 .padding(SPSpacing.x3)71 .frame(maxWidth: .infinity, maxHeight: .infinity)72 }73}7475/// Placeholder rows matching the active density while the first page loads. A soft pulse, static under Reduce Motion.76struct SkeletonRows: View {77 var style = RowStyle()78 var count = 879 @Environment(\.accessibilityReduceMotion) private var reduceMotion80 @State private var dim = false8182 private func bar(_ width: CGFloat, _ height: CGFloat = 10) -> some View {83 RoundedRectangle(cornerRadius: 4, style: .continuous).fill(SPColor.surface3).frame(width: width, height: height)84 }8586 var body: some View {87 VStack(spacing: 0) {88 ForEach(0..<count, id: \.self) { n in89 HStack(alignment: .top, spacing: SPSpacing.m) {90 Color.clear.frame(width: 8, height: 8)91 if style.showAvatars { Circle().fill(SPColor.surface3).frame(width: style.density.avatarSize, height: style.density.avatarSize) }92 VStack(alignment: .leading, spacing: 7) {93 HStack { bar(CGFloat(90 + (n * 37) % 70)); Spacer(); bar(34, 8) }94 if !style.density.isSingleLine {95 bar(CGFloat(160 + (n * 53) % 90))96 if style.previewLines > 0 { bar(CGFloat(120 + (n * 71) % 120), 8) }97 }98 }99 }100 .padding(.vertical, style.density.verticalPadding).padding(.horizontal, SPSpacing.xl)101 .frame(minHeight: style.density.minRowHeight)102 }103 Spacer(minLength: 0)104 }105 .opacity(dim ? 0.55 : 1)106 .onAppear { if !reduceMotion { withAnimation(.easeInOut(duration: 0.9).repeatForever(autoreverses: true)) { dim = true } } }107 .accessibilityElement(children: .ignore)108 .accessibilityLabel(Text("Chargement"))109 }110}111112// MARK: - toast113114struct ToastView: View {115 let toast: Toast116 let dismiss: () -> Void117 private var icon: (name: String, color: SPToken)? {118 switch toast.kind {119 case .info: nil120 case .success: ("checkmark.circle.fill", SPColor.ok)121 case .warning: ("exclamationmark.triangle.fill", SPColor.warn)122 case .error: ("exclamationmark.octagon.fill", SPColor.danger)123 }124 }125 private func content(wrapping: Bool) -> some View {126 HStack(spacing: SPSpacing.l) {127 if let icon { Image(systemName: icon.name).foregroundStyle(icon.color) }128 Text(toast.message).font(SPFont.callout).foregroundStyle(SPColor.text).lineLimit(wrapping ? 4 : 1).fixedSize(horizontal: !wrapping, vertical: true)129 if let title = toast.actionTitle, let action = toast.action {130 Button(title) { dismiss(); Task { await action() } }131 .buttonStyle(.borderless).font(SPFont.callout.weight(.semibold)).foregroundStyle(SPColor.accent).fixedSize()132 #if os(iOS)133 .frame(minHeight: 44)134 #endif135 }136 }137 }138139 var body: some View {140 // hugs a short message; a long one wraps inside 460 pt141 ViewThatFits(in: .horizontal) {142 content(wrapping: false)143 content(wrapping: true).frame(maxWidth: 460, alignment: .leading)144 }145 .padding(.horizontal, SPSpacing.xl).padding(.vertical, toast.action == nil ? 10 : 6)146 .spGlass(RoundedRectangle(cornerRadius: SPRadius.dialog, style: .continuous))147 .contentShape(Rectangle())148 .onTapGesture { if toast.action == nil { dismiss() } }149 .accessibilityElement(children: .contain)150 .accessibilityAddTraits(.isStaticText)151 }152}153154/// Shows the app's current toast: bottom-leading on macOS, top on iOS / iPadOS. Auto-dismiss after 6 s, paused while the pointer is on it.155struct ToastHost: ViewModifier {156 @Bindable var app: AppModel157 @State private var hovering = false158 @Environment(\.accessibilityReduceMotion) private var reduceMotion159160 #if os(macOS)161 private let alignment = Alignment.bottomLeading162 private let edge = Edge.bottom163 #else164 private let alignment = Alignment.top165 private let edge = Edge.top166 #endif167168 func body(content: Content) -> some View {169 content.overlay(alignment: alignment) {170 if let toast = app.toast {171 ToastView(toast: toast) { app.toast = nil }172 .padding(SPSpacing.xl)173 #if os(macOS)174 .onHover { hovering = $0 }175 #endif176 .transition(reduceMotion ? .opacity : .move(edge: edge).combined(with: .opacity))177 .task(id: "\(toast.id)-\(hovering)") {178 guard !hovering else { return }179 try? await Task.sleep(for: .seconds(6))180 if !Task.isCancelled, app.toast?.id == toast.id { app.toast = nil }181 }182 .onAppear { AccessibilityNotification.Announcement(toast.message).post() }183 .id(toast.id)184 }185 }186 .spAnimation(SPMotion.quick, value: app.toast?.id)187 }188}189190extension View {191 func toastHost(_ app: AppModel) -> some View { modifier(ToastHost(app: app)) }192}193