Update of the CoinTicker model.

pull/2/head
Oleg Gordiichuk 7 years ago committed by Michael Scoff
parent 7fba29e14b
commit 1c3ee98899
  1. 97
      Trust/EtherClient/CoinMarket/Types/CoinTicker.swift
  2. 2
      Trust/Tokens/Types/TokenObject.swift
  3. 7
      Trust/Tokens/ViewModels/TokenViewCellViewModel.swift
  4. 2
      Trust/Tokens/ViewModels/TokensViewModel.swift

@ -4,101 +4,8 @@ import Foundation
import RealmSwift
struct CoinTicker {
let id: String
let name: String
let symbol: String
let rank: String
let usdPrice: String
let btcPrice: String
let volumeUsd24h: String?
let marketCapUsd: String?
let availableSupply: String?
let totalSupply: String?
let percentChange1h: String?
let percentChange24h: String?
let percentChange7d: String?
let lastUpdated: String
let img: String
static let path = "https://files.coinmarketcap.com/static/img/coins/32x32/"
}
extension CoinTicker: Decodable {
enum CoinTickerKeys: String, CodingKey {
case id = "id"
case name = "name"
case symbol = "symbol"
case rank = "rank"
case usdPrice = "price_usd"
case btcPrice = "price_btc"
case volumeUsd24h = "24h_volume_usd"
case marketCapUsd = "market_cap_usd"
case availableSupply = "available_supply"
case totalSupply = "total_supply"
case percentChange1h = "percent_change_1h"
case percentChange24h = "percent_change_24h"
case percentChange7d = "percent_change_7d"
case lastUpdated = "last_updated"
case img = "img"
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CoinTickerKeys.self)
let id: String = try container.decode(String.self, forKey: .id)
let name: String = try container.decode(String.self, forKey: .name)
let symbol: String = try container.decode(String.self, forKey: .symbol)
let rank: String = try container.decode(String.self, forKey: .rank)
let usdPrice: String = try container.decode(String.self, forKey: .usdPrice)
let btcPrice: String = try container.decode(String.self, forKey: .btcPrice)
let volumeUsd24h: String? = try container.decodeIfPresent(String.self, forKey: .volumeUsd24h)
let marketCapUsd: String? = try container.decodeIfPresent(String.self, forKey: .marketCapUsd)
let availableSupply: String? = try container.decodeIfPresent(String.self, forKey: .availableSupply)
let totalSupply: String? = try container.decodeIfPresent(String.self, forKey: .totalSupply)
let percentChange1h: String? = try container.decodeIfPresent(String.self, forKey: .percentChange1h)
let percentChange24h: String? = try container.decodeIfPresent(String.self, forKey: .percentChange24h)
let percentChange7d: String? = try container.decodeIfPresent(String.self, forKey: .percentChange7d)
let lastUpdated: String = try container.decode(String.self, forKey: .lastUpdated)
let img: String = CoinTicker.path + id + ".png"
self.init(id: id, name: name, symbol: symbol, rank: rank, usdPrice: usdPrice, btcPrice: btcPrice, volumeUsd24h: volumeUsd24h, marketCapUsd: marketCapUsd, availableSupply: availableSupply, totalSupply: totalSupply, percentChange1h: percentChange1h, percentChange24h: percentChange24h, percentChange7d: percentChange7d, lastUpdated: lastUpdated, img: img)
}
}
class RealmCoinTicker: Object {
@objc dynamic var id: String = ""
@objc dynamic var name: String = ""
@objc dynamic var symbol: String = ""
@objc dynamic var rank: String = ""
@objc dynamic var usdPrice: String = ""
@objc dynamic var btcPrice: String = ""
@objc dynamic var volumeUsd24h: String = ""
@objc dynamic var marketCapUsd: String = ""
@objc dynamic var availableSupply: String = ""
@objc dynamic var totalSupply: String = ""
@objc dynamic var percentChange1h: String = ""
@objc dynamic var percentChange24h: String = ""
@objc dynamic var percentChange7d: String = ""
@objc dynamic var lastUpdated: String = ""
@objc dynamic var img: String = ""
override static func primaryKey() -> String? {
return "id"
}
convenience init(coin: CoinTicker) {
self.init()
id = coin.id
name = coin.name
symbol = coin.symbol
rank = coin.rank
usdPrice = coin.usdPrice
btcPrice = coin.btcPrice
volumeUsd24h = coin.volumeUsd24h ?? ""
marketCapUsd = coin.marketCapUsd ?? ""
availableSupply = coin.availableSupply ?? ""
totalSupply = coin.totalSupply ?? ""
percentChange1h = coin.percentChange1h ?? ""
percentChange24h = coin.percentChange24h ?? ""
percentChange7d = coin.percentChange7d ?? ""
lastUpdated = coin.lastUpdated
img = coin.img
}
var entity: CoinTicker {
return CoinTicker(id: id, name: name, symbol: symbol, rank: rank, usdPrice: usdPrice, btcPrice: btcPrice, volumeUsd24h: volumeUsd24h, marketCapUsd: marketCapUsd, availableSupply: availableSupply, totalSupply: totalSupply, percentChange1h: percentChange1h, percentChange24h: percentChange24h, percentChange7d: percentChange7d, lastUpdated: lastUpdated, img: img)
}
let price: String
let percent_change_24h: String
}

@ -17,6 +17,8 @@ class TokenObject: Object {
@objc dynamic var value: String = ""
@objc dynamic var isCustom: Bool = false
@objc dynamic var isDisabled: Bool = false
static let tokenLogoPath = "https://files.coinmarketcap.com/static/img/coins/64x64/"
convenience init(
contract: String = "",

@ -39,7 +39,7 @@ struct TokenViewCellViewModel {
let noResult = "-"
guard let ticker = ticker else { return noResult }
let tokenValue = CurrencyFormatter.plainFormatter.string(from: token.valueBigInt, decimals: token.decimals).doubleValue
let priceInUsd = Double(ticker.usdPrice) ?? 0
let priceInUsd = Double(ticker.price) ?? 0
let amount = tokenValue * priceInUsd
guard amount > 0 else { return noResult }
return CurrencyFormatter.formatter.string(from: NSNumber(value: amount))
@ -70,9 +70,6 @@ struct TokenViewCellViewModel {
}
var imageUrl: URL? {
guard let path = self.ticker?.img else {
return nil
}
return URL(string: path)
return URL(string: TokenObject.tokenLogoPath + token.name)
}
}

@ -28,7 +28,7 @@ struct TokensViewModel {
guard let tickers = tickers else { return 0 }
guard !token.valueBigInt.isZero, let price = tickers[token.symbol] else { return 0 }
let tokenValue = CurrencyFormatter.plainFormatter.string(from: token.valueBigInt, decimals: token.decimals).doubleValue
let priceInUsd = Double(price.usdPrice) ?? 0
let priceInUsd = Double(price.price) ?? 0
return tokenValue * priceInUsd
}

Loading…
Cancel
Save