Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : QYVORA7// File : App/Sources/AppModel.swift8// Module : Sources9// Repository : https://git.spboucher.ai/qyvora10// Project Website : Not specified11// Created : 2026-12-0412// Last Modified : 2026-12-0413//14// Author : Simon-Pierre Boucher15// Position : Professor of Finance16// Institution : Université du Québec en Outaouais (UQO)17// Organization : SP Boucher18// Website : https://spboucher.ai19// Git : https://git.spboucher.ai20//21// Preferred Contact: contact@spboucher.ai22// Development : dev@spboucher.ai23// Support : support@spboucher.ai24// Security/Admin : admin@spboucher.ai25//26// Copyright (c) 202627// Simon-Pierre Boucher28// All rights reserved where applicable. This file is governed by the29// applicable project license, copyright notices and relevant law.30// Existing third-party rights and notices remain unaffected.31//32// License : None specified (no license file in this repository)33//34// Do not embed credentials, private keys, access tokens,35// certificates or confidential personal information.36//37// =============================================================================38// END OF SP BOUCHER SOURCE HEADER39// =============================================================================4041import SwiftUI42import QyvoraCrypto43import QyvoraNetworking4445/// Where the application sends the user once the engine is trusted.46///47/// PLACEHOLDER ROUTING: until the wallet service exists, nothing decides between these cases from real state.48/// The wallet developer sets `AppModel.route` from the vault (no wallet → `.onboarding`, sealed vault →49/// `.locked`, unlocked → `.main`).50enum AppRoute: String, Equatable, Sendable {51 case onboarding52 case locked53 case main54}5556/// Start-up phases. `failed` is terminal: the application offers nothing else.57enum BootPhase: Equatable, Sendable {58 case booting59 case selfTesting60 case failed61 case ready62}6364/// Outcome of the last engine self-test, shown on the cryptographic information screen.65struct SelfTestRecord: Equatable, Sendable {66 let passed: Bool67 let date: Date68 let duration: Duration69}7071enum AppAppearance: String, CaseIterable, Identifiable, Sendable {72 case system, light, dark7374 var id: String { rawValue }7576 var title: String {77 switch self {78 case .system: return "System"79 case .light: return "Light"80 case .dark: return "Dark"81 }82 }8384 var detail: String {85 switch self {86 case .system: return "Follows the appearance of the device."87 case .light: return "Always light."88 case .dark: return "Always dark."89 }90 }9192 var systemImage: String {93 switch self {94 case .system: return "circle.lefthalf.filled"95 case .light: return "sun.max"96 case .dark: return "moon"97 }98 }99100 var colorScheme: ColorScheme? {101 switch self {102 case .system: return nil103 case .light: return .light104 case .dark: return .dark105 }106 }107108 static let storageKey = "appearance"109}110111/// Application-level state that exists without a wallet: boot phase, route, endpoint configuration.112@MainActor113@Observable114final class AppModel {115 private(set) var phase: BootPhase = .booting116 var route: AppRoute = .onboarding117 private(set) var selfTest: SelfTestRecord?118 private(set) var selfTestRunning = false119120 /// User-editable endpoints, persisted as JSON in the app's defaults (they are settings, not secrets).121 /// The wallet service reads this value when it builds its network clients.122 var endpoints: EndpointConfiguration {123 didSet {124 guard endpoints != oldValue else { return }125 if let data = try? JSONEncoder().encode(endpoints) { defaults.set(data, forKey: Self.endpointsKey) }126 }127 }128129 private let defaults: UserDefaults130 private let selfTestRun: @Sendable () -> Bool131 static let endpointsKey = "endpointConfiguration.v1"132133 init(defaults: UserDefaults = .standard, selfTestRun: @escaping @Sendable () -> Bool = { QyvoraEngine.selfTest() }) {134 self.defaults = defaults135 self.selfTestRun = selfTestRun136 if let data = defaults.data(forKey: Self.endpointsKey), let stored = try? JSONDecoder().decode(EndpointConfiguration.self, from: data) {137 endpoints = stored138 } else {139 endpoints = .default140 }141 }142143 /// Runs the known-answer self-test off the main actor. A failure blocks the application.144 func boot() async {145 guard phase == .booting else { return }146 phase = .selfTesting147 phase = await runSelfTest() ? .ready : .failed148 }149150 /// Runs the self-test again (cryptographic information screen). A failure also blocks the application.151 @discardableResult152 func runSelfTest() async -> Bool {153 guard !selfTestRunning else { return selfTest?.passed ?? false }154 selfTestRunning = true155 defer { selfTestRunning = false }156 let run = selfTestRun157 let clock = ContinuousClock()158 let start = clock.now159 let passed = await Task.detached(priority: .userInitiated) { run() }.value160 selfTest = SelfTestRecord(passed: passed, date: Date(), duration: clock.now - start)161 if !passed { phase = .failed }162 return passed163 }164}165