From 4f7e1822857f35826457b110af6fce8ef3679b0a Mon Sep 17 00:00:00 2001 From: Tangem Date: Wed, 28 Aug 2024 16:04:23 +0300 Subject: [PATCH] Updated on 2026-08-14 --- .../com/tangem/common/ui/charts/state/Utils.kt | 13 +++++++++++++ .../converter/PriceAndTimePointValuesConverter.kt | 11 ++--------- .../details/impl/model/MarketsTokenDetailsModel.kt | 5 +++-- .../token/block/impl/model/TokenMarketBlockModel.kt | 8 ++++++-- .../model/converters/MarketsTokenItemConverter.kt | 3 ++- 5 files changed, 26 insertions(+), 14 deletions(-) create mode 100644 common/ui-charts/src/main/kotlin/com/tangem/common/ui/charts/state/Utils.kt diff --git a/common/ui-charts/src/main/kotlin/com/tangem/common/ui/charts/state/Utils.kt b/common/ui-charts/src/main/kotlin/com/tangem/common/ui/charts/state/Utils.kt new file mode 100644 index 0000000000..558f63dbde --- /dev/null +++ b/common/ui-charts/src/main/kotlin/com/tangem/common/ui/charts/state/Utils.kt @@ -0,0 +1,13 @@ +package com.tangem.common.ui.charts.state + +import kotlinx.collections.immutable.toImmutableList + +fun MarketChartData.Data.sorted(): MarketChartData.Data { + val points = this.x.zip(this.y).sortedBy { it.first } + val (x, y) = points.unzip() + + return MarketChartData.Data( + x = x.toImmutableList(), + y = y.toImmutableList(), + ) +} \ No newline at end of file diff --git a/common/ui-charts/src/main/kotlin/com/tangem/common/ui/charts/state/converter/PriceAndTimePointValuesConverter.kt b/common/ui-charts/src/main/kotlin/com/tangem/common/ui/charts/state/converter/PriceAndTimePointValuesConverter.kt index 8eeec1a691..e51a8f72a6 100644 --- a/common/ui-charts/src/main/kotlin/com/tangem/common/ui/charts/state/converter/PriceAndTimePointValuesConverter.kt +++ b/common/ui-charts/src/main/kotlin/com/tangem/common/ui/charts/state/converter/PriceAndTimePointValuesConverter.kt @@ -22,11 +22,6 @@ class PriceAndTimePointValuesConverter( private val formatYValuesCache = mutableMapOf() private val formatXValuesCache = mutableMapOf() - private data class Point( - val x: BigDecimal, - val y: BigDecimal, - ) - override fun convert(data: MarketChartData.Data): MarketChartRawData { formatYValuesCache.clear() formatXValuesCache.clear() @@ -38,10 +33,8 @@ class PriceAndTimePointValuesConverter( ) minMaxCache = cache - val points = data.x.zip(data.y) { x, y -> Point(x, y) }.sortedBy { it.x } - - val normY = points.map { it.y }.normalizeToDouble(min = cache.minY, max = cache.maxY) - val normX = points.map { it.x }.normalizeTime(min = cache.minX, max = cache.maxX) + val normY = data.y.normalizeToDouble(min = cache.minY, max = cache.maxY) + val normX = data.x.normalizeTime(min = cache.minX, max = cache.maxX) return if (normX.size > MAX_POINTS) { LTThreeBuckets diff --git a/features/markets/impl/src/main/kotlin/com/tangem/features/markets/details/impl/model/MarketsTokenDetailsModel.kt b/features/markets/impl/src/main/kotlin/com/tangem/features/markets/details/impl/model/MarketsTokenDetailsModel.kt index 3f01b427d9..e899cfeb23 100644 --- a/features/markets/impl/src/main/kotlin/com/tangem/features/markets/details/impl/model/MarketsTokenDetailsModel.kt +++ b/features/markets/impl/src/main/kotlin/com/tangem/features/markets/details/impl/model/MarketsTokenDetailsModel.kt @@ -5,6 +5,7 @@ import arrow.core.getOrElse import com.tangem.common.ui.charts.state.MarketChartData import com.tangem.common.ui.charts.state.MarketChartDataProducer import com.tangem.common.ui.charts.state.MarketChartLook +import com.tangem.common.ui.charts.state.sorted import com.tangem.core.decompose.di.ComponentScoped import com.tangem.core.decompose.model.Model import com.tangem.core.decompose.model.ParamsContainer @@ -237,9 +238,9 @@ internal class MarketsTokenDetailsModel @Inject constructor( chart.onRight { chartDataProducer.runTransactionSuspend { chartData = MarketChartData.Data( - x = it.timeStamps.map { it.toBigDecimal() }.toImmutableList(), y = it.priceY.toImmutableList(), - ) + x = it.timeStamps.map { it.toBigDecimal() }.toImmutableList(), + ).sorted() updateLook { it.copy( diff --git a/features/markets/impl/src/main/kotlin/com/tangem/features/markets/token/block/impl/model/TokenMarketBlockModel.kt b/features/markets/impl/src/main/kotlin/com/tangem/features/markets/token/block/impl/model/TokenMarketBlockModel.kt index 5967eaa915..bb584f1731 100644 --- a/features/markets/impl/src/main/kotlin/com/tangem/features/markets/token/block/impl/model/TokenMarketBlockModel.kt +++ b/features/markets/impl/src/main/kotlin/com/tangem/features/markets/token/block/impl/model/TokenMarketBlockModel.kt @@ -5,6 +5,7 @@ import arrow.core.getOrElse import com.tangem.common.routing.AppRoute import com.tangem.common.ui.charts.state.MarketChartData import com.tangem.common.ui.charts.state.converter.PriceAndTimePointValuesConverter +import com.tangem.common.ui.charts.state.sorted import com.tangem.core.decompose.di.ComponentScoped import com.tangem.core.decompose.model.Model import com.tangem.core.decompose.model.ParamsContainer @@ -105,13 +106,16 @@ internal class TokenMarketBlockModel @Inject constructor( ) result.onRight { res -> + // wait until quotes are loaded + state.first { it.currentPrice != null } + state.update { stateToUpdate -> stateToUpdate.copy( chartData = priceAndTimePointValuesConverter.convert( MarketChartData.Data( y = res.priceY.toImmutableList(), - x = res.timeStamps.sorted().map { it.toBigDecimal() }.toImmutableList(), - ), + x = res.timeStamps.map { it.toBigDecimal() }.toImmutableList(), + ).sorted(), ), ) } diff --git a/features/markets/impl/src/main/kotlin/com/tangem/features/markets/tokenlist/impl/model/converters/MarketsTokenItemConverter.kt b/features/markets/impl/src/main/kotlin/com/tangem/features/markets/tokenlist/impl/model/converters/MarketsTokenItemConverter.kt index c43e29f406..295cebedcf 100644 --- a/features/markets/impl/src/main/kotlin/com/tangem/features/markets/tokenlist/impl/model/converters/MarketsTokenItemConverter.kt +++ b/features/markets/impl/src/main/kotlin/com/tangem/features/markets/tokenlist/impl/model/converters/MarketsTokenItemConverter.kt @@ -3,6 +3,7 @@ package com.tangem.features.markets.tokenlist.impl.model.converters import com.tangem.common.ui.charts.state.converter.PriceAndTimePointValuesConverter import com.tangem.common.ui.charts.state.MarketChartData import com.tangem.common.ui.charts.state.MarketChartRawData +import com.tangem.common.ui.charts.state.sorted import com.tangem.core.ui.components.marketprice.PriceChangeType import com.tangem.core.ui.utils.BigDecimalFormatter import com.tangem.domain.appcurrency.model.AppCurrency @@ -115,7 +116,7 @@ internal class MarketsTokenItemConverter( MarketChartData.Data( y = ct.priceY.toImmutableList(), x = ct.timeStamps.map { it.toBigDecimal() }.toImmutableList(), - ), + ).sorted(), ) } }