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

Commit 115eb2fb authored by Jonathan Klee's avatar Jonathan Klee
Browse files

Improve Stores class

parent d865d151
Loading
Loading
Loading
Loading
+19 −7
Original line number Diff line number Diff line
@@ -13,15 +13,27 @@ class Stores @Inject constructor(
    cleanApkAppsRepository: CleanApkAppsRepository,
    cleanApkPwaRepository: CleanApkPwaRepository,
) {
    private val stores = mutableMapOf<Source, StoreRepository>()

    fun getStores(): Map<Source, StoreRepository> {
        return stores
    private val defaultStores = mapOf(
        Source.OPEN_SOURCE to cleanApkAppsRepository,
        Source.PWA to cleanApkPwaRepository,
        Source.PLAY_STORE to playStoreRepository
    )

    private val stores = defaultStores.toMutableMap()

    fun getStores(): Map<Source, StoreRepository> = stores

    fun getStore(source: Source): StoreRepository? = stores[source]

    fun enableStore(source: Source) {
        val repository = defaultStores[source] ?: throw IllegalStateException("store not found")
        stores[source] = repository
    }

    init {
        stores[Source.OPEN_SOURCE] = cleanApkAppsRepository
        stores[Source.PWA] = cleanApkPwaRepository
        stores[Source.PLAY_STORE] = playStoreRepository
    fun disableStore(source: Source) {
        stores.remove(source)
    }

    fun isStoreEnabled(source: Source): Boolean = stores.containsKey(source)
}
 No newline at end of file
+5 −7
Original line number Diff line number Diff line
@@ -26,13 +26,11 @@ import foundation.e.apps.data.enums.ResultStatus
import foundation.e.apps.data.enums.Status
import foundation.e.apps.data.enums.isUnFiltered
import foundation.e.apps.data.handleNetworkResult
import foundation.e.apps.data.preference.AppLoungePreference
import foundation.e.apps.ui.applicationlist.ApplicationDiffUtil
import javax.inject.Inject
import foundation.e.apps.data.enums.Source

class AppsApiImpl @Inject constructor(
    private val appLoungePreference: AppLoungePreference,
    private val stores: Stores,
    private val applicationDataManager: ApplicationDataManager
) : AppsApi {
@@ -40,7 +38,7 @@ class AppsApiImpl @Inject constructor(
    override suspend fun getCleanapkAppDetails(packageName: String): Pair<Application, ResultStatus> {
        var application = Application()
        val result = handleNetworkResult {
            application = stores.getStores()[Source.OPEN_SOURCE]?.getAppDetails(packageName) ?: Application()
            application = stores.getStore(Source.OPEN_SOURCE)?.getAppDetails(packageName) ?: Application()
            application.source = Source.OPEN_SOURCE
            application.updateType()
            application.updateFilterLevel()
@@ -92,7 +90,7 @@ class AppsApiImpl @Inject constructor(
        val applicationList = mutableListOf<Application>()

        for (packageName in packageNameList) {
            applicationList.add(stores.getStores()[Source.OPEN_SOURCE]?.getAppDetails(packageName) ?: Application())
            applicationList.add(stores.getStore(Source.OPEN_SOURCE)?.getAppDetails(packageName) ?: Application())
        }

        return Pair(applicationList, status)
@@ -104,7 +102,7 @@ class AppsApiImpl @Inject constructor(
        val applicationList = mutableListOf<Application>()

        for (packageName in packageNameList) {
            val app = stores.getStores()[Source.PLAY_STORE]?.getAppDetails(packageName) ?: Application()
            val app = stores.getStore(Source.PLAY_STORE)?.getAppDetails(packageName) ?: Application()
            handleFilteredApps(app, applicationList)
        }

@@ -140,7 +138,7 @@ class AppsApiImpl @Inject constructor(

        val result = handleNetworkResult {

            val store = stores.getStores()[source]
            val store = stores.getStore(source)
                ?: throw IllegalStateException("Could not get store")

            application = store.getAppDetails(packageName)
@@ -207,5 +205,5 @@ class AppsApiImpl @Inject constructor(
        return false
    }

    override fun isOpenSourceSelected() = appLoungePreference.isOpenSourceSelected()
    override fun isOpenSourceSelected() = stores.isStoreEnabled(Source.OPEN_SOURCE)
}
+4 −12
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@ import dagger.hilt.android.qualifiers.ApplicationContext
import foundation.e.apps.R
import foundation.e.apps.data.AppSourcesContainer
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.application.data.Category
@@ -38,13 +39,12 @@ import foundation.e.apps.data.enums.ResultStatus
import foundation.e.apps.data.enums.Source
import foundation.e.apps.data.enums.isUnFiltered
import foundation.e.apps.data.handleNetworkResult
import foundation.e.apps.data.preference.AppLoungePreference
import javax.inject.Inject

class CategoryApiImpl @Inject constructor(
    @ApplicationContext private val context: Context,
    private val appLoungePreference: AppLoungePreference,
    private val appSources: AppSourcesContainer,
    private val stores: Stores,
    private val applicationDataManager: ApplicationDataManager
) : CategoryApi {

@@ -62,16 +62,8 @@ class CategoryApiImpl @Inject constructor(
    ): ResultStatus {
        val categoryResults: MutableList<ResultStatus> = mutableListOf()

        if (appLoungePreference.isOpenSourceSelected()) {
            categoryResults.add(fetchCategoryResult(categoriesList, type, Source.OPEN_SOURCE))
        }

        if (appLoungePreference.isPWASelected()) {
            categoryResults.add(fetchCategoryResult(categoriesList, type, Source.PWA))
        }

        if (appLoungePreference.isGplaySelected()) {
            categoryResults.add(fetchCategoryResult(categoriesList, type, Source.PLAY_STORE))
        for ((source, _) in stores.getStores()) {
            categoryResults.add(fetchCategoryResult(categoriesList, type, source))
        }

        return categoryResults.find { it != ResultStatus.OK } ?: ResultStatus.OK
+11 −30
Original line number Diff line number Diff line
@@ -18,26 +18,20 @@

package foundation.e.apps.data.application.home

import android.content.Context
import androidx.lifecycle.LiveData
import androidx.lifecycle.liveData
import dagger.hilt.android.qualifiers.ApplicationContext
import foundation.e.apps.data.ResultSupreme
import foundation.e.apps.data.Stores
import foundation.e.apps.data.application.data.Home
import foundation.e.apps.data.application.search.FusedHomeDeferred
import foundation.e.apps.data.application.search.SearchApi
import foundation.e.apps.data.enums.ResultStatus
import foundation.e.apps.data.enums.Source
import foundation.e.apps.data.handleNetworkResult
import foundation.e.apps.data.preference.AppLoungePreference
import kotlinx.coroutines.async
import kotlinx.coroutines.coroutineScope
import javax.inject.Inject

class HomeApiImpl @Inject constructor(
    @ApplicationContext private val context: Context,
    private val appLoungePreference: AppLoungePreference,
    private val stores: Stores
) : HomeApi {

@@ -49,35 +43,22 @@ class HomeApiImpl @Inject constructor(

    override suspend fun fetchHomeScreenData(): LiveData<ResultSupreme<List<Home>>> {
        val list = mutableListOf<Home>()
        var resultGplay: FusedHomeDeferred? = null
        var resultOpenSource: FusedHomeDeferred? = null
        var resultPWA: FusedHomeDeferred? = null

        return liveData {
            coroutineScope {

                if (appLoungePreference.isGplaySelected()) {
                    resultGplay = async { loadHomeData(list, Source.PLAY_STORE) }
                if (Source.PLAY_STORE in stores.getStores()) {
                    val result = async {
                        loadHomeData(list, Source.PLAY_STORE)
                    }

                if (appLoungePreference.isOpenSourceSelected()) {
                    resultOpenSource = async { loadHomeData(list, Source.OPEN_SOURCE) }
                }

                if (appLoungePreference.isPWASelected()) {
                    resultPWA = async { loadHomeData(list, Source.PWA) }
                    emit(result.await())
                }

                resultGplay?.await()?.let {
                    emit(it)
                stores.getStores().forEach { (source, _) ->
                    val result = async {
                        loadHomeData(list, source)
                    }

                resultOpenSource?.await()?.let {
                    emit(it)
                }

                resultPWA?.await()?.let {
                    emit(it)
                    emit(result.await())
                }
            }
        }
@@ -88,7 +69,7 @@ class HomeApiImpl @Inject constructor(
        source: Source
    ): ResultSupreme<List<Home>> {
        val result = handleNetworkResult {
            val homeDataBuilder = stores.getStores()[source]
            val homeDataBuilder = stores.getStore(source)
            homeDataBuilder?.getHomeScreenData(priorList)
                ?: throw IllegalStateException("Could not find store for $source")
        }
+18 −24
Original line number Diff line number Diff line
@@ -28,7 +28,6 @@ import foundation.e.apps.data.Stores
import foundation.e.apps.data.application.ApplicationDataManager
import foundation.e.apps.data.application.apps.AppsApi
import foundation.e.apps.data.application.data.Application
import foundation.e.apps.data.application.data.Home
import foundation.e.apps.data.application.search.SearchApi.Companion.APP_TYPE_ANY
import foundation.e.apps.data.application.search.SearchApi.Companion.APP_TYPE_OPEN
import foundation.e.apps.data.application.search.SearchApi.Companion.APP_TYPE_PWA
@@ -37,18 +36,13 @@ import foundation.e.apps.data.enums.Source
import foundation.e.apps.data.handleNetworkResult
import foundation.e.apps.data.login.exceptions.CleanApkIOException
import foundation.e.apps.data.login.exceptions.GPlayIOException
import foundation.e.apps.data.preference.AppLoungePreference
import kotlinx.coroutines.Deferred
import timber.log.Timber
import javax.inject.Inject
import javax.inject.Singleton

typealias FusedHomeDeferred = Deferred<ResultSupreme<List<Home>>>

@Singleton
class SearchApiImpl @Inject constructor(
    private val appsApi: AppsApi,
    private val appLoungePreference: AppLoungePreference,
    private val appSources: AppSourcesContainer,
    private val stores: Stores,
    private val applicationDataManager: ApplicationDataManager
@@ -60,9 +54,9 @@ class SearchApiImpl @Inject constructor(

    override fun getSelectedAppTypes(): List<String> {
        val selectedAppTypes = mutableListOf<String>()
        if (appLoungePreference.isGplaySelected()) selectedAppTypes.add(APP_TYPE_ANY)
        if (appLoungePreference.isOpenSourceSelected()) selectedAppTypes.add(APP_TYPE_OPEN)
        if (appLoungePreference.isPWASelected()) selectedAppTypes.add(APP_TYPE_PWA)
        if (stores.isStoreEnabled(Source.PLAY_STORE)) selectedAppTypes.add(APP_TYPE_ANY)
        if (stores.isStoreEnabled(Source.OPEN_SOURCE)) selectedAppTypes.add(APP_TYPE_OPEN)
        if (stores.isStoreEnabled(Source.PWA)) selectedAppTypes.add(APP_TYPE_PWA)

        return selectedAppTypes
    }
@@ -85,7 +79,7 @@ class SearchApiImpl @Inject constructor(
            fetchPackageSpecificResult(query).data?.first ?: emptyList()

        val searchResult = mutableListOf<Application>()
        if (appLoungePreference.isOpenSourceSelected()) {
        if (stores.isStoreEnabled(Source.OPEN_SOURCE)) {
            finalSearchResult = fetchOpenSourceSearchResult(
                query,
                searchResult,
@@ -93,7 +87,7 @@ class SearchApiImpl @Inject constructor(
            )
        }

        if (appLoungePreference.isPWASelected()) {
        if (stores.isStoreEnabled(Source.PWA)) {
            finalSearchResult = fetchPWASearchResult(
                query,
                searchResult,
@@ -101,11 +95,11 @@ class SearchApiImpl @Inject constructor(
            )
        }

        if (!appLoungePreference.isOpenSourceSelected() && !appLoungePreference.isPWASelected()) {
        if (!stores.isStoreEnabled(Source.OPEN_SOURCE) && !stores.isStoreEnabled(Source.PWA)) {
            finalSearchResult = ResultSupreme.Success(
                Pair(
                    packageSpecificResults,
                    appLoungePreference.isGplaySelected()
                    stores.isStoreEnabled(Source.PLAY_STORE)
                )
            )
        }
@@ -121,7 +115,7 @@ class SearchApiImpl @Inject constructor(
        val pwaApps: MutableList<Application> = mutableListOf()
        val result = handleNetworkResult {
            val apps =
                stores.getStores()[Source.PWA]?.getSearchResults(query) ?: emptyList()
                stores.getStore(Source.PWA)?.getSearchResults(query) ?: emptyList()

            apps.forEach {
                applicationDataManager.updateStatus(it)
@@ -143,7 +137,7 @@ class SearchApiImpl @Inject constructor(
                    packageSpecificResults,
                    query
                ),
                appLoungePreference.isGplaySelected()
                stores.isStoreEnabled(Source.PLAY_STORE)
            ),
            exception = result.exception
        )
@@ -173,7 +167,7 @@ class SearchApiImpl @Inject constructor(
                    packageSpecificResults,
                    query
                ),
                appLoungePreference.isGplaySelected() || appLoungePreference.isPWASelected()
                stores.isStoreEnabled(Source.PLAY_STORE) || stores.isStoreEnabled(Source.PWA)
            ),
            exception = result.exception
        )
@@ -187,11 +181,11 @@ class SearchApiImpl @Inject constructor(
        var cleanapkPackageResult: Application? = null

        val result = handleNetworkResult {
            if (appLoungePreference.isGplaySelected()) {
            if (stores.isStoreEnabled(Source.PLAY_STORE)) {
                gplayPackageResult = getGplayPackageResult(query)
            }

            if (appLoungePreference.isOpenSourceSelected()) {
            if (stores.isStoreEnabled(Source.OPEN_SOURCE)) {
                cleanapkPackageResult = getCleanApkPackageResult(query)
            }
        }
@@ -203,7 +197,7 @@ class SearchApiImpl @Inject constructor(
            gplayPackageResult?.let { packageSpecificResults.add(it) }
        }

        if (appLoungePreference.isGplaySelected()) {
        if (stores.isStoreEnabled(Source.PLAY_STORE)) {
            packageSpecificResults.add(Application(isPlaceHolder = true))
        }

@@ -235,7 +229,7 @@ class SearchApiImpl @Inject constructor(

        val finalList = (packageSpecificResults + filteredResults).toMutableList()
        finalList.removeIf { it.isPlaceHolder }
        if (appLoungePreference.isGplaySelected()) {
        if (stores.isStoreEnabled(Source.PLAY_STORE)) {
            finalList.add(Application(isPlaceHolder = true))
        }

@@ -276,7 +270,7 @@ class SearchApiImpl @Inject constructor(
    private suspend fun getCleanApkSearchResult(packageName: String): ResultSupreme<Application> {
        var application = Application()
        val result = handleNetworkResult {
            val results = stores.getStores()[Source.PWA]?.getSearchResults(packageName) ?: emptyList()
            val results = stores.getStore(Source.OPEN_SOURCE)?.getSearchResults(packageName) ?: emptyList()

            if (results.isNotEmpty() && results.size == 1) {
                application = results[0]
@@ -300,7 +294,7 @@ class SearchApiImpl @Inject constructor(
    ): List<Application> {
        val list = mutableListOf<Application>()
        val response =
            stores.getStores()[Source.OPEN_SOURCE]?.getSearchResults(keyword) ?: emptyList()
            stores.getStore(Source.OPEN_SOURCE)?.getSearchResults(keyword) ?: emptyList()

        response.forEach {
            applicationDataManager.updateStatus(it)
@@ -315,7 +309,7 @@ class SearchApiImpl @Inject constructor(
        query: String,
    ): SearchResult {
        val result = handleNetworkResult {
            if (!appLoungePreference.isGplaySelected()) {
            if (!stores.isStoreEnabled(Source.PLAY_STORE)) {
                return@handleNetworkResult Pair(
                    listOf<Application>(),
                    setOf<SearchBundle.SubBundle>()
@@ -323,7 +317,7 @@ class SearchApiImpl @Inject constructor(
            }

            val searchResults =
                stores.getStores()[Source.PLAY_STORE]?.getSearchResults(query)
                stores.getStore(Source.PLAY_STORE)?.getSearchResults(query)
                    ?: throw IllegalStateException("Could not get store")

            val apps = replaceWithFDroid(searchResults).toMutableList()
Loading