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 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<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.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<Pair<Product, Repository>>,
@ -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 {