comparison 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
comparison
equal deleted inserted replaced
15:a02f463aa906 16:aec2e86e5dbd
1 // 1 //
2 // CurrencyRow.swift 2 // CurrencyRow.swift
3 // Simoleon 3 // Simoleon
4 // 4 //
5 // Created by Dennis Concepción Martín on 11/07/2021. 5 // Created by Dennis Concepción Martín on 15/07/2021.
6 // 6 //
7 7
8 import SwiftUI 8 import SwiftUI
9 9
10 struct CurrencyRow: View { 10 struct CurrencyRow: View {
11 var currencyQuote: CurrencyQuoteModel 11 let currencyMetadata: [String: CurrencyMetadataModel] = parseJson("CurrencyMetadata.json")
12 let currenciesMetadata: [String: CurrencyMetadataModel] = parseJson("CurrencyMetadata.json") 12 var currency: String
13 13
14 var body: some View { 14 var body: some View {
15 RoundedRectangle(cornerRadius: 10) 15 let currencies = currency.components(separatedBy: "/")
16 .rectangleModifier(Color(.systemBackground), 80) 16 let mainCurrency = String(currencies[0])
17 .overlay( 17 let secondaryCurrency = String(currencies[1])
18 HStack { 18 HStack {
19 let symbols = currencyQuote.symbol!.split(separator: "/") 19 Image(currencyMetadata[mainCurrency]!.flag)
20 let mainCurrencyFlag = currenciesMetadata[String(symbols[0])]!.flag 20 .resizable()
21 let secondaryCurrencyFlag = currenciesMetadata[String(symbols[1])]!.flag 21 .aspectRatio(contentMode: .fill)
22 22 .frame(width: 30, height: 30)
23 FlagPair(mainCurrencyFlag: mainCurrencyFlag, secondaryCurrencyFlag: secondaryCurrencyFlag) 23 .clipShape(Circle())
24 24 .overlay(Circle().stroke(Color(.systemGray), lineWidth: 1))
25 VStack(alignment: .leading) { 25
26 Text("\(String(symbols[0]))") 26 Image(currencyMetadata[secondaryCurrency]!.flag)
27 .fontWeight(.semibold) 27 .resizable()
28 28 .aspectRatio(contentMode: .fill)
29 Text("\(String(symbols[1]))") 29 .frame(width: 30, height: 30)
30 .fontWeight(.semibold) 30 .clipShape(Circle())
31 } 31 .overlay(Circle().stroke(Color(.systemGray), lineWidth: 1))
32 .padding(.horizontal) 32 .offset(x: -20)
33 33 .padding(.trailing, -20)
34 VStack(alignment: .leading) { 34
35 Text("Bid") 35 VStack(alignment: .leading) {
36 let bid = currencyQuote.bid! 36 Text("\(currency)")
37 Text("\(bid, specifier: createSpecifier(bid))") 37 .fontWeight(.semibold)
38 .fontWeight(.semibold) 38
39 .lineLimit(1) 39 Text("\(currencyMetadata[mainCurrency]!.name)/\(currencyMetadata[secondaryCurrency]!.name)")
40 40 .font(.footnote)
41 } 41 .fontWeight(.semibold)
42 .padding(.trailing) 42 .opacity(0.5)
43 43 .lineLimit(1)
44 VStack(alignment: .leading) { 44 }
45 Text("Ask")
46 let ask = currencyQuote.ask!
47 Text("\(ask, specifier: createSpecifier(ask))")
48 .fontWeight(.semibold)
49 .lineLimit(1)
50
51 }
52
53 Spacer()
54
55 }
56 .padding(.horizontal)
57 )
58 .padding(.horizontal) 45 .padding(.horizontal)
59 }
60
61 /*
62 Choose how many decimals depending on whether the price is hundreds, thousands, etc
63 */
64
65 private func createSpecifier(_ amount: Float) -> String {
66 if amount >= 10 {
67 return "%.2f"
68 } else {
69 return "%.4f"
70 } 46 }
71 }
72
73 /*
74 Convert unix time into human readable
75 */
76 private func convertUnixTime(_ timestamp: Int) -> String {
77 let now = Date()
78 let convertedDate = Date(timeIntervalSince1970: TimeInterval(timestamp))
79 let formatter = DateComponentsFormatter()
80 formatter.unitsStyle = .abbreviated
81
82 return formatter.string(from: convertedDate, to: now)!
83 }
84 }
85 extension RoundedRectangle {
86 func rectangleModifier(_ colour: Color, _ height: CGFloat) -> some View {
87 self
88 .strokeBorder(Color("Border"), lineWidth: 2)
89 .background(RoundedRectangle(cornerRadius: 10).foregroundColor(colour))
90 .frame(height: height)
91
92 } 47 }
93 } 48 }
94 49
95 struct CurrencyRow_Previews: PreviewProvider { 50 struct CurrencyRow_Previews: PreviewProvider {
96 static var previews: some View { 51 static var previews: some View {
97 let currencyQuote: CurrencyQuoteModel = parseJson("CurrencyQuoteData.json") 52 CurrencyRow(currency: "USD/GBP")
98
99 CurrencyRow(currencyQuote: currencyQuote)
100 } 53 }
101 } 54 }