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 77a663043e3b15d043954571f5fc9e6f2a7c11c1..d992aa61d13330c5a147961598a0deefc1c61dcd 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 @@ -61,13 +61,18 @@ import foundation.e.apps.data.fused.utils.CategoryUtils import foundation.e.apps.data.fusedDownload.models.FusedDownload import foundation.e.apps.data.gplay.GplayStoreRepository import foundation.e.apps.data.handleNetworkResult +import foundation.e.apps.data.login.AuthObject import foundation.e.apps.data.preference.PreferenceManagerModule import foundation.e.apps.install.pkg.PWAManagerModule import foundation.e.apps.install.pkg.PkgManagerModule import foundation.e.apps.ui.home.model.HomeChildFusedAppDiffUtil +import foundation.e.apps.utils.eventBus.AppEvent +import foundation.e.apps.utils.eventBus.EventBus import kotlinx.coroutines.Deferred +import kotlinx.coroutines.MainScope import kotlinx.coroutines.async import kotlinx.coroutines.coroutineScope +import kotlinx.coroutines.launch import retrofit2.Response import timber.log.Timber import javax.inject.Inject @@ -91,6 +96,8 @@ class FusedApiImpl @Inject constructor( private const val CATEGORY_TITLE_REPLACEABLE_CONJUNCTION = "&" private const val CATEGORY_OPEN_GAMES_ID = "game_open_games" private const val CATEGORY_OPEN_GAMES_TITLE = "Open games" + private const val THRESHOLD_LIMITED_RESULT_HOME_PAGE = 4 + private const val KEYWORD_TEST_SEARCH = "facebook" } /** @@ -1030,6 +1037,8 @@ class FusedApiImpl @Inject constructor( val fusedAppList = searchResults.first.map { app -> replaceWithFDroid(app) }.toMutableList() + handleLimitedResult(fusedAppList) + if (searchResults.second.isNotEmpty()) { fusedAppList.add(FusedApp(isPlaceHolder = true)) } @@ -1038,6 +1047,18 @@ class FusedApiImpl @Inject constructor( } } + private suspend fun handleLimitedResult(appList: List?) { + if (appList.isNullOrEmpty()) { + // Call search api with a common keyword (ex: facebook) + // to ensure Gplay is returning empty as search result for other keywords as well + val searchResult = gplayRepository.getSearchResult(KEYWORD_TEST_SEARCH, null) + if (searchResult.first.isEmpty()) { + Timber.w("Limited result for search is found...") + refreshToken() + } + } + } + /* * This function will replace a GPlay app with F-Droid app if exists, * else will show the GPlay app itself. @@ -1174,10 +1195,30 @@ class FusedApiImpl @Inject constructor( } list.add(FusedHome(it.key, fusedApps)) } - Timber.d("===> $list") + + handleLimitedResult(list) + Timber.d("HomePageData: $list") + return list } + private fun handleLimitedResult(fusedHomeList: List) { + val gplayHomes = fusedHomeList.filter { fusedHome -> fusedHome.source.isEmpty() } + val hasGplayLimitedResult = gplayHomes.any { fusedHome -> fusedHome.list.size < THRESHOLD_LIMITED_RESULT_HOME_PAGE } + if (hasGplayLimitedResult) { + Timber.w("Limited result is found for homepage...") + refreshToken() + } + } + + private fun refreshToken() { + MainScope().launch { + EventBus.invokeEvent( + AppEvent.InvalidAuthEvent(AuthObject.GPlayAuth::class.java.simpleName) + ) + } + } + /* * FusedApp-related internal extensions and functions */