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

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

App Lounge: conflict resolved: merging main -> 5159-paid_app_update

parents 07fc7b21 35489d12
Loading
Loading
Loading
Loading
Loading
+23 −55
Original line number Diff line number Diff line
@@ -21,9 +21,6 @@ package foundation.e.apps.api.fused
import android.content.Context
import android.text.format.Formatter
import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.liveData
import androidx.lifecycle.map
import com.aurora.gplayapi.Constants
import com.aurora.gplayapi.SearchSuggestEntry
import com.aurora.gplayapi.data.models.App
@@ -179,33 +176,21 @@ class FusedAPIImpl @Inject constructor(
     * Fetches search results from cleanAPK and GPlay servers and returns them
     * @param query Query
     * @param authData [AuthData]
     * @return A livedata list of non-nullable [FusedApp].
     * Observe this livedata to display new apps as they are fetched from the network.
     * @return A list of nullable [FusedApp]
     */
    fun getSearchResults(query: String, authData: AuthData): LiveData<List<FusedApp>> {
        /*
         * Returning livedata to improve performance, so that we do not have to wait forever
         * for all results to be fetched from network before showing them.
         * Issue: https://gitlab.e.foundation/e/backlog/-/issues/5171
         */
        return liveData {
    suspend fun getSearchResults(query: String, authData: AuthData): List<FusedApp> {
        val fusedResponse = mutableListOf<FusedApp>()

        when (preferenceManagerModule.preferredApplicationType()) {
            APP_TYPE_ANY -> {
                    val cleanApkResults = getCleanAPKSearchResults(query)
                    if (cleanApkResults.isNotEmpty()) {
                        /*
                         * If cleanapk results are empty, dont emit emit data as it may
                         * briefly show "No apps found..."
                         */
                        emit(cleanApkResults)
                    }
                    emitSource(getGplayAndCleanapkCombinedResults(query, authData, cleanApkResults))
                fusedResponse.addAll(getCleanAPKSearchResults(query))
                fusedResponse.addAll(getGplaySearchResults(query, authData))
            }
            APP_TYPE_OPEN -> {
                    emit(getCleanAPKSearchResults(query))
                fusedResponse.addAll(getCleanAPKSearchResults(query))
            }
            APP_TYPE_PWA -> {
                    emit(
                fusedResponse.addAll(
                    getCleanAPKSearchResults(
                        query,
                        CleanAPKInterface.APP_SOURCE_ANY,
@@ -214,7 +199,7 @@ class FusedAPIImpl @Inject constructor(
                )
            }
        }
        }
        return fusedResponse.distinctBy { it.package_name }
    }

    suspend fun getSearchSuggestions(query: String, authData: AuthData): List<SearchSuggestEntry> {
@@ -604,27 +589,10 @@ class FusedAPIImpl @Inject constructor(
        return list
    }

    /*
     * Function to return a livedata with value from cleanapk and Google Play store combined.
     * Issue: https://gitlab.e.foundation/e/backlog/-/issues/5171
     */
    private fun getGplayAndCleanapkCombinedResults(
        query: String,
        authData: AuthData,
        cleanApkResults: List<FusedApp>
    ): LiveData<List<FusedApp>> {
        val localList = ArrayList<FusedApp>(cleanApkResults)
        return getGplaySearchResults(query, authData).map { list ->
            localList.apply {
                addAll(list)
            }.distinctBy { it.package_name }
        }
    }

    private fun getGplaySearchResults(query: String, authData: AuthData): LiveData<List<FusedApp>> {
    private suspend fun getGplaySearchResults(query: String, authData: AuthData): List<FusedApp> {
        val searchResults = gPlayAPIRepository.getSearchResults(query, authData)
        return searchResults.map {
            it.map { app -> app.transformToFusedApp() }
        return searchResults.map { app ->
            app.transformToFusedApp()
        }
    }

+1 −2
Original line number Diff line number Diff line
@@ -18,7 +18,6 @@

package foundation.e.apps.api.fused

import androidx.lifecycle.LiveData
import com.aurora.gplayapi.SearchSuggestEntry
import com.aurora.gplayapi.data.models.AuthData
import com.aurora.gplayapi.data.models.Category
@@ -92,7 +91,7 @@ class FusedAPIRepository @Inject constructor(
        return fusedAPIImpl.fetchAuthData(email, aasToken)
    }

    fun getSearchResults(query: String, authData: AuthData): LiveData<List<FusedApp>> {
    suspend fun getSearchResults(query: String, authData: AuthData): List<FusedApp> {
        return fusedAPIImpl.getSearchResults(query, authData)
    }

+18 −28
Original line number Diff line number Diff line
@@ -19,8 +19,6 @@
package foundation.e.apps.api.gplay

import android.content.Context
import androidx.lifecycle.LiveData
import androidx.lifecycle.liveData
import com.aurora.gplayapi.SearchSuggestEntry
import com.aurora.gplayapi.data.models.App
import com.aurora.gplayapi.data.models.AuthData
@@ -84,37 +82,29 @@ class GPlayAPIImpl @Inject constructor(
        return searchData.filter { it.suggestedQuery.isNotBlank() }
    }

    fun getSearchResults(query: String, authData: AuthData): LiveData<List<App>> {
        /*
         * Send livedata to improve UI performance, so we don't have to wait for loading all results.
         * Issue: https://gitlab.e.foundation/e/backlog/-/issues/5171
         */
        return liveData {
    suspend fun getSearchResults(query: String, authData: AuthData): List<App> {
        val searchData = mutableListOf<App>()
        withContext(Dispatchers.IO) {
                /*
                 * Variable names and logic made same as that of Aurora store.
                 * Issue: https://gitlab.e.foundation/e/backlog/-/issues/5171
                 */
            val searchHelper = SearchHelper(authData).using(gPlayHttpClient)
                val searchBundle = searchHelper.searchResults(query)

                emit(searchBundle.appList)
            val searchResult = searchHelper.searchResults(query)
            searchData.addAll(searchResult.appList)

                var nextSubBundleSet: MutableSet<SearchBundle.SubBundle>
            // Fetch more results in case the given result is a promoted app
            if (searchData.size == 1) {
                val bundleSet: MutableSet<SearchBundle.SubBundle> = searchResult.subBundles
                do {
                    nextSubBundleSet = searchBundle.subBundles
                    val newSearchBundle = searchHelper.next(nextSubBundleSet)
                    if (newSearchBundle.appList.isNotEmpty()) {
                        searchBundle.apply {
                            subBundles.clear()
                            subBundles.addAll(newSearchBundle.subBundles)
                            appList.addAll(newSearchBundle.appList)
                            emit(searchBundle.appList)
                    val searchBundle = searchHelper.next(bundleSet)
                    if (searchBundle.appList.isNotEmpty()) {
                        searchData.addAll(searchBundle.appList)
                    }
                    bundleSet.apply {
                        clear()
                        addAll(searchBundle.subBundles)
                    }
                } while (nextSubBundleSet.isNotEmpty())
                } while (bundleSet.isNotEmpty())
            }
        }
        return searchData
    }

    suspend fun getDownloadInfo(
+1 −2
Original line number Diff line number Diff line
@@ -18,7 +18,6 @@

package foundation.e.apps.api.gplay

import androidx.lifecycle.LiveData
import com.aurora.gplayapi.SearchSuggestEntry
import com.aurora.gplayapi.data.models.App
import com.aurora.gplayapi.data.models.AuthData
@@ -47,7 +46,7 @@ class GPlayAPIRepository @Inject constructor(
        return gPlayAPIImpl.getSearchSuggestions(query, authData)
    }

    fun getSearchResults(query: String, authData: AuthData): LiveData<List<App>> {
    suspend fun getSearchResults(query: String, authData: AuthData): List<App> {
        return gPlayAPIImpl.getSearchResults(query, authData)
    }

+2 −15
Original line number Diff line number Diff line
@@ -79,7 +79,6 @@ class SearchFragment :
    private val appProgressViewModel: AppProgressViewModel by viewModels()

    private val SUGGESTION_KEY = "suggestion"
    private var lastSearch = ""

    private var searchView: SearchView? = null
    private var shimmerLayout: ShimmerFrameLayout? = null
@@ -194,19 +193,7 @@ class SearchFragment :
            }
            listAdapter?.registerAdapterDataObserver(object : RecyclerView.AdapterDataObserver() {
                override fun onItemRangeInserted(positionStart: Int, itemCount: Int) {
                    searchView?.run {
                        /*
                         * Only scroll back to 0 position for a new search.
                         *
                         * If we are getting new results from livedata for the old search query,
                         * do not scroll to top as the user may be scrolling to see already
                         * populated results.
                         */
                        if (lastSearch != query?.toString()) {
                            recyclerView?.scrollToPosition(0)
                            lastSearch = query.toString()
                        }
                    }
                    recyclerView!!.scrollToPosition(0)
                }
            })
        }
@@ -231,7 +218,7 @@ class SearchFragment :
            shimmerLayout?.visibility = View.VISIBLE
            recyclerView?.visibility = View.GONE
            noAppsFoundLayout?.visibility = View.GONE
            mainActivityViewModel.authData.value?.let { searchViewModel.getSearchResults(text, it, this) }
            mainActivityViewModel.authData.value?.let { searchViewModel.getSearchResults(text, it) }
        }
        return false
    }
Loading