Create separate CurrencyPickerView and tighten up ContentView
This commit is contained in:
@@ -8,70 +8,23 @@ import SwiftUI
|
||||
public struct ContentView: View {
|
||||
@ObservedObject private var satsViewModel = SatsViewModel()
|
||||
|
||||
@State private var priceSource: PriceSource
|
||||
|
||||
@State private var expandAddCurrencySection: Bool = false
|
||||
|
||||
private let dateFormatter: DateFormatter
|
||||
|
||||
private let priceFetcherDelegator: PriceFetcherDelegator
|
||||
|
||||
init(_ priceSource: PriceSource) {
|
||||
init() {
|
||||
dateFormatter = DateFormatter()
|
||||
dateFormatter.dateStyle = .short
|
||||
dateFormatter.timeStyle = .short
|
||||
|
||||
self.priceSource = priceSource
|
||||
priceFetcherDelegator = PriceFetcherDelegator(priceSource)
|
||||
}
|
||||
|
||||
@MainActor
|
||||
func updatePrice() async {
|
||||
do {
|
||||
let currencies = Set([satsViewModel.currentCurrency] + satsViewModel.currencyValueStrings.keys)
|
||||
let prices = try await priceFetcherDelegator.convertBTC(toCurrencies: Array(currencies))
|
||||
|
||||
satsViewModel.currencyPrices = prices
|
||||
satsViewModel.updateCurrencyValueStrings()
|
||||
} catch {
|
||||
satsViewModel.clearCurrencyValueStrings()
|
||||
}
|
||||
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)
|
||||
}
|
||||
NavigationLink(
|
||||
destination: {
|
||||
CurrencyPickerView(satsViewModel: satsViewModel)
|
||||
},
|
||||
label: {
|
||||
Text("Change Currencies")
|
||||
}
|
||||
#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> {
|
||||
@@ -89,7 +42,7 @@ public struct ContentView: View {
|
||||
NavigationStack {
|
||||
Form {
|
||||
Section {
|
||||
Picker("Price Source", selection: $priceSource) {
|
||||
Picker("Price Source", selection: $satsViewModel.priceSource) {
|
||||
ForEach(PriceSource.allCases, id: \.self) {
|
||||
Text($0.description)
|
||||
}
|
||||
@@ -97,14 +50,14 @@ public struct ContentView: View {
|
||||
|
||||
HStack {
|
||||
TextField("1 BTC to \(satsViewModel.currentCurrency.identifier)", text: satsViewModel.btcToCurrencyString(for: satsViewModel.currentCurrency))
|
||||
.disabled(priceSource != .manual)
|
||||
.disabled(satsViewModel.priceSource != .manual)
|
||||
#if os(iOS) || SKIP
|
||||
.keyboardType(.decimalPad)
|
||||
#endif
|
||||
if priceSource != .manual {
|
||||
if satsViewModel.priceSource != .manual {
|
||||
Button(action: {
|
||||
Task {
|
||||
await updatePrice()
|
||||
await satsViewModel.updatePrice()
|
||||
}
|
||||
}) {
|
||||
Image(systemName: "arrow.clockwise.circle")
|
||||
@@ -114,31 +67,27 @@ public struct ContentView: View {
|
||||
} header: {
|
||||
Text("1 BTC to \(satsViewModel.currentCurrency.identifier)")
|
||||
} footer: {
|
||||
if priceSource != .manual, let lastUpdated = satsViewModel.lastUpdated {
|
||||
if satsViewModel.priceSource != .manual, let lastUpdated = satsViewModel.lastUpdated {
|
||||
Text("Last updated: \(dateFormatter.string(from: lastUpdated))")
|
||||
}
|
||||
}
|
||||
|
||||
Section {
|
||||
TextField("Sats", text: $satsViewModel.satsString)
|
||||
HStack {
|
||||
Text("Sats")
|
||||
TextField("Sats", text: $satsViewModel.satsString)
|
||||
#if os(iOS) || SKIP
|
||||
.keyboardType(.numberPad)
|
||||
.keyboardType(.numberPad)
|
||||
#endif
|
||||
} header: {
|
||||
Text("Sats")
|
||||
} footer: {
|
||||
if satsViewModel.exceedsMaximum {
|
||||
Text("2100000000000000 sats is the maximum.")
|
||||
}
|
||||
}
|
||||
|
||||
Section {
|
||||
TextField("BTC", text: $satsViewModel.btcString)
|
||||
HStack {
|
||||
Text("BTC")
|
||||
TextField("BTC", text: $satsViewModel.btcString)
|
||||
#if os(iOS) || SKIP
|
||||
.keyboardType(.decimalPad)
|
||||
.keyboardType(.decimalPad)
|
||||
#endif
|
||||
} header: {
|
||||
Text("BTC")
|
||||
}
|
||||
} footer: {
|
||||
if satsViewModel.exceedsMaximum {
|
||||
Text("21000000 BTC is the maximum.")
|
||||
@@ -146,38 +95,41 @@ public struct ContentView: View {
|
||||
}
|
||||
|
||||
Section {
|
||||
TextField(satsViewModel.currentCurrency.identifier, text: satsViewModel.currencyValueString(for: satsViewModel.currentCurrency))
|
||||
HStack {
|
||||
Text(satsViewModel.currentCurrency.identifier)
|
||||
TextField(satsViewModel.currentCurrency.identifier, text: satsViewModel.currencyValueString(for: satsViewModel.currentCurrency))
|
||||
#if os(iOS) || SKIP
|
||||
.keyboardType(.decimalPad)
|
||||
.keyboardType(.decimalPad)
|
||||
#endif
|
||||
} header: {
|
||||
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))
|
||||
Section {
|
||||
if satsViewModel.priceSource != .manual {
|
||||
ForEach(satsViewModel.selectedCurrencies.sorted { $0.identifier < $1.identifier }.filter { $0 != satsViewModel.currentCurrency }, id: \.identifier) { currency in
|
||||
HStack {
|
||||
Text(currency.identifier)
|
||||
TextField(currency.identifier, text: satsViewModel.currencyValueString(for: currency))
|
||||
#if os(iOS) || SKIP
|
||||
.keyboardType(.decimalPad)
|
||||
.keyboardType(.decimalPad)
|
||||
#endif
|
||||
} header: {
|
||||
Text(currencyAndPrice.key.identifier)
|
||||
}
|
||||
.tag(currency.identifier)
|
||||
}
|
||||
.tag(currencyAndPrice.key.identifier)
|
||||
}
|
||||
}
|
||||
|
||||
if satsViewModel.priceSource != .manual {
|
||||
addCurrencyView
|
||||
}
|
||||
}
|
||||
.task {
|
||||
await updatePrice()
|
||||
await satsViewModel.updatePrice()
|
||||
}
|
||||
.onChange(of: priceSource) { newPriceSource in
|
||||
.onChange(of: satsViewModel.priceSource) { newPriceSource in
|
||||
satsViewModel.lastUpdated = nil
|
||||
priceFetcherDelegator.priceSource = newPriceSource
|
||||
Task {
|
||||
await updatePrice()
|
||||
await satsViewModel.updatePrice()
|
||||
}
|
||||
}
|
||||
#if os(macOS)
|
||||
@@ -188,9 +140,5 @@ public struct ContentView: View {
|
||||
}
|
||||
|
||||
#Preview {
|
||||
#if DEBUG
|
||||
ContentView(.fake)
|
||||
#else
|
||||
ContentView(.coinbase)
|
||||
#endif
|
||||
ContentView()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user