comparison Simoleon/UI/ConversionBox.swift @ 161:3913aff613e8

Fix bug that didn't request API on symbol change
author Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
date Tue, 31 Aug 2021 10:57:34 +0100
parents 0c589138a6f3
children 1940db1ef321
comparison
equal deleted inserted replaced
160:0c589138a6f3 161:3913aff613e8
6 // 6 //
7 7
8 import SwiftUI 8 import SwiftUI
9 9
10 struct ConversionBox: View { 10 struct ConversionBox: View {
11 @ObservedObject var currencyPair: CurrencyPair 11 @ObservedObject var currencyConversion: CurrencyConversion
12 @State private var amount = "" 12 @State private var amount = ""
13 @State private var isEditing = false 13 @State private var isEditing = false
14 @State private var showingConversion = false 14
15 @State private var currencyQuote = CurrencyQuoteModel()
16 @State private var showingAlert = false
17
18 let networkHelper = NetworkHelper() 15 let networkHelper = NetworkHelper()
19 let currencyDetails: [String: CurrencyModel] = try! readJson(from: "Currencies.json") 16 let currencyDetails: [String: CurrencyModel] = try! readJson(from: "Currencies.json")
20 17
21 var body: some View { 18 var body: some View {
22 VStack(alignment: .leading) { 19 VStack(alignment: .leading) {
23 let baseCurrencyName = currencyDetails[currencyPair.baseSymbol]!.name 20 let baseCurrencyName = currencyDetails[currencyConversion.baseSymbol]!.name
24 Text("\(baseCurrencyName) (\(currencyPair.baseSymbol))") 21 Text("\(baseCurrencyName) (\(currencyConversion.baseSymbol))")
25 .font(.callout) 22 .font(.callout)
26 .fontWeight(.semibold) 23 .fontWeight(.semibold)
27 24
28 ConversionTextfield(amount: $amount, isEditing: $isEditing) 25 ConversionTextfield(amount: $amount, isEditing: $isEditing)
29 Divider() 26 Divider()
30 27
31 let quoteCurrencyName = currencyDetails[currencyPair.quoteSymbol]!.name 28 let quoteCurrencyName = currencyDetails[currencyConversion.quoteSymbol]!.name
32 Text("\(quoteCurrencyName) (\(currencyPair.quoteSymbol))") 29 Text("\(quoteCurrencyName) (\(currencyConversion.quoteSymbol))")
33 .font(.callout) 30 .font(.callout)
34 .fontWeight(.semibold) 31 .fontWeight(.semibold)
35 32
36 if showingConversion { 33 if currencyConversion.isShowing {
37 let conversion = convert() 34 let conversion = convert()
38 Text("\(conversion, specifier: "%.2f")") 35 Text("\(conversion, specifier: "%.2f")")
39 .font(Font.title.weight(.semibold)) 36 .font(Font.title.weight(.semibold))
40 .lineLimit(1) 37 .lineLimit(1)
41 } else { 38 } else {
52 Text("Done") 49 Text("Done")
53 } 50 }
54 } 51 }
55 } 52 }
56 } 53 }
57
58 .onAppear {
59 showingConversion = false
60 let pair = "\(currencyPair.baseSymbol)/\(currencyPair.quoteSymbol)"
61 let apiKey = readConfig(withKey: "API_KEY")!
62 let url = "https://api.1forge.com/quotes?pairs=\(pair)&api_key=\(apiKey)"
63 try? networkHelper.httpRequest(url: url, model: [CurrencyQuoteModel].self) { response in
64 if let currencyQuote = response.first {
65 self.currencyQuote = currencyQuote
66 } else {
67 showingAlert = true
68 }
69
70 showingConversion = true
71 }
72 }
73 .alert(isPresented: $showingAlert) {
74 Alert(
75 title: Text("Currencies not supported."),
76 message: Text("Currently, we are unable to convert from \(currencyPair.baseSymbol) to \(currencyPair.quoteSymbol)."),
77 dismissButton: .default(Text("Dismiss")
78 )
79 )
80 }
81 } 54 }
82 55
83 private func convert() -> Double { 56 private func convert() -> Double {
84 guard let amount = Double(amount) else { return 0 } 57 guard let amount = Double(amount) else { return 0 }
85 guard let price = currencyQuote.price else { return 0 } 58 guard let price = currencyConversion.quote.price else { return 0 }
86 59
87 return amount * price 60 return amount * price
88 } 61 }
89 } 62 }
90 63
91 struct ConversionBox_Previews: PreviewProvider { 64 struct ConversionBox_Previews: PreviewProvider {
92 static var previews: some View { 65 static var previews: some View {
93 ConversionBox(currencyPair: CurrencyPair()) 66 ConversionBox(currencyConversion: CurrencyConversion())
94 } 67 }
95 } 68 }