Mercurial > public > simoleon
diff 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 |
line wrap: on
line diff
--- a/Simoleon/UI/ConversionBox.swift Sun Aug 29 19:04:34 2021 +0100 +++ b/Simoleon/UI/ConversionBox.swift Tue Aug 31 10:57:34 2021 +0100 @@ -8,32 +8,29 @@ import SwiftUI struct ConversionBox: View { - @ObservedObject var currencyPair: CurrencyPair + @ObservedObject var currencyConversion: CurrencyConversion @State private var amount = "" @State private var isEditing = false - @State private var showingConversion = false - @State private var currencyQuote = CurrencyQuoteModel() - @State private var showingAlert = false - + let networkHelper = NetworkHelper() let currencyDetails: [String: CurrencyModel] = try! readJson(from: "Currencies.json") var body: some View { VStack(alignment: .leading) { - let baseCurrencyName = currencyDetails[currencyPair.baseSymbol]!.name - Text("\(baseCurrencyName) (\(currencyPair.baseSymbol))") + let baseCurrencyName = currencyDetails[currencyConversion.baseSymbol]!.name + Text("\(baseCurrencyName) (\(currencyConversion.baseSymbol))") .font(.callout) .fontWeight(.semibold) ConversionTextfield(amount: $amount, isEditing: $isEditing) Divider() - - let quoteCurrencyName = currencyDetails[currencyPair.quoteSymbol]!.name - Text("\(quoteCurrencyName) (\(currencyPair.quoteSymbol))") + + let quoteCurrencyName = currencyDetails[currencyConversion.quoteSymbol]!.name + Text("\(quoteCurrencyName) (\(currencyConversion.quoteSymbol))") .font(.callout) .fontWeight(.semibold) - if showingConversion { + if currencyConversion.isShowing { let conversion = convert() Text("\(conversion, specifier: "%.2f")") .font(Font.title.weight(.semibold)) @@ -54,42 +51,18 @@ } } } - - .onAppear { - showingConversion = false - let pair = "\(currencyPair.baseSymbol)/\(currencyPair.quoteSymbol)" - let apiKey = readConfig(withKey: "API_KEY")! - let url = "https://api.1forge.com/quotes?pairs=\(pair)&api_key=\(apiKey)" - try? networkHelper.httpRequest(url: url, model: [CurrencyQuoteModel].self) { response in - if let currencyQuote = response.first { - self.currencyQuote = currencyQuote - } else { - showingAlert = true - } - - showingConversion = true - } - } - .alert(isPresented: $showingAlert) { - Alert( - title: Text("Currencies not supported."), - message: Text("Currently, we are unable to convert from \(currencyPair.baseSymbol) to \(currencyPair.quoteSymbol)."), - dismissButton: .default(Text("Dismiss") - ) - ) - } } private func convert() -> Double { guard let amount = Double(amount) else { return 0 } - guard let price = currencyQuote.price else { return 0 } - + guard let price = currencyConversion.quote.price else { return 0 } + return amount * price } } struct ConversionBox_Previews: PreviewProvider { static var previews: some View { - ConversionBox(currencyPair: CurrencyPair()) + ConversionBox(currencyConversion: CurrencyConversion()) } }