diff 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
line wrap: on
line diff
--- a/Simoleon/Conversion.swift	Mon Jul 19 10:12:23 2021 +0100
+++ b/Simoleon/Conversion.swift	Mon Jul 19 19:27:12 2021 +0100
@@ -9,14 +9,12 @@
 import Alamofire
 
 struct Conversion: View {
-    @State private var mainCurrency = "USD"
-    @State private var secondaryCurrency = "GBP"
+    @State private var currencyPair = "USD/GBP"
     @State private var amountToConvert = "1000"
     @State private var price: Double = 1.00
     @State private var showingConversion = false
     @State private var showingCurrencySelector = false
-    @State private var selectingMainCurrency = false
-    @State private var currencyPairNotFound = false
+    @State private var isEditing = false
     
     let currencyMetadata: [String: CurrencyMetadataModel] = parseJson("CurrencyMetadata.json")
     
@@ -24,57 +22,66 @@
         ScrollView(showsIndicators: false) {
             VStack(alignment: .leading) {
                 HStack {
-                    Button(action: { selectingMainCurrency = true; showingCurrencySelector = true }) {
-                        CurrencyButton(currency: $mainCurrency)
+                    Button(action: { showingCurrencySelector = true }) {
+                        RoundedRectangle(cornerRadius: 25)
+                            .foregroundColor(Color(.secondarySystemBackground))
+                            .frame(height: 75)
+                            .overlay(CurrencyRow(currencyPair: currencyPair).padding(.horizontal))
                     }
                     
-                    Button(action: { selectingMainCurrency = false; showingCurrencySelector = true }) {
-                        CurrencyButton(currency: $secondaryCurrency)
-                    }
+                    FavouriteButton(currencyPair: currencyPair)
                 }
                 
                 ConversionBox(
-                    mainCurrency: $mainCurrency,
-                    secondaryCurrency: $secondaryCurrency,
+                    currencyPair: $currencyPair,
                     amountToConvert: $amountToConvert,
                     price: $price,
                     showingConversion: $showingConversion,
                     showingCurrencySelector: $showingCurrencySelector,
-                    currencyPairNotFound: $currencyPairNotFound
+                    isEditing: $isEditing
                 )
             }
             .padding()
-            .onAppear { requestApi(mainCurrency, secondaryCurrency) }
+            .onAppear { request(currencyPair) }
             .onChange(of: showingCurrencySelector, perform: { showingCurrencySelector in
                 if !showingCurrencySelector {
-                    requestApi(mainCurrency, secondaryCurrency)
+                    request(currencyPair)
                 }
             })
             .sheet(isPresented: $showingCurrencySelector) {
-                CurrencySelector(
-                    mainCurrencySelected: $mainCurrency,
-                    secondaryCurrencySelected: $secondaryCurrency,
-                    showingCurrencySelector: $showingCurrencySelector,
-                    selectingMainCurrency: $selectingMainCurrency
-                )
+                CurrencySelector(currencyPair: $currencyPair, showingCurrencySelector: $showingCurrencySelector)
             }
         }
-        .navigationBarHidden(true)
+        .if(UIDevice.current.userInterfaceIdiom == .phone) { content in
+            NavigationView {
+                content
+                    .navigationTitle("Conversion")
+                    .toolbar {
+                        ToolbarItem(placement: .cancellationAction) {
+                            if isEditing {
+                                Button("Cancel", action: {
+                                    UIApplication.shared.dismissKeyboard()
+                                    isEditing = false
+                                })
+                            }
+                        }
+                    }
+            }
+        }
     }
     
-    private func requestApi(_ mainCurrency: String, _ secondaryCurrency: String) {
-        let url = "https://api.1forge.com/quotes?pairs=\(mainCurrency)/\(secondaryCurrency)&api_key=BFWeJQ3jJtqqpDv5ArNis59pAlFcQ4KF"
+    private func request(_ currencyPair: String) {
+        let url = "https://api.1forge.com/quotes?pairs=\(currencyPair)&api_key=BFWeJQ3jJtqqpDv5ArNis59pAlFcQ4KF"
         
         AF.request(url).responseDecodable(of: [CurrencyQuoteModel].self) { response in
             self.showingConversion = false
-            self.currencyPairNotFound = false
             
             if let currencyQuotes = response.value {
                 if let price = currencyQuotes.first?.price {
                     self.price = price
                     self.showingConversion =  true
                 } else {
-                    self.currencyPairNotFound = true
+//                    Handle error
                 }
             } else {
 //               Handle error
@@ -86,8 +93,6 @@
 
 struct Conversion_Previews: PreviewProvider {
     static var previews: some View {
-        NavigationView {
-            Conversion()
-        }
+        Conversion()
     }
 }