From f3e0ee56dfcdca2034800a46341b9e53149b7637 Mon Sep 17 00:00:00 2001 From: Konrad Pozniak Date: Wed, 19 Jul 2023 09:39:07 +0200 Subject: [PATCH] Check for empty trending response (#3853) Fixes #3852 --- .../trending/viewmodel/TrendingViewModel.kt | 32 +++++++++++-------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/components/trending/viewmodel/TrendingViewModel.kt b/app/src/main/java/com/keylesspalace/tusky/components/trending/viewmodel/TrendingViewModel.kt index d1877a2fc..67447b308 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/trending/viewmodel/TrendingViewModel.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/trending/viewmodel/TrendingViewModel.kt @@ -82,22 +82,26 @@ class TrendingViewModel @Inject constructor( mastodonApi.trendingTags().fold( { tagResponse -> - val homeFilters = deferredFilters.await().getOrNull()?.filter { filter -> - filter.context.contains(Filter.Kind.HOME.kind) - } - val tags = tagResponse - .filter { tag -> - homeFilters?.none { filter -> - filter.keywords.any { keyword -> keyword.keyword.equals(tag.name, ignoreCase = true) } - } ?: false + + val firstTag = tagResponse.firstOrNull() + _uiState.value = if (firstTag == null) { + TrendingUiState(emptyList(), LoadingState.LOADED) + } else { + val homeFilters = deferredFilters.await().getOrNull()?.filter { filter -> + filter.context.contains(Filter.Kind.HOME.kind) } - .sortedByDescending { tag -> tag.history.sumOf { it.uses.toLongOrNull() ?: 0 } } - .toViewData() + val tags = tagResponse + .filter { tag -> + homeFilters?.none { filter -> + filter.keywords.any { keyword -> keyword.keyword.equals(tag.name, ignoreCase = true) } + } ?: false + } + .sortedByDescending { tag -> tag.history.sumOf { it.uses.toLongOrNull() ?: 0 } } + .toViewData() - val firstTag = tagResponse.first() - val header = TrendingViewData.Header(firstTag.start(), firstTag.end()) - - _uiState.value = TrendingUiState(listOf(header) + tags, LoadingState.LOADED) + val header = TrendingViewData.Header(firstTag.start(), firstTag.end()) + TrendingUiState(listOf(header) + tags, LoadingState.LOADED) + } }, { error -> Log.w(TAG, "failed loading trending tags", error)