mirror of
https://github.com/Aviortheking/Neo-Store.git
synced 2025-04-23 19:32:16 +00:00
Improve: MainScope() is for UI changes
This commit is contained in:
parent
c2d722d385
commit
64a61da973
@ -19,7 +19,8 @@ import com.looker.droidify.service.SyncService
|
||||
import com.looker.droidify.utility.Utils.setLanguage
|
||||
import com.looker.droidify.utility.Utils.toInstalledItem
|
||||
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.launch
|
||||
import java.net.InetSocketAddress
|
||||
@ -89,7 +90,7 @@ class MainApplication : Application(), ImageLoaderFactory {
|
||||
var lastAutoSync = Preferences[Preferences.Key.AutoSync]
|
||||
var lastUpdateUnstable = Preferences[Preferences.Key.UpdateUnstable]
|
||||
var lastLanguage = Preferences[Preferences.Key.Language]
|
||||
MainScope().launch {
|
||||
CoroutineScope(Dispatchers.Default).launch {
|
||||
Preferences.subject.collect {
|
||||
if (it == Preferences.Key.ProxyType || it == Preferences.Key.ProxyHost || it == Preferences.Key.ProxyPort) {
|
||||
updateProxy()
|
||||
|
@ -8,8 +8,8 @@ import com.looker.droidify.Common.PREFS_LANGUAGE_DEFAULT
|
||||
import com.looker.droidify.R
|
||||
import com.looker.droidify.entity.ProductItem
|
||||
import com.looker.droidify.utility.extension.android.Android
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.MainScope
|
||||
import kotlinx.coroutines.flow.MutableSharedFlow
|
||||
import kotlinx.coroutines.flow.asSharedFlow
|
||||
import kotlinx.coroutines.launch
|
||||
@ -41,7 +41,7 @@ object Preferences {
|
||||
context.getSharedPreferences("${context.packageName}_preferences",
|
||||
Context.MODE_PRIVATE)
|
||||
preferences.registerOnSharedPreferenceChangeListener { _, keyString ->
|
||||
MainScope().launch(Dispatchers.IO) {
|
||||
CoroutineScope(Dispatchers.Default).launch {
|
||||
keys[keyString]?.let {
|
||||
_subject.emit(it)
|
||||
}
|
||||
|
@ -7,36 +7,33 @@ import com.looker.droidify.entity.ProductPreference
|
||||
import com.looker.droidify.utility.extension.json.Json
|
||||
import com.looker.droidify.utility.extension.json.parseDictionary
|
||||
import com.looker.droidify.utility.extension.json.writeDictionary
|
||||
import io.reactivex.rxjava3.schedulers.Schedulers
|
||||
import io.reactivex.rxjava3.subjects.PublishSubject
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
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.nio.charset.Charset
|
||||
|
||||
object ProductPreferences {
|
||||
private val defaultProductPreference = ProductPreference(false, 0L)
|
||||
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) {
|
||||
preferences = context.getSharedPreferences("product_preferences", Context.MODE_PRIVATE)
|
||||
Database.LockAdapter.putAll(preferences.all.keys
|
||||
.mapNotNull { packageName ->
|
||||
this[packageName].databaseVersionCode?.let {
|
||||
Pair(
|
||||
packageName,
|
||||
it
|
||||
)
|
||||
}
|
||||
this[packageName].databaseVersionCode?.let { Pair(packageName, it) }
|
||||
})
|
||||
subject
|
||||
.observeOn(Schedulers.io())
|
||||
.subscribe { (packageName, versionCode) ->
|
||||
if (versionCode != null) {
|
||||
Database.LockAdapter.put(Pair(packageName, versionCode))
|
||||
} else {
|
||||
Database.LockAdapter.delete(packageName)
|
||||
}
|
||||
CoroutineScope(Dispatchers.Default).launch {
|
||||
subject.collect { (packageName, versionCode) ->
|
||||
if (versionCode != null) Database.LockAdapter.put(Pair(packageName, versionCode))
|
||||
else Database.LockAdapter.delete(packageName)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private val ProductPreference.databaseVersionCode: Long?
|
||||
@ -71,7 +68,9 @@ object ProductPreferences {
|
||||
if (oldProductPreference.ignoreUpdates != productPreference.ignoreUpdates ||
|
||||
oldProductPreference.ignoreVersionCode != productPreference.ignoreVersionCode
|
||||
) {
|
||||
subject.onNext(Pair(packageName, productPreference.databaseVersionCode))
|
||||
CoroutineScope(Dispatchers.Default).launch {
|
||||
mutableSubject.emit(Pair(packageName, productPreference.databaseVersionCode))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -27,8 +27,7 @@ import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
|
||||
import io.reactivex.rxjava3.core.Observable
|
||||
import io.reactivex.rxjava3.disposables.Disposable
|
||||
import io.reactivex.rxjava3.subjects.PublishSubject
|
||||
import kotlinx.coroutines.MainScope
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.*
|
||||
import java.io.File
|
||||
import java.security.MessageDigest
|
||||
import java.util.concurrent.TimeUnit
|
||||
@ -45,6 +44,8 @@ class DownloadService : ConnectionService<DownloadService.Binder>() {
|
||||
private val downloadingSubject = PublishSubject.create<State.Downloading>()
|
||||
}
|
||||
|
||||
val scope = CoroutineScope(Dispatchers.Default)
|
||||
|
||||
class Receiver : BroadcastReceiver() {
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
val action = intent.action.orEmpty()
|
||||
@ -171,6 +172,7 @@ class DownloadService : ConnectionService<DownloadService.Binder>() {
|
||||
|
||||
downloadingDisposable?.dispose()
|
||||
downloadingDisposable = null
|
||||
scope.cancel()
|
||||
cancelTasks(null)
|
||||
cancelCurrentTask(null)
|
||||
}
|
||||
@ -310,7 +312,7 @@ class DownloadService : ConnectionService<DownloadService.Binder>() {
|
||||
})
|
||||
if (!consumed) {
|
||||
if (rootInstallerEnabled) {
|
||||
MainScope().launch {
|
||||
scope.launch {
|
||||
AppInstaller.getInstance(this@DownloadService)
|
||||
?.defaultInstaller?.install(task.release.cacheFileName)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user