Improve: Update more fun to suspend fun

This commit is contained in:
LooKeR 2021-11-26 00:53:52 +05:30
parent a4a1d1b3ab
commit 7204c204bf
2 changed files with 42 additions and 27 deletions

View File

@ -82,8 +82,10 @@ class ProductFragment() : ScreenFragment(), ProductAdapter.Callbacks {
private var productDisposable: Disposable? = null private var productDisposable: Disposable? = null
private var downloadDisposable: Disposable? = null private var downloadDisposable: Disposable? = null
private val downloadConnection = Connection(DownloadService::class.java, onBind = { _, binder -> private val downloadConnection = Connection(DownloadService::class.java, onBind = { _, binder ->
updateDownloadState(binder.getState(packageName)) lifecycleScope.launch { updateDownloadState(binder.getState(packageName)) }
downloadDisposable = binder.events(packageName).subscribe { updateDownloadState(it) } downloadDisposable = binder.events(packageName).subscribe {
lifecycleScope.launch { updateDownloadState(it) }
}
}, onUnbind = { _, _ -> }, onUnbind = { _, _ ->
downloadDisposable?.dispose() downloadDisposable?.dispose()
downloadDisposable = null downloadDisposable = null
@ -220,7 +222,7 @@ class ProductFragment() : ScreenFragment(), ProductAdapter.Callbacks {
installedItem.value installedItem.value
) )
} }
updateButtons() lifecycleScope.launch { updateButtons() }
} }
} }
@ -248,11 +250,11 @@ class ProductFragment() : ScreenFragment(), ProductAdapter.Callbacks {
adapterState?.let { outState.putParcelable(STATE_ADAPTER, it) } adapterState?.let { outState.putParcelable(STATE_ADAPTER, it) }
} }
private fun updateButtons() { private suspend fun updateButtons() {
updateButtons(ProductPreferences[packageName]) updateButtons(ProductPreferences[packageName])
} }
private fun updateButtons(preference: ProductPreference) { private suspend fun updateButtons(preference: ProductPreference) {
val installed = installed val installed = installed
val product = Product.findSuggested(products, installed?.installedItem) { it.first }?.first val product = Product.findSuggested(products, installed?.installedItem) { it.first }?.first
val compatible = product != null && product.selectedReleases.firstOrNull() val compatible = product != null && product.selectedReleases.firstOrNull()
@ -307,7 +309,7 @@ class ProductFragment() : ScreenFragment(), ProductAdapter.Callbacks {
toolbar.menu.findItem(action.id).isEnabled = !downloading toolbar.menu.findItem(action.id).isEnabled = !downloading
} }
this.actions = Pair(actions, primaryAction) this.actions = Pair(actions, primaryAction)
lifecycleScope.launch { updateToolbarButtons() } withContext(Dispatchers.Main) { updateToolbarButtons() }
} }
private suspend fun updateToolbarTitle() { 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) { val status = when (state) {
is DownloadService.State.Pending -> ProductAdapter.Status.Pending is DownloadService.State.Pending -> ProductAdapter.Status.Pending
is DownloadService.State.Connecting -> ProductAdapter.Status.Connecting is DownloadService.State.Connecting -> ProductAdapter.Status.Connecting
@ -357,7 +359,7 @@ class ProductFragment() : ScreenFragment(), ProductAdapter.Callbacks {
} }
(recyclerView?.adapter as? ProductAdapter)?.setStatus(status) (recyclerView?.adapter as? ProductAdapter)?.setStatus(status)
if (state is DownloadService.State.Success && isResumed) { if (state is DownloadService.State.Success && isResumed) {
lifecycleScope.launch { withContext(Dispatchers.Default) {
state.consume() state.consume()
AppInstaller.getInstance(context)?.defaultInstaller?.install(state.release.cacheFileName) AppInstaller.getInstance(context)?.defaultInstaller?.install(state.release.cacheFileName)
} }
@ -387,7 +389,15 @@ class ProductFragment() : ScreenFragment(), ProductAdapter.Callbacks {
ProductAdapter.Action.UPDATE, ProductAdapter.Action.UPDATE,
-> { -> {
val installedItem = installed?.installedItem val installedItem = installed?.installedItem
startUpdate(packageName, installedItem, products, downloadConnection) lifecycleScope.launch {
startUpdate(
packageName,
installedItem,
products,
downloadConnection
)
}
Unit
} }
ProductAdapter.Action.LAUNCH -> { ProductAdapter.Action.LAUNCH -> {
val launcherActivities = installed?.launcherActivities.orEmpty() val launcherActivities = installed?.launcherActivities.orEmpty()
@ -447,7 +457,7 @@ class ProductFragment() : ScreenFragment(), ProductAdapter.Callbacks {
} }
override fun onPreferenceChanged(preference: ProductPreference) { override fun onPreferenceChanged(preference: ProductPreference) {
updateButtons(preference) lifecycleScope.launch { updateButtons(preference) }
} }
override fun onPermissionsClick(group: String?, permissions: List<String>) { override fun onPermissionsClick(group: String?, permissions: List<String>) {

View File

@ -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.resources.getDrawableCompat
import com.looker.droidify.utility.extension.text.hex import com.looker.droidify.utility.extension.text.hex
import com.topjohnwu.superuser.Shell import com.topjohnwu.superuser.Shell
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.collect
import java.security.MessageDigest import java.security.MessageDigest
import java.security.cert.Certificate import java.security.cert.Certificate
import java.security.cert.CertificateEncodingException import java.security.cert.CertificateEncodingException
@ -86,7 +88,7 @@ object Utils {
get() = Preferences[Preferences.Key.RootPermission] && get() = Preferences[Preferences.Key.RootPermission] &&
(Shell.getCachedShell()?.isRoot ?: Shell.getShell().isRoot) (Shell.getCachedShell()?.isRoot ?: Shell.getShell().isRoot)
fun startUpdate( suspend fun startUpdate(
packageName: String, packageName: String,
installedItem: InstalledItem?, installedItem: InstalledItem?,
products: List<Pair<Product, Repository>>, products: List<Pair<Product, Repository>>,
@ -95,24 +97,27 @@ object Utils {
val productRepository = Product.findSuggested(products, installedItem) { it.first } val productRepository = Product.findSuggested(products, installedItem) { it.first }
val compatibleReleases = productRepository?.first?.selectedReleases.orEmpty() val compatibleReleases = productRepository?.first?.selectedReleases.orEmpty()
.filter { installedItem == null || installedItem.signature == it.signature } .filter { installedItem == null || installedItem.signature == it.signature }
val release = if (compatibleReleases.size >= 2) { val releaseFlow = MutableStateFlow(compatibleReleases.firstOrNull())
compatibleReleases if (compatibleReleases.size > 1) {
.filter { it.platforms.contains(Android.primaryPlatform) } releaseFlow.emit(
.minByOrNull { it.platforms.size } compatibleReleases
?: compatibleReleases.minByOrNull { it.platforms.size } .filter { it.platforms.contains(Android.primaryPlatform) }
?: compatibleReleases.firstOrNull() .minByOrNull { it.platforms.size }
} else { ?: compatibleReleases.minByOrNull { it.platforms.size }
compatibleReleases.firstOrNull() ?: compatibleReleases.firstOrNull()
)
} }
val binder = downloadConnection.binder val binder = downloadConnection.binder
if (productRepository != null && release != null && binder != null) { releaseFlow.collect {
binder.enqueue( if (productRepository != null && it != null && binder != null) {
packageName, binder.enqueue(
productRepository.first.name, packageName,
productRepository.second, productRepository.first.name,
release productRepository.second,
) it
} else Unit )
}
}
} }
fun Context.setLanguage(): Configuration { fun Context.setLanguage(): Configuration {