diff --git a/app/src/main/java/foundation/e/apps/application/ApplicationFragment.kt b/app/src/main/java/foundation/e/apps/application/ApplicationFragment.kt index 365a5473eadb3814bcff0f240b549b0a830e6d8f..9dac14ef55d94221d87c0126ba17afa68ac9b08b 100644 --- a/app/src/main/java/foundation/e/apps/application/ApplicationFragment.kt +++ b/app/src/main/java/foundation/e/apps/application/ApplicationFragment.kt @@ -468,7 +468,7 @@ class ApplicationFragment : TimeoutFragment(R.layout.fragment_application) { appSize: MaterialTextView ) { installButton.apply { - text = getString(R.string.retry) + enableInstallButton(R.string.retry) setOnClickListener { applicationIcon?.let { mainActivityViewModel.getApplication(fusedApp, it) @@ -504,7 +504,7 @@ class ApplicationFragment : TimeoutFragment(R.layout.fragment_application) { downloadPB: RelativeLayout, appSize: MaterialTextView ) { - installButton.isEnabled = false + installButton.disableInstallButton(R.string.installing) downloadPB.visibility = View.GONE appSize.visibility = View.VISIBLE } @@ -516,6 +516,7 @@ class ApplicationFragment : TimeoutFragment(R.layout.fragment_application) { appSize: MaterialTextView ) { installButton.apply { + enableInstallButton(R.string.cancel) text = getString(R.string.cancel) setOnClickListener { mainActivityViewModel.cancelDownload(fusedApp) @@ -539,6 +540,7 @@ class ApplicationFragment : TimeoutFragment(R.layout.fragment_application) { downloadPB.visibility = View.GONE appSize.visibility = View.VISIBLE installButton.apply { + enableInstallButton(R.string.cancel) text = getString(R.string.cancel) setOnClickListener { mainActivityViewModel.cancelDownload(fusedApp) @@ -553,6 +555,7 @@ class ApplicationFragment : TimeoutFragment(R.layout.fragment_application) { appSize: MaterialTextView ) { installButton.apply { + enableInstallButton(R.string.install) text = when { mainActivityViewModel.checkUnsupportedApplication(fusedApp) -> getString(R.string.not_available) @@ -565,6 +568,7 @@ class ApplicationFragment : TimeoutFragment(R.layout.fragment_application) { } applicationIcon?.let { if (fusedApp.isFree) { + disableInstallButton(R.string.cancel) installApplication(fusedApp, it) } else { if (!mainActivityViewModel.shouldShowPaidAppsSnackBar(fusedApp)) { @@ -590,6 +594,24 @@ class ApplicationFragment : TimeoutFragment(R.layout.fragment_application) { appSize.visibility = View.VISIBLE } + private fun MaterialButton.disableInstallButton(buttonStringID: Int) { + isEnabled = false + text = context.getString(buttonStringID) + strokeColor = ContextCompat.getColorStateList(context, R.color.light_grey) + setTextColor(context.getColor(R.color.light_grey)) + backgroundTintList = + ContextCompat.getColorStateList(context, android.R.color.transparent) + } + + private fun MaterialButton.enableInstallButton(buttonStringID: Int) { + isEnabled = true + text = context.getString(buttonStringID) + strokeColor = ContextCompat.getColorStateList(context, R.color.colorAccent) + setTextColor(context.getColor(R.color.colorAccent)) + backgroundTintList = + ContextCompat.getColorStateList(context, android.R.color.transparent) + } + private fun installApplication( fusedApp: FusedApp, it: ImageView @@ -616,6 +638,7 @@ class ApplicationFragment : TimeoutFragment(R.layout.fragment_application) { appSize: MaterialTextView ) { installButton.apply { + enableInstallButton(R.string.not_available) text = if (mainActivityViewModel.checkUnsupportedApplication(fusedApp)) getString(R.string.not_available) else getString(R.string.update) @@ -645,8 +668,7 @@ class ApplicationFragment : TimeoutFragment(R.layout.fragment_application) { downloadPB.visibility = View.GONE appSize.visibility = View.VISIBLE installButton.apply { - isEnabled = true - text = getString(R.string.open) + enableInstallButton(R.string.open) setTextColor(Color.WHITE) backgroundTintList = ContextCompat.getColorStateList(view.context, R.color.colorAccent) diff --git a/app/src/main/java/foundation/e/apps/applicationlist/model/ApplicationDiffUtil.kt b/app/src/main/java/foundation/e/apps/applicationlist/ApplicationDiffUtil.kt similarity index 98% rename from app/src/main/java/foundation/e/apps/applicationlist/model/ApplicationDiffUtil.kt rename to app/src/main/java/foundation/e/apps/applicationlist/ApplicationDiffUtil.kt index 69e4b7935be33facfba2666b852bd460be3ce7d1..e1c2c3a65b37847884d17476bd7de5bfba3911a7 100644 --- a/app/src/main/java/foundation/e/apps/applicationlist/model/ApplicationDiffUtil.kt +++ b/app/src/main/java/foundation/e/apps/applicationlist/ApplicationDiffUtil.kt @@ -15,7 +15,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -package foundation.e.apps.applicationlist.model +package foundation.e.apps.applicationlist import androidx.recyclerview.widget.DiffUtil import foundation.e.apps.api.fused.data.FusedApp diff --git a/app/src/main/java/foundation/e/apps/applicationlist/ApplicationListFragment.kt b/app/src/main/java/foundation/e/apps/applicationlist/ApplicationListFragment.kt index 0158c66ea322d27b620bddc6823239e2623bff6a..371808ea5d285fdd54af21ee854b2b32f4da1d6b 100644 --- a/app/src/main/java/foundation/e/apps/applicationlist/ApplicationListFragment.kt +++ b/app/src/main/java/foundation/e/apps/applicationlist/ApplicationListFragment.kt @@ -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 diff --git a/app/src/main/java/foundation/e/apps/applicationlist/model/ApplicationListRVAdapter.kt b/app/src/main/java/foundation/e/apps/applicationlist/ApplicationListRVAdapter.kt similarity index 82% rename from app/src/main/java/foundation/e/apps/applicationlist/model/ApplicationListRVAdapter.kt rename to app/src/main/java/foundation/e/apps/applicationlist/ApplicationListRVAdapter.kt index ecc072724dcb306ba2689c3f85f25e7b17485075..e1897ab3c5451475bf7450da6e1aa4be8d576523 100644 --- a/app/src/main/java/foundation/e/apps/applicationlist/model/ApplicationListRVAdapter.kt +++ b/app/src/main/java/foundation/e/apps/applicationlist/ApplicationListRVAdapter.kt @@ -1,24 +1,22 @@ /* - * 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 - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ -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,29 +398,20 @@ class ApplicationListRVAdapter( appPrivacyScore.visibility = View.VISIBLE } - private fun ApplicationListItemBinding.handleInstalling(view: View, holder: ViewHolder) { + private fun ApplicationListItemBinding.handleInstalling() { installButton.apply { - isEnabled = false - text = context.getString(R.string.installing) - 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) + 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) } @@ -433,21 +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() + text = context.getText(R.string.cancel) installApplication(searchApp, appIcon) } else { paidAppHandler?.invoke(searchApp) @@ -464,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) @@ -482,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 @@ -492,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 @@ -514,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) diff --git a/app/src/main/java/foundation/e/apps/home/model/HomeChildRVAdapter.kt b/app/src/main/java/foundation/e/apps/home/model/HomeChildRVAdapter.kt index a7de334ff816d8ab0b49d346d63b29d1e422bf0b..3309c595ba9f1a987a42ed2aed41997343df2967 100644 --- a/app/src/main/java/foundation/e/apps/home/model/HomeChildRVAdapter.kt +++ b/app/src/main/java/foundation/e/apps/home/model/HomeChildRVAdapter.kt @@ -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,17 +156,10 @@ class HomeChildRVAdapter( progressBarInstall.visibility = View.GONE } - private fun HomeChildListItemBinding.handleInstalling(view: View) { + private fun HomeChildListItemBinding.handleInstalling() { installButton.apply { - isEnabled = false - setTextColor(context.getColor(R.color.light_grey)) + disableInstallButton() text = context.getString(R.string.installing) - backgroundTintList = ContextCompat.getColorStateList( - view.context, - android.R.color.transparent - ) - strokeColor = - ContextCompat.getColorStateList(view.context, R.color.light_grey) } progressBarInstall.visibility = View.GONE } @@ -180,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( @@ -199,22 +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() + text = context.getString(R.string.cancel) installApplication(homeApp, appIcon) } else { paidAppHandler?.invoke(homeApp) @@ -228,14 +212,10 @@ class HomeChildRVAdapter( 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 @@ -251,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) @@ -276,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 } diff --git a/app/src/main/java/foundation/e/apps/search/SearchFragment.kt b/app/src/main/java/foundation/e/apps/search/SearchFragment.kt index 545c13f67df509587afe4cfaaae9ed30ec699428..35de693ce48cd94c1652670479a94a8cff74f1a0 100644 --- a/app/src/main/java/foundation/e/apps/search/SearchFragment.kt +++ b/app/src/main/java/foundation/e/apps/search/SearchFragment.kt @@ -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 diff --git a/app/src/main/java/foundation/e/apps/updates/UpdatesFragment.kt b/app/src/main/java/foundation/e/apps/updates/UpdatesFragment.kt index 5e8d1a1009037984b9021cdec2827acc0a86c4cc..a192c48a15186221440191a1ad2fe0c79495d905 100644 --- a/app/src/main/java/foundation/e/apps/updates/UpdatesFragment.kt +++ b/app/src/main/java/foundation/e/apps/updates/UpdatesFragment.kt @@ -40,7 +40,7 @@ import foundation.e.apps.api.fused.FusedAPIImpl 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.FragmentUpdatesBinding import foundation.e.apps.manager.download.data.DownloadProgress import foundation.e.apps.manager.pkg.PkgManagerModule diff --git a/app/src/main/java/foundation/e/apps/utils/MaterialButtonUtils.kt b/app/src/main/java/foundation/e/apps/utils/MaterialButtonUtils.kt new file mode 100644 index 0000000000000000000000000000000000000000..1d6b29825b8844ba20d1356990564136fe52a491 --- /dev/null +++ b/app/src/main/java/foundation/e/apps/utils/MaterialButtonUtils.kt @@ -0,0 +1,63 @@ +/* + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package foundation.e.apps.utils + +import android.graphics.Color +import androidx.core.content.ContextCompat +import com.google.android.material.button.MaterialButton +import foundation.e.apps.R +import foundation.e.apps.utils.enums.Status + +fun MaterialButton.disableInstallButton(status: Status? = null) { + toggleEnableMaterialButton(false, status) +} + +fun MaterialButton.enableInstallButton(status: Status? = null) { + toggleEnableMaterialButton(true, status) +} + +private fun MaterialButton.toggleEnableMaterialButton(isEnabled: Boolean, status: Status?) { + this.isEnabled = isEnabled + strokeColor = getStrokeColor(isEnabled) + setButtonTextColor(isEnabled, status) + backgroundTintList = + getBackgroundTintList(status) +} + +private fun MaterialButton.getBackgroundTintList(status: Status?) = + if (status == Status.INSTALLED || status == Status.UPDATABLE) { + ContextCompat.getColorStateList(this.context, R.color.colorAccent) + } else + ContextCompat.getColorStateList(this.context, android.R.color.transparent) + +private fun MaterialButton.getStrokeColor( + isEnabled: Boolean, +) = if (isEnabled) { + ContextCompat.getColorStateList(this.context, R.color.colorAccent) +} else { + ContextCompat.getColorStateList(this.context, R.color.light_grey) +} + +private fun MaterialButton.setButtonTextColor(isEnabled: Boolean, status: Status?) = + if (isEnabled && (status == Status.INSTALLED || status == Status.UPDATABLE)) { + setTextColor(Color.WHITE) + } else if (isEnabled) { + setTextColor(context.getColor(R.color.colorAccent)) + } else { + setTextColor(context.getColor(R.color.light_grey)) + } diff --git a/app/src/main/res/layout/application_list_item.xml b/app/src/main/res/layout/application_list_item.xml index 0984cf0a69e202845519aaf207e341aa24abd227..2205bd5b313a49ef673e112904a18a67fbd5c2cc 100644 --- a/app/src/main/res/layout/application_list_item.xml +++ b/app/src/main/res/layout/application_list_item.xml @@ -68,7 +68,6 @@ android:textColor="?android:textColorPrimary" android:textSize="16sp" app:layout_constraintLeft_toRightOf="@+id/app_icon_card" - app:layout_constraintRight_toLeftOf="@+id/installButton" app:layout_constraintTop_toTopOf="@+id/app_icon_card" tools:text="Google Chrome - Fast and Secured" /> @@ -96,7 +95,7 @@ android:progressBackgroundTint="@color/colorAccent" android:progressTint="@color/colorAccent" android:stepSize="0.1" - app:layout_constraintBottom_toBottomOf="@id/app_icon_card" + app:layout_constraintTop_toBottomOf="@+id/installButton" app:layout_constraintLeft_toLeftOf="@+id/app_title" /> + app:layout_constraintTop_toBottomOf="@+id/app_title" />