Updated on 2026-08-14

This commit is contained in:
Tangem 2026-01-26 08:43:08 +01:00
parent e7b7412222
commit fc0dd403c6

View file

@ -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<SortByTypeUM, ImmutableList<MarketsListItemUM>>,
@ -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<MarketsListItemUM>,
): 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<MarketsListItemUM>,
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<SortByTypeUM, MarketChartUM>) {
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<SortByTypeUM, Throwable?>,
itemsByOrder: Map<SortByTypeUM, ImmutableList<MarketsListItemUM>>,