diff Simoleon/Helpers/CurrencyRow.swift @ 16:aec2e86e5dbd

Change design and icon
author Dennis Concepción Martín <dennisconcepcionmartin@gmail.com>
date Thu, 15 Jul 2021 19:03:24 +0100
parents a02f463aa906
children 4a81e39186f1
line wrap: on
line diff
--- a/Simoleon/Helpers/CurrencyRow.swift	Thu Jul 15 10:58:29 2021 +0100
+++ b/Simoleon/Helpers/CurrencyRow.swift	Thu Jul 15 19:03:24 2021 +0100
@@ -2,100 +2,53 @@
 //  CurrencyRow.swift
 //  Simoleon
 //
-//  Created by Dennis Concepción Martín on 11/07/2021.
+//  Created by Dennis Concepción Martín on 15/07/2021.
 //
 
 import SwiftUI
 
 struct CurrencyRow: View {
-    var currencyQuote: CurrencyQuoteModel
-    let currenciesMetadata: [String: CurrencyMetadataModel] = parseJson("CurrencyMetadata.json")
+    let currencyMetadata: [String: CurrencyMetadataModel] = parseJson("CurrencyMetadata.json")
+    var currency: String
     
     var body: some View {
-        RoundedRectangle(cornerRadius: 10)
-            .rectangleModifier(Color(.systemBackground), 80)
-            .overlay(
-                HStack {
-                    let symbols = currencyQuote.symbol!.split(separator: "/")
-                    let mainCurrencyFlag = currenciesMetadata[String(symbols[0])]!.flag
-                    let secondaryCurrencyFlag = currenciesMetadata[String(symbols[1])]!.flag
-                    
-                    FlagPair(mainCurrencyFlag: mainCurrencyFlag, secondaryCurrencyFlag: secondaryCurrencyFlag)
-                    
-                    VStack(alignment: .leading) {
-                        Text("\(String(symbols[0]))")
-                            .fontWeight(.semibold)
-                        
-                        Text("\(String(symbols[1]))")
-                            .fontWeight(.semibold)
-                    }
-                    .padding(.horizontal)
-                    
-                    VStack(alignment: .leading) {
-                        Text("Bid")
-                        let bid = currencyQuote.bid!
-                        Text("\(bid, specifier: createSpecifier(bid))")
-                            .fontWeight(.semibold)
-                            .lineLimit(1)
-                            
-                    }
-                    .padding(.trailing)
-                    
-                    VStack(alignment: .leading) {
-                        Text("Ask")
-                        let ask = currencyQuote.ask!
-                        Text("\(ask, specifier: createSpecifier(ask))")
-                            .fontWeight(.semibold)
-                            .lineLimit(1)
-                            
-                    }
-                    
-                    Spacer()
-                    
-                }
-                .padding(.horizontal)
-            )
+        let currencies = currency.components(separatedBy: "/")
+        let mainCurrency = String(currencies[0])
+        let secondaryCurrency = String(currencies[1])
+        HStack {
+            Image(currencyMetadata[mainCurrency]!.flag)
+                .resizable()
+                .aspectRatio(contentMode: .fill)
+                .frame(width: 30, height: 30)
+                .clipShape(Circle())
+                .overlay(Circle().stroke(Color(.systemGray), lineWidth: 1))
+            
+            Image(currencyMetadata[secondaryCurrency]!.flag)
+                .resizable()
+                .aspectRatio(contentMode: .fill)
+                .frame(width: 30, height: 30)
+                .clipShape(Circle())
+                .overlay(Circle().stroke(Color(.systemGray), lineWidth: 1))
+                .offset(x: -20)
+                .padding(.trailing, -20)
+            
+            VStack(alignment: .leading) {
+                Text("\(currency)")
+                    .fontWeight(.semibold)
+                
+                Text("\(currencyMetadata[mainCurrency]!.name)/\(currencyMetadata[secondaryCurrency]!.name)")
+                    .font(.footnote)
+                    .fontWeight(.semibold)
+                    .opacity(0.5)
+                    .lineLimit(1)
+            }
             .padding(.horizontal)
-    }
-    
-    /*
-     Choose how many decimals depending on whether the price is hundreds, thousands, etc
-     */
-    
-    private func createSpecifier(_ amount: Float) -> String {
-        if amount >= 10 {
-            return "%.2f"
-        } else {
-            return "%.4f"
         }
     }
-    
-    /*
-     Convert unix time into human readable
-     */
-    private func convertUnixTime(_ timestamp: Int) -> String {
-        let now = Date()
-        let convertedDate = Date(timeIntervalSince1970: TimeInterval(timestamp))
-        let formatter = DateComponentsFormatter()
-        formatter.unitsStyle = .abbreviated
-        
-        return formatter.string(from: convertedDate, to: now)!
-    }
-}
-extension RoundedRectangle {
-    func rectangleModifier(_ colour: Color, _ height: CGFloat) -> some View {
-        self
-            .strokeBorder(Color("Border"), lineWidth: 2)
-            .background(RoundedRectangle(cornerRadius: 10).foregroundColor(colour))
-            .frame(height: height)
-            
-    }
 }
 
 struct CurrencyRow_Previews: PreviewProvider {
     static var previews: some View {
-        let currencyQuote: CurrencyQuoteModel = parseJson("CurrencyQuoteData.json")
-        
-        CurrencyRow(currencyQuote: currencyQuote)
+        CurrencyRow(currency: "USD/GBP")
     }
 }