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

Commit 8527c4ec authored by Aayush Gupta's avatar Aayush Gupta
Browse files

FusedAPIImpl: Move private functions below public

parent d41e3ac1
Loading
Loading
Loading
Loading
+159 −159
Original line number Diff line number Diff line
@@ -107,153 +107,6 @@ class FusedAPIImpl @Inject constructor(
        return categoriesList
    }

    private suspend fun handleCleanApkCategories(
        preferredApplicationType: String,
        categoriesList: MutableList<FusedCategory>,
        type: Category.Type
    ) {
        val data = getCleanApkCategories(preferredApplicationType)

        data?.let { category ->
            categoriesList.addAll(
                getFusedCategoryBasedOnCategoryType(
                    category,
                    type,
                    getCategoryTag(preferredApplicationType)
                )
            )
        }
    }

    private fun getCategoryTag(preferredApplicationType: String): String {
        return if (preferredApplicationType == APP_TYPE_OPEN) context.getString(R.string.open_source) else context.getString(
            R.string.pwa
        )
    }

    private suspend fun getCleanApkCategories(preferredApplicationType: String): Categories? {
        return if (preferredApplicationType == APP_TYPE_OPEN) {
            getOpenSourceCategories()
        } else {
            getPWAsCategories()
        }
    }

    private suspend fun handleAllSourcesCategories(
        categoriesList: MutableList<FusedCategory>,
        type: Category.Type,
        authData: AuthData
    ) {
        var data = getOpenSourceCategories()
        data?.let {
            categoriesList.addAll(
                getFusedCategoryBasedOnCategoryType(
                    it,
                    type,
                    context.getString(R.string.open_source)
                )
            )
        }
        data = getPWAsCategories()
        data?.let {
            categoriesList.addAll(
                getFusedCategoryBasedOnCategoryType(
                    it, type, context.getString(R.string.pwa)
                )
            )
        }
        val playResponse = gPlayAPIRepository.getCategoriesList(type, authData).map { app ->
            val category = app.transformToFusedCategory()
            updateCategoryDrawable(category, app)
            category
        }
        categoriesList.addAll(playResponse)
    }

    private fun updateCategoryDrawable(
        category: FusedCategory,
        app: Category
    ) {
        category.drawable =
            if (app.type == Category.Type.APPLICATION) CategoryUtils.provideAppsCategoryIconResource(
                getCategoryIconName(category)
            ) else CategoryUtils.provideGamesCategoryIconResource(
                getCategoryIconName(
                    category
                )
            )
    }

    private fun getCategoryIconName(category: FusedCategory): String {
        var categoryTitle = category.title
        if (categoryTitle.contains(CATEGORY_TITLE_REPLACABLE_CONJUNCTION)) {
            categoryTitle = categoryTitle.replace(CATEGORY_TITLE_REPLACABLE_CONJUNCTION, "and")
        }
        categoryTitle = categoryTitle.replace(' ', '_')
        return categoryTitle.lowercase()
    }

    private fun getFusedCategoryBasedOnCategoryType(
        categories: Categories,
        categoryType: Category.Type,
        tag: String
    ): List<FusedCategory> {
        return when (categoryType) {
            Category.Type.APPLICATION -> {
                getAppsCategoriesAsFusedCategory(categories, tag)
            }
            Category.Type.GAME -> {
                getGamesCategoriesAsFusedCategory(categories, tag)
            }
        }
    }

    private fun getAppsCategoriesAsFusedCategory(
        categories: Categories,
        tag: String
    ): List<FusedCategory> {
        return categories.apps.map { category ->
            createFusedCategoryFromCategory(category, categories, Category.Type.APPLICATION, tag)
        }
    }

    private fun getGamesCategoriesAsFusedCategory(
        categories: Categories,
        tag: String
    ): List<FusedCategory> {
        return categories.games.map { category ->
            createFusedCategoryFromCategory(category, categories, Category.Type.GAME, tag)
        }
    }

    private fun createFusedCategoryFromCategory(
        category: String,
        categories: Categories,
        appType: Category.Type,
        tag: String
    ) = FusedCategory(
        id = category,
        title = categories.translations.getOrDefault(category, ""),
        drawable = if (appType == Category.Type.APPLICATION) CategoryUtils.provideAppsCategoryIconResource(
            category
        ) else CategoryUtils.provideGamesCategoryIconResource(category),
        tag = tag
    )

    private suspend fun getPWAsCategories(): Categories? {
        return cleanAPKRepository.getCategoriesList(
            CleanAPKInterface.APP_TYPE_PWA,
            CleanAPKInterface.APP_SOURCE_ANY
        ).body()
    }

    private suspend fun getOpenSourceCategories(): Categories? {
        return cleanAPKRepository.getCategoriesList(
            CleanAPKInterface.APP_TYPE_ANY,
            CleanAPKInterface.APP_SOURCE_FOSS
        ).body()
    }

    /**
     * Fetches search results from cleanAPK and GPlay servers and returns them
     * @param query Query
@@ -376,18 +229,6 @@ class FusedAPIImpl @Inject constructor(
        }
    }

    private suspend fun getOpenSourceAppsResponse(category: String) = cleanAPKRepository.listApps(
        category,
        CleanAPKInterface.APP_SOURCE_FOSS,
        CleanAPKInterface.APP_TYPE_ANY
    ).body()

    private suspend fun getPWAAppsResponse(category: String) = cleanAPKRepository.listApps(
        category,
        CleanAPKInterface.APP_SOURCE_ANY,
        CleanAPKInterface.APP_TYPE_PWA
    ).body()

    suspend fun getApplicationDetails(
        packageNameList: List<String>,
        authData: AuthData,
@@ -439,6 +280,165 @@ class FusedAPIImpl @Inject constructor(
        return response ?: FusedApp()
    }

    private suspend fun handleCleanApkCategories(
        preferredApplicationType: String,
        categoriesList: MutableList<FusedCategory>,
        type: Category.Type
    ) {
        val data = getCleanApkCategories(preferredApplicationType)

        data?.let { category ->
            categoriesList.addAll(
                getFusedCategoryBasedOnCategoryType(
                    category,
                    type,
                    getCategoryTag(preferredApplicationType)
                )
            )
        }
    }

    private fun getCategoryTag(preferredApplicationType: String): String {
        return if (preferredApplicationType == APP_TYPE_OPEN) context.getString(R.string.open_source) else context.getString(
            R.string.pwa
        )
    }

    private suspend fun getCleanApkCategories(preferredApplicationType: String): Categories? {
        return if (preferredApplicationType == APP_TYPE_OPEN) {
            getOpenSourceCategories()
        } else {
            getPWAsCategories()
        }
    }

    private suspend fun handleAllSourcesCategories(
        categoriesList: MutableList<FusedCategory>,
        type: Category.Type,
        authData: AuthData
    ) {
        var data = getOpenSourceCategories()
        data?.let {
            categoriesList.addAll(
                getFusedCategoryBasedOnCategoryType(
                    it,
                    type,
                    context.getString(R.string.open_source)
                )
            )
        }
        data = getPWAsCategories()
        data?.let {
            categoriesList.addAll(
                getFusedCategoryBasedOnCategoryType(
                    it, type, context.getString(R.string.pwa)
                )
            )
        }
        val playResponse = gPlayAPIRepository.getCategoriesList(type, authData).map { app ->
            val category = app.transformToFusedCategory()
            updateCategoryDrawable(category, app)
            category
        }
        categoriesList.addAll(playResponse)
    }

    private fun updateCategoryDrawable(
        category: FusedCategory,
        app: Category
    ) {
        category.drawable =
            if (app.type == Category.Type.APPLICATION) CategoryUtils.provideAppsCategoryIconResource(
                getCategoryIconName(category)
            ) else CategoryUtils.provideGamesCategoryIconResource(
                getCategoryIconName(
                    category
                )
            )
    }

    private fun getCategoryIconName(category: FusedCategory): String {
        var categoryTitle = category.title
        if (categoryTitle.contains(CATEGORY_TITLE_REPLACABLE_CONJUNCTION)) {
            categoryTitle = categoryTitle.replace(CATEGORY_TITLE_REPLACABLE_CONJUNCTION, "and")
        }
        categoryTitle = categoryTitle.replace(' ', '_')
        return categoryTitle.lowercase()
    }

    private fun getFusedCategoryBasedOnCategoryType(
        categories: Categories,
        categoryType: Category.Type,
        tag: String
    ): List<FusedCategory> {
        return when (categoryType) {
            Category.Type.APPLICATION -> {
                getAppsCategoriesAsFusedCategory(categories, tag)
            }
            Category.Type.GAME -> {
                getGamesCategoriesAsFusedCategory(categories, tag)
            }
        }
    }

    private fun getAppsCategoriesAsFusedCategory(
        categories: Categories,
        tag: String
    ): List<FusedCategory> {
        return categories.apps.map { category ->
            createFusedCategoryFromCategory(category, categories, Category.Type.APPLICATION, tag)
        }
    }

    private fun getGamesCategoriesAsFusedCategory(
        categories: Categories,
        tag: String
    ): List<FusedCategory> {
        return categories.games.map { category ->
            createFusedCategoryFromCategory(category, categories, Category.Type.GAME, tag)
        }
    }

    private fun createFusedCategoryFromCategory(
        category: String,
        categories: Categories,
        appType: Category.Type,
        tag: String
    ) = FusedCategory(
        id = category,
        title = categories.translations.getOrDefault(category, ""),
        drawable = if (appType == Category.Type.APPLICATION) CategoryUtils.provideAppsCategoryIconResource(
            category
        ) else CategoryUtils.provideGamesCategoryIconResource(category),
        tag = tag
    )

    private suspend fun getPWAsCategories(): Categories? {
        return cleanAPKRepository.getCategoriesList(
            CleanAPKInterface.APP_TYPE_PWA,
            CleanAPKInterface.APP_SOURCE_ANY
        ).body()
    }

    private suspend fun getOpenSourceCategories(): Categories? {
        return cleanAPKRepository.getCategoriesList(
            CleanAPKInterface.APP_TYPE_ANY,
            CleanAPKInterface.APP_SOURCE_FOSS
        ).body()
    }

    private suspend fun getOpenSourceAppsResponse(category: String) = cleanAPKRepository.listApps(
        category,
        CleanAPKInterface.APP_SOURCE_FOSS,
        CleanAPKInterface.APP_TYPE_ANY
    ).body()

    private suspend fun getPWAAppsResponse(category: String) = cleanAPKRepository.listApps(
        category,
        CleanAPKInterface.APP_SOURCE_ANY,
        CleanAPKInterface.APP_TYPE_PWA
    ).body()

    private suspend fun getCleanAPKSearchResults(
        keyword: String,
        source: String = CleanAPKInterface.APP_SOURCE_FOSS,