Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : QYVORA7// File : App/Sources/Wallet/PriceChartSection.swift8// Module : Wallet9// 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 QyvoraWallet43import QyvoraUI4445/// Price history of one asset, loaded from the price source of the profile for the selected range.46/// Without data the chart says why; a series kept from an earlier request is labelled with its date.47struct PriceChartSection: View {48 /// Price identifier ("bitcoin", "ethereum", a token's `priceID`); nil when the asset has no price source.49 let assetID: String?50 let currency: DisplayCurrency51 /// Prices are those of the mainnet asset while the wallet is on a test network.52 let isTestNetwork: Bool5354 @Environment(WalletStore.self) private var wallet55 @State private var range: PriceRange = .week56 @State private var chart: Loadable<PriceChart> = .idle57 @State private var attempt = 05859 var body: some View {60 VStack(alignment: .leading, spacing: 10) {61 SectionHeader("Price", subtitle: isTestNetwork ? "Mainnet price, for reference. Test coins have no value." : nil)62 if let assetID {63 PriceChartView(points: points, range: $range, isLoading: chart.isLoading, unavailableReason: failure,64 format: { WalletFormat.fiat($0, currency) }, onRetry: { attempt += 1 })65 .task(id: "\(assetID)|\(range.rawValue)|\(currency.rawValue)|\(attempt)") { await load(assetID) }66 if let loaded = chart.value, loaded.isStale {67 Label("Series of \(loaded.fetchedAt.formatted(date: .abbreviated, time: .shortened)); the refresh failed.", systemImage: "clock")68 .font(QFont.caption).foregroundStyle(QColor.warning)69 }70 } else {71 UnavailableDataView(reason: "This token has no price source: the wallet knows no price identifier for it.", title: "No price", compact: true)72 }73 }74 }7576 var points: [ChartPoint] {77 guard let loaded = chart.value, loaded.range == range.chartRange else { return [] }78 return loaded.points.map { ChartPoint(date: $0.date, value: $0.price) }79 }8081 var failure: String? {82 if case .failed(let reason) = chart { return reason }83 return nil84 }8586 func load(_ assetID: String) async {87 chart = .loading88 let wanted = range.chartRange89 do {90 let loaded = try await wallet.run { try await $0.priceChart(assetID: assetID, range: wanted) }91 guard !Task.isCancelled else { return }92 chart = .loaded(loaded)93 } catch {94 guard !Task.isCancelled else { return }95 chart = .failed(WalletStore.message(for: error))96 }97 }98}99