Mercurial > public > simoleon
comparison Simoleon/ConversionView.swift @ 160:0c589138a6f3
Implement Conversion Box
author | Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com> |
---|---|
date | Sun, 29 Aug 2021 19:04:34 +0100 |
parents | 35628bac01f5 |
children | 3913aff613e8 |
comparison
equal
deleted
inserted
replaced
159:35628bac01f5 | 160:0c589138a6f3 |
---|---|
8 import SwiftUI | 8 import SwiftUI |
9 import Purchases | 9 import Purchases |
10 | 10 |
11 struct ConversionView: View { | 11 struct ConversionView: View { |
12 var showNavigationView: Bool? | 12 var showNavigationView: Bool? |
13 @State var currencyPair: CurrencyPairModel | 13 @StateObject var currencyPair = CurrencyPair() |
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 | 14 |
21 var body: some View { | 15 var body: some View { |
22 ScrollView(showsIndicators: false) { | 16 ScrollView(showsIndicators: false) { |
23 VStack(alignment: .leading) { | 17 VStack(alignment: .leading, spacing: 20) { |
24 HStack { | 18 HStack { |
25 CurrencySelector(currencyPair: currencyPair) | 19 CurrencySelector(currencyPair: currencyPair) |
26 FavoriteButton(currencyPair: currencyPair) | 20 FavoriteButton(currencyPair: currencyPair) |
27 } | 21 } |
22 | |
23 ConversionBox(currencyPair: currencyPair) | |
24 .padding(.top) | |
28 } | 25 } |
29 .padding() | 26 .padding() |
30 } | 27 } |
31 .onAppear(perform: createUrlAndRequest) | |
32 .navigationTitle("Convert") | 28 .navigationTitle("Convert") |
33 .toolbar { | |
34 ToolbarItem(placement: .navigationBarTrailing) { | |
35 if amountIsEditing { | |
36 Button(action: { | |
37 UIApplication.shared.dismissKeyboard() | |
38 amountIsEditing = false | |
39 }) { | |
40 Text("Done") | |
41 } | |
42 } | |
43 } | |
44 } | |
45 .if(UIDevice.current.userInterfaceIdiom == .phone && showNavigationView ?? true) { content in | 29 .if(UIDevice.current.userInterfaceIdiom == .phone && showNavigationView ?? true) { content in |
46 NavigationView { content } | 30 NavigationView { content } |
47 } | |
48 } | |
49 | |
50 private func createUrlAndRequest() { | |
51 showingConversion = false | |
52 let baseUrl = readConfigVariable(withKey: "API_URL")! | |
53 let apiKey = readConfigVariable(withKey: "API_KEY")! | |
54 let currencyPair = "\(currencyPair.baseSymbol)/\(currencyPair.quoteSymbol)" | |
55 let url = "\(baseUrl)quotes?pairs=\(currencyPair)&api_key=\(apiKey)" | |
56 | |
57 httpRequest(url: url, model: [CurrencyQuoteModel].self) { response in | |
58 if let price = response.first?.price { | |
59 self.price = price | |
60 showingConversion = true | |
61 } | |
62 } | 31 } |
63 } | 32 } |
64 } | 33 } |
65 | 34 |
66 | 35 |
67 struct ConversionView_Previews: PreviewProvider { | 36 struct ConversionView_Previews: PreviewProvider { |
68 static var previews: some View { | 37 static var previews: some View { |
69 ConversionView(showNavigationView: true, currencyPair: CurrencyPairModel(baseSymbol: "USD", quoteSymbol: "EUR")) | 38 ConversionView(showNavigationView: true) |
70 } | 39 } |
71 } | 40 } |