Add support for multiple currencies

This commit is contained in:
2024-11-10 23:10:33 +01:00
parent 9faaa24e8d
commit 9321f920b4
5 changed files with 214 additions and 124 deletions

View File

@@ -10,6 +10,8 @@ public struct ContentView: View {
@State private var priceSource: PriceSource @State private var priceSource: PriceSource
@State private var expandAddCurrencySection: Bool = false
private let dateFormatter: DateFormatter private let dateFormatter: DateFormatter
private let priceFetcherDelegator: PriceFetcherDelegator private let priceFetcherDelegator: PriceFetcherDelegator
@@ -26,18 +28,63 @@ public struct ContentView: View {
@MainActor @MainActor
func updatePrice() async { func updatePrice() async {
do { do {
guard let price = try await priceFetcherDelegator.convertBTC(toCurrency: satsViewModel.selectedCurrency) else { let currencies = Set([satsViewModel.currentCurrency] + satsViewModel.currencyValueStrings.keys)
satsViewModel.btcToCurrencyString = "" let prices = try await priceFetcherDelegator.convertBTC(toCurrencies: Array(currencies))
return
}
satsViewModel.btcToCurrencyString = "\(price)" satsViewModel.currencyPrices = prices
satsViewModel.updateCurrencyValueStrings()
} catch { } catch {
satsViewModel.btcToCurrencyString = "" satsViewModel.clearCurrencyValueStrings()
} }
satsViewModel.lastUpdated = Date.now satsViewModel.lastUpdated = Date.now
} }
public var addCurrencyView: some View {
DisclosureGroup("Add Currency", isExpanded: $expandAddCurrencySection) {
Picker("Currency", selection: $satsViewModel.selectedCurrency) {
ForEach(satsViewModel.currencies, id: \.self) { currency in
Group {
if let localizedCurrency = Locale.current.localizedString(forCurrencyCode: currency.identifier) {
Text("\(currency.identifier) - \(localizedCurrency)")
} else {
Text(currency.identifier)
}
}
.tag(currency.identifier)
}
}
#if os(iOS) || SKIP
.pickerStyle(.navigationLink)
#endif
let selectedCurrency = satsViewModel.selectedCurrency
if selectedCurrency == satsViewModel.currentCurrency || satsViewModel.currencyValueStrings.keys.contains(selectedCurrency) {
Text("\(selectedCurrency.identifier) has already been added")
.foregroundStyle(.secondary)
} else {
Button("Add \(selectedCurrency.identifier)") {
satsViewModel.currencyValueStrings[selectedCurrency] = ""
expandAddCurrencySection = false
Task {
await updatePrice()
}
}
}
}
}
public func selectedCurrencyBinding(_ currency: Locale.Currency) -> Binding<String> {
Binding(
get: {
satsViewModel.currencyValueStrings[currency, default: ""]
},
set: { priceString in
satsViewModel.currencyValueStrings[currency] = priceString
}
)
}
public var body: some View { public var body: some View {
NavigationStack { NavigationStack {
Form { Form {
@@ -48,21 +95,8 @@ public struct ContentView: View {
} }
} }
Picker("Currency", selection: $satsViewModel.selectedCurrency) {
ForEach(satsViewModel.currencies, id: \.self) {
if let localizedCurrency = Locale.current.localizedString(forCurrencyCode: $0.identifier) {
Text("\($0.identifier) - \(localizedCurrency)")
} else {
Text($0.identifier)
}
}
}
#if os(iOS) || SKIP
.pickerStyle(.navigationLink)
#endif
HStack { HStack {
TextField("1 BTC to \(satsViewModel.selectedCurrency.identifier)", text: $satsViewModel.btcToCurrencyString) TextField("1 BTC to \(satsViewModel.currentCurrency.identifier)", text: satsViewModel.btcToCurrencyString(for: satsViewModel.currentCurrency))
.disabled(priceSource != .manual) .disabled(priceSource != .manual)
#if os(iOS) || SKIP #if os(iOS) || SKIP
.keyboardType(.decimalPad) .keyboardType(.decimalPad)
@@ -78,7 +112,7 @@ public struct ContentView: View {
} }
} }
} header: { } header: {
Text("1 BTC to \(satsViewModel.selectedCurrency.identifier)") Text("1 BTC to \(satsViewModel.currentCurrency.identifier)")
} footer: { } footer: {
if priceSource != .manual, let lastUpdated = satsViewModel.lastUpdated { if priceSource != .manual, let lastUpdated = satsViewModel.lastUpdated {
Text("Last updated: \(dateFormatter.string(from: lastUpdated))") Text("Last updated: \(dateFormatter.string(from: lastUpdated))")
@@ -112,23 +146,33 @@ public struct ContentView: View {
} }
Section { Section {
TextField(satsViewModel.selectedCurrency.identifier, text: $satsViewModel.currencyValueString) TextField(satsViewModel.currentCurrency.identifier, text: satsViewModel.currencyValueString(for: satsViewModel.currentCurrency))
#if os(iOS) || SKIP #if os(iOS) || SKIP
.keyboardType(.decimalPad) .keyboardType(.decimalPad)
#endif #endif
} header: { } header: {
Text(satsViewModel.selectedCurrency.identifier) Text(satsViewModel.currentCurrency.identifier)
}
if priceSource != .manual {
ForEach(satsViewModel.currencyValueStrings.sorted { $0.key.identifier < $1.key.identifier }.filter { $0.key != satsViewModel.currentCurrency }, id: \.key.identifier) { currencyAndPrice in
Section {
TextField(currencyAndPrice.key.identifier, text: satsViewModel.currencyValueString(for: currencyAndPrice.key))
#if os(iOS) || SKIP
.keyboardType(.decimalPad)
#endif
} header: {
Text(currencyAndPrice.key.identifier)
}
.tag(currencyAndPrice.key.identifier)
}
addCurrencyView
} }
} }
.task { .task {
await updatePrice() await updatePrice()
} }
.onChange(of: satsViewModel.selectedCurrency) { newCurrency in
satsViewModel.lastUpdated = nil
Task {
await updatePrice()
}
}
.onChange(of: priceSource) { newPriceSource in .onChange(of: priceSource) { newPriceSource in
satsViewModel.lastUpdated = nil satsViewModel.lastUpdated = nil
priceFetcherDelegator.priceSource = newPriceSource priceFetcherDelegator.priceSource = newPriceSource

View File

@@ -75,7 +75,7 @@ class CoinGeckoPriceFetcher : PriceFetcher {
var results: [Locale.Currency : Decimal] = [:] var results: [Locale.Currency : Decimal] = [:]
for currency in currencies { for currency in currencies {
if let price = priceResponse.bitcoin[currency.identifier] { if let price = priceResponse.bitcoin[currency.identifier.lowercased()] {
#if !SKIP #if !SKIP
results[currency] = price results[currency] = price
#else #else

View File

@@ -13,6 +13,9 @@
} }
} }
} }
},
"%@ has already been added" : {
}, },
"1 BTC to %@" : { "1 BTC to %@" : {
@@ -22,6 +25,12 @@
}, },
"2100000000000000 sats is the maximum." : { "2100000000000000 sats is the maximum." : {
},
"Add %@" : {
},
"Add Currency" : {
}, },
"BTC" : { "BTC" : {

View File

@@ -15,11 +15,14 @@ import SwiftUI
class SatsViewModel: ObservableObject { class SatsViewModel: ObservableObject {
@Published var lastUpdated: Date? @Published var lastUpdated: Date?
@Published var btcToCurrencyStringInternal: String = ""
@Published var satsStringInternal: String = "" @Published var satsStringInternal: String = ""
@Published var btcStringInternal: String = "" @Published var btcStringInternal: String = ""
@Published var currencyValueStringInternal: String = ""
@Published var selectedCurrency: Locale.Currency = Locale.current.currency ?? Locale.Currency("USD") @Published var selectedCurrency: Locale.Currency = Locale.current.currency ?? Locale.Currency("USD")
@Published var currencyValueStrings: [Locale.Currency: String] = [:]
var currencyPrices: [Locale.Currency: Decimal] = [:]
let currentCurrency: Locale.Currency = Locale.current.currency ?? Locale.Currency("USD")
var currencies: [Locale.Currency] { var currencies: [Locale.Currency] {
let commonISOCurrencyCodes = Set(Locale.commonISOCurrencyCodes) let commonISOCurrencyCodes = Set(Locale.commonISOCurrencyCodes)
@@ -34,25 +37,6 @@ class SatsViewModel: ObservableObject {
} }
} }
var btcToCurrencyString: String {
get {
btcToCurrencyStringInternal
}
set {
guard btcToCurrencyStringInternal != newValue else {
return
}
btcToCurrencyStringInternal = newValue
if let btc, let btcToCurrency {
currencyValueStringInternal = (btc * btcToCurrency).formatString()
} else {
currencyValueStringInternal = ""
}
}
}
var satsString: String { var satsString: String {
get { get {
satsStringInternal satsStringInternal
@@ -71,14 +55,11 @@ class SatsViewModel: ObservableObject {
let btc = sats.divide(Decimal(100000000), 20, java.math.RoundingMode.DOWN) let btc = sats.divide(Decimal(100000000), 20, java.math.RoundingMode.DOWN)
#endif #endif
btcStringInternal = btc.formatString() btcStringInternal = btc.formatString()
if let btcToCurrency {
currencyValueStringInternal = (btc * btcToCurrency).formatString() updateCurrencyValueStrings()
} else {
currencyValueStringInternal = ""
}
} else { } else {
btcStringInternal = "" btcStringInternal = ""
currencyValueStringInternal = "" clearCurrencyValueStrings()
} }
} }
} }
@@ -98,65 +79,131 @@ class SatsViewModel: ObservableObject {
let sats = btc * Decimal(100000000) let sats = btc * Decimal(100000000)
satsStringInternal = sats.formatString() satsStringInternal = sats.formatString()
if let btcToCurrency { updateCurrencyValueStrings()
currencyValueStringInternal = (btc * btcToCurrency).formatString()
} else {
currencyValueStringInternal = ""
}
} else { } else {
satsStringInternal = "" satsStringInternal = ""
currencyValueStringInternal = "" clearCurrencyValueStrings()
} }
} }
} }
var currencyValueString: String { func updateCurrencyValueStrings(excludedCurrency: Locale.Currency? = nil) {
get { if let btc {
currencyValueStringInternal let currencies = Set([currentCurrency] + currencyValueStrings.keys)
} .filter { $0 != excludedCurrency }
set {
guard currencyValueStringInternal != newValue else {
return
}
currencyValueStringInternal = newValue for currency in currencies {
if let btcToCurrency = btcToCurrency(for: currency) {
if let currencyValue { currencyValueStrings[currency] = (btc * btcToCurrency).formatString()
if let btcToCurrency {
#if !SKIP
let btc = currencyValue / btcToCurrency
#else
let btc = currencyValue.divide(btcToCurrency, 20, java.math.RoundingMode.DOWN)
#endif
btcStringInternal = btc.formatString()
let sats = btc * Decimal(100000000)
satsStringInternal = sats.formatString()
} else { } else {
satsStringInternal = "" currencyValueStrings[currency] = ""
btcStringInternal = ""
currencyValueStringInternal = ""
} }
} else {
satsStringInternal = ""
btcStringInternal = ""
currencyValueStringInternal = ""
} }
} else {
clearCurrencyValueStrings()
} }
} }
var btcToCurrency: Decimal? { func clearCurrencyValueStrings() {
for currency in currencyValueStrings.keys {
currencyValueStrings[currency] = ""
}
}
func currencyValueString(for currency: Locale.Currency) -> Binding<String> {
Binding<String>(
get: {
self.currencyValueStrings[currency, default: ""]
},
set: { newValue in
guard self.currencyValueStrings[currency] != newValue else {
return
}
self.currencyValueStrings[currency] = newValue
if let currencyValue = self.currencyValue(for: currency) {
if let btcToCurrency = self.currencyPrices[currency] {
#if !SKIP
let btc = currencyValue / btcToCurrency
#else
let btc = currencyValue.divide(btcToCurrency, 20, java.math.RoundingMode.DOWN)
#endif
self.btcStringInternal = btc.formatString()
let sats = btc * Decimal(100000000)
self.satsStringInternal = sats.formatString()
self.updateCurrencyValueStrings(excludedCurrency: currency)
} else {
self.satsStringInternal = ""
self.btcStringInternal = ""
self.clearCurrencyValueStrings()
}
} else {
self.satsStringInternal = ""
self.btcStringInternal = ""
self.clearCurrencyValueStrings()
}
}
)
}
func currencyValue(for currency: Locale.Currency) -> Decimal? {
guard let currencyValueString = currencyValueStrings[currency] else {
return nil
}
#if !SKIP #if !SKIP
return Decimal(string: btcToCurrencyStringInternal) return Decimal(string: currencyValueString)
#else #else
do { do {
return Decimal(btcToCurrencyStringInternal) return Decimal(currencyValueString)
} catch { } catch {
return nil return nil
} }
#endif #endif
} }
func btcToCurrency(for currency: Locale.Currency) -> Decimal? {
currencyPrices[currency]
}
func btcToCurrencyString(for currency: Locale.Currency) -> Binding<String> {
Binding<String>(
get: {
self.currencyPrices[currency]?.formatString() ?? ""
},
set: { newValue in
#if !SKIP
if let newPrice = Decimal(string: newValue), self.currencyPrices[currency] != newPrice {
self.currencyPrices[currency] = Decimal(string: newValue)
if let btc = self.btc {
self.currencyValueStrings[currency] = (btc * newPrice).formatString()
} else {
self.currencyValueStrings[currency] = ""
}
}
#else
do {
if let newPrice = Decimal(newValue), self.currencyPrices[currency] != newPrice {
self.currencyPrices[currency] = Decimal(newValue)
if let btc = self.btc {
self.currencyValueStrings[currency] = (btc * newPrice).formatString()
} else {
self.currencyValueStrings[currency] = ""
}
}
} catch {
self.currencyPrices.removeValue(forKey: currency)
}
#endif
}
)
}
var sats: Decimal? { var sats: Decimal? {
#if !SKIP #if !SKIP
return Decimal(string: satsStringInternal) return Decimal(string: satsStringInternal)
@@ -181,18 +228,6 @@ class SatsViewModel: ObservableObject {
#endif #endif
} }
var currencyValue: Decimal? {
#if !SKIP
return Decimal(string: currencyValueStringInternal)
#else
do {
return Decimal(currencyValueStringInternal)
} catch {
return nil
}
#endif
}
var exceedsMaximum: Bool { var exceedsMaximum: Bool {
if let btc, btc > Decimal(21000000) { if let btc, btc > Decimal(21000000) {
return true return true

View File

@@ -14,82 +14,84 @@ import XCTest
final class SatsViewModelTests: XCTestCase { final class SatsViewModelTests: XCTestCase {
let currency = Locale.Currency("USD")
func testSatsViewModel() { func testSatsViewModel() {
let satsViewModel = SatsViewModel() let satsViewModel = SatsViewModel()
satsViewModel.btcToCurrencyString = "54321" satsViewModel.btcToCurrencyString(for: currency).wrappedValue = "54321"
// Test BTC updates. // Test BTC updates.
satsViewModel.btcString = "1" satsViewModel.btcString = "1"
#if !SKIP #if !SKIP
XCTAssertEqual(satsViewModel.btc, Decimal(string: "1")) XCTAssertEqual(satsViewModel.btc, Decimal(string: "1"))
XCTAssertEqual(satsViewModel.sats, Decimal(string: "100000000")) XCTAssertEqual(satsViewModel.sats, Decimal(string: "100000000"))
XCTAssertEqual(satsViewModel.currencyValue, Decimal(string: "54321")) XCTAssertEqual(satsViewModel.currencyValue(for: currency), Decimal(string: "54321"))
#else #else
XCTAssertEqual(satsViewModel.btc, Decimal("1")) XCTAssertEqual(satsViewModel.btc, Decimal("1"))
XCTAssertEqual(satsViewModel.sats, Decimal("100000000")) XCTAssertEqual(satsViewModel.sats, Decimal("100000000"))
XCTAssertEqual(satsViewModel.currencyValue, Decimal("54321")) XCTAssertEqual(satsViewModel.currencyValue(for: currency), Decimal("54321"))
#endif #endif
XCTAssertEqual(satsViewModel.btcString, "1") XCTAssertEqual(satsViewModel.btcString, "1")
XCTAssertEqual(satsViewModel.satsString, "100000000") XCTAssertEqual(satsViewModel.satsString, "100000000")
XCTAssertEqual(satsViewModel.currencyValueString, "54321") XCTAssertEqual(satsViewModel.currencyValueString(for: currency).wrappedValue, "54321")
// Test Sats updates. // Test Sats updates.
satsViewModel.satsString = "200000000" satsViewModel.satsString = "200000000"
#if !SKIP #if !SKIP
XCTAssertEqual(satsViewModel.btc, Decimal(string: "2")) XCTAssertEqual(satsViewModel.btc, Decimal(string: "2"))
XCTAssertEqual(satsViewModel.sats, Decimal(string: "200000000")) XCTAssertEqual(satsViewModel.sats, Decimal(string: "200000000"))
XCTAssertEqual(satsViewModel.currencyValue, Decimal(string: "108642")) XCTAssertEqual(satsViewModel.currencyValue(for: currency), Decimal(string: "108642"))
#else #else
XCTAssertEqual(satsViewModel.btc, Decimal("2")) XCTAssertEqual(satsViewModel.btc, Decimal("2"))
XCTAssertEqual(satsViewModel.sats, Decimal("200000000")) XCTAssertEqual(satsViewModel.sats, Decimal("200000000"))
XCTAssertEqual(satsViewModel.currencyValue, Decimal("108642")) XCTAssertEqual(satsViewModel.currencyValue(for: currency), Decimal("108642"))
#endif #endif
XCTAssertEqual(satsViewModel.btcString, "2") XCTAssertEqual(satsViewModel.btcString, "2")
XCTAssertEqual(satsViewModel.satsString, "200000000") XCTAssertEqual(satsViewModel.satsString, "200000000")
XCTAssertEqual(satsViewModel.currencyValueString, "108642") XCTAssertEqual(satsViewModel.currencyValueString(for: currency).wrappedValue, "108642")
// Test currency value updates. // Test currency value updates.
satsViewModel.currencyValueString = "162963" satsViewModel.currencyValueString(for: currency).wrappedValue = "162963"
#if !SKIP #if !SKIP
XCTAssertEqual(satsViewModel.btc, Decimal(string: "3")) XCTAssertEqual(satsViewModel.btc, Decimal(string: "3"))
XCTAssertEqual(satsViewModel.sats, Decimal(string: "300000000")) XCTAssertEqual(satsViewModel.sats, Decimal(string: "300000000"))
XCTAssertEqual(satsViewModel.currencyValue, Decimal(string: "162963")) XCTAssertEqual(satsViewModel.currencyValue(for: currency), Decimal(string: "162963"))
#else #else
XCTAssertEqual(satsViewModel.btc, Decimal("3")) XCTAssertEqual(satsViewModel.btc, Decimal("3"))
XCTAssertEqual(satsViewModel.sats, Decimal("300000000")) XCTAssertEqual(satsViewModel.sats, Decimal("300000000"))
XCTAssertEqual(satsViewModel.currencyValue, Decimal("162963")) XCTAssertEqual(satsViewModel.currencyValue(for: currency), Decimal("162963"))
#endif #endif
XCTAssertEqual(satsViewModel.btcString, "3") XCTAssertEqual(satsViewModel.btcString, "3")
XCTAssertEqual(satsViewModel.satsString, "300000000") XCTAssertEqual(satsViewModel.satsString, "300000000")
XCTAssertEqual(satsViewModel.currencyValueString, "162963") XCTAssertEqual(satsViewModel.currencyValueString(for: currency).wrappedValue, "162963")
// Test fractional amounts. // Test fractional amounts.
// Precision between platforms on this calculation is different so we have different assertions for each. // Precision between platforms on this calculation is different so we have different assertions for each.
satsViewModel.currencyValueString = "1" satsViewModel.currencyValueString(for: currency).wrappedValue = "1"
#if !SKIP #if !SKIP
XCTAssertEqual(satsViewModel.btc, Decimal(string: "0.00001840908672520756245282671526665562")) XCTAssertEqual(satsViewModel.btc, Decimal(string: "0.00001840908672520756245282671526665562"))
XCTAssertEqual(satsViewModel.btcString, "0.00001840908672520756245282671526665562") XCTAssertEqual(satsViewModel.btcString, "0.00001840908672520756245282671526665562")
XCTAssertEqual(satsViewModel.sats, Decimal(string: "1840.908672520756245282671526665562")) XCTAssertEqual(satsViewModel.sats, Decimal(string: "1840.908672520756245282671526665562"))
XCTAssertEqual(satsViewModel.satsString, "1840.908672520756245282671526665562") XCTAssertEqual(satsViewModel.satsString, "1840.908672520756245282671526665562")
XCTAssertEqual(satsViewModel.currencyValue, Decimal(string: "1")) XCTAssertEqual(satsViewModel.currencyValue(for: currency), Decimal(string: "1"))
#else #else
XCTAssertEqual(satsViewModel.btc, Decimal("0.00001840908672520756")) XCTAssertEqual(satsViewModel.btc, Decimal("0.00001840908672520756"))
XCTAssertEqual(satsViewModel.btcString, "0.00001840908672520756") XCTAssertEqual(satsViewModel.btcString, "0.00001840908672520756")
XCTAssertEqual(satsViewModel.sats, Decimal("1840.908672520756")) XCTAssertEqual(satsViewModel.sats, Decimal("1840.908672520756"))
XCTAssertEqual(satsViewModel.satsString, "1840.908672520756") XCTAssertEqual(satsViewModel.satsString, "1840.908672520756")
XCTAssertEqual(satsViewModel.currencyValue, Decimal("1")) XCTAssertEqual(satsViewModel.currencyValue(for: currency), Decimal("1"))
#endif #endif
XCTAssertEqual(satsViewModel.currencyValueString, "1") XCTAssertEqual(satsViewModel.currencyValueString(for: currency).wrappedValue, "1")
// Test large amounts that exceed the cap of 21M BTC. // Test large amounts that exceed the cap of 21M BTC.
// Precision between platforms on this calculation is different so we have different assertions for each. // Precision between platforms on this calculation is different so we have different assertions for each.
satsViewModel.currencyValueString = "11407419999999" satsViewModel.currencyValueString(for: currency).wrappedValue = "11407419999999"
#if !SKIP #if !SKIP
XCTAssertEqual(satsViewModel.btc, Decimal(string: "210000184.09084884298889932070469983984")) XCTAssertEqual(satsViewModel.btc, Decimal(string: "210000184.09084884298889932070469983984"))
XCTAssertEqual(satsViewModel.btcString, "210000184.09084884298889932070469983984") XCTAssertEqual(satsViewModel.btcString, "210000184.09084884298889932070469983984")
XCTAssertEqual(satsViewModel.sats, Decimal(string: "21000018409084884.298889932070469983984")) XCTAssertEqual(satsViewModel.sats, Decimal(string: "21000018409084884.298889932070469983984"))
XCTAssertEqual(satsViewModel.satsString, "21000018409084884.298889932070469983984") XCTAssertEqual(satsViewModel.satsString, "21000018409084884.298889932070469983984")
XCTAssertEqual(satsViewModel.currencyValue, Decimal(string: "11407419999999")) XCTAssertEqual(satsViewModel.currencyValue(for: currency), Decimal(string: "11407419999999"))
#else #else
XCTAssertEqual(satsViewModel.btc, Decimal("210000184.0908488429888993207")) XCTAssertEqual(satsViewModel.btc, Decimal("210000184.0908488429888993207"))
XCTAssertEqual(satsViewModel.btcString, "210000184.0908488429888993207") XCTAssertEqual(satsViewModel.btcString, "210000184.0908488429888993207")
@@ -97,7 +99,7 @@ final class SatsViewModelTests: XCTestCase {
XCTAssertEqual(satsViewModel.satsString, "21000018409084884.29888993207") XCTAssertEqual(satsViewModel.satsString, "21000018409084884.29888993207")
XCTAssertEqual(satsViewModel.currencyValue, Decimal("11407419999999")) XCTAssertEqual(satsViewModel.currencyValue, Decimal("11407419999999"))
#endif #endif
XCTAssertEqual(satsViewModel.currencyValueString, "11407419999999") XCTAssertEqual(satsViewModel.currencyValueString(for: currency).wrappedValue, "11407419999999")
} }
} }