From 7204c204bf337e7b24ff61b4bbab66d2d17efadd Mon Sep 17 00:00:00 2001 From: LooKeR Date: Fri, 26 Nov 2021 00:53:52 +0530 Subject: [PATCH] Improve: Update more fun to suspend fun --- .../looker/droidify/screen/ProductFragment.kt | 30 +++++++++----- .../com/looker/droidify/utility/Utils.kt | 39 +++++++++++-------- 2 files changed, 42 insertions(+), 27 deletions(-) diff --git a/src/main/kotlin/com/looker/droidify/screen/ProductFragment.kt b/src/main/kotlin/com/looker/droidify/screen/ProductFragment.kt index 54505841..6211298e 100644 --- a/src/main/kotlin/com/looker/droidify/screen/ProductFragment.kt +++ b/src/main/kotlin/com/looker/droidify/screen/ProductFragment.kt @@ -82,8 +82,10 @@ class ProductFragment() : ScreenFragment(), ProductAdapter.Callbacks { private var productDisposable: Disposable? = null private var downloadDisposable: Disposable? = null private val downloadConnection = Connection(DownloadService::class.java, onBind = { _, binder -> - updateDownloadState(binder.getState(packageName)) - downloadDisposable = binder.events(packageName).subscribe { updateDownloadState(it) } + lifecycleScope.launch { updateDownloadState(binder.getState(packageName)) } + downloadDisposable = binder.events(packageName).subscribe { + lifecycleScope.launch { updateDownloadState(it) } + } }, onUnbind = { _, _ -> downloadDisposable?.dispose() downloadDisposable = null @@ -220,7 +222,7 @@ class ProductFragment() : ScreenFragment(), ProductAdapter.Callbacks { installedItem.value ) } - updateButtons() + lifecycleScope.launch { updateButtons() } } } @@ -248,11 +250,11 @@ class ProductFragment() : ScreenFragment(), ProductAdapter.Callbacks { adapterState?.let { outState.putParcelable(STATE_ADAPTER, it) } } - private fun updateButtons() { + private suspend fun updateButtons() { updateButtons(ProductPreferences[packageName]) } - private fun updateButtons(preference: ProductPreference) { + private suspend fun updateButtons(preference: ProductPreference) { val installed = installed val product = Product.findSuggested(products, installed?.installedItem) { it.first }?.first val compatible = product != null && product.selectedReleases.firstOrNull() @@ -307,7 +309,7 @@ class ProductFragment() : ScreenFragment(), ProductAdapter.Callbacks { toolbar.menu.findItem(action.id).isEnabled = !downloading } this.actions = Pair(actions, primaryAction) - lifecycleScope.launch { updateToolbarButtons() } + withContext(Dispatchers.Main) { updateToolbarButtons() } } private suspend fun updateToolbarTitle() { @@ -340,7 +342,7 @@ class ProductFragment() : ScreenFragment(), ProductAdapter.Callbacks { } } - private fun updateDownloadState(state: DownloadService.State?) { + private suspend fun updateDownloadState(state: DownloadService.State?) { val status = when (state) { is DownloadService.State.Pending -> ProductAdapter.Status.Pending is DownloadService.State.Connecting -> ProductAdapter.Status.Connecting @@ -357,7 +359,7 @@ class ProductFragment() : ScreenFragment(), ProductAdapter.Callbacks { } (recyclerView?.adapter as? ProductAdapter)?.setStatus(status) if (state is DownloadService.State.Success && isResumed) { - lifecycleScope.launch { + withContext(Dispatchers.Default) { state.consume() AppInstaller.getInstance(context)?.defaultInstaller?.install(state.release.cacheFileName) } @@ -387,7 +389,15 @@ class ProductFragment() : ScreenFragment(), ProductAdapter.Callbacks { ProductAdapter.Action.UPDATE, -> { val installedItem = installed?.installedItem - startUpdate(packageName, installedItem, products, downloadConnection) + lifecycleScope.launch { + startUpdate( + packageName, + installedItem, + products, + downloadConnection + ) + } + Unit } ProductAdapter.Action.LAUNCH -> { val launcherActivities = installed?.launcherActivities.orEmpty() @@ -447,7 +457,7 @@ class ProductFragment() : ScreenFragment(), ProductAdapter.Callbacks { } override fun onPreferenceChanged(preference: ProductPreference) { - updateButtons(preference) + lifecycleScope.launch { updateButtons(preference) } } override fun onPermissionsClick(group: String?, permissions: List) { diff --git a/src/main/kotlin/com/looker/droidify/utility/Utils.kt b/src/main/kotlin/com/looker/droidify/utility/Utils.kt index 541dbb03..58e42d43 100644 --- a/src/main/kotlin/com/looker/droidify/utility/Utils.kt +++ b/src/main/kotlin/com/looker/droidify/utility/Utils.kt @@ -22,6 +22,8 @@ import com.looker.droidify.utility.extension.resources.getColorFromAttr import com.looker.droidify.utility.extension.resources.getDrawableCompat import com.looker.droidify.utility.extension.text.hex import com.topjohnwu.superuser.Shell +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.collect import java.security.MessageDigest import java.security.cert.Certificate import java.security.cert.CertificateEncodingException @@ -86,7 +88,7 @@ object Utils { get() = Preferences[Preferences.Key.RootPermission] && (Shell.getCachedShell()?.isRoot ?: Shell.getShell().isRoot) - fun startUpdate( + suspend fun startUpdate( packageName: String, installedItem: InstalledItem?, products: List>, @@ -95,24 +97,27 @@ object Utils { val productRepository = Product.findSuggested(products, installedItem) { it.first } val compatibleReleases = productRepository?.first?.selectedReleases.orEmpty() .filter { installedItem == null || installedItem.signature == it.signature } - val release = if (compatibleReleases.size >= 2) { - compatibleReleases - .filter { it.platforms.contains(Android.primaryPlatform) } - .minByOrNull { it.platforms.size } - ?: compatibleReleases.minByOrNull { it.platforms.size } - ?: compatibleReleases.firstOrNull() - } else { - compatibleReleases.firstOrNull() + val releaseFlow = MutableStateFlow(compatibleReleases.firstOrNull()) + if (compatibleReleases.size > 1) { + releaseFlow.emit( + compatibleReleases + .filter { it.platforms.contains(Android.primaryPlatform) } + .minByOrNull { it.platforms.size } + ?: compatibleReleases.minByOrNull { it.platforms.size } + ?: compatibleReleases.firstOrNull() + ) } val binder = downloadConnection.binder - if (productRepository != null && release != null && binder != null) { - binder.enqueue( - packageName, - productRepository.first.name, - productRepository.second, - release - ) - } else Unit + releaseFlow.collect { + if (productRepository != null && it != null && binder != null) { + binder.enqueue( + packageName, + productRepository.first.name, + productRepository.second, + it + ) + } + } } fun Context.setLanguage(): Configuration {