Repository Public
1// SP Wallet iOS2// File: Features/Portfolio/PortfolioView.swift3// Description: Every asset of the registry with the wallet's balance: held first, then supported. Search, per-network detail,4// and on iPad a sortable table. Opens the asset page.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import SwiftUI9import SPWalletCore1011struct PortfolioView: View {12 @Environment(AppModel.self) private var model13 @Environment(Router.self) private var router14 @Environment(\.horizontalSizeClass) private var sizeClass15 @State private var query = ""16 @State private var path: [String] = []17 @State private var sort = [KeyPathComparator(\HoldingRow.value, order: .reverse)]1819 var body: some View {20 let p = model.portfolio.value21 let all = p.map { Holding.group($0, currency: model.currency) } ?? []22 let filtered = all.filter { query.isEmpty || $0.symbol.localizedCaseInsensitiveContains(query) || $0.name.localizedCaseInsensitiveContains(query) }23 let held = filtered.filter(\.isHeld), others = filtered.filter { !$0.isHeld }24 Group {25 if sizeClass == .regular, let p {26 table(filtered, p)27 } else {28 List {29 Section { FreshnessBanner().listRowInsets(.init()).listRowBackground(Color.clear) }30 if let p {31 if !held.isEmpty {32 Section(String(localized: "Détenus")) { ForEach(held) { h in row(h, p) } }33 }34 Section(String(localized: "Pris en charge")) {35 ForEach(others) { h in row(h, p) }36 if others.isEmpty && held.isEmpty { Text(String(localized: "Aucun résultat")).foregroundStyle(Theme.mute) }37 }38 } else if let e = model.portfolio.error { ErrorNotice(error: e) { Task { await model.refreshAll() } } } else { ProgressView() }39 }40 .listStyle(.insetGrouped).scrollContentBackground(.hidden).spPage()41 }42 }43 .searchable(text: $query, prompt: String(localized: "Rechercher un actif"))44 .refreshable { await model.refreshPortfolio() }45 .navigationTitle(AppSection.portfolio.title)46 .navigationDestination(for: String.self) { AssetDetailView(symbol: $0).wallet(model) }47 .navigationDestination(item: Binding(get: { router.pendingAsset }, set: { router.pendingAsset = $0 })) { AssetDetailView(symbol: $0).wallet(model) }48 }4950 private func row(_ h: Holding, _ p: Portfolio) -> some View {51 NavigationLink(value: h.symbol) { AssetRow(holding: h, price: p.prices?[h.symbol]) }.accessibilityIdentifier("asset.\(h.symbol)")52 }5354 private func table(_ holdings: [Holding], _ p: Portfolio) -> some View {55 let rows = holdings.map { HoldingRow(holding: $0, price: p.prices?[$0.symbol], currency: model.currency) }.sorted(using: sort)56 return VStack(spacing: 0) {57 FreshnessBanner().padding(.horizontal, Theme.Space.gutter).padding(.top, 8)58 Table(rows, sortOrder: $sort) {59 TableColumn(String(localized: "Actif"), value: \.symbol) { r in60 Button { router.pendingAsset = r.symbol } label: {61 HStack(spacing: 10) { AssetIcon(symbol: r.symbol, size: 30); VStack(alignment: .leading) { Text(r.holding.name).font(.system(size: 15, weight: .semibold)); Text(r.symbol).font(.spFootnote).foregroundStyle(Theme.mute) } }62 }.buttonStyle(.plain)63 }64 TableColumn(String(localized: "Prix"), value: \.priceValue) { r in Text(r.price.map { Money.price($0.value(in: model.currency), model.currency) } ?? "—").monospacedDigit() }65 TableColumn("24 h", value: \.change) { r in66 Text(Money.change(r.price?.change24h) ?? "—").monospacedDigit().foregroundStyle((r.price?.change24h ?? 0) >= 0 ? Theme.emerald : Theme.danger)67 }68 TableColumn(String(localized: "Solde")) { r in Masked(text: r.holding.totalDisplay.map { Money.brief($0) } ?? "—").monospacedDigit() }69 TableColumn(String(localized: "Valeur estimée"), value: \.value) { r in70 if r.holding.hasUnavailable && r.holding.value == nil { Text(String(localized: "indisponible")).foregroundStyle(Theme.warn) }71 else { Masked(text: r.holding.value.map { Money.fiat($0, model.currency) } ?? "—").monospacedDigit().fontWeight(.semibold) }72 }73 TableColumn(String(localized: "Réseaux")) { r in74 HStack(spacing: -6) { ForEach(r.holding.balances.prefix(6)) { NetworkIcon(network: $0.network, size: 20) } }75 }76 }77 }78 .spPage()79 }80}8182struct HoldingRow: Identifiable {83 let holding: Holding84 let price: MarketPrice?85 let currency: FiatCurrency86 var id: String { holding.symbol }87 var symbol: String { holding.symbol }88 var value: Double { holding.value ?? -1 }89 var priceValue: Double { price?.value(in: currency) ?? -1 }90 var change: Double { price?.change24h ?? -.infinity }91}92