comparison Simoleon/Helpers/CurrencyConversion.swift @ 185:2fc95efcb1ee

connect backend
author Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
date Wed, 22 Dec 2021 16:12:23 +0100
parents
children 1ebd1c5dd302
comparison
equal deleted inserted replaced
184:7cb2b0b2b3f3 185:2fc95efcb1ee
1 //
2 // CurrencyConversion.swift
3 // Simoleon
4 //
5 // Created by Dennis Concepción Martín on 21/12/21.
6 //
7
8 import SwiftUI
9
10 struct CurrencyConversion: View {
11 var conversion: CurrencyConversionResponse
12 var currencyCode: String
13 @Binding var showConversion: Bool
14
15 var body: some View {
16 VStack {
17 RoundedRectangle(cornerRadius: 15)
18 .frame(height: 60)
19 .foregroundColor(Color(.secondarySystemBackground))
20 .overlay(
21 VStack {
22 if showConversion {
23 let amount = conversion.message.first!.amount
24 let formattedAmount = format(currency: amount)
25 Text(formattedAmount)
26 .font(.title2)
27 } else {
28 ProgressView()
29 }
30 }
31 .padding(.leading, 15)
32
33 , alignment: .leading
34 )
35
36 if showConversion {
37 let timestamp = conversion.message.first!.timestamp
38 Text("Last updated: \(converToDate(epoch: timestamp))")
39 .font(.caption)
40 .opacity(0.6)
41 }
42 }
43 }
44
45 // Format conversion to specific currency format
46 private func format(currency: Double) -> String {
47 let formatter = NumberFormatter()
48 formatter.currencyCode = currencyCode
49 formatter.numberStyle = .currency
50
51 return formatter.string(from: NSNumber(value: currency))!
52 }
53
54 // COnvert epoch to date
55 private func converToDate(epoch: Int) -> String {
56 let dateFormatter = DateFormatter()
57 dateFormatter.timeStyle = DateFormatter.Style.medium
58 dateFormatter.dateStyle = DateFormatter.Style.medium
59 let date = Date(timeIntervalSince1970: TimeInterval(epoch/1000))
60
61 return dateFormatter.string(from: date)
62 }
63 }
64
65 struct CurrencyConversion_Previews: PreviewProvider {
66 static var previews: some View {
67 CurrencyConversion(
68 conversion:
69 CurrencyConversionResponse(
70 message: [
71 CurrencyConversionResult(
72 rate: 1.31,
73 timestamp: 1288282222000,
74 amount: 95.63
75 )
76 ]
77 ),
78 currencyCode: "CHF",
79 showConversion: .constant(true)
80 )
81 }
82 }