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 16b3d2cefb7e6ac34f476ba3c510a6fd199aaf6b..cb4f227a6c36a8b94cc2a86cd10e01b117043c53 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 296b15d232b82538bbd6c09cfd908f4fb0956b63..9a30a62a97e7c58b70460ab9ab9133652fe2bfa5 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 191ab13406083cf2f52c74d7d175d5a98cff126d..a0b02883a0e4c23f931bc186a8d9b067ca9ba26d 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 e1c740ccce4e249ca88f374ce45156176d08ebe7..6ecc4c0a34c982a17314675133ebe98e5ad0f398 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 b7b9451642d08e3f5557b796611bc2cbce9a3157..07322322d77375990043c8a6931582c822ee18ec 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