comparison Simoleon/UI/CurrencySelector.swift @ 161:3913aff613e8

Fix bug that didn't request API on symbol change
author Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
date Tue, 31 Aug 2021 10:57:34 +0100
parents 0c589138a6f3
children
comparison
equal deleted inserted replaced
160:0c589138a6f3 161:3913aff613e8
6 // 6 //
7 7
8 import SwiftUI 8 import SwiftUI
9 9
10 struct CurrencySelector: View { 10 struct CurrencySelector: View {
11 @ObservedObject var currencyPair: CurrencyPair 11 @ObservedObject var currencyConversion: CurrencyConversion
12 @State private var modalSelection: ModalType = .allCurrencies
12 @State private var showingList = false 13 @State private var showingList = false
13 @State private var modalSelection: ModalType = .allCurrencies 14
14 let currencyPairsSupported: [String] = try! readJson(from: "CurrencyPairsSupported.json") 15 let currencyPairsSupported: [String] = try! readJson(from: "CurrencyPairsSupported.json")
15 16
16 private enum ModalType { 17 private enum ModalType {
17 case allCurrencies, compatibleCurrencies 18 case allCurrencies, compatibleCurrencies
18 } 19 }
21 HStack { 22 HStack {
22 Button(action: { 23 Button(action: {
23 modalSelection = .allCurrencies 24 modalSelection = .allCurrencies
24 showingList = true 25 showingList = true
25 }) { 26 }) {
26 CurrencyButton(selectedCurrency: currencyPair.baseSymbol) 27 CurrencyButton(selectedCurrency: currencyConversion.baseSymbol)
27 } 28 }
28 29
29 Button(action: { 30 Button(action: {
30 modalSelection = .compatibleCurrencies 31 modalSelection = .compatibleCurrencies
31 showingList = true 32 showingList = true
32 }) { 33 }) {
33 CurrencyButton(selectedCurrency: currencyPair.quoteSymbol) 34 CurrencyButton(selectedCurrency: currencyConversion.quoteSymbol)
34 } 35 }
35 } 36 }
36 .onChange(of: currencyPair.baseSymbol) { _ in 37 .onChange(of: currencyConversion.baseSymbol) { _ in
37 // If the previous quote symbol is not compatible anymore with base symbol 38 // If the previous quote symbol is not compatible anymore with base symbol
38 // return the first symbol of the new compatible symbols list 39 // return the first symbol of the new compatible symbols list
39 let compatibleCurrencies = get(currencyType: .compatible(with: currencyPair.baseSymbol), from: currencyPairsSupported) 40 let compatibleCurrencies = get(currencyType: .compatible(with: currencyConversion.baseSymbol), from: currencyPairsSupported)
40 if !compatibleCurrencies.contains(currencyPair.quoteSymbol) { 41 if !compatibleCurrencies.contains(currencyConversion.quoteSymbol) {
41 currencyPair.quoteSymbol = compatibleCurrencies.sorted().first! 42 currencyConversion.quoteSymbol = compatibleCurrencies.sorted().first!
42 } 43 }
43 } 44 }
44 .sheet(isPresented: $showingList) { 45 .sheet(isPresented: $showingList) {
45 if modalSelection == .allCurrencies { 46 if modalSelection == .allCurrencies {
46 let currencies = get(currencyType: .all, from: currencyPairsSupported) 47 let currencies = get(currencyType: .all, from: currencyPairsSupported)
47 CurrencyList(currencies: currencies, selectedCurrency: $currencyPair.baseSymbol) 48 CurrencyList(currencies: currencies, selection: .baseSymbol, currencyConversion: currencyConversion)
48 } else { 49 } else {
49 let currencies = get(currencyType: .compatible(with: currencyPair.baseSymbol), from: currencyPairsSupported) 50 let currencies = get(currencyType: .compatible(with: currencyConversion.baseSymbol), from: currencyPairsSupported)
50 CurrencyList(currencies: currencies, selectedCurrency: $currencyPair.quoteSymbol) 51 CurrencyList(currencies: currencies, selection: .quoteSymbol, currencyConversion: currencyConversion)
51 } 52 }
52 } 53 }
53 } 54 }
54 55
55 enum CurrencyType { 56 enum CurrencyType {
82 } 83 }
83 } 84 }
84 85
85 struct CurrencySelector_Previews: PreviewProvider { 86 struct CurrencySelector_Previews: PreviewProvider {
86 static var previews: some View { 87 static var previews: some View {
87 CurrencySelector(currencyPair: CurrencyPair()) 88 CurrencySelector(currencyConversion: CurrencyConversion())
88 } 89 }
89 } 90 }