Create separate CurrencyPickerView and tighten up ContentView
This commit is contained in:
84
Sources/SatsPrice/CurrencyPickerView.swift
Normal file
84
Sources/SatsPrice/CurrencyPickerView.swift
Normal file
@@ -0,0 +1,84 @@
|
||||
// This is free software: you can redistribute and/or modify it
|
||||
// under the terms of the GNU General Public License 3.0
|
||||
// as published by the Free Software Foundation https://fsf.org
|
||||
//
|
||||
// CurrencyPickerView.swift
|
||||
// sats-price
|
||||
//
|
||||
// Created by Terry Yiu on 11/10/24.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
|
||||
struct CurrencyPickerView: View {
|
||||
@ObservedObject var satsViewModel: SatsViewModel
|
||||
|
||||
var body: some View {
|
||||
let currentCurrency = satsViewModel.currentCurrency
|
||||
|
||||
List {
|
||||
Section("Current Currency") {
|
||||
let currentCurrency = satsViewModel.currentCurrency
|
||||
if let localizedCurrency = Locale.current.localizedString(forCurrencyCode: currentCurrency.identifier) {
|
||||
Text("\(currentCurrency.identifier) - \(localizedCurrency)")
|
||||
} else {
|
||||
Text(currentCurrency.identifier)
|
||||
}
|
||||
}
|
||||
|
||||
if !satsViewModel.selectedCurrencies.isEmpty {
|
||||
Section("Selected Currencies") {
|
||||
ForEach(satsViewModel.selectedCurrencies.filter { $0 != currentCurrency }.sorted { $0.identifier < $1.identifier }, id: \.identifier) { currency in
|
||||
Button(
|
||||
action: {
|
||||
satsViewModel.selectedCurrencies.remove(currency)
|
||||
},
|
||||
label: {
|
||||
HStack {
|
||||
Group {
|
||||
if let localizedCurrency = Locale.current.localizedString(forCurrencyCode: currency.identifier) {
|
||||
Text("\(currency.identifier) - \(localizedCurrency)")
|
||||
} else {
|
||||
Text(currency.identifier)
|
||||
}
|
||||
}
|
||||
.frame(maxWidth: .infinity, alignment: .leading)
|
||||
|
||||
Image(systemName: "checkmark")
|
||||
}
|
||||
}
|
||||
)
|
||||
.buttonStyle(.plain)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Section("Currencies") {
|
||||
ForEach(satsViewModel.currencies.filter { $0 != currentCurrency && !satsViewModel.selectedCurrencies.contains($0) }, id: \.identifier) { currency in
|
||||
Button(
|
||||
action: {
|
||||
satsViewModel.selectedCurrencies.insert(currency)
|
||||
},
|
||||
label: {
|
||||
if let localizedCurrency = Locale.current.localizedString(forCurrencyCode: currency.identifier) {
|
||||
Text("\(currency.identifier) - \(localizedCurrency)")
|
||||
} else {
|
||||
Text(currency.identifier)
|
||||
}
|
||||
}
|
||||
)
|
||||
.buttonStyle(.plain)
|
||||
}
|
||||
}
|
||||
}
|
||||
.onDisappear(perform: {
|
||||
Task {
|
||||
await satsViewModel.updatePrice()
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#Preview {
|
||||
CurrencyPickerView(satsViewModel: SatsViewModel())
|
||||
}
|
||||
Reference in New Issue
Block a user