comparison Simoleon/Helpers/CurrencySelector.swift @ 59:1303c1e50843

Fixes slow list updates
author Dennis Concepción Martín <dennisconcepcionmartin@gmail.com>
date Tue, 27 Jul 2021 18:31:54 +0100
parents 7a6a7c677851
children 84ce5e5f0381
comparison
equal deleted inserted replaced
58:cdb5ec112739 59:1303c1e50843
16 @State private var showingSubscriptionPaywall = false 16 @State private var showingSubscriptionPaywall = false
17 @State private var alertTitle = "" 17 @State private var alertTitle = ""
18 @State private var alertMessage = "" 18 @State private var alertMessage = ""
19 @State private var showingAlert = false 19 @State private var showingAlert = false
20 20
21 let currencyPairs: [String] = parseJson("CurrencyPairs.json")
22
21 var body: some View { 23 var body: some View {
22 NavigationView { 24 NavigationView {
23 Form { 25 VStack {
24 TextField("Search ...", text: $searchCurrency) 26 TextField("Search ...", text: $searchCurrency)
25 .accessibilityIdentifier("SearchBar") 27 .padding(10)
26 28 .background(
27 Section(header: Text("All currencies", comment: "Section header in currency selector")) { 29 RoundedRectangle(cornerRadius: 15)
28 ForEach(currencyPairs(), id: \.self) { currencyPair in 30 .foregroundColor(Color(.systemGray6))
29 Button(action: { select(currencyPair) }) { 31 )
30 CurrencyRow(currencyPair: currencyPair) 32 .padding()
31 } 33
34 List(searchResults, id: \.self) { currencyPair in
35 Button(action: { select(currencyPair) }) {
36 CurrencyRow(currencyPair: currencyPair)
32 } 37 }
33 } 38 }
39 .id(UUID())
40 .listStyle(PlainListStyle())
41 .gesture(DragGesture()
42 .onChanged({ _ in
43 UIApplication.shared.dismissKeyboard()
44 })
45 )
34 } 46 }
35 .gesture(DragGesture()
36 .onChanged({ _ in
37 UIApplication.shared.dismissKeyboard()
38 })
39 )
40 .sheet(isPresented: $showingSubscriptionPaywall) { 47 .sheet(isPresented: $showingSubscriptionPaywall) {
41 SubscriptionPaywall(showingSubscriptionPaywall: $showingSubscriptionPaywall) 48 SubscriptionPaywall(showingSubscriptionPaywall: $showingSubscriptionPaywall)
42 } 49 }
43 .navigationTitle(Text("Currencies", comment: "Navigation title")) 50 .navigationTitle("Currencies")
44 .navigationBarTitleDisplayMode(.inline) 51 .navigationBarTitleDisplayMode(.inline)
45 .toolbar { 52 .toolbar {
46 ToolbarItem(placement: .cancellationAction) { 53 ToolbarItem(placement: .cancellationAction) {
47 Button(action: { showingCurrencySelector = false }) { 54 Button(action: { showingCurrencySelector = false }) {
48 Text("Cancel", comment: "Button to dismiss currency selector") 55 Text("Cancel")
49 } 56 }
50 } 57 }
51 } 58 }
52 } 59 }
53 .alert(isPresented: $showingAlert) { 60 .alert(isPresented: $showingAlert) {
59 If searched currency string is empty: 66 If searched currency string is empty:
60 * Show all currencies 67 * Show all currencies
61 else: 68 else:
62 * Show filtered list of currencies containing searched currency string 69 * Show filtered list of currencies containing searched currency string
63 */ 70 */
64 private func currencyPairs() -> [String] { 71 var searchResults: [String] {
65 let currencyPairs: [String] = parseJson("CurrencyPairs.json")
66
67 if searchCurrency.isEmpty { 72 if searchCurrency.isEmpty {
68 return currencyPairs 73 return currencyPairs
69 } else { 74 } else {
70 return currencyPairs.filter { $0.contains(searchCurrency.uppercased()) } 75 return currencyPairs.filter { $0.contains(searchCurrency.uppercased()) }
71 } 76 }
72 } 77 }
73
74 78
75 /* 79 /*
76 If user is subscribed: 80 If user is subscribed:
77 * Select currency and dismiss currency selector 81 * Select currency and dismiss currency selector
78 else: 82 else:
104 } 108 }
105 109
106 110
107 struct CurrencySelector_Previews: PreviewProvider { 111 struct CurrencySelector_Previews: PreviewProvider {
108 static var previews: some View { 112 static var previews: some View {
109 CurrencySelector(currencyPair: .constant("USD/GBP"), showingCurrencySelector: .constant(false)) 113 CurrencySelector(
114 currencyPair: .constant("USD/GBP"),
115 showingCurrencySelector: .constant(false)
116 )
110 } 117 }
111 } 118 }