Mercurial > public > simoleon
comparison Simoleon/Conversion.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 | 3596690dda73 |
comparison
equal
deleted
inserted
replaced
20:f8aabe5b7251 | 21:c3dda63f50ed |
---|---|
7 | 7 |
8 import SwiftUI | 8 import SwiftUI |
9 import Alamofire | 9 import Alamofire |
10 | 10 |
11 struct Conversion: View { | 11 struct Conversion: View { |
12 @State private var mainCurrency = "USD" | 12 @State private var currencyPair = "USD/GBP" |
13 @State private var secondaryCurrency = "GBP" | |
14 @State private var amountToConvert = "1000" | 13 @State private var amountToConvert = "1000" |
15 @State private var price: Double = 1.00 | 14 @State private var price: Double = 1.00 |
16 @State private var showingConversion = false | 15 @State private var showingConversion = false |
17 @State private var showingCurrencySelector = false | 16 @State private var showingCurrencySelector = false |
18 @State private var selectingMainCurrency = false | 17 @State private var isEditing = false |
19 @State private var currencyPairNotFound = false | |
20 | 18 |
21 let currencyMetadata: [String: CurrencyMetadataModel] = parseJson("CurrencyMetadata.json") | 19 let currencyMetadata: [String: CurrencyMetadataModel] = parseJson("CurrencyMetadata.json") |
22 | 20 |
23 var body: some View { | 21 var body: some View { |
24 ScrollView(showsIndicators: false) { | 22 ScrollView(showsIndicators: false) { |
25 VStack(alignment: .leading) { | 23 VStack(alignment: .leading) { |
26 HStack { | 24 HStack { |
27 Button(action: { selectingMainCurrency = true; showingCurrencySelector = true }) { | 25 Button(action: { showingCurrencySelector = true }) { |
28 CurrencyButton(currency: $mainCurrency) | 26 RoundedRectangle(cornerRadius: 25) |
27 .foregroundColor(Color(.secondarySystemBackground)) | |
28 .frame(height: 75) | |
29 .overlay(CurrencyRow(currencyPair: currencyPair).padding(.horizontal)) | |
29 } | 30 } |
30 | 31 |
31 Button(action: { selectingMainCurrency = false; showingCurrencySelector = true }) { | 32 FavouriteButton(currencyPair: currencyPair) |
32 CurrencyButton(currency: $secondaryCurrency) | |
33 } | |
34 } | 33 } |
35 | 34 |
36 ConversionBox( | 35 ConversionBox( |
37 mainCurrency: $mainCurrency, | 36 currencyPair: $currencyPair, |
38 secondaryCurrency: $secondaryCurrency, | |
39 amountToConvert: $amountToConvert, | 37 amountToConvert: $amountToConvert, |
40 price: $price, | 38 price: $price, |
41 showingConversion: $showingConversion, | 39 showingConversion: $showingConversion, |
42 showingCurrencySelector: $showingCurrencySelector, | 40 showingCurrencySelector: $showingCurrencySelector, |
43 currencyPairNotFound: $currencyPairNotFound | 41 isEditing: $isEditing |
44 ) | 42 ) |
45 } | 43 } |
46 .padding() | 44 .padding() |
47 .onAppear { requestApi(mainCurrency, secondaryCurrency) } | 45 .onAppear { request(currencyPair) } |
48 .onChange(of: showingCurrencySelector, perform: { showingCurrencySelector in | 46 .onChange(of: showingCurrencySelector, perform: { showingCurrencySelector in |
49 if !showingCurrencySelector { | 47 if !showingCurrencySelector { |
50 requestApi(mainCurrency, secondaryCurrency) | 48 request(currencyPair) |
51 } | 49 } |
52 }) | 50 }) |
53 .sheet(isPresented: $showingCurrencySelector) { | 51 .sheet(isPresented: $showingCurrencySelector) { |
54 CurrencySelector( | 52 CurrencySelector(currencyPair: $currencyPair, showingCurrencySelector: $showingCurrencySelector) |
55 mainCurrencySelected: $mainCurrency, | |
56 secondaryCurrencySelected: $secondaryCurrency, | |
57 showingCurrencySelector: $showingCurrencySelector, | |
58 selectingMainCurrency: $selectingMainCurrency | |
59 ) | |
60 } | 53 } |
61 } | 54 } |
62 .navigationBarHidden(true) | 55 .if(UIDevice.current.userInterfaceIdiom == .phone) { content in |
56 NavigationView { | |
57 content | |
58 .navigationTitle("Conversion") | |
59 .toolbar { | |
60 ToolbarItem(placement: .cancellationAction) { | |
61 if isEditing { | |
62 Button("Cancel", action: { | |
63 UIApplication.shared.dismissKeyboard() | |
64 isEditing = false | |
65 }) | |
66 } | |
67 } | |
68 } | |
69 } | |
70 } | |
63 } | 71 } |
64 | 72 |
65 private func requestApi(_ mainCurrency: String, _ secondaryCurrency: String) { | 73 private func request(_ currencyPair: String) { |
66 let url = "https://api.1forge.com/quotes?pairs=\(mainCurrency)/\(secondaryCurrency)&api_key=BFWeJQ3jJtqqpDv5ArNis59pAlFcQ4KF" | 74 let url = "https://api.1forge.com/quotes?pairs=\(currencyPair)&api_key=BFWeJQ3jJtqqpDv5ArNis59pAlFcQ4KF" |
67 | 75 |
68 AF.request(url).responseDecodable(of: [CurrencyQuoteModel].self) { response in | 76 AF.request(url).responseDecodable(of: [CurrencyQuoteModel].self) { response in |
69 self.showingConversion = false | 77 self.showingConversion = false |
70 self.currencyPairNotFound = false | |
71 | 78 |
72 if let currencyQuotes = response.value { | 79 if let currencyQuotes = response.value { |
73 if let price = currencyQuotes.first?.price { | 80 if let price = currencyQuotes.first?.price { |
74 self.price = price | 81 self.price = price |
75 self.showingConversion = true | 82 self.showingConversion = true |
76 } else { | 83 } else { |
77 self.currencyPairNotFound = true | 84 // Handle error |
78 } | 85 } |
79 } else { | 86 } else { |
80 // Handle error | 87 // Handle error |
81 } | 88 } |
82 } | 89 } |
84 } | 91 } |
85 | 92 |
86 | 93 |
87 struct Conversion_Previews: PreviewProvider { | 94 struct Conversion_Previews: PreviewProvider { |
88 static var previews: some View { | 95 static var previews: some View { |
89 NavigationView { | 96 Conversion() |
90 Conversion() | |
91 } | |
92 } | 97 } |
93 } | 98 } |