diff 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
line wrap: on
line diff
--- a/Simoleon/ConversionView.swift	Mon Dec 20 12:28:22 2021 +0100
+++ b/Simoleon/ConversionView.swift	Wed Dec 22 16:12:23 2021 +0100
@@ -10,23 +10,101 @@
 struct ConversionView: View {
     var showNavigationView: Bool?
     
+    // CurrencySelector variables
+    @State private var baseCurrency = SupportedCurrencyResult(code: "EUR", name: "Euro", isCrypto: 0)
+    @State private var quoteCurrency = SupportedCurrencyResult(code: "CHF", name: "Swiss Franc", isCrypto: 0)
+    @State private var showingCurrencyList = false
+    @State private var selecting: Selection = .baseCurrency
+    
+    // CurrencyTextfield variables
+    @State private var amount = "1"
+    
+    // CurrencyConversion variables
+    @State private var showConversion = false
+    @State private var conversion = CurrencyConversionResponse(message: [CurrencyConversionResult]())
+    
     var body: some View {
         ScrollView(showsIndicators: false) {
             VStack(alignment: .leading, spacing: 20) {
+                // MARK: - Currency selector
                 HStack {
-                    CurrencySelector()
-                    // FavoriteButton
+                    Button(action: { showCurrencyList(selecting: .baseCurrency) }) {
+                       CurrencySelectorLabel(currency: baseCurrency)
+                    }
+                    
+                    Button(action: { showCurrencyList(selecting: .quoteCurrency)}) {
+                        CurrencySelectorLabel(currency: quoteCurrency)
+                    }
+                    
+                    // MARK: - Favorite button
+                    FavoriteButton()
+                    
                 }
+                .padding(.bottom)
                 
-                // ConversionBox
+                // MARK: - Conversion box
+                Text("\(baseCurrency.code) - \(baseCurrency.name)")
+                    .font(.callout)
+                    .fontWeight(.semibold)
+                
+                CurrencyTextfield(currencyCode: baseCurrency.code, amount: $amount)
+                    .onChange(of: amount) { _ in
+                        showConversion = false
+                        getConversion()
+                    }
+                
+                Divider()
+                Text("\(quoteCurrency.code) - \(quoteCurrency.name)")
+                    .font(.callout)
+                    .fontWeight(.semibold)
+                
+                CurrencyConversion(
+                    conversion: conversion,
+                    currencyCode: quoteCurrency.code,
+                    showConversion: $showConversion
+                )
             }
             .padding()
+            .sheet(isPresented: $showingCurrencyList) {
+                CurrencyList(baseCurrency: $baseCurrency, quoteCurrency: $quoteCurrency, selecting: selecting)
+            }
         }
+        .onAppear(perform: getConversion)
         .navigationTitle("Convert")
         .if(UIDevice.current.userInterfaceIdiom == .phone && showNavigationView ?? true) { content in
             NavigationView { content }
         }
     }
+    
+    // Change selection and show CurrencyList()
+    private func showCurrencyList(selecting: Selection) {
+        self.selecting = selecting
+        showingCurrencyList.toggle()
+    }
+    
+    // Request conversion
+    private func getConversion() {
+        guard let amount = Float(amount) else {
+            amount = ""
+            showConversion = true
+            return
+        }
+        
+        let currencyPair = "\(baseCurrency.code)\(quoteCurrency.code)"
+        let url = "https://api.simoleon.app/fx/convert?symbols=\(currencyPair)&amount=\(amount)"
+        httpRequest(url: url, model: CurrencyConversionResponse.self) { response in
+            conversion = response
+            if conversion.message.isEmpty {
+                // Handle exception
+            } else {
+                showConversion = true
+            }
+        }
+    }
+}
+
+enum Selection {
+    case baseCurrency, quoteCurrency
 }
 
 struct ConversionView_Previews: PreviewProvider {