SPB Git

Repository Public

SettingsView.swift 9.0 KB 205 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project          : QYVORA7// File             : App/Sources/SettingsView.swift8// Module           : Sources9// Repository       : https://git.spboucher.ai/qyvora10// Project Website  : Not specified11// Created          : 2026-12-0412// Last Modified    : 2026-12-0513//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// =============================================================================40import SwiftUI41import QyvoraCore42import QyvoraCrypto43import QyvoraNetworking44import QyvoraWallet45import QyvoraUI4647enum SettingsRoute: Hashable, Sendable {48    case endpoints49    case endpointList(EndpointListID)50    case appearance51    case about52}5354extension DisplayCurrency {55    var title: String {56        switch self {57        case .usd: return "USD"58        case .cad: return "CAD"59        case .eur: return "EUR"60        }61    }62}6364/// Settings root of the multi-chain wallet: Wallet (vault, identity, recovery, cold mode), Networks, Display, Engine65/// and About.66///67/// `endpoints` is the application's copy of the core endpoint configuration. The wallet profile is the source of68/// truth: every edit is saved through the wallet service by the endpoints editor and the binding follows the profile.69struct SettingsView: View {70    @Binding var endpoints: EndpointConfiguration71    @Environment(WalletStore.self) private var store72    @AppStorage(AppAppearance.storageKey) private var appearance: AppAppearance = .system73    @AppStorage(WalletPreference.hideAmounts) private var hideAmounts = false74    @State private var error: String?75    @State private var busy = false76    @State private var changeCount = 077    #if DEBUG78    @State private var debugRoute: SecurityRoute?79    #endif8081    init(endpoints: Binding<EndpointConfiguration>) {82        _endpoints = endpoints83    }8485    private var snapshot: WalletSnapshot? { store.snapshot }86    private var hasWallet: Bool { snapshot?.hasWallet == true }8788    var body: some View {89        SecurityScroll {90            if let startupError = store.startupError {91                UnavailableDataView(reason: startupError, title: "Wallet storage unavailable", compact: true)92            }93            SecInlineError(error)94            walletSection.id("wallet")95            networksSection.id("networks")96            displaySection.id("display")97            engineSection.id("engine")98            QListSection {99                SecLinkRow(title: "About QYVORA", systemImage: "info.circle", trailing: AppInfo.version, value: SettingsRoute.about)100            }101            .id("about")102        }103        .navigationTitle("Settings")104        .navigationDestination(for: SettingsRoute.self) { route in105            switch route {106            case .endpoints: EndpointsEditorView()107            case .endpointList(let id): SecEndpointListStandalone(id: id)108            case .appearance: AppearanceSettingsView()109            case .about: AboutView()110            }111        }112        .engineDestinations()113        .securityDestinations()114        .task(id: snapshot?.profile.endpoints) {115            // The profile is the source of truth; the application's copy follows it.116            if let stored = snapshot?.profile.endpoints, stored != endpoints { endpoints = stored }117        }118        .qHaptic(.selection, trigger: changeCount)119        #if DEBUG120        .navigationDestination(item: $debugRoute) { SecurityDebug.destination($0) }121        .task {122            await SecurityDebug.prepare(store)123            if SecurityDebug.host == .settings {124                debugRoute = SecurityDebug.screen == "mainnet-warning" ? .networks : SecurityDebug.route125            }126        }127        #endif128    }129130    // MARK: Wallet131132    private var walletSection: some View {133        QListSection("Wallet", footnote: snapshot != nil && !hasWallet ? "No wallet on this device yet: these screens open once a wallet exists." : nil) {134            SecLinkRow(title: "Quantum Vault", subtitle: "Password, PIN, biometrics, auto-lock", systemImage: "lock.shield", value: SecurityRoute.vault)135            SecLinkRow(title: "Quantum Identity", subtitle: "Post-quantum signing identity of this wallet", systemImage: "person.text.rectangle", value: SecurityRoute.identity)136            SecLinkRow(title: "Quantum Recovery", subtitle: "Recovery phrase, encrypted backups, transfer", systemImage: "arrow.counterclockwise.circle", value: SecurityRoute.recovery)137            SecLinkRow(title: "Cold mode", subtitle: "Offline signing with QR codes, for every chain that supports it", systemImage: "snowflake",138                       trailing: snapshot?.profile.coldMode == true ? "On" : nil, value: SecurityRoute.coldMode)139        }140    }141142    // MARK: Networks143144    @ViewBuilder private var networksSection: some View {145        if let snapshot {146            let isMain = snapshot.networkMode == .main && snapshot.mainnetEnabled147            let count = snapshot.profile.enabledChains.count148            QListSection("Networks", footnote: isMain149                ? "The enabled chains are on their main networks: balances are real funds. \(MainnetWording.statement)"150                : "The enabled chains are on their test networks: their coins have no monetary value.") {151                SecLinkRow(title: "Chains and networks", subtitle: "\(count) of \(ChainRegistry.mainChains.count) chains enabled · mode, test networks, endpoints",152                           systemImage: "point.3.connected.trianglepath.dotted", tint: isMain ? QColor.warning : QColor.textPrimary,153                           trailing: isMain ? "Main" : "Test", value: SecurityRoute.networks)154            }155        } else if store.startupError == nil {156            SecSkeletonSection(title: "Networks", rows: 1)157        }158    }159160    // MARK: Display161162    private var displaySection: some View {163        QListSection("Display", footnote: "The currency is used for prices and valuations; amounts on chain are never converted. Hidden amounts can be shown again from the wallet screen.") {164            if let snapshot {165                SecMenuRow(title: "Currency", systemImage: "banknote", choices: DisplayCurrency.allCases, name: { $0.title },166                           selection: Binding(get: { snapshot.profile.displayCurrency }, set: { setCurrency($0) }))167                    .disabled(busy)168            }169            SecLinkRow(title: "Appearance", systemImage: "circle.lefthalf.filled", trailing: appearance.title, value: SettingsRoute.appearance)170            SecToggleRow(title: "Hide amounts", subtitle: "Balances are masked until you choose to show them", systemImage: "eye.slash", isOn: $hideAmounts)171        }172    }173174    // MARK: Engine175176    private var engineSection: some View {177        QListSection("Engine", footnote: QyvoraEngine.auditStatus) {178            SecLinkRow(title: "Cryptographic information", subtitle: "Algorithms, self-test, audit status", systemImage: "function", value: EngineRoute.cryptography)179            SecLinkRow(title: "Benchmarks", subtitle: "Measure the engine on this device", systemImage: "gauge.with.needle", value: EngineRoute.benchmarks)180            SecLinkRow(title: "Migration engine", subtitle: "What each network offers today against quantum attacks", systemImage: "arrow.triangle.branch", value: EngineRoute.migration)181        }182    }183184    // MARK: Actions185186    private func setCurrency(_ currency: DisplayCurrency) {187        guard !busy else { return }188        busy = true189        error = nil190        Task {191            do {192                try await store.run { try await $0.setDisplayCurrency(currency) }193                await store.refresh()194                changeCount += 1195                if store.snapshot?.profile.coldMode != true, let service = store.service {196                    Task { _ = try? await service.refreshMarket(); await store.refresh() }197                }198            } catch {199                self.error = SecurityError.text(error)200            }201            busy = false202        }203    }204}205
Files main
Menu

Theme