2019-11-03 01:25:55 +01:00
|
|
|
//
|
|
|
|
// API.swift
|
|
|
|
// TodayExtension
|
|
|
|
//
|
|
|
|
// Created by Marcos Rodriguez on 11/2/19.
|
|
|
|
// Copyright © 2019 Facebook. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
2020-11-02 14:11:28 +01:00
|
|
|
class TodayAPI {
|
2019-11-03 01:25:55 +01:00
|
|
|
|
|
|
|
static func fetchPrice(currency: String, completion: @escaping ((Dictionary<String, Any>?, Error?) -> Void)) {
|
|
|
|
guard let url = URL(string: "https://api.coindesk.com/v1/bpi/currentPrice/\(currency).json") else {return}
|
|
|
|
|
|
|
|
URLSession.shared.dataTask(with: url) { (data, response, error) in
|
|
|
|
guard let dataResponse = data,
|
2020-04-12 05:03:51 +02:00
|
|
|
let json = ((try? JSONSerialization.jsonObject(with: dataResponse, options: .mutableContainers) as? Dictionary<String, Any>) as Dictionary<String, Any>??),
|
2019-11-03 01:25:55 +01:00
|
|
|
error == nil else {
|
|
|
|
print(error?.localizedDescription ?? "Response Error")
|
|
|
|
completion(nil, error)
|
|
|
|
return }
|
|
|
|
|
|
|
|
completion(json, nil)
|
|
|
|
}.resume()
|
|
|
|
}
|
|
|
|
|
|
|
|
static func getUserPreferredCurrency() -> String {
|
|
|
|
guard let userDefaults = UserDefaults(suiteName: "group.io.bluewallet.bluewallet"),
|
|
|
|
let preferredCurrency = userDefaults.string(forKey: "preferredCurrency")
|
|
|
|
else {
|
|
|
|
return "USD"
|
|
|
|
}
|
|
|
|
|
2020-11-02 14:11:28 +01:00
|
|
|
if preferredCurrency != TodayAPI.getLastSelectedCurrency() {
|
2019-11-03 01:25:55 +01:00
|
|
|
UserDefaults.standard.removeObject(forKey: TodayData.TodayCachedDataStoreKey)
|
|
|
|
UserDefaults.standard.removeObject(forKey: TodayData.TodayDataStoreKey)
|
|
|
|
UserDefaults.standard.synchronize()
|
|
|
|
}
|
|
|
|
|
|
|
|
return preferredCurrency
|
|
|
|
}
|
|
|
|
|
|
|
|
static func getUserPreferredCurrencyLocale() -> String {
|
|
|
|
guard let userDefaults = UserDefaults(suiteName: "group.io.bluewallet.bluewallet"),
|
|
|
|
let preferredCurrency = userDefaults.string(forKey: "preferredCurrencyLocale")
|
|
|
|
else {
|
|
|
|
return "en_US"
|
|
|
|
}
|
|
|
|
return preferredCurrency
|
|
|
|
}
|
|
|
|
|
|
|
|
static func getLastSelectedCurrency() -> String {
|
|
|
|
guard let dataStore = UserDefaults.standard.string(forKey: "currency") else {
|
|
|
|
return "USD"
|
|
|
|
}
|
|
|
|
|
|
|
|
return dataStore
|
|
|
|
}
|
|
|
|
|
|
|
|
static func saveNewSelectedCurrency() {
|
2020-11-02 14:11:28 +01:00
|
|
|
UserDefaults.standard.setValue(TodayAPI.getUserPreferredCurrency(), forKey: "currency")
|
2019-11-03 01:25:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|