Improve: MainScope() is for UI changes

This commit is contained in:
LooKeR 2021-11-23 11:39:36 +05:30
parent c2d722d385
commit 64a61da973
4 changed files with 27 additions and 25 deletions

View File

@ -19,7 +19,8 @@ import com.looker.droidify.service.SyncService
import com.looker.droidify.utility.Utils.setLanguage import com.looker.droidify.utility.Utils.setLanguage
import com.looker.droidify.utility.Utils.toInstalledItem import com.looker.droidify.utility.Utils.toInstalledItem
import com.looker.droidify.utility.extension.android.Android import com.looker.droidify.utility.extension.android.Android
import kotlinx.coroutines.MainScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.collect import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import java.net.InetSocketAddress import java.net.InetSocketAddress
@ -89,7 +90,7 @@ class MainApplication : Application(), ImageLoaderFactory {
var lastAutoSync = Preferences[Preferences.Key.AutoSync] var lastAutoSync = Preferences[Preferences.Key.AutoSync]
var lastUpdateUnstable = Preferences[Preferences.Key.UpdateUnstable] var lastUpdateUnstable = Preferences[Preferences.Key.UpdateUnstable]
var lastLanguage = Preferences[Preferences.Key.Language] var lastLanguage = Preferences[Preferences.Key.Language]
MainScope().launch { CoroutineScope(Dispatchers.Default).launch {
Preferences.subject.collect { Preferences.subject.collect {
if (it == Preferences.Key.ProxyType || it == Preferences.Key.ProxyHost || it == Preferences.Key.ProxyPort) { if (it == Preferences.Key.ProxyType || it == Preferences.Key.ProxyHost || it == Preferences.Key.ProxyPort) {
updateProxy() updateProxy()

View File

@ -8,8 +8,8 @@ import com.looker.droidify.Common.PREFS_LANGUAGE_DEFAULT
import com.looker.droidify.R import com.looker.droidify.R
import com.looker.droidify.entity.ProductItem import com.looker.droidify.entity.ProductItem
import com.looker.droidify.utility.extension.android.Android import com.looker.droidify.utility.extension.android.Android
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.asSharedFlow import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@ -41,7 +41,7 @@ object Preferences {
context.getSharedPreferences("${context.packageName}_preferences", context.getSharedPreferences("${context.packageName}_preferences",
Context.MODE_PRIVATE) Context.MODE_PRIVATE)
preferences.registerOnSharedPreferenceChangeListener { _, keyString -> preferences.registerOnSharedPreferenceChangeListener { _, keyString ->
MainScope().launch(Dispatchers.IO) { CoroutineScope(Dispatchers.Default).launch {
keys[keyString]?.let { keys[keyString]?.let {
_subject.emit(it) _subject.emit(it)
} }

View File

@ -7,34 +7,31 @@ import com.looker.droidify.entity.ProductPreference
import com.looker.droidify.utility.extension.json.Json import com.looker.droidify.utility.extension.json.Json
import com.looker.droidify.utility.extension.json.parseDictionary import com.looker.droidify.utility.extension.json.parseDictionary
import com.looker.droidify.utility.extension.json.writeDictionary import com.looker.droidify.utility.extension.json.writeDictionary
import io.reactivex.rxjava3.schedulers.Schedulers import kotlinx.coroutines.CoroutineScope
import io.reactivex.rxjava3.subjects.PublishSubject import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import java.io.ByteArrayOutputStream import java.io.ByteArrayOutputStream
import java.nio.charset.Charset import java.nio.charset.Charset
object ProductPreferences { object ProductPreferences {
private val defaultProductPreference = ProductPreference(false, 0L) private val defaultProductPreference = ProductPreference(false, 0L)
private lateinit var preferences: SharedPreferences private lateinit var preferences: SharedPreferences
private val subject = PublishSubject.create<Pair<String, Long?>>() private val mutableSubject = MutableSharedFlow<Pair<String, Long?>>()
private val subject = mutableSubject.asSharedFlow()
fun init(context: Context) { fun init(context: Context) {
preferences = context.getSharedPreferences("product_preferences", Context.MODE_PRIVATE) preferences = context.getSharedPreferences("product_preferences", Context.MODE_PRIVATE)
Database.LockAdapter.putAll(preferences.all.keys Database.LockAdapter.putAll(preferences.all.keys
.mapNotNull { packageName -> .mapNotNull { packageName ->
this[packageName].databaseVersionCode?.let { this[packageName].databaseVersionCode?.let { Pair(packageName, it) }
Pair(
packageName,
it
)
}
}) })
subject CoroutineScope(Dispatchers.Default).launch {
.observeOn(Schedulers.io()) subject.collect { (packageName, versionCode) ->
.subscribe { (packageName, versionCode) -> if (versionCode != null) Database.LockAdapter.put(Pair(packageName, versionCode))
if (versionCode != null) { else Database.LockAdapter.delete(packageName)
Database.LockAdapter.put(Pair(packageName, versionCode))
} else {
Database.LockAdapter.delete(packageName)
} }
} }
} }
@ -71,7 +68,9 @@ object ProductPreferences {
if (oldProductPreference.ignoreUpdates != productPreference.ignoreUpdates || if (oldProductPreference.ignoreUpdates != productPreference.ignoreUpdates ||
oldProductPreference.ignoreVersionCode != productPreference.ignoreVersionCode oldProductPreference.ignoreVersionCode != productPreference.ignoreVersionCode
) { ) {
subject.onNext(Pair(packageName, productPreference.databaseVersionCode)) CoroutineScope(Dispatchers.Default).launch {
mutableSubject.emit(Pair(packageName, productPreference.databaseVersionCode))
}
} }
} }
} }

View File

@ -27,8 +27,7 @@ import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.core.Observable import io.reactivex.rxjava3.core.Observable
import io.reactivex.rxjava3.disposables.Disposable import io.reactivex.rxjava3.disposables.Disposable
import io.reactivex.rxjava3.subjects.PublishSubject import io.reactivex.rxjava3.subjects.PublishSubject
import kotlinx.coroutines.MainScope import kotlinx.coroutines.*
import kotlinx.coroutines.launch
import java.io.File import java.io.File
import java.security.MessageDigest import java.security.MessageDigest
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
@ -45,6 +44,8 @@ class DownloadService : ConnectionService<DownloadService.Binder>() {
private val downloadingSubject = PublishSubject.create<State.Downloading>() private val downloadingSubject = PublishSubject.create<State.Downloading>()
} }
val scope = CoroutineScope(Dispatchers.Default)
class Receiver : BroadcastReceiver() { class Receiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) { override fun onReceive(context: Context, intent: Intent) {
val action = intent.action.orEmpty() val action = intent.action.orEmpty()
@ -171,6 +172,7 @@ class DownloadService : ConnectionService<DownloadService.Binder>() {
downloadingDisposable?.dispose() downloadingDisposable?.dispose()
downloadingDisposable = null downloadingDisposable = null
scope.cancel()
cancelTasks(null) cancelTasks(null)
cancelCurrentTask(null) cancelCurrentTask(null)
} }
@ -310,7 +312,7 @@ class DownloadService : ConnectionService<DownloadService.Binder>() {
}) })
if (!consumed) { if (!consumed) {
if (rootInstallerEnabled) { if (rootInstallerEnabled) {
MainScope().launch { scope.launch {
AppInstaller.getInstance(this@DownloadService) AppInstaller.getInstance(this@DownloadService)
?.defaultInstaller?.install(task.release.cacheFileName) ?.defaultInstaller?.install(task.release.cacheFileName)
} }