SPB Git

Repository Public

SecChainSupport.swift 12.4 KB 331 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project          : QYVORA7// File             : App/Sources/Security/SecChainSupport.swift8// Module           : Security9// Repository       : https://git.spboucher.ai/qyvora10// Project Website  : Not specified11// Created          : 2026-12-0512// 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// =============================================================================4041import SwiftUI42import QyvoraCore43import QyvoraBitcoin44import QyvoraWallet45import QyvoraUI4647// Shared pieces of the security, settings and quantum screens for the multi-chain wallet: the bridge between the48// service's chain registry and the design system's chains, and the rows every screen of the area is built from.4950// MARK: - Registry → design system5152/// Helpers of the security screens around the chain registry. The mapping of a chain to the design system's53/// `QChain` / `QNetwork` is not here: every screen uses `ChainDescriptor.qNetwork` (WalletChainAdapter.swift).54enum SecChain {55    /// The descriptor of a UTXO network.56    static func descriptor(_ network: BitcoinNetwork) -> ChainDescriptor? { ChainRegistry.descriptor(of: network) }5758    /// "0.00012345 BTC": exact, integer arithmetic, with the coin's own symbol and decimals.59    static func amount(_ value: UInt256, decimals: Int, symbol: String) -> String {60        BaseUnits.format(value, decimals: decimals) + " " + symbol61    }6263    /// An amount of a UTXO coin in its own unit ("1.5 LTC", "0.0001 tBTC").64    static func amount(_ value: UInt64, on network: BitcoinNetwork) -> String {65        amount(UInt256(value), decimals: network.decimals, symbol: network.ticker)66    }6768    /// The test network the wallet uses for a main chain in test mode: the user's choice, else the registry default.69    static func chosenTestNetwork(of main: ChainID, profile: WalletProfile) -> ChainDescriptor? {70        guard let descriptor = ChainRegistry.descriptor(main) else { return nil }71        let chosen = profile.testNetworks[main.rawValue].flatMap { descriptor.testCounterparts.contains($0) ? $0 : nil } ?? descriptor.testCounterparts.first72        return chosen.flatMap(ChainRegistry.descriptor)73    }7475    /// What the registry says a network cannot do, as short sentences for the networks screen.76    static func capabilityNotes(_ descriptor: ChainDescriptor) -> [String] {77        var notes = [String]()78        if !descriptor.capabilities.historyAvailable {79            notes.append(descriptor.family == .utxo ? "No indexer for this network: balance and history cannot be read" : "History not available on this network")80        }81        if !descriptor.capabilities.coldSigning { notes.append("Cold signing not available yet") }82        return notes83    }84}8586// MARK: - Rows of a QListSection8788/// The content of a settings-style row: a glyph tile aligned with the chain glyphs, a title, an optional subtitle,89/// an optional trailing value and an optional chevron.90struct SecRowLabel: View {91    let title: String92    var subtitle: String?93    var systemImage: String?94    var tint: Color = QColor.textPrimary95    var trailing: String?96    var chevron = false97    @Environment(\.dynamicTypeSize) private var typeSize9899    var body: some View {100        HStack(spacing: QSpace.m) {101            if let systemImage {102                QIconTile(systemImage, tint: tint).frame(minWidth: 40)103            }104            if typeSize.isAccessibilitySize {105                VStack(alignment: .leading, spacing: 3) {106                    titles107                    if let trailing { trailingText(trailing) }108                }109                Spacer(minLength: 0)110            } else {111                titles.frame(maxWidth: .infinity, alignment: .leading).layoutPriority(1)112                if let trailing { trailingText(trailing).lineLimit(1).fixedSize().layoutPriority(2) }113            }114            if chevron {115                Image(systemName: "chevron.right")116                    .font(.footnote.weight(.semibold))117                    .foregroundStyle(QColor.textTertiary)118                    .accessibilityHidden(true)119            }120        }121        .padding(.vertical, QSpace.m)122        .frame(minHeight: QMetric.rowMinHeight)123        .accessibilityElement(children: .combine)124    }125126    private var titles: some View {127        VStack(alignment: .leading, spacing: 3) {128            Text(title).font(QFont.headline).foregroundStyle(QColor.textPrimary)129            if let subtitle {130                Text(subtitle).font(QFont.caption).foregroundStyle(QColor.textSecondary)131            }132        }133        .fixedSize(horizontal: false, vertical: true)134    }135136    private func trailingText(_ text: String) -> some View {137        Text(text).font(QFont.callout).foregroundStyle(QColor.textSecondary)138    }139}140141/// A row that pushes a value on the enclosing navigation stack.142struct SecLinkRow<Value: Hashable>: View {143    let title: String144    var subtitle: String?145    var systemImage: String?146    var tint: Color = QColor.textPrimary147    var trailing: String?148    let value: Value149150    var body: some View {151        NavigationLink(value: value) {152            SecRowLabel(title: title, subtitle: subtitle, systemImage: systemImage, tint: tint, trailing: trailing, chevron: true)153        }154        .buttonStyle(.qRow)155    }156}157158/// A row that runs an action (opens a sheet, starts an operation).159struct SecActionRow: View {160    let title: String161    var subtitle: String?162    var systemImage: String?163    var tint: Color = QColor.textPrimary164    var trailing: String?165    var chevron = true166    let action: () -> Void167168    var body: some View {169        Button(action: action) {170            SecRowLabel(title: title, subtitle: subtitle, systemImage: systemImage, tint: tint, trailing: trailing, chevron: chevron)171        }172        .buttonStyle(.qRow)173    }174}175176/// A row with a switch.177struct SecToggleRow: View {178    let title: String179    var subtitle: String?180    var systemImage: String?181    var tint: Color = QColor.textPrimary182    var switchTint: Color = QColor.violet183    @Binding var isOn: Bool184185    var body: some View {186        Toggle(isOn: $isOn) {187            SecRowLabel(title: title, subtitle: subtitle, systemImage: systemImage, tint: tint)188        }189        .tint(switchTint)190        .qHaptic(.toggled, trigger: isOn)191    }192}193194/// A row with a menu of choices; the current choice is the trailing value.195struct SecMenuRow<Choice: Hashable>: View {196    let title: String197    var subtitle: String?198    var systemImage: String?199    let choices: [Choice]200    let name: (Choice) -> String201    @Binding var selection: Choice202203    var body: some View {204        Menu {205            Picker(title, selection: $selection) {206                ForEach(choices, id: \.self) { Text(name($0)).tag($0) }207            }208        } label: {209            HStack(spacing: QSpace.s) {210                SecRowLabel(title: title, subtitle: subtitle, systemImage: systemImage, trailing: name(selection))211                Image(systemName: "chevron.up.chevron.down")212                    .font(.footnote.weight(.semibold))213                    .foregroundStyle(QColor.violet)214                    .accessibilityHidden(true)215            }216            .contentShape(Rectangle())217        }218        .buttonStyle(.qRow)219        .qHaptic(.selection, trigger: selection)220        .accessibilityLabel(title)221        .accessibilityValue(name(selection))222    }223}224225/// A fact of a card: label on the left, value on the right; stacked at accessibility sizes. Use `ReviewRow` inside a226/// `ReviewSection`; this one carries its own vertical padding for a `QListSection`.227struct SecFactRow: View {228    let label: String229    let value: String230    var mono = false231    var tone: QTone?232    @Environment(\.dynamicTypeSize) private var typeSize233234    var body: some View {235        Group {236            if typeSize.isAccessibilitySize || mono {237                VStack(alignment: .leading, spacing: 3) { labelText; valueText }238                    .frame(maxWidth: .infinity, alignment: .leading)239            } else {240                HStack(alignment: .firstTextBaseline, spacing: QSpace.m) {241                    labelText242                    Spacer(minLength: QSpace.s)243                    valueText.multilineTextAlignment(.trailing)244                }245            }246        }247        .padding(.vertical, QSpace.m)248        .accessibilityElement(children: .combine)249    }250251    private var labelText: some View {252        Text(label).font(QFont.callout).foregroundStyle(QColor.textSecondary).fixedSize(horizontal: false, vertical: true)253    }254255    private var valueText: some View {256        Text(mono ? SecurityFormat.breakingLongTokens(value, longerThan: 12) : value)257            .font(mono ? QFont.mono : QFont.callout.weight(.medium))258            .foregroundStyle(tone?.color ?? QColor.textPrimary)259            .fixedSize(horizontal: false, vertical: true)260            .accessibilityLabel(value)261    }262}263264/// A paragraph inside a `QListSection` (a statement, a note), with the row's vertical padding.265struct SecTextRow: View {266    let text: String267    var systemImage: String?268    var tone: QTone = .neutral269270    var body: some View {271        HStack(alignment: .firstTextBaseline, spacing: QSpace.s) {272            if let systemImage {273                Image(systemName: systemImage).font(QFont.callout).foregroundStyle(tone.color).accessibilityHidden(true)274            }275            Text(SecurityFormat.breakingLongTokens(text))276                .font(QFont.callout)277                .foregroundStyle(tone == .neutral ? QColor.textSecondary : QColor.textPrimary)278                .fixedSize(horizontal: false, vertical: true)279                .accessibilityLabel(text)280        }281        .frame(maxWidth: .infinity, alignment: .leading)282        .padding(.vertical, QSpace.m)283        .accessibilityElement(children: .combine)284    }285}286287/// The header of a chain inside a card: glyph, network name with its test tag, one line of detail.288struct SecChainHeader: View {289    let network: QNetwork290    var detail: String?291    var trailing: String?292293    var body: some View {294        HStack(spacing: QSpace.m) {295            ChainGlyph(network)296            VStack(alignment: .leading, spacing: 3) {297                HStack(spacing: 6) {298                    Text(network.fullName).font(QFont.headline).foregroundStyle(QColor.textPrimary)299                    if network.isTest { QTestTag() }300                }301                if let detail {302                    Text(detail).font(QFont.caption).foregroundStyle(QColor.textSecondary)303                }304            }305            .fixedSize(horizontal: false, vertical: true)306            Spacer(minLength: QSpace.s)307            if let trailing {308                Text(trailing).font(QFont.callout).foregroundStyle(QColor.textSecondary)309            }310        }311        .padding(.vertical, QSpace.m)312        .frame(minHeight: QMetric.rowMinHeight)313        .accessibilityElement(children: .combine)314        .accessibilityLabel([network.spokenName, detail, trailing].compactMap { $0 }.joined(separator: ", "))315    }316}317318/// Placeholder rows of a card whose content is being read (same shape as the rows that will replace them).319struct SecSkeletonSection: View {320    var title: String?321    var rows = 3322323    var body: some View {324        QListSection(title) {325            ForEach(0..<rows, id: \.self) { _ in QSkeletonRow() }326        }327        .accessibilityElement(children: .ignore)328        .accessibilityLabel("Loading")329    }330}331
Files main
Menu

Theme