From 484cb0ab2a58889eac82dc48b5771a8ba526006c Mon Sep 17 00:00:00 2001 From: Hasib Prince Date: Fri, 25 Aug 2023 23:35:05 +0600 Subject: [PATCH] fixed: duplicate item in search page --- .../e/apps/data/fused/FusedApiImpl.kt | 19 +++++++++++++------ .../apps/data/gplay/utils/GPlayHttpClient.kt | 15 +++++++-------- .../e/apps/ui/search/SearchFragment.kt | 7 +------ .../e/apps/ui/search/SearchViewModel.kt | 14 ++++++++++++-- app/src/main/res/layout/fragment_search.xml | 9 --------- 5 files changed, 33 insertions(+), 31 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 16b3d2cef..cb4f227a6 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 @@ -259,11 +259,7 @@ class FusedApiImpl @Inject constructor( val packageSpecificResults = ArrayList() var finalSearchResult: ResultSupreme, Boolean>> = ResultSupreme.Error() - fetchPackageSpecificResult(authData, query, packageSpecificResults).let { - if (it.data?.second != true) { // if there are no data to load - return it - } - } + fetchPackageSpecificResult(authData, query, packageSpecificResults) val searchResult = mutableListOf() val cleanApkResults = mutableListOf() @@ -375,6 +371,10 @@ class FusedApiImpl @Inject constructor( gplayPackageResult?.let { packageSpecificResults.add(it) } } + if (preferenceManagerModule.isGplaySelected()) { + packageSpecificResults.add(FusedApp(isPlaceHolder = true)) + } + /* * If there was a timeout, return it and don't try to fetch anything else. * Also send true in the pair to signal more results being loaded. @@ -400,7 +400,14 @@ class FusedApiImpl @Inject constructor( ): List { val filteredResults = list.distinctBy { it.package_name } .filter { packageSpecificResults.isEmpty() || it.package_name != query } - return packageSpecificResults + filteredResults + + val finalList = (packageSpecificResults + filteredResults).toMutableList() + finalList.removeIf { it.isPlaceHolder } + if (preferenceManagerModule.isGplaySelected()) { + finalList.add(FusedApp(isPlaceHolder = true)) + } + + return finalList } private suspend fun getCleanApkPackageResult( diff --git a/app/src/main/java/foundation/e/apps/data/gplay/utils/GPlayHttpClient.kt b/app/src/main/java/foundation/e/apps/data/gplay/utils/GPlayHttpClient.kt index 296b15d23..9a30a62a9 100644 --- a/app/src/main/java/foundation/e/apps/data/gplay/utils/GPlayHttpClient.kt +++ b/app/src/main/java/foundation/e/apps/data/gplay/utils/GPlayHttpClient.kt @@ -193,16 +193,8 @@ class GPlayHttpClient @Inject constructor( return PlayResponse().apply { isSuccessful = response.isSuccessful code = response.code - Timber.d("$TAG: Url: ${response.request.url}\nStatus: $code") - // TODO: exception will be thrown for all apis when all gplay api implementation - // will handle the exceptions. this will be done in following issue. - // Issue: https://gitlab.e.foundation/e/os/backlog/-/issues/1483 - if (response.request.url.toString().contains(SEARCH) && code != 200) { - throw GplayHttpRequestException(code, response.message) - } - if (code == 401) { MainScope().launch { EventBus.invokeEvent( @@ -211,6 +203,13 @@ class GPlayHttpClient @Inject constructor( } } + // TODO: exception will be thrown for all apis when all gplay api implementation + // will handle the exceptions. this will be done in following issue. + // Issue: https://gitlab.e.foundation/e/os/backlog/-/issues/1483 + if (response.request.url.toString().contains(SEARCH) && code != 200) { + throw GplayHttpRequestException(code, response.message) + } + if (response.body != null) { responseBytes = response.body!!.bytes() } diff --git a/app/src/main/java/foundation/e/apps/ui/search/SearchFragment.kt b/app/src/main/java/foundation/e/apps/ui/search/SearchFragment.kt index 191ab1340..a0b02883a 100644 --- a/app/src/main/java/foundation/e/apps/ui/search/SearchFragment.kt +++ b/app/src/main/java/foundation/e/apps/ui/search/SearchFragment.kt @@ -30,7 +30,6 @@ import android.widget.ImageView import android.widget.LinearLayout import androidx.appcompat.app.AlertDialog import androidx.appcompat.widget.SearchView -import androidx.core.view.isVisible import androidx.cursoradapter.widget.CursorAdapter import androidx.cursoradapter.widget.SimpleCursorAdapter import androidx.fragment.app.activityViewModels @@ -195,10 +194,7 @@ class SearchFragment : private fun updateSearchResult( listAdapter: ApplicationListRVAdapter?, appList: List, - hasMore: Boolean, ): Boolean { - binding.loadingProgressBar.isVisible = hasMore - val currentList = listAdapter?.currentList ?: listOf() if (!searchViewModel.isAnyAppUpdated(appList, currentList)) { return false @@ -289,9 +285,8 @@ class SearchFragment : val searchList = searchViewModel.searchResult.value?.data?.first?.toMutableList() ?: emptyList() - val hasMoreDataToLoad = searchViewModel.searchResult.value?.data?.second == true mainActivityViewModel.updateStatusOfFusedApps(searchList, fusedDownloadList) - updateSearchResult(applicationListRVAdapter, searchList, hasMoreDataToLoad) + updateSearchResult(applicationListRVAdapter, searchList) } override fun onTimeout( diff --git a/app/src/main/java/foundation/e/apps/ui/search/SearchViewModel.kt b/app/src/main/java/foundation/e/apps/ui/search/SearchViewModel.kt index e1c740ccc..6ecc4c0a3 100644 --- a/app/src/main/java/foundation/e/apps/ui/search/SearchViewModel.kt +++ b/app/src/main/java/foundation/e/apps/ui/search/SearchViewModel.kt @@ -35,10 +35,12 @@ import foundation.e.apps.data.login.exceptions.CleanApkException import foundation.e.apps.data.login.exceptions.GPlayException import foundation.e.apps.data.login.exceptions.UnknownSourceException import foundation.e.apps.ui.parentFragment.LoadingViewModel +import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import timber.log.Timber import javax.inject.Inject +import kotlin.coroutines.coroutineContext @HiltViewModel class SearchViewModel @Inject constructor( @@ -154,8 +156,16 @@ class SearchViewModel @Inject constructor( handleException(gplaySearchResult.exception ?: UnknownSourceException()) } + val isFirstFetch = nextSubBundle == null nextSubBundle = gplaySearchResult.data?.second + // if first page has less data, then fetch next page data without waiting for users' scroll + if (isFirstFetch && gplaySearchResult.data?.first?.size!! < 4) { + CoroutineScope(coroutineContext).launch { + fetchGplayData(query) + } + } + val currentAppList = updateCurrentAppList(gplaySearchResult) val finalResult = ResultSupreme.Success( Pair(currentAppList.toList(), nextSubBundle?.isNotEmpty() ?: false) @@ -165,12 +175,12 @@ class SearchViewModel @Inject constructor( isLoading = false } - private fun updateCurrentAppList(gplaySearchResult: GplaySearchResult): MutableList { + private fun updateCurrentAppList(gplaySearchResult: GplaySearchResult): List { val currentSearchResult = searchResult.value?.data val currentAppList = currentSearchResult?.first?.toMutableList() ?: mutableListOf() currentAppList.removeIf { item -> item.isPlaceHolder } currentAppList.addAll(gplaySearchResult.data?.first ?: emptyList()) - return currentAppList + return currentAppList.distinctBy { it.package_name } } private fun handleException(exception: Exception) { diff --git a/app/src/main/res/layout/fragment_search.xml b/app/src/main/res/layout/fragment_search.xml index b7b945164..07322322d 100644 --- a/app/src/main/res/layout/fragment_search.xml +++ b/app/src/main/res/layout/fragment_search.xml @@ -95,13 +95,4 @@ - - \ No newline at end of file -- GitLab