mirror of
https://github.com/Aviortheking/Neo-Store.git
synced 2025-06-20 22:29:19 +00:00
Improve: MainScope() is for UI changes
This commit is contained in:
@ -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))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user