Remove: Allowing queries on MainThread

This commit is contained in:
machiav3lli 2022-01-13 00:53:40 +01:00
parent 251b6195d6
commit d427968ccb
8 changed files with 111 additions and 102 deletions

View File

@ -86,8 +86,10 @@ class MainApplication : Application(), ImageLoaderFactory {
val installedItems = val installedItems =
packageManager.getInstalledPackages(Android.PackageManager.signaturesFlag) packageManager.getInstalledPackages(Android.PackageManager.signaturesFlag)
.map { it.toInstalledItem() } .map { it.toInstalledItem() }
CoroutineScope(Dispatchers.Default).launch {
db.installedDao.put(*installedItems.toTypedArray()) db.installedDao.put(*installedItems.toTypedArray())
} }
}
private fun listenPreferences() { private fun listenPreferences() {
updateProxy() updateProxy()

View File

@ -12,7 +12,6 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.asSharedFlow import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import java.io.ByteArrayOutputStream import java.io.ByteArrayOutputStream
import java.nio.charset.Charset import java.nio.charset.Charset
@ -27,9 +26,10 @@ object ProductPreferences {
fun init(context: Context) { fun init(context: Context) {
db = DatabaseX.getInstance(context) db = DatabaseX.getInstance(context)
preferences = context.getSharedPreferences("product_preferences", Context.MODE_PRIVATE) preferences = context.getSharedPreferences("product_preferences", Context.MODE_PRIVATE)
CoroutineScope(Dispatchers.Default).launch {
db.lockDao.insert(*preferences.all.keys db.lockDao.insert(*preferences.all.keys
.mapNotNull { pName -> .mapNotNull { pName ->
this[pName].databaseVersionCode?.let { this@ProductPreferences[pName].databaseVersionCode?.let {
Lock().apply { Lock().apply {
package_name = pName package_name = pName
version_code = it version_code = it
@ -38,7 +38,6 @@ object ProductPreferences {
} }
.toTypedArray() .toTypedArray()
) )
CoroutineScope(Dispatchers.Default).launch {
subject.collect { (packageName, versionCode) -> subject.collect { (packageName, versionCode) ->
if (versionCode != null) db.lockDao.insert(Lock().apply { if (versionCode != null) db.lockDao.insert(Lock().apply {
package_name = packageName package_name = packageName

View File

@ -6,6 +6,9 @@ import androidx.room.Room
import androidx.room.RoomDatabase import androidx.room.RoomDatabase
import androidx.room.TypeConverters import androidx.room.TypeConverters
import com.looker.droidify.entity.Repository.Companion.defaultRepositories import com.looker.droidify.entity.Repository.Companion.defaultRepositories
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
@Database( @Database(
entities = [ entities = [
@ -42,14 +45,15 @@ abstract class DatabaseX : RoomDatabase() {
"main_database.db" "main_database.db"
) )
.fallbackToDestructiveMigration() .fallbackToDestructiveMigration()
.allowMainThreadQueries()
.build() .build()
INSTANCE?.let { instance -> INSTANCE?.let { instance ->
GlobalScope.launch(Dispatchers.IO) {
if (instance.repositoryDao.count == 0) defaultRepositories.forEach { if (instance.repositoryDao.count == 0) defaultRepositories.forEach {
instance.repositoryDao.put(it) instance.repositoryDao.put(it)
} }
} }
} }
}
return INSTANCE!! return INSTANCE!!
} }
} }

View File

@ -99,10 +99,12 @@ class SyncService : ConnectionService<SyncService.Binder>() {
} }
fun sync(request: SyncRequest) { fun sync(request: SyncRequest) {
GlobalScope.launch {
val ids = db.repositoryDao.all.mapNotNull { it.trueData } val ids = db.repositoryDao.all.mapNotNull { it.trueData }
.asSequence().filter { it.enabled }.map { it.id }.toList() .asSequence().filter { it.enabled }.map { it.id }.toList()
sync(ids, request) sync(ids, request)
} }
}
fun sync(repository: Repository) { fun sync(repository: Repository) {
if (repository.enabled) { if (repository.enabled) {
@ -332,6 +334,7 @@ class SyncService : ConnectionService<SyncService.Binder>() {
private fun handleNextTask(hasUpdates: Boolean) { private fun handleNextTask(hasUpdates: Boolean) {
if (currentTask == null) { if (currentTask == null) {
GlobalScope.launch {
if (tasks.isNotEmpty()) { if (tasks.isNotEmpty()) {
val task = tasks.removeAt(0) val task = tasks.removeAt(0)
val repository = db.repositoryDao.get(task.repositoryId)?.trueData val repository = db.repositoryDao.get(task.repositoryId)?.trueData
@ -349,7 +352,7 @@ class SyncService : ConnectionService<SyncService.Binder>() {
val unstable = Preferences[Preferences.Key.UpdateUnstable] val unstable = Preferences[Preferences.Key.UpdateUnstable]
lateinit var disposable: Disposable lateinit var disposable: Disposable
disposable = RepositoryUpdater disposable = RepositoryUpdater
.update(this, repository, unstable) { stage, progress, total -> .update(this@SyncService, repository, unstable) { stage, progress, total ->
if (!disposable.isDisposed) { if (!disposable.isDisposed) {
scope.launch { scope.launch {
mutableStateSubject.emit( mutableStateSubject.emit(
@ -422,6 +425,7 @@ class SyncService : ConnectionService<SyncService.Binder>() {
} }
} }
} }
}
/** /**
* Performs automatic update after a repo sync if it is enabled. Otherwise, it continues on to * Performs automatic update after a repo sync if it is enabled. Otherwise, it continues on to

View File

@ -63,7 +63,7 @@ class ExploreFragment : MainNavFragmentX(), CursorOwner.Callback {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
viewModel.fillList(source) //viewModel.fillList(source)
viewModel.db.repositoryDao.allFlowable viewModel.db.repositoryDao.allFlowable
.observeOn(Schedulers.io()) .observeOn(Schedulers.io())
.flatMapSingle { list -> RxUtils.querySingle { list.mapNotNull { it.trueData } } } .flatMapSingle { list -> RxUtils.querySingle { list.mapNotNull { it.trueData } } }

View File

@ -80,7 +80,7 @@ class InstalledFragment : MainNavFragmentX(), CursorOwner.Callback {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
viewModel.fillList(source) //viewModel.fillList(source)
viewModel.db.repositoryDao.allFlowable viewModel.db.repositoryDao.allFlowable
.observeOn(Schedulers.io()) .observeOn(Schedulers.io())
.flatMapSingle { list -> RxUtils.querySingle { list.mapNotNull { it.trueData } } } .flatMapSingle { list -> RxUtils.querySingle { list.mapNotNull { it.trueData } } }

View File

@ -80,7 +80,7 @@ class LatestFragment : MainNavFragmentX(), CursorOwner.Callback {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
viewModel.fillList(source) //viewModel.fillList(source)
viewModel.db.repositoryDao.allFlowable viewModel.db.repositoryDao.allFlowable
.observeOn(Schedulers.io()) .observeOn(Schedulers.io())
.flatMapSingle { list -> RxUtils.querySingle { list.mapNotNull { it.trueData } } } .flatMapSingle { list -> RxUtils.querySingle { list.mapNotNull { it.trueData } } }

View File

@ -18,7 +18,7 @@ abstract class MainNavFragmentX : Fragment(), CursorOwner.Callback {
internal fun setSearchQuery(searchQuery: String) { internal fun setSearchQuery(searchQuery: String) {
viewModel.setSearchQuery(searchQuery) { viewModel.setSearchQuery(searchQuery) {
if (view != null) { if (view != null) {
viewModel.fillList(source) //viewModel.fillList(source)
} }
} }
} }
@ -26,7 +26,7 @@ abstract class MainNavFragmentX : Fragment(), CursorOwner.Callback {
internal fun setSection(section: ProductItem.Section) { internal fun setSection(section: ProductItem.Section) {
viewModel.setSection(section) { viewModel.setSection(section) {
if (view != null) { if (view != null) {
viewModel.fillList(source) //viewModel.fillList(source)
} }
} }
} }
@ -34,7 +34,7 @@ abstract class MainNavFragmentX : Fragment(), CursorOwner.Callback {
internal fun setOrder(order: ProductItem.Order) { internal fun setOrder(order: ProductItem.Order) {
viewModel.setOrder(order) { viewModel.setOrder(order) {
if (view != null) { if (view != null) {
viewModel.fillList(source) //viewModel.fillList(source)
} }
} }
} }