Mercurial > public > simoleon
comparison Simoleon/ConversionView.swift @ 185:2fc95efcb1ee
connect backend
author | Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com> |
---|---|
date | Wed, 22 Dec 2021 16:12:23 +0100 |
parents | d2398f02e1ce |
children | 1ebd1c5dd302 |
comparison
equal
deleted
inserted
replaced
184:7cb2b0b2b3f3 | 185:2fc95efcb1ee |
---|---|
8 import SwiftUI | 8 import SwiftUI |
9 | 9 |
10 struct ConversionView: View { | 10 struct ConversionView: View { |
11 var showNavigationView: Bool? | 11 var showNavigationView: Bool? |
12 | 12 |
13 // CurrencySelector variables | |
14 @State private var baseCurrency = SupportedCurrencyResult(code: "EUR", name: "Euro", isCrypto: 0) | |
15 @State private var quoteCurrency = SupportedCurrencyResult(code: "CHF", name: "Swiss Franc", isCrypto: 0) | |
16 @State private var showingCurrencyList = false | |
17 @State private var selecting: Selection = .baseCurrency | |
18 | |
19 // CurrencyTextfield variables | |
20 @State private var amount = "1" | |
21 | |
22 // CurrencyConversion variables | |
23 @State private var showConversion = false | |
24 @State private var conversion = CurrencyConversionResponse(message: [CurrencyConversionResult]()) | |
25 | |
13 var body: some View { | 26 var body: some View { |
14 ScrollView(showsIndicators: false) { | 27 ScrollView(showsIndicators: false) { |
15 VStack(alignment: .leading, spacing: 20) { | 28 VStack(alignment: .leading, spacing: 20) { |
29 // MARK: - Currency selector | |
16 HStack { | 30 HStack { |
17 CurrencySelector() | 31 Button(action: { showCurrencyList(selecting: .baseCurrency) }) { |
18 // FavoriteButton | 32 CurrencySelectorLabel(currency: baseCurrency) |
33 } | |
34 | |
35 Button(action: { showCurrencyList(selecting: .quoteCurrency)}) { | |
36 CurrencySelectorLabel(currency: quoteCurrency) | |
37 } | |
38 | |
39 // MARK: - Favorite button | |
40 FavoriteButton() | |
41 | |
19 } | 42 } |
43 .padding(.bottom) | |
20 | 44 |
21 // ConversionBox | 45 // MARK: - Conversion box |
46 Text("\(baseCurrency.code) - \(baseCurrency.name)") | |
47 .font(.callout) | |
48 .fontWeight(.semibold) | |
49 | |
50 CurrencyTextfield(currencyCode: baseCurrency.code, amount: $amount) | |
51 .onChange(of: amount) { _ in | |
52 showConversion = false | |
53 getConversion() | |
54 } | |
55 | |
56 Divider() | |
57 Text("\(quoteCurrency.code) - \(quoteCurrency.name)") | |
58 .font(.callout) | |
59 .fontWeight(.semibold) | |
60 | |
61 CurrencyConversion( | |
62 conversion: conversion, | |
63 currencyCode: quoteCurrency.code, | |
64 showConversion: $showConversion | |
65 ) | |
22 } | 66 } |
23 .padding() | 67 .padding() |
68 .sheet(isPresented: $showingCurrencyList) { | |
69 CurrencyList(baseCurrency: $baseCurrency, quoteCurrency: $quoteCurrency, selecting: selecting) | |
70 } | |
24 } | 71 } |
72 .onAppear(perform: getConversion) | |
25 .navigationTitle("Convert") | 73 .navigationTitle("Convert") |
26 .if(UIDevice.current.userInterfaceIdiom == .phone && showNavigationView ?? true) { content in | 74 .if(UIDevice.current.userInterfaceIdiom == .phone && showNavigationView ?? true) { content in |
27 NavigationView { content } | 75 NavigationView { content } |
28 } | 76 } |
29 } | 77 } |
78 | |
79 // Change selection and show CurrencyList() | |
80 private func showCurrencyList(selecting: Selection) { | |
81 self.selecting = selecting | |
82 showingCurrencyList.toggle() | |
83 } | |
84 | |
85 // Request conversion | |
86 private func getConversion() { | |
87 guard let amount = Float(amount) else { | |
88 amount = "" | |
89 showConversion = true | |
90 return | |
91 } | |
92 | |
93 let currencyPair = "\(baseCurrency.code)\(quoteCurrency.code)" | |
94 let url = "https://api.simoleon.app/fx/convert?symbols=\(currencyPair)&amount=\(amount)" | |
95 httpRequest(url: url, model: CurrencyConversionResponse.self) { response in | |
96 conversion = response | |
97 if conversion.message.isEmpty { | |
98 // Handle exception | |
99 } else { | |
100 showConversion = true | |
101 } | |
102 } | |
103 } | |
104 } | |
105 | |
106 enum Selection { | |
107 case baseCurrency, quoteCurrency | |
30 } | 108 } |
31 | 109 |
32 struct ConversionView_Previews: PreviewProvider { | 110 struct ConversionView_Previews: PreviewProvider { |
33 static var previews: some View { | 111 static var previews: some View { |
34 ConversionView(showNavigationView: true) | 112 ConversionView(showNavigationView: true) |