Mercurial > public > simoleon
comparison Simoleon/ConversionView.swift @ 157:8c3bbd640103
Implement Currency Selector
author | Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com> |
---|---|
date | Sat, 28 Aug 2021 11:15:41 +0100 |
parents | |
children | 35628bac01f5 |
comparison
equal
deleted
inserted
replaced
156:84137052813d | 157:8c3bbd640103 |
---|---|
1 // | |
2 // ConversionView.swift | |
3 // Simoleon | |
4 // | |
5 // Created by Dennis Concepción Martín on 18/07/2021. | |
6 // | |
7 | |
8 import SwiftUI | |
9 import Purchases | |
10 | |
11 struct ConversionView: View { | |
12 var showNavigationView: Bool? | |
13 @State var currencyPair: CurrencyPairModel | |
14 | |
15 // Conversion | |
16 @State private var showingConversion = false | |
17 @State private var amountIsEditing = false | |
18 @State private var amountToConvert = "" | |
19 @State private var price: Double = 0 | |
20 | |
21 var body: some View { | |
22 ScrollView(showsIndicators: false) { | |
23 VStack(alignment: .leading) { | |
24 CurrencySelector(currencyPair: currencyPair) | |
25 } | |
26 .padding() | |
27 } | |
28 .onAppear(perform: createUrlAndRequest) | |
29 .navigationTitle("Convert") | |
30 .toolbar { | |
31 ToolbarItem(placement: .navigationBarTrailing) { | |
32 if amountIsEditing { | |
33 Button(action: { | |
34 UIApplication.shared.dismissKeyboard() | |
35 amountIsEditing = false | |
36 }) { | |
37 Text("Done") | |
38 } | |
39 } | |
40 } | |
41 } | |
42 .if(UIDevice.current.userInterfaceIdiom == .phone && showNavigationView ?? true) { content in | |
43 NavigationView { content } | |
44 } | |
45 } | |
46 | |
47 private func createUrlAndRequest() { | |
48 showingConversion = false | |
49 let baseUrl = readConfigVariable(withKey: "API_URL")! | |
50 let apiKey = readConfigVariable(withKey: "API_KEY")! | |
51 let currencyPair = "\(currencyPair.baseSymbol)/\(currencyPair.quoteSymbol)" | |
52 let url = "\(baseUrl)quotes?pairs=\(currencyPair)&api_key=\(apiKey)" | |
53 | |
54 httpRequest(url: url, model: [CurrencyQuoteModel].self) { response in | |
55 if let price = response.first?.price { | |
56 self.price = price | |
57 showingConversion = true | |
58 } | |
59 } | |
60 } | |
61 } | |
62 | |
63 | |
64 //struct ConversionView_Previews: PreviewProvider { | |
65 // static var previews: some View { | |
66 // ConversionView() | |
67 // } | |
68 //} |