This commit is contained in:
Marcos Rodriguez Velez 2024-10-28 13:27:53 -04:00
parent 3cd15744a4
commit 57dbbdddaf
2 changed files with 13 additions and 8 deletions

View File

@ -73,6 +73,9 @@
},
"Source: %@" : {
},
"Updated: %@" : {
},
"View the current Bitcoin market rate." : {

View File

@ -1,6 +1,7 @@
import AppIntents
import SwiftUI
@available(iOS 16.0, *)
struct PriceIntent: AppIntent {
static var title: LocalizedStringResource = "Market Rate"
@ -12,7 +13,7 @@ struct PriceIntent: AppIntent {
}
@MainActor
func perform() async throws -> some IntentResult & ReturnsValue<String> & ProvidesDialog & ShowsSnippetView {
func perform() async throws -> some IntentResult & ReturnsValue<Double> & ProvidesDialog & ShowsSnippetView {
let userPreferredCurrency = Currency.getUserPreferredCurrency()
let currencyCode = userPreferredCurrency.uppercased()
let dataSource = fiatUnit(currency: userPreferredCurrency)?.source ?? "Unknown Source"
@ -22,24 +23,24 @@ struct PriceIntent: AppIntent {
}
var lastUpdated = "--"
var resultValue: String = "--"
var priceDouble: Double = 0.0
var resultValue: Double = 0.0
do {
guard let data = try await MarketAPI.fetchPrice(currency: userPreferredCurrency) else {
throw NSError(domain: "PriceIntentErrorDomain", code: -1, userInfo: [NSLocalizedDescriptionKey: "Failed to fetch price data."])
}
}
priceDouble = data.rateDouble
resultValue = data.rateDouble
lastUpdated = formattedDate(from: data.lastUpdate)
resultValue = formatPrice(priceDouble, currencyCode: currencyCode)
} catch {
throw error
}
let formattedPrice = formatPrice(resultValue, currencyCode: currencyCode)
let view = CompactPriceView(
price: resultValue,
price: formattedPrice,
lastUpdated: lastUpdated,
currencyCode: currencyCode,
dataSource: dataSource
@ -70,7 +71,7 @@ struct PriceIntent: AppIntent {
formatter.currencyCode = currencyCode
formatter.locale = Locale.current
// Omit cents if they are zero
// Omit cents if price is a whole number
if price.truncatingRemainder(dividingBy: 1) == 0 {
formatter.maximumFractionDigits = 0
formatter.minimumFractionDigits = 0
@ -83,6 +84,7 @@ struct PriceIntent: AppIntent {
}
}
@available(iOS 16.0, *)
struct CompactPriceView: View {
let price: String