update displaying historical price changes, changes percentage

pull/2753/head
Vladyslav shepitko 3 years ago
parent fbd7255f4b
commit 885b04cba2
  1. 4
      AlphaWallet/Core/CoinTicker/CoinTickersFetcher.swift
  2. 5
      AlphaWallet/Tokens/Types/ChartHistory.swift
  3. 50
      AlphaWallet/Transfer/ViewModels/SendHeaderViewViewModel.swift

@ -150,7 +150,7 @@ class CoinTickersFetcher: CoinTickersFetcherType {
self.cacheChartHistory(result: $0, period: period, for: ticker)
})
}
}.recover { e -> Promise<ChartHistory> in
}.recover { _ -> Promise<ChartHistory> in
if shouldRetry {
return self.fetchChartHistory(force: force, period: period, for: key, shouldRetry: false)
} else {
@ -173,7 +173,7 @@ class CoinTickersFetcher: CoinTickersFetcherType {
case .week, .month, .threeMonth, .year:
hasCacheExpired = false
}
if hasCacheExpired {
if hasCacheExpired || cached.history.prices.isEmpty {
//TODO improve by returning the cached value and returning again after refetching. Harder to do with current implement because promises only resolves once. Maybe the Promise's type should be a subscribable?
return .value((ticker: ticker, history: nil))
} else {

@ -71,6 +71,11 @@ struct ChartHistory: Codable, CustomDebugStringConvertible {
}
}
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(prices, forKey: .prices)
}
init(prices: [HistoryValue]) {
self.prices = prices
}

@ -89,7 +89,7 @@ struct TokenInfoPageViewModel {
var yearLowViewModel: TickerFieldValueViewModel {
let value: String = {
let history = values[safe: ChartHistoryPeriod.year.index]
if let min = HistoryHelper(history: history, ticker: ticker).minMax?.min, let value = NumberFormatter.usd.string(from: min) {
if let min = HistoryHelper(history: history).minMax?.min, let value = NumberFormatter.usd.string(from: min) {
return value
} else {
return "-"
@ -106,7 +106,7 @@ struct TokenInfoPageViewModel {
var yearHightViewModel: TickerFieldValueViewModel {
let value: String = {
let history = values[safe: ChartHistoryPeriod.year.index]
if let max = HistoryHelper(history: history, ticker: ticker).minMax?.max, let value = NumberFormatter.usd.string(from: max) {
if let max = HistoryHelper(history: history).minMax?.max, let value = NumberFormatter.usd.string(from: max) {
return value
} else {
return "-"
@ -142,7 +142,7 @@ struct TokenInfoPageViewModel {
private func attributedHistoryValue(period: ChartHistoryPeriod) -> NSAttributedString {
let result: (String, UIColor) = {
let result = HistoryHelper(history: values[safe: period.index], ticker: ticker)
let result = HistoryHelper(history: values[safe: period.index])
switch result.change {
case .appreciate(let percentage, let value):
@ -252,11 +252,9 @@ struct HistoryHelper {
}
private let history: ChartHistory?
private let ticker: CoinTicker?
init(history: ChartHistory?, ticker: CoinTicker?) {
init(history: ChartHistory?) {
self.history = history
self.ticker = ticker
}
var minMax: (min: Double, max: Double)? {
@ -267,34 +265,32 @@ struct HistoryHelper {
}
var change: HistoryHelper.Change {
if let percentage = changePercentage, let value = changeValue {
if isValueAppreciated24h(value) {
return .appreciate(percentage: percentage, value: value)
} else if isValueDepreciated24h(value) {
return .depreciate(percentage: percentage, value: value)
changeValues.flatMap { values -> HistoryHelper.Change in
if isValueAppreciated24h(values.percentage) {
return .appreciate(percentage: values.percentage, value: values.change)
} else if isValueDepreciated24h(values.percentage) {
return .depreciate(percentage: values.percentage, value: values.change)
} else {
return .none
}
} else {
return .none
}
} ?? .none
}
private var historicalValues: (older: Double, newer: Double)? {
guard let history = history else { return nil }
guard let older = history.prices.first?.value, let newer = history.prices.last?.value else { return nil }
return (older, newer)
}
private var changeValues: (change: Double, percentage: Double)? {
history.flatMap { history -> (Double, Double)? in
let value = history.prices
if value.isEmpty { return nil }
private var changeValue: Double? {
guard let val = historicalValues, let ticker = ticker else { return nil }
return val.older - ticker.price_usd
}
var changeSum: Double = 0
var percChangeSum: Double = 0
for i in 0 ..< value.count - 1 {
let change = value[i+1].value - value[i].value
private var changePercentage: Double? {
guard let val = historicalValues, let ticker = ticker else { return nil }
return ticker.price_usd / val.older * 100.0
changeSum += change
percChangeSum += change / value[i+1].value
}
return (changeSum, percChangeSum * 100)
}
}
private func isValueAppreciated24h(_ value: Double?) -> Bool {

Loading…
Cancel
Save