Update WidgetAPI.swift

This commit is contained in:
marcosrdz 2020-11-07 21:41:31 -05:00
parent d1edcc3624
commit e16de64c1a

View file

@ -41,11 +41,16 @@ class WidgetAPI {
completion(nil, APIError())
return
}
if let response = String(bytes: data, encoding: .utf8), let nextBlockResponse = response.components(separatedBy: #"result":"#).last?.components(separatedBy: ",").first, let nextBlockResponseDouble = Double(nextBlockResponse.trimmingCharacters(in: .whitespacesAndNewlines)) {
let characterSet = Set("0123456789.")
if let response = String(bytes: data, encoding: .utf8), let nextBlockResponse = response.components(separatedBy: #"result":"#).last?.components(separatedBy: ",").first, let nextBlockResponseDouble = Double(nextBlockResponse.filter({characterSet.contains($0)}).trimmingCharacters(in: .whitespacesAndNewlines)) {
print("Successfully obtained response from Electrum sever")
print(userElectrumSettings)
let marketData = MarketData(nextBlock: String(format: "%.0f", (nextBlockResponseDouble / 1024) * 100000000), sats: "0", price: "0", rate: 0)
client.close()
completion(MarketData(nextBlock: String(format: "%.0f", (nextBlockResponseDouble / 1024) * 100000000), sats: "0", price: "0", rate: 0), nil)
completion(marketData, nil)
} else {
client.close()
completion(nil, APIError())
}
case .failure(let error):
print(error)