From 1c3ee988994e39697e78d41d6e14943fc444847e Mon Sep 17 00:00:00 2001 From: Oleg Gordiichuk Date: Sun, 24 Dec 2017 00:25:36 +0200 Subject: [PATCH] Update of the CoinTicker model. --- .../CoinMarket/Types/CoinTicker.swift | 97 +------------------ Trust/Tokens/Types/TokenObject.swift | 2 + .../ViewModels/TokenViewCellViewModel.swift | 7 +- Trust/Tokens/ViewModels/TokensViewModel.swift | 2 +- 4 files changed, 7 insertions(+), 101 deletions(-) diff --git a/Trust/EtherClient/CoinMarket/Types/CoinTicker.swift b/Trust/EtherClient/CoinMarket/Types/CoinTicker.swift index 1b45348dd..0a04b7786 100644 --- a/Trust/EtherClient/CoinMarket/Types/CoinTicker.swift +++ b/Trust/EtherClient/CoinMarket/Types/CoinTicker.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 } diff --git a/Trust/Tokens/Types/TokenObject.swift b/Trust/Tokens/Types/TokenObject.swift index 808fa55b7..3807498fb 100644 --- a/Trust/Tokens/Types/TokenObject.swift +++ b/Trust/Tokens/Types/TokenObject.swift @@ -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 = "", diff --git a/Trust/Tokens/ViewModels/TokenViewCellViewModel.swift b/Trust/Tokens/ViewModels/TokenViewCellViewModel.swift index 070d32a79..2c58bd6ac 100644 --- a/Trust/Tokens/ViewModels/TokenViewCellViewModel.swift +++ b/Trust/Tokens/ViewModels/TokenViewCellViewModel.swift @@ -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) } } diff --git a/Trust/Tokens/ViewModels/TokensViewModel.swift b/Trust/Tokens/ViewModels/TokensViewModel.swift index 07aeb22a2..1c630dea9 100644 --- a/Trust/Tokens/ViewModels/TokensViewModel.swift +++ b/Trust/Tokens/ViewModels/TokensViewModel.swift @@ -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 }