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

Verified Commit 4f87fba5 authored by Fahim M. Choudhury's avatar Fahim M. Choudhury
Browse files

refactor: introduce PlayStoreSearchHelper to delegate search responsibility...

refactor: introduce PlayStoreSearchHelper to delegate search responsibility from PlayStoreRepository
parent 35410b90
Loading
Loading
Loading
Loading
+5 −14
Original line number Diff line number Diff line
@@ -19,7 +19,6 @@
package foundation.e.apps.data.playstore

import android.content.Context
import com.aurora.gplayapi.SearchSuggestEntry
import com.aurora.gplayapi.data.models.Category
import com.aurora.gplayapi.data.models.ContentRating
import com.aurora.gplayapi.data.models.File
@@ -32,7 +31,6 @@ import com.aurora.gplayapi.helpers.contracts.TopChartsContract.Type
import com.aurora.gplayapi.helpers.web.WebAppDetailsHelper
import com.aurora.gplayapi.helpers.web.WebCategoryHelper
import com.aurora.gplayapi.helpers.web.WebCategoryStreamHelper
import com.aurora.gplayapi.helpers.web.WebSearchHelper
import com.aurora.gplayapi.helpers.web.WebTopChartsHelper
import dagger.Lazy
import dagger.hilt.android.qualifiers.ApplicationContext
@@ -61,7 +59,8 @@ class PlayStoreRepository @Inject constructor(
    @ApplicationContext private val context: Context,
    private val gPlayHttpClient: GPlayHttpClient,
    private val authenticatorRepository: AuthenticatorRepository,
    private val applicationDataManager: Lazy<ApplicationDataManager> // Used Lazy to break circular dependency
    private val applicationDataManager: Lazy<ApplicationDataManager>, // Used Lazy to break circular dependency
    private val playStoreSearchHelper: PlayStoreSearchHelper
) : StoreRepository {

    override suspend fun getHomeScreenData(list: MutableList<Home>): List<Home> {
@@ -101,20 +100,12 @@ class PlayStoreRepository @Inject constructor(
    )

    override suspend fun getSearchResults(pattern: String): List<Application> {
        val searchResult = WebSearchHelper().using(gPlayHttpClient).searchResults(pattern)
        return searchResult.appList.map {
            it.toApplication(context)
        }
        val searchResult = playStoreSearchHelper.getSearchResults(pattern)
        return searchResult.map { it.toApplication(context) }
    }

    suspend fun getSearchSuggestions(query: String): List<SearchSuggestion> {
        val searchData = mutableListOf<SearchSuggestEntry>()
        withContext(Dispatchers.IO) {
            val searchHelper = WebSearchHelper().using(gPlayHttpClient)
            searchData.addAll(searchHelper.searchSuggestions(query))
        }
        return searchData.filter { it.title.isNotBlank() }
            .map { SearchSuggestion(it.title, source = Source.PLAY_STORE) }
        return playStoreSearchHelper.getSearchSuggestions(query)
    }

    fun getAppsByCategory(category: String, pageUrl: String?): StreamCluster {
+59 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 e Foundation
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 */

package foundation.e.apps.data.playstore

import com.aurora.gplayapi.helpers.web.WebSearchHelper
import foundation.e.apps.data.application.search.SearchSuggestion
import foundation.e.apps.data.enums.Source
import foundation.e.apps.data.playstore.utils.GPlayHttpClient
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import timber.log.Timber
import javax.inject.Inject

class PlayStoreSearchHelper @Inject constructor(
    private val gPlayHttpClient: GPlayHttpClient,
) {
    private val source = Source.PLAY_STORE

    suspend fun getSearchResults(keyword: String) = withContext(Dispatchers.IO) {
        try {
            WebSearchHelper()
                .using(gPlayHttpClient)
                .searchResults(keyword).appList
        } catch (e: Exception) {
            Timber.w("Failed to get search results for $keyword.", e)
            emptyList()
        }
    }

    suspend fun getSearchSuggestions(keyword: String) = withContext(Dispatchers.IO) {
        try {
            WebSearchHelper()
                .using(gPlayHttpClient)
                .searchSuggestions(keyword)
                .filter { it.title.isNotBlank() }
                .map { SearchSuggestion(it.title, source) }
        } catch (e: Exception) {
            Timber.w("Failed to find suggestions for keyword: $keyword")
            emptyList()
        }
    }
}