From a191fc7e4b693cf2cf88471840422368e7f6b1f4 Mon Sep 17 00:00:00 2001 From: Sayantan Roychowdhury Date: Tue, 25 Jul 2023 19:23:56 +0530 Subject: [PATCH] make it more readable. --- .../e/apps/data/fused/FusedApiImpl.kt | 50 +++++++++++-------- 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/foundation/e/apps/data/fused/FusedApiImpl.kt b/app/src/main/java/foundation/e/apps/data/fused/FusedApiImpl.kt index 510337d26..c2de36bce 100644 --- a/app/src/main/java/foundation/e/apps/data/fused/FusedApiImpl.kt +++ b/app/src/main/java/foundation/e/apps/data/fused/FusedApiImpl.kt @@ -337,30 +337,36 @@ class FusedApiImpl @Inject constructor( packageSpecificResults: ArrayList ): GplaySearchResultLiveData { return runFlowWithTimeout( - { - getGplaySearchResult(query) - }, { - it.second - }, { - Pair(listOf(), false) // empty data for timeout - } + { getGplaySearchResult(query) }, + { it.second }, // End condition of flow - provided by Gplay lib. + { Pair(listOf(), false) } // empty data for timeout ).map { - if (it.isSuccess()) { - searchResult.addAll(it.data!!.first) - ResultSupreme.Success( - Pair( - filterWithKeywordSearch( - searchResult, - packageSpecificResults, - query - ), - it.data!!.second - ) - ) - } else { - it - } + filterAndAccumulateSearch(it, searchResult, packageSpecificResults, query) + } + } + + private fun filterAndAccumulateSearch( + result: ResultSupreme, Boolean>>, + searchResult: MutableList, + packageSpecificResults: List, + query: String, + ): ResultSupreme, Boolean>> { + if (!result.isSuccess()) { + return result } + + val searchResultList = result.data!!.first + val moreLoading = result.data!!.second + + searchResult.addAll(searchResultList) + + val filteredApps = filterWithKeywordSearch( + searchResult, + packageSpecificResults, + query + ) + + return ResultSupreme.Success(Pair(filteredApps, moreLoading)) } private suspend fun fetchOpenSourceSearchResult( -- GitLab