Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: App/RootView.swift3// Description: The main window: gate (sign-in), lock overlay, sidebar and the current section, plus the status strip that always says whether the data is live, cached or offline, and whether the signer can sign.4import SwiftUI5import SPWalletCore67struct RootView: View {8 let model: AppModel9 @Bindable var nav: Navigator1011 var body: some View {12 content13 .overlay { if model.lock.locked && model.phase != .signedOut && model.phase != .launching { LockOverlay(model: model) } }14 }1516 @ViewBuilder private var content: some View {17 switch model.phase {18 case .launching: ProgressView("Connexion au portefeuille…").controlSize(.large).frame(maxWidth: .infinity, maxHeight: .infinity)19 case .signedOut, .setupRequired: GateView(model: model)20 case .signedIn, .offlineBrowse: shell21 }22 }2324 private var shell: some View {25 NavigationSplitView {26 List(selection: Binding(get: { nav.section }, set: { if let s = $0 { nav.section = s } })) {27 ForEach(Section.groups, id: \.0) { group in28 SwiftUI.Section(group.0) {29 ForEach(group.1) { s in30 Label(s.title, systemImage: s.symbol).tag(s)31 .foregroundStyle(model.phase == .offlineBrowse && !s.readOnly ? .tertiary : .primary)32 }33 }34 }35 }36 .navigationSplitViewColumnWidth(min: 210, ideal: 232, max: 290)37 .safeAreaInset(edge: .top, spacing: 0) { SidebarHeader(model: model) }38 .safeAreaInset(edge: .bottom, spacing: 0) { SidebarFooter(model: model) }39 } detail: {40 // the strip is a top safe-area inset, NOT a VStack sibling of the page: a VStack here is laid out at twice the41 // window's height on macOS 27 and the top of the sidebar and of every page is clipped42 detail43 .safeAreaInset(edge: .top, spacing: 0) { StatusStrip(model: model) }44 }45 // alerts are watched (and notified) from sign-in, not only once the Alerts screen has been opened46 .task(id: model.phase) { if model.phase == .signedIn { AlertsCenter.shared(model).start() } else { AlertsCenter.shared(model).stop() } }47 .toolbar {48 ToolbarItem(placement: .primaryAction) {49 Button { Task { await model.refreshAll() } } label: { Label("Actualiser", systemImage: "arrow.clockwise") }50 .disabled(model.phase != .signedIn).help("Relire les soldes, l’activité et l’état du signataire")51 }52 }53 }5455 @ViewBuilder private var detail: some View {56 if model.phase == .offlineBrowse && !nav.section.readOnly {57 EmptyState(symbol: "wifi.slash", title: "Indisponible hors connexion", detail: "Cette section prépare ou autorise des opérations : elle exige une session active et un portefeuille joignable.")58 } else {59 switch nav.section {60 case .dashboard: DashboardView(model: model, nav: nav)61 case .portfolio: PortfolioView(model: model, nav: nav)62 case .assets: AssetsView(model: model, nav: nav)63 case .send: SendView(model: model, nav: nav)64 case .receive: ReceiveView(model: model)65 case .swap: SwapView(model: model)66 case .bridge: BridgeView(model: model)67 case .nfts: NFTsView(model: model)68 case .defi: DeFiView(model: model, nav: nav)69 case .futures: FuturesView(model: model)70 case .agents: TradingAgentsView(model: model)71 case .alerts: AlertsView(model: model)72 case .activity: ActivityView(model: model)73 case .research: ResearchView(model: model)74 case .security: SecurityView(model: model)75 case .settings: SettingsView(model: model)76 }77 }78 }79}8081/// Always visible under the title bar when something limits what the screen shows: offline, cached data, halt, unreachable signer.82/// Kept deliberately plain (fixed line count, no flexible-height text): this view sits in the detail column's top safe-area inset,83/// where a view of unbounded ideal height makes macOS 27 lay the whole split view out at twice the window's height.84struct StatusStrip: View {85 let model: AppModel8687 private struct Item: Identifiable { let id: String; let kind: Banner.Kind; let text: String; var action: String?; var run: (@MainActor () async -> Void)? }8889 private var items: [Item] {90 var out: [Item] = []91 if model.phase == .offlineBrowse {92 out.append(Item(id: "browse", kind: .warning, text: "Consultation hors connexion — données du \(Fmt.date(model.portfolio?.fetchedAt)). Soldes et prix possiblement périmés. Aucune opération possible.", action: "Se reconnecter", run: { [model] in await model.leaveOfflineBrowse() }))93 } else if model.isOffline {94 out.append(Item(id: "offline", kind: .warning, text: "Portefeuille injoignable — dernières données synchronisées (\(Fmt.relative(model.portfolio?.fetchedAt))). Soldes et prix possiblement périmés ; aucune opération possible.", action: "Réessayer", run: { [model] in await model.refreshAll() }))95 }96 if model.portfolio?.value.halted == true {97 out.append(Item(id: "halt", kind: .danger, text: "ARRÊT D’URGENCE ACTIF — le signataire refuse tout ce qui déplace des fonds ou ajoute du risque. Levée : opérateur sur la machine du signataire."))98 } else if let s = model.portfolio?.value.signer, !s.reachable, !model.isOffline {99 out.append(Item(id: "signer", kind: .warning, text: "Signataire isolé injoignable — soldes lus par l’API aux dernières adresses connues ; rien ne peut être signé pour l’instant."))100 }101 if AppEnvironment.isDevelopment {102 out.append(Item(id: "dev", kind: .info, text: "VARIANTE DE DÉVELOPPEMENT — pile locale jetable (\(model.configuration.originHeader)), clés de test, passkey logicielle. Aucun fonds réel."))103 }104 return out105 }106107 var body: some View {108 let list = items109 VStack(spacing: 0) {110 ForEach(list) { item in111 HStack(spacing: 10) {112 Image(systemName: item.kind.symbol).foregroundStyle(item.kind.color)113 Text(item.text).font(.callout).lineLimit(2).help(item.text)114 Spacer(minLength: 8)115 if let a = item.action, let run = item.run { Button(a) { Task { await run() } }.controlSize(.small) }116 }117 .padding(.horizontal, 16).frame(height: 36).frame(maxWidth: .infinity)118 .background(item.kind.color.opacity(0.13))119 }120 }121 .frame(height: CGFloat(list.count) * 36)122 .background(.bar)123 }124}125126/// The wallet's identity at the top of the sidebar (fixed height: see the note on `StatusStrip`).127struct SidebarHeader: View {128 let model: AppModel129 var body: some View {130 HStack(spacing: 10) {131 Image(nsImage: NSApp.applicationIconImage).resizable().frame(width: 34, height: 34)132 VStack(alignment: .leading, spacing: 0) {133 Text("SP Wallet").font(.system(.headline, design: .rounded).weight(.bold))134 Text(model.accounts?.value.keyId ?? (model.configuration.origin.host ?? "")).font(.caption2).foregroundStyle(.secondary).lineLimit(1).truncationMode(.middle)135 }136 Spacer(minLength: 0)137 }138 .padding(.horizontal, 14).frame(height: 52)139 }140}141142struct SidebarFooter: View {143 let model: AppModel144 var body: some View {145 let (level, text) = SecurityIndicator.assess(model)146 HStack(spacing: 8) {147 Image(systemName: level.symbol).foregroundStyle(level.color)148 VStack(alignment: .leading, spacing: 0) {149 Text(text).font(.caption.weight(.medium)).lineLimit(1)150 Text(model.configuration.origin.host ?? "").font(.caption2).foregroundStyle(.secondary).lineLimit(1)151 }152 Spacer(minLength: 0)153 }154 .padding(.horizontal, 10).frame(height: 44)155 .background(level.color.opacity(0.10), in: RoundedRectangle(cornerRadius: 11, style: .continuous))156 .padding(.horizontal, 10).padding(.bottom, 10).frame(height: 54)157 .help("Les clés privées ne quittent jamais la machine du signataire ; ce Mac n’en détient aucune.")158 }159}160161struct LockOverlay: View {162 let model: AppModel163 var body: some View {164 ZStack {165 Rectangle().fill(.regularMaterial).ignoresSafeArea()166 VStack(spacing: 14) {167 Image(systemName: "lock.fill").font(.system(size: 42)).foregroundStyle(.secondary)168 Text("SP Wallet est verrouillé").font(.title2.weight(.semibold))169 Button("Déverrouiller avec \(AppLock.biometryName)") { Task { await model.lock.unlock() } }.controlSize(.large).keyboardShortcut(.defaultAction)170 if let e = model.lock.lastError { Text(e).foregroundStyle(Theme.negative).font(.callout) }171 Text("Le déverrouillage affiche les écrans ; il n’autorise aucune opération.").font(.caption).foregroundStyle(.secondary)172 }173 }174 .task { await model.lock.unlock() }175 }176}177