Mercurial > public > simoleon
comparison Simoleon/Helpers/CurrencySelector.swift @ 21:c3dda63f50ed v1.1
Added Core Data and UI changes
- Implement Watchlist
- Change conversion design
- Improve UX
author | Dennis Concepción Martín <dennisconcepcionmartin@gmail.com> |
---|---|
date | Mon, 19 Jul 2021 19:27:12 +0100 |
parents | 94fd7ac93060 |
children | 4f862c618b44 |
comparison
equal
deleted
inserted
replaced
20:f8aabe5b7251 | 21:c3dda63f50ed |
---|---|
6 // | 6 // |
7 | 7 |
8 import SwiftUI | 8 import SwiftUI |
9 | 9 |
10 struct CurrencySelector: View { | 10 struct CurrencySelector: View { |
11 @Binding var mainCurrencySelected: String | 11 @Binding var currencyPair: String |
12 @Binding var secondaryCurrencySelected: String | |
13 @Binding var showingCurrencySelector: Bool | 12 @Binding var showingCurrencySelector: Bool |
14 @Binding var selectingMainCurrency: Bool | 13 @State private var searchCurrency = "" |
14 @State private var searching = false | |
15 | 15 |
16 var body: some View { | 16 var body: some View { |
17 NavigationView { | 17 NavigationView { |
18 Form { | 18 Form { |
19 ForEach(generateCurrencyList(), id: \.self) { currency in | 19 TextField("Search ...", text: $searchCurrency) { startedEditing in |
20 Button(action: { select(currency) }) { | 20 if startedEditing { |
21 CurrencyRow(currency: currency) | 21 withAnimation { |
22 searching = true | |
23 } | |
24 } | |
25 } onCommit: { | |
26 withAnimation { | |
27 searching = false | |
28 } | |
29 } | |
30 | |
31 Section(header: Text("All currencies")) { | |
32 ForEach(currencyPairs(), id: \.self) { currencyPair in | |
33 Button(action: { | |
34 self.currencyPair = currencyPair | |
35 showingCurrencySelector = false | |
36 }) { | |
37 CurrencyRow(currencyPair: currencyPair) | |
38 } | |
22 } | 39 } |
23 } | 40 } |
24 } | 41 } |
42 .gesture(DragGesture() | |
43 .onChanged({ _ in | |
44 UIApplication.shared.dismissKeyboard() | |
45 }) | |
46 ) | |
25 .navigationTitle("Currencies") | 47 .navigationTitle("Currencies") |
26 .navigationBarTitleDisplayMode(.inline) | 48 .navigationBarTitleDisplayMode(.inline) |
27 .toolbar { | 49 .toolbar { |
28 ToolbarItem(placement: .confirmationAction) { | 50 ToolbarItem(placement: .confirmationAction) { |
29 Button("OK", action: { showingCurrencySelector = false }) | 51 Button("OK", action: { showingCurrencySelector = false }) |
30 } | 52 } |
53 | |
54 ToolbarItem(placement: .cancellationAction) { | |
55 if searching { | |
56 Button("Cancel") { | |
57 searchCurrency = "" | |
58 withAnimation { | |
59 searching = false | |
60 UIApplication.shared.dismissKeyboard() | |
61 } | |
62 } | |
63 } | |
64 } | |
31 } | 65 } |
32 } | 66 } |
33 } | 67 } |
34 | 68 |
35 private func generateCurrencyList() -> [String] { | 69 private func currencyPairs() -> [String] { |
36 let currencyPairs: [String] = parseJson("CurrencyPairs.json") | 70 let currencyPairs: [String] = parseJson("CurrencyPairs.json") |
37 var currencies: [String] = [] | |
38 | 71 |
39 for currencyPair in currencyPairs { | 72 if searchCurrency.isEmpty { |
40 let splittedCurrencies = currencyPair.split(separator: "/") | 73 return currencyPairs |
41 let mainCurrency = String(splittedCurrencies[0]) | 74 } else { |
42 if !currencies.contains(mainCurrency) { | 75 return currencyPairs.filter { $0.contains(searchCurrency.uppercased()) } |
43 currencies.append(mainCurrency) | |
44 } | |
45 } | 76 } |
46 | |
47 return currencies | |
48 } | |
49 | |
50 private func select(_ currency: String) { | |
51 if selectingMainCurrency { | |
52 self.mainCurrencySelected = currency | |
53 } else { | |
54 self.secondaryCurrencySelected = currency | |
55 } | |
56 | |
57 showingCurrencySelector = false | |
58 } | 77 } |
59 } | 78 } |
60 | 79 |
80 | |
61 struct CurrencySelector_Previews: PreviewProvider { | 81 struct CurrencySelector_Previews: PreviewProvider { |
62 static var previews: some View { | 82 static var previews: some View { |
63 CurrencySelector(mainCurrencySelected: .constant(""), secondaryCurrencySelected: .constant(""), showingCurrencySelector: .constant(false), selectingMainCurrency: .constant(true)) | 83 CurrencySelector(currencyPair: .constant("USD/GBP"), showingCurrencySelector: .constant(false)) |
64 } | 84 } |
65 } | 85 } |