Change currency picker style to navigation link

This commit is contained in:
2024-09-14 11:35:22 -04:00
parent ba0be8bb93
commit e38c8dac85

View File

@@ -39,102 +39,105 @@ public struct ContentView: View {
} }
public var body: some View { public var body: some View {
Form { NavigationStack {
Section { Form {
Picker("Price Source", selection: $priceSource) { Section {
ForEach(PriceSource.allCases, id: \.self) { Picker("Price Source", selection: $priceSource) {
Text($0.description) ForEach(PriceSource.allCases, id: \.self) {
} Text($0.description)
}
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)
} }
} }
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)
}
}
}
.pickerStyle(.navigationLink)
HStack {
TextField("", text: $satsViewModel.btcToCurrencyString)
.disabled(priceSource != .manual)
#if os(iOS) || SKIP
.keyboardType(.decimalPad)
#endif
if priceSource != .manual {
Button(action: {
Task {
await updatePrice()
}
}) {
Image(systemName: "arrow.clockwise.circle")
}
}
}
} header: {
Text("1 BTC to \(satsViewModel.selectedCurrency.identifier)")
} footer: {
if priceSource != .manual, let lastUpdated = satsViewModel.lastUpdated {
Text("Last updated: \(dateFormatter.string(from: lastUpdated))")
}
} }
HStack { Section {
TextField("", text: $satsViewModel.btcToCurrencyString) TextField("", text: $satsViewModel.satsString)
.disabled(priceSource != .manual) #if os(iOS) || SKIP
.keyboardType(.numberPad)
#endif
} header: {
Text("Sats")
} footer: {
if satsViewModel.exceedsMaximum {
Text("2100000000000000 sats is the maximum.")
}
}
Section {
TextField("", text: $satsViewModel.btcString)
#if os(iOS) || SKIP #if os(iOS) || SKIP
.keyboardType(.decimalPad) .keyboardType(.decimalPad)
#endif #endif
if priceSource != .manual { } header: {
Button(action: { Text("BTC")
Task { } footer: {
await updatePrice() if satsViewModel.exceedsMaximum {
} Text("21000000 BTC is the maximum.")
}) {
Image(systemName: "arrow.clockwise.circle")
}
} }
} }
} header: {
Text("1 BTC to \(satsViewModel.selectedCurrency.identifier)") Section {
} footer: { TextField("", text: $satsViewModel.currencyValueString)
if priceSource != .manual, let lastUpdated = satsViewModel.lastUpdated { #if os(iOS) || SKIP
Text("Last updated: \(dateFormatter.string(from: lastUpdated))") .keyboardType(.decimalPad)
#endif
} header: {
Text(satsViewModel.selectedCurrency.identifier)
} }
} }
.task {
Section {
TextField("", text: $satsViewModel.satsString)
#if os(iOS) || SKIP
.keyboardType(.numberPad)
#endif
} header: {
Text("Sats")
} footer: {
if satsViewModel.exceedsMaximum {
Text("2100000000000000 sats is the maximum.")
}
}
Section {
TextField("", text: $satsViewModel.btcString)
#if os(iOS) || SKIP
.keyboardType(.decimalPad)
#endif
} header: {
Text("BTC")
} footer: {
if satsViewModel.exceedsMaximum {
Text("21000000 BTC is the maximum.")
}
}
Section {
TextField("", text: $satsViewModel.currencyValueString)
#if os(iOS) || SKIP
.keyboardType(.decimalPad)
#endif
} header: {
Text(satsViewModel.selectedCurrency.identifier)
}
}
.task {
await updatePrice()
}
.onChange(of: satsViewModel.selectedCurrency) { newCurrency in
satsViewModel.lastUpdated = nil
Task {
await updatePrice() await updatePrice()
} }
} .onChange(of: satsViewModel.selectedCurrency) { newCurrency in
.onChange(of: priceSource) { newPriceSource in satsViewModel.lastUpdated = nil
satsViewModel.lastUpdated = nil Task {
priceFetcherDelegator.priceSource = newPriceSource await updatePrice()
Task { }
await updatePrice() }
.onChange(of: priceSource) { newPriceSource in
satsViewModel.lastUpdated = nil
priceFetcherDelegator.priceSource = newPriceSource
Task {
await updatePrice()
}
} }
}
#if os(macOS) #if os(macOS)
.formStyle(.grouped) .formStyle(.grouped)
#endif #endif
}
} }
} }