Mercurial > public > simoleon
comparison Simoleon/Helpers/CurrencyConversion.swift @ 17:4a81e39186f1 v1.0
Minor UI changes
author | Dennis Concepción Martín <dennisconcepcionmartin@gmail.com> |
---|---|
date | Thu, 15 Jul 2021 20:24:13 +0100 |
parents | aec2e86e5dbd |
children |
comparison
equal
deleted
inserted
replaced
16:aec2e86e5dbd | 17:4a81e39186f1 |
---|---|
8 import SwiftUI | 8 import SwiftUI |
9 import Alamofire | 9 import Alamofire |
10 | 10 |
11 struct CurrencyConversion: View { | 11 struct CurrencyConversion: View { |
12 var currency: String | 12 var currency: String |
13 @State private var price: Float = 1 | 13 @State private var price: Double = 1.00 |
14 @State private var amountToConvert = "100" | 14 @State private var amountToConvert = "100" |
15 @State private var isEditing = false | 15 @State private var isEditing = false |
16 @State private var showConversion = false | |
16 let currencyMetadata: [String: CurrencyMetadataModel] = parseJson("CurrencyMetadata.json") | 17 let currencyMetadata: [String: CurrencyMetadataModel] = parseJson("CurrencyMetadata.json") |
17 | 18 |
18 var body: some View { | 19 var body: some View { |
19 let currencies = currency.components(separatedBy: "/") | 20 let currencies = currency.components(separatedBy: "/") |
20 let mainCurrency = String(currencies[0]) | 21 let mainCurrency = String(currencies[0]) |
44 onCommit: { | 45 onCommit: { |
45 withAnimation { | 46 withAnimation { |
46 isEditing = false | 47 isEditing = false |
47 } | 48 } |
48 } | 49 } |
50 .keyboardType(.decimalPad) | |
51 .lineLimit(1) | |
49 .padding(.horizontal) | 52 .padding(.horizontal) |
50 | 53 |
51 Text("\(mainCurrency)") | 54 Text("\(mainCurrency)") |
52 .fontWeight(.semibold) | 55 .fontWeight(.semibold) |
53 } | 56 } |
66 .aspectRatio(contentMode: .fill) | 69 .aspectRatio(contentMode: .fill) |
67 .frame(width: 30, height: 30) | 70 .frame(width: 30, height: 30) |
68 .clipShape(Circle()) | 71 .clipShape(Circle()) |
69 .overlay(Circle().stroke(Color(.systemGray), lineWidth: 1)) | 72 .overlay(Circle().stroke(Color(.systemGray), lineWidth: 1)) |
70 | 73 |
71 Text("\(makeConversion(), specifier: "%.2f")") | 74 if showConversion { |
72 .padding(.horizontal) | 75 Text("\(makeConversion(), specifier: "%.4f")") |
76 .lineLimit(1) | |
77 .padding(.horizontal) | |
78 } else { | |
79 ProgressView() | |
80 .padding(.horizontal) | |
81 } | |
73 | 82 |
74 Spacer() | 83 Spacer() |
75 Text("\(secondaryCurrency)") | 84 Text("\(secondaryCurrency)") |
76 .fontWeight(.semibold) | 85 .fontWeight(.semibold) |
77 } | 86 } |
78 .padding(.horizontal) | 87 .padding(.horizontal) |
79 } | 88 } |
80 .frame(height: 50) | 89 .frame(height: 50) |
81 .cornerRadius(13) | 90 .cornerRadius(13) |
82 | 91 |
83 Text("From \(currencyMetadata[secondaryCurrency]!.name) to \(currencyMetadata[mainCurrency]!.name) at \(price, specifier: "%.2f") exchange rate.") | 92 if showConversion { |
84 .multilineTextAlignment(.center) | 93 Text("From \(currencyMetadata[mainCurrency]!.name) to \(currencyMetadata[secondaryCurrency]!.name) at \(price, specifier: "%.4f") exchange rate.") |
94 .multilineTextAlignment(.center) | |
95 } | |
85 | 96 |
86 } | 97 } |
87 .padding() | 98 .padding() |
88 } | 99 } |
89 .onAppear { requestApi(mainCurrency, secondaryCurrency) } | 100 .onAppear { requestApi(mainCurrency, secondaryCurrency) } |
90 .navigationTitle("Conversion") | 101 .navigationTitle("Conversion") |
91 } | 102 } |
92 | 103 |
93 private func makeConversion() -> Float { | 104 private func makeConversion() -> Double { |
94 if amountToConvert.isEmpty { /// Avoid nil error when string is empty | 105 if amountToConvert.isEmpty { /// Avoid nil error when string is empty |
95 return 0 | 106 return 0 |
96 } else { | 107 } else { |
97 let conversion = Float(amountToConvert)! * price | 108 let conversion = Double(amountToConvert)! * price |
98 | 109 |
99 return conversion | 110 return conversion |
100 } | 111 } |
101 } | 112 } |
102 | 113 |
103 private func requestApi(_ mainCurrency: String, _ secondaryCurrency: String) { | 114 private func requestApi(_ mainCurrency: String, _ secondaryCurrency: String) { |
104 let url = "https://api.simoleon.app/quotes=\(mainCurrency)-\(secondaryCurrency)" | 115 let url = "https://api.simoleon.app/quotes=\(mainCurrency)-\(secondaryCurrency)" |
105 AF.request(url).responseDecodable(of: [CurrencyQuoteModel].self) { response in | 116 AF.request(url).responseDecodable(of: [CurrencyQuoteModel].self) { response in |
106 if let price = response.value![0].price { | 117 if let currencyQuotes = response.value { |
107 self.price = price | 118 if let price = currencyQuotes[0].price { |
119 self.price = price | |
120 } | |
121 self.showConversion = true | |
108 } else { | 122 } else { |
109 // Handle error | 123 // Handle error |
110 } | 124 } |
111 } | 125 } |
112 } | 126 } |