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

Commit 44475b4b authored by Aayush Gupta's avatar Aayush Gupta
Browse files

Merge branch 'issue_4422' of gitlab.e.foundation:ecorp/apps/apps into epic_176-all-refactorAndGplay

 Conflicts:
	app/src/main/java/foundation/e/apps/api/fused/FusedAPIImpl.kt
parents dca91d80 db94ffaa
Loading
Loading
Loading
Loading
+177 −79
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ package foundation.e.apps.api.fused
import android.app.DownloadManager
import android.content.Context
import android.net.Uri
import android.text.format.Formatter
import android.util.Log
import com.aurora.gplayapi.SearchSuggestEntry
import com.aurora.gplayapi.data.models.App
@@ -32,6 +33,7 @@ import dagger.hilt.android.qualifiers.ApplicationContext
import foundation.e.apps.R
import foundation.e.apps.api.cleanapk.CleanAPKInterface
import foundation.e.apps.api.cleanapk.CleanAPKRepository
import foundation.e.apps.api.cleanapk.data.categories.Categories
import foundation.e.apps.api.cleanapk.data.home.Home
import foundation.e.apps.api.fused.data.FusedApp
import foundation.e.apps.api.fused.data.FusedCategory
@@ -57,14 +59,22 @@ class FusedAPIImpl @Inject constructor(
    @ApplicationContext private val context: Context,
    @Named("cacheDir") private val cacheDir: String
) {

    companion object {
        private const val CATEGORY_TITLE_REPLACABLE_CONJUNCTION = "&"
        private const val APP_TYPE_ANY = "any"
        private const val APP_TYPE_OPEN = "open"
        private const val APP_TYPE_PWA = "pwa"
    }

    private var TAG = FusedAPIImpl::class.java.simpleName

    suspend fun getHomeScreenData(authData: AuthData): List<FusedHome> {
        val list = mutableListOf<FusedHome>()
        val preferredApplicationType = preferenceManagerModule.preferredApplicationType()

        if (preferredApplicationType != "any") {
            val response = if (preferredApplicationType == "open") {
        if (preferredApplicationType != APP_TYPE_ANY) {
            val response = if (preferredApplicationType == APP_TYPE_OPEN) {
                cleanAPKRepository.getHomeScreenData(
                    CleanAPKInterface.APP_TYPE_ANY,
                    CleanAPKInterface.APP_SOURCE_FOSS
@@ -88,49 +98,160 @@ class FusedAPIImpl @Inject constructor(
        val categoriesList = mutableListOf<FusedCategory>()
        val preferredApplicationType = preferenceManagerModule.preferredApplicationType()

        if (preferredApplicationType != "any") {
            val data = if (preferredApplicationType == "open") {
                cleanAPKRepository.getCategoriesList(
                    CleanAPKInterface.APP_TYPE_ANY,
                    CleanAPKInterface.APP_SOURCE_FOSS
                ).body()
        if (preferredApplicationType != APP_TYPE_ANY) {
            handleCleanApkCategories(preferredApplicationType, categoriesList, type)
        } else {
                cleanAPKRepository.getCategoriesList(
                    CleanAPKInterface.APP_TYPE_PWA,
                    CleanAPKInterface.APP_SOURCE_ANY
                ).body()
            handleAllSourcesCategories(categoriesList, type, authData)
        }
        categoriesList.sortBy { item -> item.title.lowercase() }
        return categoriesList
    }

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

        data?.let { category ->
                when (type) {
                    Category.Type.APPLICATION -> {
                        for (cat in category.apps) {
                            val categoryApp = FusedCategory(
                                id = cat,
                                title = category.translations.getOrDefault(cat, ""),
                                drawable = CategoryUtils.provideCategoryIconResource(cat)
            categoriesList.addAll(
                getFusedCategoryBasedOnCategoryType(
                    category,
                    type,
                    getCategoryTag(preferredApplicationType)
                )
            )
                            categoriesList.add(categoryApp)
        }
    }
                    Category.Type.GAME -> {
                        for (cat in category.games) {
                            val categoryApp = FusedCategory(
                                id = cat,
                                title = category.translations.getOrDefault(cat, ""),
                                drawable = CategoryUtils.provideCategoryIconResource(cat)

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

    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)
                )
            )
        }
        } else {
        val playResponse = gPlayAPIRepository.getCategoriesList(type, authData).map { app ->
                app.transformToFusedCategory()
            val category = app.transformToFusedCategory()
            updateCategoryDrawable(category, app)
            category
        }
        categoriesList.addAll(playResponse)
    }
        return categoriesList

    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()
    }

    /**
@@ -143,14 +264,14 @@ class FusedAPIImpl @Inject constructor(
        val fusedResponse = mutableListOf<FusedApp>()

        when (preferenceManagerModule.preferredApplicationType()) {
            "any" -> {
            APP_TYPE_ANY -> {
                fusedResponse.addAll(getCleanAPKSearchResults(query))
                fusedResponse.addAll(getGplaySearchResults(query, authData))
            }
            "open" -> {
            APP_TYPE_OPEN -> {
                fusedResponse.addAll(getCleanAPKSearchResults(query))
            }
            "pwa" -> {
            APP_TYPE_PWA -> {
                fusedResponse.addAll(
                    getCleanAPKSearchResults(
                        query,
@@ -213,8 +334,8 @@ class FusedAPIImpl @Inject constructor(
    suspend fun listApps(category: String, browseUrl: String, authData: AuthData): List<FusedApp>? {
        val preferredApplicationType = preferenceManagerModule.preferredApplicationType()

        if (preferredApplicationType != "any") {
            val response = if (preferredApplicationType == "open") {
        if (preferredApplicationType != APP_TYPE_ANY) {
            val response = if (preferredApplicationType == APP_TYPE_OPEN) {
                cleanAPKRepository.listApps(
                    category,
                    CleanAPKInterface.APP_SOURCE_FOSS,
@@ -326,7 +447,7 @@ class FusedAPIImpl @Inject constructor(

    private fun generateCleanAPKHome(home: Home, prefType: String): List<FusedHome> {
        val list = mutableListOf<FusedHome>()
        val headings = if (prefType == "open") {
        val headings = if (prefType == APP_TYPE_OPEN) {
            mapOf(
                "top_updated_apps" to context.getString(R.string.top_updated_apps),
                "top_updated_games" to context.getString(R.string.top_updated_games),
@@ -386,36 +507,12 @@ class FusedAPIImpl @Inject constructor(
    private suspend fun fetchGPlayHome(authData: AuthData): List<FusedHome> {
        val list = mutableListOf<FusedHome>()
        val homeElements = mutableMapOf(
            context.getString(R.string.topselling_free_apps) to
                mapOf(
                    TopChartsHelper.Chart.TOP_SELLING_FREE to
                        TopChartsHelper.Type.APPLICATION
                ),
            context.getString(R.string.topselling_free_games) to
                mapOf(
                    TopChartsHelper.Chart.TOP_SELLING_FREE to
                        TopChartsHelper.Type.GAME
                ),
            context.getString(R.string.topgrossing_apps) to
                mapOf(
                    TopChartsHelper.Chart.TOP_GROSSING to
                        TopChartsHelper.Type.APPLICATION
                ),
            context.getString(R.string.topgrossing_games) to
                mapOf(
                    TopChartsHelper.Chart.TOP_GROSSING to
                        TopChartsHelper.Type.GAME
                ),
            context.getString(R.string.movers_shakers_apps) to
                mapOf(
                    TopChartsHelper.Chart.MOVERS_SHAKERS to
                        TopChartsHelper.Type.APPLICATION
                ),
            context.getString(R.string.movers_shakers_games) to
                mapOf(
                    TopChartsHelper.Chart.MOVERS_SHAKERS to
                        TopChartsHelper.Type.GAME
                ),
            context.getString(R.string.topselling_free_apps) to mapOf(TopChartsHelper.Chart.TOP_SELLING_FREE to TopChartsHelper.Type.APPLICATION),
            context.getString(R.string.topselling_free_games) to mapOf(TopChartsHelper.Chart.TOP_SELLING_FREE to TopChartsHelper.Type.GAME),
            context.getString(R.string.topgrossing_apps) to mapOf(TopChartsHelper.Chart.TOP_GROSSING to TopChartsHelper.Type.APPLICATION),
            context.getString(R.string.topgrossing_games) to mapOf(TopChartsHelper.Chart.TOP_GROSSING to TopChartsHelper.Type.GAME),
            context.getString(R.string.movers_shakers_apps) to mapOf(TopChartsHelper.Chart.MOVERS_SHAKERS to TopChartsHelper.Type.APPLICATION),
            context.getString(R.string.movers_shakers_games) to mapOf(TopChartsHelper.Chart.MOVERS_SHAKERS to TopChartsHelper.Type.GAME),
        )
        homeElements.forEach {
            val chart = it.value.keys.iterator().next()
@@ -448,7 +545,8 @@ class FusedAPIImpl @Inject constructor(
            offer_type = this.offerType,
            status = pkgManagerModule.getPackageStatus(this.packageName, this.versionCode),
            origin = Origin.GPLAY,
            shareUrl = this.shareUrl
            shareUrl = this.shareUrl,
            appSize = Formatter.formatFileSize(context, this.size)
        )
    }

+2 −1
Original line number Diff line number Diff line
@@ -37,5 +37,6 @@ data class FusedApp(
    val offer_type: Int = -1,
    var status: Status = Status.UNAVAILABLE,
    var origin: Origin = Origin.CLEANAPK,
    val shareUrl: String = String()
    val shareUrl: String = String(),
    val appSize: String = String()
)
+2 −1
Original line number Diff line number Diff line
@@ -25,5 +25,6 @@ data class FusedCategory(
    val title: String = String(),
    val browseUrl: String = String(),
    val imageUrl: String = String(),
    val drawable: Int = -1,
    var drawable: Int = -1,
    var tag: String = String()
)
+50 −2
Original line number Diff line number Diff line
@@ -21,8 +21,7 @@ package foundation.e.apps.api.fused.utils
import foundation.e.apps.R

object CategoryUtils {

    fun provideCategoryIconResource(categoryId: String): Int {
    fun provideAppsCategoryIconResource(categoryId: String): Int {
        return when (categoryId) {
            "comics" ->
                R.drawable.ic_cat_comics
@@ -56,6 +55,8 @@ object CategoryUtils {
                R.drawable.ic_cat_lifestyle
            "video_players" ->
                R.drawable.ic_cat_video_players
            "video_players_and_editors" ->
                R.drawable.ic_cat_video_players
            "events" ->
                R.drawable.ic_cat_events
            "productivity" ->
@@ -168,6 +169,53 @@ object CategoryUtils {
                R.drawable.ic_business
            "web_games" ->
                R.drawable.ic_cat_sports
            "watch_faces" ->
                R.drawable.ic_watchface
            "watch_apps" ->
                R.drawable.ic_watch_apps
            else ->
                R.drawable.ic_cat_default
        }
    }

    fun provideGamesCategoryIconResource(categoryId: String): Int {
        return when (categoryId) {
            "action" ->
                R.drawable.ic_action
            "adventure" ->
                R.drawable.ic_adventure
            "arcade" ->
                R.drawable.ic_arcade
            "board" ->
                R.drawable.ic_board
            "card" ->
                R.drawable.ic_card
            "casino" ->
                R.drawable.ic_casino
            "casual" ->
                R.drawable.ic_casual
            "educational" ->
                R.drawable.ic_educational
            "music" ->
                R.drawable.ic_music
            "game_open_games" ->
                R.drawable.ic_cat_open_games
            "puzzle" ->
                R.drawable.ic_puzzle
            "racing" ->
                R.drawable.ic_racing
            "role_playing" ->
                R.drawable.ic_role_playing
            "simulation" ->
                R.drawable.ic_simulation
            "sports" ->
                R.drawable.ic_sports
            "strategy" ->
                R.drawable.ic_strategy
            "trivia" ->
                R.drawable.ic_trivia
            "word" ->
                R.drawable.ic_word
            else ->
                R.drawable.ic_cat_default
        }
+8 −3
Original line number Diff line number Diff line
@@ -152,7 +152,6 @@ class ApplicationFragment : Fragment(R.layout.fragment_application) {
                appName.text = it.name
                appAuthor.text = it.author
                categoryTitle.text = it.category

                if (args.origin == Origin.CLEANAPK) {
                    appIcon.load(CleanAPKInterface.ASSET_URL + it.icon_image_path)
                } else {
@@ -160,11 +159,16 @@ class ApplicationFragment : Fragment(R.layout.fragment_application) {
                }
            }

            binding.downloadInclude.appSize.text = it.appSize

            // Ratings widgets
            binding.ratingsInclude.apply {
                if (it.ratings.usageQualityScore != -1.0) {
                    appRating.text =
                        getString(R.string.rating_out_of, it.ratings.usageQualityScore.toString())
                        getString(
                            R.string.rating_out_of,
                            applicationViewModel.handleRatingFormat(it.ratings.usageQualityScore)
                        )
                }
                appRatingLayout.setOnClickListener {
                    ApplicationDialogFragment(
@@ -193,7 +197,8 @@ class ApplicationFragment : Fragment(R.layout.fragment_application) {
                Html.fromHtml(it.description, Html.FROM_HTML_MODE_COMPACT)

            binding.appDescriptionMore.setOnClickListener { view ->
                val action = ApplicationFragmentDirections.actionApplicationFragmentToDescriptionFragment(it.description)
                val action =
                    ApplicationFragmentDirections.actionApplicationFragmentToDescriptionFragment(it.description)
                view.findNavController().navigate(action)
            }

Loading