Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit dd340bb9 authored by Hasib Prince's avatar Hasib Prince
Browse files

fetch search result api is updated

parent 50b76170
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -90,8 +90,8 @@ android {

    buildTypes {
        debug {
            versionNameSuffix ".debug"
            applicationIdSuffix ".debug"
//            versionNameSuffix ".debug"
//            applicationIdSuffix ".debug"
            signingConfig signingConfigs.debugConfig
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
+1 −1
Original line number Diff line number Diff line
@@ -1118,7 +1118,7 @@ class FusedApiImpl @Inject constructor(
    private suspend fun getGplaySearchResult(
        query: String,
    ): Flow<Pair<List<FusedApp>, Boolean>> {
        val searchResults = gplayRepository.getSearchResult(query)
        val searchResults = gplayRepository.getSearchResult(query, null)
        return searchResults.map {
            val fusedAppList = it.first.map { app -> replaceWithFDroid(app) }
            Pair(
+2 −1
Original line number Diff line number Diff line
@@ -22,12 +22,13 @@ import com.aurora.gplayapi.SearchSuggestEntry
import com.aurora.gplayapi.data.models.App
import com.aurora.gplayapi.data.models.Category
import com.aurora.gplayapi.data.models.File
import com.aurora.gplayapi.data.models.SearchBundle
import foundation.e.apps.data.BaseStoreRepository
import foundation.e.apps.data.fused.utils.CategoryType
import kotlinx.coroutines.flow.Flow

interface GplayStoreRepository : BaseStoreRepository {
    suspend fun getSearchResult(query: String): Flow<Pair<List<App>, Boolean>>
    suspend fun getSearchResult(query: String, subBundle: MutableSet<SearchBundle.SubBundle>?): Flow<Pair<List<App>, Boolean>>
    suspend fun getSearchSuggestions(query: String): List<SearchSuggestEntry>
    suspend fun getAppsByCategory(category: String, pageUrl: String? = null): Any
    suspend fun getCategories(type: CategoryType? = null): List<Category>
+66 −36
Original line number Diff line number Diff line
@@ -44,6 +44,7 @@ import kotlinx.coroutines.flow.FlowCollector
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.withContext
import timber.log.Timber
import javax.inject.Inject

class GplayStoreRepositoryImpl @Inject constructor(
@@ -76,52 +77,81 @@ class GplayStoreRepositoryImpl @Inject constructor(
        context.getString(R.string.movers_shakers_games) to mapOf(Chart.MOVERS_SHAKERS to TopChartsHelper.Type.GAME),
    )

//    override suspend fun getSearchResult(
//        query: String,
//    ): Flow<Pair<List<App>, Boolean>> {
//        return flow {
//
//            /*
//             * Variable names and logic made same as that of Aurora store.
//             * Issue: https://gitlab.e.foundation/e/backlog/-/issues/5171
//             */
//            var authData = loginSourceRepository.gplayAuth ?: return@flow
//
//            val searchHelper =
//                SearchHelper(authData).using(gPlayHttpClient)
//            val searchBundle = searchHelper.searchResults(query)
//
//            val initialReplacedList = mutableListOf<App>()
//            val INITIAL_LIMIT = 4
//
//            emitReplacedList(
//                this@flow,
//                initialReplacedList,
//                INITIAL_LIMIT,
//                searchBundle,
//                true,
//            )
//
//            var nextSubBundleSet: MutableSet<SearchBundle.SubBundle>
//            do {
//                nextSubBundleSet = fetchNextSubBundle(
//                    searchBundle,
//                    searchHelper,
//                    this@flow,
//                    initialReplacedList,
//                    INITIAL_LIMIT
//                )
//            } while (nextSubBundleSet.isNotEmpty())
//
//            /*
//             * If initialReplacedList size is less than INITIAL_LIMIT,
//             * it means the results were very less and nothing has been emitted so far.
//             * Hence emit the list.
//             */
//            if (initialReplacedList.size < INITIAL_LIMIT) {
//                emitInMain(this@flow, initialReplacedList, false)
//            }
//        }.flowOn(Dispatchers.IO)
//    }

    override suspend fun getSearchResult(
        query: String,
        subBundle: MutableSet<SearchBundle.SubBundle>?
    ): Flow<Pair<List<App>, Boolean>> {
        return flow {

            /*
             * Variable names and logic made same as that of Aurora store.
             * Issue: https://gitlab.e.foundation/e/backlog/-/issues/5171
             */
            var authData = loginSourceRepository.gplayAuth ?: return@flow

            val searchHelper =
                SearchHelper(authData).using(gPlayHttpClient)
            val searchBundle = searchHelper.searchResults(query)

            val initialReplacedList = mutableListOf<App>()
            val INITIAL_LIMIT = 4

            emitReplacedList(
                this@flow,
                initialReplacedList,
                INITIAL_LIMIT,
                searchBundle,
                true,
            )
            Timber.d("Fetching search result for $query, subBundle: $subBundle")

            var nextSubBundleSet: MutableSet<SearchBundle.SubBundle>
            do {
                nextSubBundleSet = fetchNextSubBundle(
                    searchBundle,
                    searchHelper,
                    this@flow,
                    initialReplacedList,
                    INITIAL_LIMIT
                )
            } while (nextSubBundleSet.isNotEmpty())
            subBundle?.let { 
                val searchResult = searchHelper.next(it)
                emitSearchResult(searchResult)
                return@let
            }

            /*
             * If initialReplacedList size is less than INITIAL_LIMIT,
             * it means the results were very less and nothing has been emitted so far.
             * Hence emit the list.
             */
            if (initialReplacedList.size < INITIAL_LIMIT) {
                emitInMain(this@flow, initialReplacedList, false)
            val searchResult = searchHelper.searchResults(query)
            emitSearchResult(searchResult)
        }
    }
        }.flowOn(Dispatchers.IO)

    private suspend fun FlowCollector<Pair<List<App>, Boolean>>.emitSearchResult(
        searchBundle: SearchBundle
    ) {
        val apps = searchBundle.appList
        Timber.d("Search result is found: ${apps.size}")
        emit(Pair(apps, searchBundle.subBundles.isNotEmpty()))
    }

    private suspend fun fetchNextSubBundle(