comparison Simoleon/UI/CurrencyList.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 1940db1ef321
comparison
equal deleted inserted replaced
160:0c589138a6f3 161:3913aff613e8
7 7
8 import SwiftUI 8 import SwiftUI
9 9
10 struct CurrencyList: View { 10 struct CurrencyList: View {
11 var currencies: [String] 11 var currencies: [String]
12 @Binding var selectedCurrency: String 12 var selection: Selection
13 @ObservedObject var currencyConversion: CurrencyConversion
13 @State private var searchCurrency = "" 14 @State private var searchCurrency = ""
15
14 @Environment(\.presentationMode) private var presentation 16 @Environment(\.presentationMode) private var presentation
15 let currencyDetails: [String: CurrencyModel] = try! readJson(from: "Currencies.json") 17 let currencyDetails: [String: CurrencyModel] = try! readJson(from: "Currencies.json")
16 18
17 var searchResults: [String] { 19 var searchResults: [String] {
18 if searchCurrency.isEmpty { 20 if searchCurrency.isEmpty {
28 SearchBar(placeholder: "Search...", text: $searchCurrency) 30 SearchBar(placeholder: "Search...", text: $searchCurrency)
29 .padding(.vertical) 31 .padding(.vertical)
30 .accessibilityIdentifier("CurrencySearchBar") 32 .accessibilityIdentifier("CurrencySearchBar")
31 33
32 ForEach(searchResults, id: \.self) { symbol in 34 ForEach(searchResults, id: \.self) { symbol in
33 Button(action: {selectedCurrency = symbol; presentation.wrappedValue.dismiss()}) { 35 Button(action: {
36 if selection == .baseSymbol {
37 currencyConversion.baseSymbol = symbol
38 } else {
39 currencyConversion.quoteSymbol = symbol
40 }
41
42 presentation.wrappedValue.dismiss()
43 }) {
34 let currency = currencyDetails[symbol]! 44 let currency = currencyDetails[symbol]!
35 CurrencyRow(currency: currency) 45 CurrencyRow(currency: currency)
36 } 46 }
37 } 47 }
38 } 48 }
46 } 56 }
47 } 57 }
48 } 58 }
49 } 59 }
50 } 60 }
61
62 enum Selection {
63 case baseSymbol, quoteSymbol
64 }
51 } 65 }
52 extension View { 66 extension View {
53 func listStyle() -> some View { 67 func listStyle() -> some View {
54 self.modifier(ListModifier()) 68 self.modifier(ListModifier())
55 } 69 }
56 } 70 }
57 71
58 struct CurrencyList_Previews: PreviewProvider { 72 struct CurrencyList_Previews: PreviewProvider {
59 static var previews: some View { 73 static var previews: some View {
60 CurrencyList(currencies: ["USD"], selectedCurrency: .constant("USD")) 74 CurrencyList(currencies: ["USD"], selection: .baseSymbol, currencyConversion: CurrencyConversion())
61 } 75 }
62 } 76 }