diff Simoleon/Helpers/CurrencyConversion.swift @ 186:1ebd1c5dd302

finish ConversionView
author Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
date Thu, 23 Dec 2021 11:30:38 +0100
parents 2fc95efcb1ee
children 13d5a8deb6c2
line wrap: on
line diff
--- a/Simoleon/Helpers/CurrencyConversion.swift	Wed Dec 22 16:12:23 2021 +0100
+++ b/Simoleon/Helpers/CurrencyConversion.swift	Thu Dec 23 11:30:38 2021 +0100
@@ -8,9 +8,9 @@
 import SwiftUI
 
 struct CurrencyConversion: View {
-    var conversion: CurrencyConversionResponse
+    var latestRate: CurrencyLatestRateResponse
     var currencyCode: String
-    @Binding var showConversion: Bool
+    @Binding var amount: String
     
     var body: some View {
         VStack {
@@ -19,13 +19,13 @@
                 .foregroundColor(Color(.secondarySystemBackground))
                 .overlay(
                     VStack {
-                        if showConversion {
-                            let amount = conversion.message.first!.amount
-                            let formattedAmount = format(currency: amount)
+                        if latestRate.message.isEmpty {
+                            ProgressView()
+                        } else {
+                            let conversion = convert(amount: amount)
+                            let formattedAmount = format(currency: conversion)
                             Text(formattedAmount)
                                 .font(.title2)
-                        } else {
-                            ProgressView()
                         }
                     }
                     .padding(.leading, 15)
@@ -33,8 +33,8 @@
                     , alignment: .leading
                 )
             
-            if showConversion {
-                let timestamp = conversion.message.first!.timestamp
+            if !latestRate.message.isEmpty {
+                let timestamp = latestRate.message.first!.timestamp
                 Text("Last updated: \(converToDate(epoch: timestamp))")
                     .font(.caption)
                     .opacity(0.6)
@@ -51,7 +51,7 @@
         return formatter.string(from: NSNumber(value: currency))!
     }
     
-    // COnvert epoch to date
+    // Convert epoch to date
     private func converToDate(epoch: Int) -> String {
         let dateFormatter = DateFormatter()
         dateFormatter.timeStyle = DateFormatter.Style.medium
@@ -60,23 +60,34 @@
         
         return dateFormatter.string(from: date)
     }
+    
+    // Compute conversion
+    private func convert(amount: String) -> Double {
+        guard let amount = Double(amount) else {
+            return Double()
+        }
+        
+        let rate = latestRate.message.first!.rate
+        let conversion = amount * rate
+        
+        return conversion
+    }
 }
 
 struct CurrencyConversion_Previews: PreviewProvider {
     static var previews: some View {
         CurrencyConversion(
-            conversion:
-                CurrencyConversionResponse(
+            latestRate:
+                CurrencyLatestRateResponse(
                     message: [
-                        CurrencyConversionResult(
+                        CurrencyLatestRateResult(
                             rate: 1.31,
-                            timestamp: 1288282222000,
-                            amount: 95.63
+                            timestamp: 1288282222000
                         )
                     ]
                 ),
             currencyCode: "CHF",
-            showConversion: .constant(true)
+            amount: .constant("1")
         )
     }
 }