comparison Simoleon/Models/CurrencyConversion.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
children
comparison
equal deleted inserted replaced
160:0c589138a6f3 161:3913aff613e8
1 //
2 // CurrencyConversion.swift
3 // Simoleon
4 //
5 // Created by Dennis Concepción Martín on 26/8/21.
6 //
7
8 import Foundation
9
10 class CurrencyConversion: ObservableObject {
11 // Forex pair -> XXX/YYY where XXX = base symbol, YYY = quote symbol
12
13 @Published var baseSymbol = "USD" {
14 didSet {
15 getQuote()
16 }
17 }
18 @Published var quoteSymbol = "EUR" {
19 didSet {
20 getQuote()
21 }
22 }
23 @Published var quote = CurrencyQuoteModel()
24 @Published var isShowing = false
25 @Published var showingAlert = false
26
27 let networkHelper = NetworkHelper()
28
29 init() {
30 getQuote()
31 }
32
33 func getQuote() {
34 self.isShowing = false
35 let pair = "\(baseSymbol)/\(quoteSymbol)"
36 let apiKey = readConfig(withKey: "API_KEY")!
37 let url = "https://api.1forge.com/quotes?pairs=\(pair)&api_key=\(apiKey)"
38
39 try? networkHelper.httpRequest(url: url, model: [CurrencyQuoteModel].self) { response in
40 if let quote = response.first {
41 self.quote = quote
42 }
43
44 self.isShowing = true
45 }
46 }
47 }