diff --git a/features/feed/impl/src/main/kotlin/com/tangem/features/feed/model/feed/state/transformers/UpdateMarketChartsTransformer.kt b/features/feed/impl/src/main/kotlin/com/tangem/features/feed/model/feed/state/transformers/UpdateMarketChartsTransformer.kt index f843d1b3eb..d41a6b655b 100644 --- a/features/feed/impl/src/main/kotlin/com/tangem/features/feed/model/feed/state/transformers/UpdateMarketChartsTransformer.kt +++ b/features/feed/impl/src/main/kotlin/com/tangem/features/feed/model/feed/state/transformers/UpdateMarketChartsTransformer.kt @@ -10,7 +10,7 @@ import com.tangem.features.feed.ui.feed.state.MarketChartUM import com.tangem.features.feed.ui.feed.state.SortChartConfigUM import kotlinx.collections.immutable.ImmutableList import kotlinx.collections.immutable.persistentListOf -import kotlinx.collections.immutable.toPersistentHashMap +import kotlinx.collections.immutable.toPersistentMap internal class UpdateMarketChartsTransformer( private val itemsByOrder: Map>, @@ -21,56 +21,16 @@ internal class UpdateMarketChartsTransformer( ) : FeedListUMTransformer { override fun transform(prevState: FeedListUM): FeedListUM { - val newMarketCharts = buildMap { - SortByTypeUM.entries.forEach { sortByType -> - val items = itemsByOrder[sortByType] ?: persistentListOf() - val isLoading = loadingStatesByOrder[sortByType] == true - val hasError = errorStatesByOrder[sortByType] != null - - when { - hasError -> { - put( - key = sortByType, - value = MarketChartUM.LoadingError( - onRetryClicked = { onReload(sortByType) }, - ), - ) - } - isLoading -> { - put(key = sortByType, value = MarketChartUM.Loading) - } - items.isEmpty() -> { - put( - key = sortByType, - value = MarketChartUM.LoadingError( - onRetryClicked = { onReload(sortByType) }, - ), - ) - } - else -> { - put( - key = sortByType, - value = MarketChartUM.Content( - items = items, - sortChartConfig = SortChartConfigUM( - sortByType = sortByType, - isSelected = sortByType == prevState.marketChartConfig.currentSortByType, - ), - ), - ) - } - } + val newMarketCharts = SortByTypeUM.entries + .associateWith { sortByType -> + getNewMarketChart( + sortByType = sortByType, + prevChart = prevState.marketChartConfig.marketCharts[sortByType], + ) } - }.toPersistentHashMap() + .toPersistentMap() - val wasAnyErrorBefore = prevState.marketChartConfig.marketCharts.values.any { - it is MarketChartUM.LoadingError - } - val isAnyErrorNow = newMarketCharts.values.any { it is MarketChartUM.LoadingError } - - if (!wasAnyErrorBefore && isAnyErrorNow) { - sendErrorAnalytics(errorStatesByOrder, itemsByOrder) - } + handleErrorAnalytics(prevState, newMarketCharts) return prevState.copy( marketChartConfig = prevState.marketChartConfig.copy( @@ -79,6 +39,72 @@ internal class UpdateMarketChartsTransformer( ) } + private fun getNewMarketChart(sortByType: SortByTypeUM, prevChart: MarketChartUM?): MarketChartUM { + val items = itemsByOrder[sortByType] ?: persistentListOf() + val isLoading = loadingStatesByOrder[sortByType] == true + val error = errorStatesByOrder[sortByType] + + val shouldBeError = !isLoading && (items.isEmpty() || error != null) + val shouldBeContent = !isLoading && items.isNotEmpty() && error == null + val isDataChanged = isDataChanged( + prevChart = prevChart, + isLoading = isLoading, + shouldBeError = shouldBeError, + shouldBeContent = shouldBeContent, + items = items, + ) + + if (!isDataChanged && prevChart != null) { + return prevChart + } + + return when { + isLoading -> MarketChartUM.Loading + shouldBeError -> MarketChartUM.LoadingError(onRetryClicked = { onReload(sortByType) }) + else -> createContentChart(prevChart, items, sortByType) + } + } + + private fun isDataChanged( + prevChart: MarketChartUM?, + isLoading: Boolean, + shouldBeError: Boolean, + shouldBeContent: Boolean, + items: ImmutableList, + ): Boolean { + return when (prevChart) { + is MarketChartUM.Loading -> !isLoading + is MarketChartUM.LoadingError -> !shouldBeError + is MarketChartUM.Content -> !shouldBeContent || prevChart.items != items + null -> true + } + } + + private fun createContentChart( + prevChart: MarketChartUM?, + items: ImmutableList, + sortByType: SortByTypeUM, + ): MarketChartUM.Content { + val prevContent = prevChart as? MarketChartUM.Content + return MarketChartUM.Content( + items = items, + sortChartConfig = prevContent?.sortChartConfig + ?: SortChartConfigUM( + sortByType = sortByType, + isSelected = false, + ), + ) + } + + private fun handleErrorAnalytics(prevState: FeedListUM, newMarketCharts: Map) { + val wasAnyErrorBefore = prevState.marketChartConfig.marketCharts.values.any { it is MarketChartUM.LoadingError } + val isAnyErrorNow = newMarketCharts.values.any { it is MarketChartUM.LoadingError } + + if (!wasAnyErrorBefore && isAnyErrorNow) { + sendErrorAnalytics(errorStatesByOrder, itemsByOrder) + } + } + private fun sendErrorAnalytics( errorStatesByOrder: Map, itemsByOrder: Map>,