Show each wallet's balance in USD

This commit is contained in:
2026-09-29 21:02:45 +08:00
parent 0b1b253d30
commit 58d862161e
4 changed files with 115 additions and 0 deletions
+26
View File
@@ -15,6 +15,8 @@ final class AppModel: ObservableObject {
@Published var moneroReady = false
@Published var moneroAddress: String?
@Published var moneroAccounts: [MoneroAccount] = []
@Published var prices = PriceService.Prices()
/// Whether the app is behind the biometric lock screen right now.
@Published var locked = false
@@ -53,6 +55,28 @@ final class AppModel: ObservableObject {
var symbol: String { chain.symbol }
// MARK: - Fiat display
private static let fiatFormatter: NumberFormatter = {
let formatter = NumberFormatter()
formatter.numberStyle = .currency
formatter.currencyCode = "USD"
formatter.locale = Locale(identifier: "en_US")
return formatter
}()
/// The current chain's balance in USD, when a price is known.
var fiatBalance: String? {
let price = chain == .xrp ? prices.xrp : prices.xmr
guard let price, let amount = Decimal(string: balance.total) else { return nil }
let usd = amount * Decimal(price)
return Self.fiatFormatter.string(from: usd as NSDecimalNumber)
}
private func refreshPrices() async {
prices = await PriceService.shared.prices()
}
// MARK: - Biometric lock
func authenticate() async {
@@ -112,6 +136,7 @@ final class AppModel: ObservableObject {
showCached(chain)
await refresh(force: true)
startAutoRefresh()
Task { await refreshPrices() }
}
private func adoptMoneroWallet(named name: String) async {
@@ -263,6 +288,7 @@ final class AppModel: ObservableObject {
refreshToken = token
let requestChain = chain
errorMessage = nil
await refreshPrices()
if chain == .xrp, let wallet = xrpWallet {
do {
async let balanceCall = XRPWalletService.shared.balance(address: wallet.address)