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

Verified Commit 893f5d2d authored by Fahim M. Choudhury's avatar Fahim M. Choudhury
Browse files

refactor: improve replaceWithFDroid code for Play Store apps

parent 558c254d
Loading
Loading
Loading
Loading
+30 −22
Original line number Diff line number Diff line
@@ -273,33 +273,41 @@ class SearchRepositoryImpl @Inject constructor(
    }

    /*
     * This function will replace a GPlay app with F-Droid app if exists,
     * else will show the GPlay app itself.
     * Replaces Play Store apps with their F-Droid counterpart if exist,
     * otherwise returns the Play Store apps as-is.
     */
    private suspend fun replaceWithFDroid(gPlayApps: List<Application>): List<Application> {
        try {
            if (gPlayApps.isEmpty()) return emptyList()

            val packageNames = gPlayApps.map { it.package_name }
            val storeRepository = stores.getStore(Source.OPEN_SOURCE)

            if (storeRepository !is CleanApkRepository) {
                return gPlayApps
    private suspend fun replaceWithFDroid(playStoreApps: List<Application>): List<Application> {
        if (playStoreApps.isEmpty()) {
            return emptyList()
        }

        val replacedApps = try {
            when (val storeRepository = stores.getStore(Source.OPEN_SOURCE)) {
                is CleanApkRepository -> {
                    val packageNames = playStoreApps.map { it.package_name }
                    val response = storeRepository.checkAvailablePackages(packageNames)

                    if (response.isSuccessful) {
                        val availableApps = response.body()?.apps ?: emptyList()

            return gPlayApps.map { gPlayApp ->
                availableApps.find { it.package_name == gPlayApp.package_name }?.apply {
                        playStoreApps.map { playStoreApp ->
                            availableApps.find { it.package_name == playStoreApp.package_name }
                                ?.apply {
                                    isGplayReplaced = true
                                    source = Source.OPEN_SOURCE
                } ?: gPlayApp
                                } ?: playStoreApp
                        }
                    } else {
                        playStoreApps
                    }
                }

                else -> playStoreApps
            }
        } catch (e: Exception) {
            Timber.w(e, "Failed to replace Google apps with their F-Droid counterparts.")
            return gPlayApps
            Timber.w(e, "Failed to replace Play Store apps with their F-Droid counterparts.")
            playStoreApps
        }

        return replacedApps
    }
}