BlueWallet/ios/WalletInformationWidget/Widgets/Shared/WidgetAPI.swift

124 lines
4.3 KiB
Swift
Raw Normal View History

2020-11-02 08:11:28 -05:00
//
// WidgetAPI.swift
// TodayExtension
//
// Created by Marcos Rodriguez on 11/2/19.
// Copyright © 2019 Facebook. All rights reserved.
//
import Foundation
struct CurrencyError: LocalizedError {
var errorDescription: String = "Failed to parse response"
}
2020-11-02 08:11:28 -05:00
var numberFormatter: NumberFormatter {
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
formatter.maximumFractionDigits = 0
formatter.locale = Locale.current
formatter.groupingSeparator = " "
return formatter
}
class WidgetAPI {
static func fetchPrice(currency: String, completion: @escaping ((WidgetDataStore?, Error?) -> Void)) {
2020-11-20 22:58:04 -05:00
let currencyToFiatUnit = fiatUnit(currency: currency)
guard let source = currencyToFiatUnit?.source, let endPointKey = currencyToFiatUnit?.endPointKey else { return }
var urlString: String
switch source {
case "Yadio":
urlString = "https://api.yadio.io/json/\(endPointKey)"
case "BitcoinduLiban":
urlString = "https://bitcoinduliban.org/api.php?key=lbpusd"
default:
urlString = "https://api.coindesk.com/v1/bpi/currentprice/\(endPointKey).json"
}
guard let url = URL(string:urlString) else { return }
2020-11-02 08:11:28 -05:00
URLSession.shared.dataTask(with: url) { (data, response, error) in
guard let dataResponse = data,
2020-11-20 22:58:04 -05:00
let json = (try? JSONSerialization.jsonObject(with: dataResponse, options: .mutableContainers) as? Dictionary<String, Any>),
error == nil
2020-11-20 22:58:04 -05:00
else {
2020-11-02 08:11:28 -05:00
print(error?.localizedDescription ?? "Response Error")
completion(nil, error)
return
}
var latestRateDataStore: WidgetDataStore?
switch source {
case "Yadio":
guard let rateDict = json[endPointKey] as? [String: Any],
let rateDouble = rateDict["price"] as? Double,
let lastUpdated = json["timestamp"] as? Int
else { break }
latestRateDataStore = WidgetDataStore(rate: String(rateDouble), lastUpdate: String(lastUpdated), rateDouble: rateDouble)
case "BitcoinduLiban":
guard let rateString = json["BTC/LBP"] as? String,
let lastUpdatedString = json["date/time"] as? String
else { break }
guard let rateDouble = Double(rateString) else {return}
latestRateDataStore = WidgetDataStore(rate: rateString, lastUpdate: lastUpdatedString, rateDouble: rateDouble)
default:
guard let bpi = json["bpi"] as? Dictionary<String, Any>,
let preferredCurrency = bpi[endPointKey] as? Dictionary<String, Any>,
let rateString = preferredCurrency["rate"] as? String,
let rateDouble = preferredCurrency["rate_float"] as? Double,
let time = json["time"] as? Dictionary<String, Any>,
let lastUpdatedString = time["updatedISO"] as? String
else { break }
latestRateDataStore = WidgetDataStore(rate: rateString, lastUpdate: lastUpdatedString, rateDouble: rateDouble)
}
if (latestRateDataStore == nil) {
completion(nil, CurrencyError())
return
}
2020-11-02 08:11:28 -05:00
completion(latestRateDataStore, nil)
}.resume()
}
2020-11-02 08:11:28 -05:00
static func getUserPreferredCurrency() -> String {
guard let userDefaults = UserDefaults(suiteName: UserDefaultsGroupKey.GroupName.rawValue),
2020-11-02 08:11:28 -05:00
let preferredCurrency = userDefaults.string(forKey: "preferredCurrency")
else {
return "USD"
}
2020-11-02 08:11:28 -05:00
if preferredCurrency != WidgetAPI.getLastSelectedCurrency() {
UserDefaults.standard.removeObject(forKey: WidgetData.WidgetCachedDataStoreKey)
UserDefaults.standard.removeObject(forKey: WidgetData.WidgetDataStoreKey)
UserDefaults.standard.synchronize()
}
2020-11-02 08:11:28 -05:00
return preferredCurrency
}
2020-11-02 08:11:28 -05:00
static func getUserPreferredCurrencyLocale() -> String {
guard let userDefaults = UserDefaults(suiteName: UserDefaultsGroupKey.GroupName.rawValue),
2020-11-02 08:11:28 -05:00
let preferredCurrency = userDefaults.string(forKey: "preferredCurrencyLocale")
else {
return "en_US"
}
return preferredCurrency
}
2020-11-02 08:11:28 -05:00
static func getLastSelectedCurrency() -> String {
guard let dataStore = UserDefaults.standard.string(forKey: "currency") else {
return "USD"
}
2020-11-02 08:11:28 -05:00
return dataStore
}
2020-11-02 08:11:28 -05:00
static func saveNewSelectedCurrency() {
UserDefaults.standard.setValue(WidgetAPI.getUserPreferredCurrency(), forKey: "currency")
}
2020-11-02 08:11:28 -05:00
}