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

Commit c196bb30 authored by Hasib Prince's avatar Hasib Prince
Browse files

Refactored state handling of install button

parent 17b11363
Loading
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -15,7 +15,7 @@
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
package foundation.e.apps.applicationlist.model
package foundation.e.apps.applicationlist

import androidx.recyclerview.widget.DiffUtil
import foundation.e.apps.api.fused.data.FusedApp
+0 −1
Original line number Diff line number Diff line
@@ -40,7 +40,6 @@ import foundation.e.apps.api.ResultSupreme
import foundation.e.apps.api.fused.FusedAPIInterface
import foundation.e.apps.api.fused.data.FusedApp
import foundation.e.apps.application.subFrags.ApplicationDialogFragment
import foundation.e.apps.applicationlist.model.ApplicationListRVAdapter
import foundation.e.apps.databinding.FragmentApplicationListBinding
import foundation.e.apps.manager.download.data.DownloadProgress
import foundation.e.apps.manager.pkg.PkgManagerModule
+32 −60
Original line number Diff line number Diff line
/*
 * Apps  Quickly and easily install Android apps onto your device!
 * Copyright (C) 2021  E FOUNDATION
 *  Copyright (C) 2022  ECORP
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
@@ -16,9 +15,8 @@
 *   along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package foundation.e.apps.applicationlist.model
package foundation.e.apps.applicationlist

import android.graphics.Color
import android.util.Log
import android.view.LayoutInflater
import android.view.View
@@ -45,11 +43,12 @@ import foundation.e.apps.R
import foundation.e.apps.api.cleanapk.CleanAPKInterface
import foundation.e.apps.api.fused.FusedAPIInterface
import foundation.e.apps.api.fused.data.FusedApp
import foundation.e.apps.applicationlist.ApplicationListFragmentDirections
import foundation.e.apps.databinding.ApplicationListItemBinding
import foundation.e.apps.manager.pkg.InstallerService
import foundation.e.apps.search.SearchFragmentDirections
import foundation.e.apps.updates.UpdatesFragmentDirections
import foundation.e.apps.utils.disableInstallButton
import foundation.e.apps.utils.enableInstallButton
import foundation.e.apps.utils.enums.Origin
import foundation.e.apps.utils.enums.Status
import foundation.e.apps.utils.enums.User
@@ -244,19 +243,19 @@ class ApplicationListRVAdapter(
        showMore.visibility = View.INVISIBLE
        when (searchApp.status) {
            Status.INSTALLED -> {
                handleInstalled(view, searchApp)
                handleInstalled(searchApp)
            }
            Status.UPDATABLE -> {
                handleUpdatable(view, searchApp)
                handleUpdatable(searchApp)
            }
            Status.UNAVAILABLE -> {
                handleUnavailable(view, searchApp, holder)
                handleUnavailable(searchApp, holder)
            }
            Status.QUEUED, Status.AWAITING, Status.DOWNLOADING, Status.DOWNLOADED -> {
                handleDownloading(view, searchApp)
                handleDownloading(searchApp)
            }
            Status.INSTALLING, Status.UNINSTALLING -> {
                handleInstalling(view, holder)
                handleInstalling()
            }
            Status.BLOCKED -> {
                handleBlocked(view)
@@ -293,10 +292,8 @@ class ApplicationListRVAdapter(
        searchApp: FusedApp
    ) {
        installButton.apply {
            isEnabled = !faultyAppResult.first
            if (faultyAppResult.first) disableInstallButton() else enableInstallButton()
            text = getInstallationIssueText(faultyAppResult, view)
            strokeColor = getStrokeColor(isEnabled, view)
            setButtonTextColor(isEnabled)
            backgroundTintList =
                ContextCompat.getColorStateList(view.context, android.R.color.transparent)
            setOnClickListener {
@@ -401,24 +398,20 @@ class ApplicationListRVAdapter(
        appPrivacyScore.visibility = View.VISIBLE
    }

    private fun ApplicationListItemBinding.handleInstalling(view: View, holder: ViewHolder) {
    private fun ApplicationListItemBinding.handleInstalling() {
        installButton.apply {
            disableInstallButton(this, R.string.installing)
            disableInstallButton()
            text = context.getText(R.string.installing)
        }
        progressBarInstall.visibility = View.GONE
    }

    private fun ApplicationListItemBinding.handleDownloading(
        view: View,
        searchApp: FusedApp,
    ) {
        installButton.apply {
            isEnabled = true
            enableInstallButton()
            text = context.getString(R.string.cancel)
            setTextColor(context.getColor(R.color.colorAccent))
            backgroundTintList =
                ContextCompat.getColorStateList(view.context, android.R.color.transparent)
            strokeColor = ContextCompat.getColorStateList(view.context, R.color.colorAccent)
            setOnClickListener {
                cancelDownload(searchApp)
            }
@@ -428,22 +421,18 @@ class ApplicationListRVAdapter(
    }

    private fun ApplicationListItemBinding.handleUnavailable(
        view: View,
        searchApp: FusedApp,
        holder: ViewHolder,
    ) {
        installButton.apply {
            updateUIByPaymentType(searchApp, this, this@handleUnavailable, holder)
            setTextColor(context.getColor(R.color.colorAccent))
            backgroundTintList =
                ContextCompat.getColorStateList(view.context, android.R.color.transparent)
            strokeColor = ContextCompat.getColorStateList(view.context, R.color.colorAccent)
            setOnClickListener {
                if (mainActivityViewModel.checkUnsupportedApplication(searchApp, context)) {
                    return@setOnClickListener
                }
                if (searchApp.isFree || searchApp.isPurchased) {
                    disableInstallButton(view, R.string.cancel)
                    disableInstallButton()
                    text = context.getText(R.string.cancel)
                    installApplication(searchApp, appIcon)
                } else {
                    paidAppHandler?.invoke(searchApp)
@@ -452,15 +441,6 @@ class ApplicationListRVAdapter(
        }
    }

    private fun MaterialButton.disableInstallButton(view: View, buttonString: Int) {
        isEnabled = false
        text = context.getString(buttonString)
        strokeColor = getStrokeColor(isEnabled, view)
        setButtonTextColor(isEnabled)
        backgroundTintList =
            ContextCompat.getColorStateList(view.context, android.R.color.transparent)
    }

    private fun updateUIByPaymentType(
        searchApp: FusedApp,
        materialButton: MaterialButton,
@@ -469,17 +449,17 @@ class ApplicationListRVAdapter(
    ) {
        when {
            mainActivityViewModel.checkUnsupportedApplication(searchApp) -> {
                materialButton.isEnabled = true
                materialButton.enableInstallButton()
                materialButton.text = materialButton.context.getString(R.string.not_available)
                applicationListItemBinding.progressBarInstall.visibility = View.GONE
            }
            searchApp.isFree -> {
                materialButton.isEnabled = true
                materialButton.enableInstallButton()
                materialButton.text = materialButton.context.getString(R.string.install)
                applicationListItemBinding.progressBarInstall.visibility = View.GONE
            }
            else -> {
                materialButton.isEnabled = false
                materialButton.disableInstallButton()
                materialButton.text = ""
                applicationListItemBinding.progressBarInstall.visibility = View.VISIBLE
                holder.isPurchasedLiveData = appInfoFetchViewModel.isAppPurchased(searchApp)
@@ -487,7 +467,7 @@ class ApplicationListRVAdapter(
                    return
                }
                holder.isPurchasedLiveData.observe(lifecycleOwner!!) {
                    materialButton.isEnabled = true
                    materialButton.enableInstallButton()
                    applicationListItemBinding.progressBarInstall.visibility = View.GONE
                    materialButton.text =
                        if (it) materialButton.context.getString(R.string.install) else searchApp.price
@@ -497,17 +477,13 @@ class ApplicationListRVAdapter(
    }

    private fun ApplicationListItemBinding.handleUpdatable(
        view: View,
        searchApp: FusedApp
    ) {
        installButton.apply {
            isEnabled = true
            enableInstallButton(Status.UPDATABLE)
            text = if (mainActivityViewModel.checkUnsupportedApplication(searchApp))
                context.getString(R.string.not_available)
            else context.getString(R.string.update)
            setTextColor(Color.WHITE)
            backgroundTintList = ContextCompat.getColorStateList(view.context, R.color.colorAccent)
            strokeColor = ContextCompat.getColorStateList(view.context, R.color.colorAccent)
            setOnClickListener {
                if (mainActivityViewModel.checkUnsupportedApplication(searchApp, context)) {
                    return@setOnClickListener
@@ -519,15 +495,11 @@ class ApplicationListRVAdapter(
    }

    private fun ApplicationListItemBinding.handleInstalled(
        view: View,
        searchApp: FusedApp,
    ) {
        installButton.apply {
            isEnabled = true
            enableInstallButton(Status.INSTALLED)
            text = context.getString(R.string.open)
            setTextColor(Color.WHITE)
            backgroundTintList = ContextCompat.getColorStateList(view.context, R.color.colorAccent)
            strokeColor = ContextCompat.getColorStateList(view.context, R.color.colorAccent)
            setOnClickListener {
                if (searchApp.is_pwa) {
                    mainActivityViewModel.launchPwa(searchApp)
+18 −48
Original line number Diff line number Diff line
@@ -18,7 +18,6 @@

package foundation.e.apps.home.model

import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
@@ -41,6 +40,8 @@ import foundation.e.apps.api.fused.FusedAPIInterface
import foundation.e.apps.api.fused.data.FusedApp
import foundation.e.apps.databinding.HomeChildListItemBinding
import foundation.e.apps.home.HomeFragmentDirections
import foundation.e.apps.utils.disableInstallButton
import foundation.e.apps.utils.enableInstallButton
import foundation.e.apps.utils.enums.Origin
import foundation.e.apps.utils.enums.Status
import foundation.e.apps.utils.enums.User
@@ -108,13 +109,13 @@ class HomeChildRVAdapter(
                    handleUpdatable(view, homeApp)
                }
                Status.UNAVAILABLE -> {
                    handleUnavailable(homeApp, holder, view)
                    handleUnavailable(homeApp, holder)
                }
                Status.QUEUED, Status.AWAITING, Status.DOWNLOADING, Status.DOWNLOADED -> {
                    handleQueued(view, homeApp)
                }
                Status.INSTALLING, Status.UNINSTALLING -> {
                    handleInstalling(view)
                    handleInstalling()
                }
                Status.BLOCKED -> {
                    handleBlocked(view)
@@ -131,14 +132,8 @@ class HomeChildRVAdapter(
        homeApp: FusedApp
    ) {
        installButton.apply {
            enableInstallButton()
            text = view.context.getString(R.string.retry)
            setTextColor(context.getColor(R.color.colorAccent))
            backgroundTintList = ContextCompat.getColorStateList(
                view.context,
                android.R.color.transparent
            )
            strokeColor =
                ContextCompat.getColorStateList(view.context, R.color.colorAccent)
            setOnClickListener {
                installApplication(homeApp, appIcon)
            }
@@ -147,6 +142,7 @@ class HomeChildRVAdapter(
    }

    private fun HomeChildListItemBinding.handleBlocked(view: View) {
        installButton.enableInstallButton()
        installButton.setOnClickListener {
            val errorMsg = when (mainActivityViewModel.getUser()) {
                User.ANONYMOUS,
@@ -160,9 +156,10 @@ class HomeChildRVAdapter(
        progressBarInstall.visibility = View.GONE
    }

    private fun HomeChildListItemBinding.handleInstalling(view: View) {
    private fun HomeChildListItemBinding.handleInstalling() {
        installButton.apply {
            disableInstallButton(this, R.string.installing)
            disableInstallButton()
            text = context.getString(R.string.installing)
        }
        progressBarInstall.visibility = View.GONE
    }
@@ -172,6 +169,7 @@ class HomeChildRVAdapter(
        homeApp: FusedApp
    ) {
        installButton.apply {
            enableInstallButton()
            text = context.getString(R.string.cancel)
            setTextColor(context.getColor(R.color.colorAccent))
            backgroundTintList = ContextCompat.getColorStateList(
@@ -191,23 +189,16 @@ class HomeChildRVAdapter(
    private fun HomeChildListItemBinding.handleUnavailable(
        homeApp: FusedApp,
        holder: ViewHolder,
        view: View
    ) {
        installButton.apply {
            updateUIByPaymentType(homeApp, this, holder.binding)
            setTextColor(context.getColor(R.color.colorAccent))
            backgroundTintList = ContextCompat.getColorStateList(
                view.context,
                android.R.color.transparent
            )
            strokeColor =
                ContextCompat.getColorStateList(view.context, R.color.colorAccent)
            setOnClickListener {
                if (mainActivityViewModel.checkUnsupportedApplication(homeApp, context)) {
                    return@setOnClickListener
                }
                if (homeApp.isFree) {
                    disableInstallButton(view, R.string.cancel)
                    disableInstallButton()
                    text = context.getString(R.string.cancel)
                    installApplication(homeApp, appIcon)
                } else {
                    paidAppHandler?.invoke(homeApp)
@@ -216,31 +207,15 @@ class HomeChildRVAdapter(
        }
    }

    private fun MaterialButton.disableInstallButton(view: View, buttonString: Int) {
        isEnabled = false
        text = context.getString(buttonString)
        setTextColor(context.getColor(R.color.light_grey))
        backgroundTintList = ContextCompat.getColorStateList(
            view.context,
            android.R.color.transparent
        )
        strokeColor =
            ContextCompat.getColorStateList(view.context, R.color.light_grey)
    }

    private fun HomeChildListItemBinding.handleUpdatable(
        view: View,
        homeApp: FusedApp
    ) {
        installButton.apply {
            enableInstallButton(Status.UPDATABLE)
            text = if (mainActivityViewModel.checkUnsupportedApplication(homeApp))
                context.getString(R.string.not_available)
            else context.getString(R.string.update)
            setTextColor(Color.WHITE)
            backgroundTintList =
                ContextCompat.getColorStateList(view.context, R.color.colorAccent)
            strokeColor =
                ContextCompat.getColorStateList(view.context, R.color.colorAccent)
            setOnClickListener {
                if (mainActivityViewModel.checkUnsupportedApplication(homeApp, context)) {
                    return@setOnClickListener
@@ -256,13 +231,8 @@ class HomeChildRVAdapter(
        homeApp: FusedApp
    ) {
        installButton.apply {
            isEnabled = true
            enableInstallButton(Status.INSTALLED)
            text = context.getString(R.string.open)
            setTextColor(Color.WHITE)
            backgroundTintList =
                ContextCompat.getColorStateList(view.context, R.color.colorAccent)
            strokeColor =
                ContextCompat.getColorStateList(view.context, R.color.colorAccent)
            setOnClickListener {
                if (homeApp.is_pwa) {
                    mainActivityViewModel.launchPwa(homeApp)
@@ -281,22 +251,22 @@ class HomeChildRVAdapter(
    ) {
        when {
            mainActivityViewModel.checkUnsupportedApplication(homeApp) -> {
                materialButton.isEnabled = true
                materialButton.enableInstallButton()
                materialButton.text = materialButton.context.getString(R.string.not_available)
            }
            homeApp.isFree -> {
                materialButton.isEnabled = true
                materialButton.enableInstallButton()
                materialButton.text = materialButton.context.getString(R.string.install)
                homeChildListItemBinding.progressBarInstall.visibility = View.GONE
            }
            else -> {
                materialButton.isEnabled = false
                materialButton.disableInstallButton()
                materialButton.text = ""
                homeChildListItemBinding.progressBarInstall.visibility = View.VISIBLE
                lifecycleOwner?.let {
                    appInfoFetchViewModel.isAppPurchased(homeApp).observe(it) {
                        materialButton.isEnabled = true
                        homeChildListItemBinding.progressBarInstall.visibility = View.GONE
                        materialButton.enableInstallButton()
                        materialButton.text =
                            if (it) materialButton.context.getString(R.string.install) else homeApp.price
                    }
+1 −1
Original line number Diff line number Diff line
@@ -50,7 +50,7 @@ import foundation.e.apps.api.ResultSupreme
import foundation.e.apps.api.fused.FusedAPIInterface
import foundation.e.apps.api.fused.data.FusedApp
import foundation.e.apps.application.subFrags.ApplicationDialogFragment
import foundation.e.apps.applicationlist.model.ApplicationListRVAdapter
import foundation.e.apps.applicationlist.ApplicationListRVAdapter
import foundation.e.apps.databinding.FragmentSearchBinding
import foundation.e.apps.manager.download.data.DownloadProgress
import foundation.e.apps.manager.pkg.PkgManagerModule
Loading