Mercurial > public > simoleon
diff Simoleon/Helpers/CurrencyList.swift @ 183:d2398f02e1ce
implement unit currency selector
author | Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com> |
---|---|
date | Mon, 20 Dec 2021 12:28:16 +0100 |
parents | |
children | 2fc95efcb1ee |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Simoleon/Helpers/CurrencyList.swift Mon Dec 20 12:28:16 2021 +0100 @@ -0,0 +1,63 @@ +// +// CurrencyList.swift +// Simoleon +// +// Created by Dennis Concepción Martín on 20/12/21. +// + +import SwiftUI + + +struct CurrencyList: View { + @Binding var baseCurrency: SupportedCurrencyResult + @Binding var quoteCurrency: SupportedCurrencyResult + var selecting: Selection + + var body: some View { + NavigationView { + List { + let currencies = getCurrencies() + ForEach(currencies, id: \.self) { currency in + Text(currency.code) + } + } + .navigationTitle("Currencies") + } + } + + // MARK: - Get compatible currencies given currency code + func getCurrencies() -> [SupportedCurrencyResult] { + let pairs: SupportedPairResponse = readJson(from: "SupportedCurrencies.json") + let currencies: SupportedCurrencyResponse = readJson(from: "SupportedCurrencies.json") + var supportedCurrencies = [SupportedCurrencyResult]() + + if selecting == .baseCurrency { + // Get base currencies compatible with quote currency + let currencyPair = pairs.pairs.filter { $0.toCurrency == quoteCurrency.code } + for currencyPair in currencyPair { + let currency = currencies.currencies.filter { $0.code == currencyPair.fromCurrency } + supportedCurrencies.append(contentsOf: currency) + } + } else { + // Get quote currencies compatible with base currencies + let currencyPair = pairs.pairs.filter { $0.fromCurrency == baseCurrency.code } + for currencyPair in currencyPair { + let currency = currencies.currencies.filter { $0.code == currencyPair.toCurrency } + supportedCurrencies.append(contentsOf: currency) + } + } + + + return supportedCurrencies + } +} + +struct CurrencyList_Previews: PreviewProvider { + static var previews: some View { + CurrencyList( + baseCurrency: .constant(SupportedCurrencyResult(code: "EUR", name: "Euro", isCrypto: 0)), + quoteCurrency: .constant(SupportedCurrencyResult(code: "CHF", name: "Swiss Franc", isCrypto: 0)), + selecting: .baseCurrency + ) + } +}