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

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

fix: allow maximum 3 open-source apps while common apps are active

When common apps are active, users will see maximum 3 open-source apps by default. If users click on a filter, all open-source apps will be visible.

When common apps are disabled in the Settings, all open-source apps will be loaded and visible.

The logic for replacing common apps with their F-Droid counterparts is removed. Those apps will be visible as part of common apps.

Same goes for PWAs.
parent 4165b1e3
Loading
Loading
Loading
Loading
Loading
+1 −36
Original line number Diff line number Diff line
@@ -25,7 +25,6 @@ import foundation.e.apps.data.ResultSupreme
import foundation.e.apps.data.Stores
import foundation.e.apps.data.application.ApplicationDataManager
import foundation.e.apps.data.application.data.Application
import foundation.e.apps.data.cleanapk.repositories.CleanApkRepository
import foundation.e.apps.data.enums.ResultStatus
import foundation.e.apps.data.enums.Source
import foundation.e.apps.data.handleNetworkResult
@@ -254,7 +253,7 @@ class SearchRepositoryImpl @Inject constructor(
            val searchResults = stores.getStore(source)?.getSearchResults(query)
                ?: error("Couldn't get Store for Source: $source")

            val apps = replaceWithFDroid(searchResults).toMutableList()
            val apps = searchResults.toMutableList()
            if (searchResults.isNotEmpty()) {
                apps.add(Application(isPlaceHolder = true))
            }
@@ -269,38 +268,4 @@ class SearchRepositoryImpl @Inject constructor(
            exception = GPlayIOException("Unable to reach Google Play API")
        )
    }

    /*
     * Replaces Play Store apps with their F-Droid counterpart if exist,
     * otherwise returns the Play Store apps as-is.
     */
    private suspend fun replaceWithFDroid(playStoreApps: List<Application>): List<Application> {
        if (playStoreApps.isEmpty()) {
            return emptyList()
        }

        val replacedApps = 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()
                    playStoreApps.map { playStoreApp ->
                        availableApps.find { it.package_name == playStoreApp.package_name }
                            ?.apply {
                                isGplayReplaced = true
                                source = Source.OPEN_SOURCE
                            } ?: playStoreApp
                    }
                } else {
                    playStoreApps
                }
            }

            else -> playStoreApps
        }

        return replacedApps
    }
}
+0 −1
Original line number Diff line number Diff line
@@ -282,7 +282,6 @@ class SearchFragment : Fragment(R.layout.fragment_search), ApplicationInstaller,
        binding.filterChipGroup.isSingleSelection = true

        val listener = OnCheckedChangeListener { _, _ ->
            showLoadingUi()
            searchViewModel.setFilterFlags(
                flagOpenSource = filterChipOpenSource.isChecked,
                flagPWA = filterChipPWA.isChecked,
+20 −1
Original line number Diff line number Diff line
@@ -63,6 +63,9 @@ class SearchViewModel @Inject constructor(

    companion object {
        private const val SEARCH_DEBOUNCE_DELAY_MILLIS = 500L

        // Reference hotfix: https://gitlab.e.foundation/e/os/backlog/-/issues/3851
        private const val REDUCED_NUMBER_OF_SEARCH_RESULTS = 3
    }

    private val _searchUiState: MutableStateFlow<SearchResultsUiState> =
@@ -110,6 +113,7 @@ class SearchViewModel @Inject constructor(
        this.flagPWA = flagPWA

        viewModelScope.launch(Dispatchers.IO) {
            loadData()
            emitFilteredResults(null)
        }
    }
@@ -142,10 +146,25 @@ class SearchViewModel @Inject constructor(
            isCleanApkDataLoading = true

            val searchResults = searchRepository.getOpenSourceSearchResults(query)
            val apps = searchResults.data?.first.orEmpty()

            val shouldShowAllOpenSourceApps =
                flagOpenSource || flagPWA || !stores.isStoreEnabled(Source.PLAY_STORE)

            val numberOfSearchResults = if (shouldShowAllOpenSourceApps) {
                apps.size
            } else {
                REDUCED_NUMBER_OF_SEARCH_RESULTS
            }

            isCleanApkDataLoading = false

            emitFilteredResults(searchResults)
            val results = ResultSupreme.create(
                searchResults.getResultStatus(),
                searchResults.data?.copy(first = apps.take(numberOfSearchResults))
            )

            emitFilteredResults(results)
        }
    }