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

Verified Commit 05e37da9 authored by Fahim M. Choudhury's avatar Fahim M. Choudhury
Browse files

refactor: introduce CleanApkSearchHelper to delegate search responsibility...

refactor: introduce CleanApkSearchHelper to delegate search responsibility from CleanAPK Apps and PWA repositories
parent 4f87fba5
Loading
Loading
Loading
Loading
Loading
+64 −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.cleanapk

import foundation.e.apps.data.application.data.Application
import foundation.e.apps.data.cleanapk.repositories.NUMBER_OF_ITEMS
import foundation.e.apps.data.cleanapk.repositories.NUMBER_OF_PAGES
import foundation.e.apps.data.enums.Source
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import timber.log.Timber
import javax.inject.Inject

class CleanApkSearchHelper @Inject constructor(
    private val cleanApkRetrofit: CleanApkRetrofit,
) {
    suspend fun getSearchResults(
        keyword: String,
        appSource: String,
        appType: String
    ): List<Application> {
        return withContext(Dispatchers.IO) {
            try {
                val searchResult = cleanApkRetrofit.searchApps(
                    keyword,
                    appSource,
                    appType,
                    NUMBER_OF_ITEMS,
                    NUMBER_OF_PAGES
                )

                val apps = searchResult.body()?.apps ?: emptyList()
                apps.forEach { app ->
                    app.source = if (app.is_pwa) {
                        Source.PWA
                    } else {
                        Source.OPEN_SOURCE
                    }
                }
                apps
            } catch (e: Exception) {
                Timber.w("Failed to get search results for $keyword.", e)
                emptyList()
            }
        }
    }
}
+7 −19
Original line number Diff line number Diff line
@@ -21,9 +21,9 @@ package foundation.e.apps.data.cleanapk.repositories
import foundation.e.apps.data.application.data.Application
import foundation.e.apps.data.application.data.Home
import foundation.e.apps.data.application.search.SearchRepository
import foundation.e.apps.data.application.search.SearchSuggestion
import foundation.e.apps.data.cleanapk.CleanApkDownloadInfoFetcher
import foundation.e.apps.data.cleanapk.CleanApkRetrofit
import foundation.e.apps.data.cleanapk.CleanApkSearchHelper
import foundation.e.apps.data.cleanapk.data.categories.Categories
import foundation.e.apps.data.cleanapk.data.download.Download
import foundation.e.apps.data.cleanapk.data.search.Search
@@ -33,7 +33,8 @@ import javax.inject.Inject

class CleanApkAppsRepository @Inject constructor(
    private val cleanApkRetrofit: CleanApkRetrofit,
    private val homeConverter: HomeConverter
    private val homeConverter: HomeConverter,
    private val cleanApkSearchHelper: CleanApkSearchHelper
) : CleanApkRepository, CleanApkDownloadInfoFetcher {

    override suspend fun getHomeScreenData(list: MutableList<Home>): List<Home> {
@@ -90,24 +91,11 @@ class CleanApkAppsRepository @Inject constructor(
    }

    override suspend fun getSearchResults(pattern: String): List<Application> {
        val searchResult = cleanApkRetrofit.searchApps(
            pattern,
            CleanApkRetrofit.APP_SOURCE_FOSS,
            CleanApkRetrofit.APP_TYPE_NATIVE,
            NUMBER_OF_ITEMS,
            NUMBER_OF_PAGES
        return cleanApkSearchHelper.getSearchResults(
            keyword = pattern,
            appSource = CleanApkRetrofit.APP_SOURCE_FOSS,
            appType = CleanApkRetrofit.APP_TYPE_NATIVE
        )

        val apps = searchResult.body()?.apps
        apps?.forEach { app ->
            app.source = if (app.is_pwa) {
                Source.PWA
            } else {
                Source.OPEN_SOURCE
            }
        }

        return apps ?: emptyList()
    }

    override suspend fun getDownloadInfo(idOrPackageName: String, versionCode: Any?): Response<Download> {
+7 −19
Original line number Diff line number Diff line
@@ -23,8 +23,8 @@ import dagger.hilt.android.qualifiers.ApplicationContext
import foundation.e.apps.data.application.data.Application
import foundation.e.apps.data.application.data.Home
import foundation.e.apps.data.application.search.SearchRepository
import foundation.e.apps.data.application.search.SearchSuggestion
import foundation.e.apps.data.cleanapk.CleanApkRetrofit
import foundation.e.apps.data.cleanapk.CleanApkSearchHelper
import foundation.e.apps.data.cleanapk.data.categories.Categories
import foundation.e.apps.data.cleanapk.data.search.Search
import foundation.e.apps.data.enums.Source
@@ -34,7 +34,8 @@ import javax.inject.Inject
class CleanApkPwaRepository @Inject constructor(
    private val cleanApkRetrofit: CleanApkRetrofit,
    private val homeConverter: HomeConverter,
    @ApplicationContext val context: Context
    @ApplicationContext val context: Context,
    private val cleanApkSearchHelper: CleanApkSearchHelper
) : CleanApkRepository {

    override suspend fun getHomeScreenData(list: MutableList<Home>): List<Home> {
@@ -88,23 +89,10 @@ class CleanApkPwaRepository @Inject constructor(
    }

    override suspend fun getSearchResults(pattern: String): List<Application> {
        val searchResult = cleanApkRetrofit.searchApps(
            pattern,
            CleanApkRetrofit.APP_SOURCE_ANY,
            CleanApkRetrofit.APP_TYPE_PWA,
            NUMBER_OF_ITEMS,
            NUMBER_OF_PAGES
        return cleanApkSearchHelper.getSearchResults(
            keyword = pattern,
            appSource = CleanApkRetrofit.APP_SOURCE_ANY,
            appType = CleanApkRetrofit.APP_TYPE_PWA,
        )

        val apps = searchResult.body()?.apps
        apps?.forEach { app ->
            app.source = if (app.is_pwa) {
                Source.PWA
            } else {
                Source.OPEN_SOURCE
            }
        }

        return apps ?: emptyList()
    }
}