comparison Simoleon/UI/CurrencySelector.swift @ 157:8c3bbd640103

Implement Currency Selector
author Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
date Sat, 28 Aug 2021 11:15:41 +0100
parents
children 35628bac01f5
comparison
equal deleted inserted replaced
156:84137052813d 157:8c3bbd640103
1 //
2 // CurrencySelector.swift
3 // Simoleon
4 //
5 // Created by Dennis Concepción Martín on 27/8/21.
6 //
7
8 import SwiftUI
9
10 struct CurrencySelector: View {
11 @State var currencyPair: CurrencyPairModel
12 @State private var showingList = false
13 @State private var modalSelection: ModalType? = nil
14 let currencyPairsSupported: [String] = try! read(json: "CurrencyPairsSupported.json")
15
16 private enum ModalType {
17 case allCurrencies, compatibleCurrencies
18 }
19
20 var body: some View {
21 HStack {
22 Button(action: {
23 showingList = true
24 modalSelection = .allCurrencies
25 }) {
26 CurrencyButton(selectedCurrency: currencyPair.baseSymbol)
27 }
28
29 Button(action: {
30 showingList = true
31 modalSelection = .compatibleCurrencies
32 }) {
33 CurrencyButton(selectedCurrency: currencyPair.quoteSymbol)
34 }
35 }
36 .onChange(of: currencyPair.baseSymbol) { _ in
37 // If the previous quote symbol is not compatible anymore with base symbol
38 // return the first symbol of the new compatible symbols list
39 let compatibleCurrencies = get(currencyType: .compatible(with: currencyPair.baseSymbol), from: currencyPairsSupported)
40 if !compatibleCurrencies.contains(currencyPair.quoteSymbol) {
41 currencyPair.quoteSymbol = compatibleCurrencies.sorted().first!
42 }
43 }
44 .sheet(isPresented: $showingList) {
45 if modalSelection == .allCurrencies {
46 let currencies = get(currencyType: .all, from: currencyPairsSupported)
47 CurrencyList(currencies: currencies, selectedCurrency: $currencyPair.baseSymbol)
48 } else {
49 let currencies = get(currencyType: .compatible(with: currencyPair.baseSymbol), from: currencyPairsSupported)
50 CurrencyList(currencies: currencies, selectedCurrency: $currencyPair.quoteSymbol)
51 }
52 }
53 }
54
55 enum CurrencyType {
56 case all
57 case compatible(with: String)
58 }
59
60 func get(currencyType: CurrencyType, from currencyPairsSupported: [String]) -> [String] {
61 var currencies = Set<String>()
62
63 switch currencyType {
64 case .all:
65 for currencyPairSupported in currencyPairsSupported {
66 let currency = currencyPairSupported.components(separatedBy: "/")[0]
67 currencies.insert(currency)
68 }
69
70 return Array(currencies)
71
72 case .compatible(with: let symbol):
73 for currencyPairSupported in currencyPairsSupported {
74 if currencyPairSupported.hasPrefix(symbol) {
75 let compatibleCurrency = currencyPairSupported.components(separatedBy: "/")[1]
76 currencies.insert(compatibleCurrency)
77 }
78 }
79
80 return Array(currencies)
81 }
82 }
83 }
84
85 struct CurrencySelector_Previews: PreviewProvider {
86 static var previews: some View {
87 CurrencySelector(currencyPair: CurrencyPairModel(baseSymbol: "USD", quoteSymbol: "EUR"))
88 }
89 }