Mercurial > public > simoleon
comparison 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 |
comparison
equal
deleted
inserted
replaced
182:ba3ebe8cefe5 | 183:d2398f02e1ce |
---|---|
1 // | |
2 // CurrencyList.swift | |
3 // Simoleon | |
4 // | |
5 // Created by Dennis Concepción Martín on 20/12/21. | |
6 // | |
7 | |
8 import SwiftUI | |
9 | |
10 | |
11 struct CurrencyList: View { | |
12 @Binding var baseCurrency: SupportedCurrencyResult | |
13 @Binding var quoteCurrency: SupportedCurrencyResult | |
14 var selecting: Selection | |
15 | |
16 var body: some View { | |
17 NavigationView { | |
18 List { | |
19 let currencies = getCurrencies() | |
20 ForEach(currencies, id: \.self) { currency in | |
21 Text(currency.code) | |
22 } | |
23 } | |
24 .navigationTitle("Currencies") | |
25 } | |
26 } | |
27 | |
28 // MARK: - Get compatible currencies given currency code | |
29 func getCurrencies() -> [SupportedCurrencyResult] { | |
30 let pairs: SupportedPairResponse = readJson(from: "SupportedCurrencies.json") | |
31 let currencies: SupportedCurrencyResponse = readJson(from: "SupportedCurrencies.json") | |
32 var supportedCurrencies = [SupportedCurrencyResult]() | |
33 | |
34 if selecting == .baseCurrency { | |
35 // Get base currencies compatible with quote currency | |
36 let currencyPair = pairs.pairs.filter { $0.toCurrency == quoteCurrency.code } | |
37 for currencyPair in currencyPair { | |
38 let currency = currencies.currencies.filter { $0.code == currencyPair.fromCurrency } | |
39 supportedCurrencies.append(contentsOf: currency) | |
40 } | |
41 } else { | |
42 // Get quote currencies compatible with base currencies | |
43 let currencyPair = pairs.pairs.filter { $0.fromCurrency == baseCurrency.code } | |
44 for currencyPair in currencyPair { | |
45 let currency = currencies.currencies.filter { $0.code == currencyPair.toCurrency } | |
46 supportedCurrencies.append(contentsOf: currency) | |
47 } | |
48 } | |
49 | |
50 | |
51 return supportedCurrencies | |
52 } | |
53 } | |
54 | |
55 struct CurrencyList_Previews: PreviewProvider { | |
56 static var previews: some View { | |
57 CurrencyList( | |
58 baseCurrency: .constant(SupportedCurrencyResult(code: "EUR", name: "Euro", isCrypto: 0)), | |
59 quoteCurrency: .constant(SupportedCurrencyResult(code: "CHF", name: "Swiss Franc", isCrypto: 0)), | |
60 selecting: .baseCurrency | |
61 ) | |
62 } | |
63 } |