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,7 +86,9 @@ class MainApplication : Application(), ImageLoaderFactory {
val installedItems =
packageManager.getInstalledPackages(Android.PackageManager.signaturesFlag)
.map { it.toInstalledItem() }
db.installedDao.put(*installedItems.toTypedArray())
CoroutineScope(Dispatchers.Default).launch {
db.installedDao.put(*installedItems.toTypedArray())
}
}
private fun listenPreferences() {

View File

@ -12,7 +12,6 @@ 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
@ -27,23 +26,23 @@ object ProductPreferences {
fun init(context: Context) {
db = DatabaseX.getInstance(context)
preferences = context.getSharedPreferences("product_preferences", Context.MODE_PRIVATE)
db.lockDao.insert(*preferences.all.keys
.mapNotNull { pName ->
this[pName].databaseVersionCode?.let {
Lock().apply {
package_name = pName
version_code = it
CoroutineScope(Dispatchers.Default).launch {
db.lockDao.insert(*preferences.all.keys
.mapNotNull { pName ->
this@ProductPreferences[pName].databaseVersionCode?.let {
Lock().apply {
package_name = pName
version_code = it
}
}
}
}
.toTypedArray()
)
CoroutineScope(Dispatchers.Default).launch {
.toTypedArray()
)
subject.collect { (packageName, versionCode) ->
if (versionCode != null) db.lockDao.insert(Lock().apply {
package_name = packageName
version_code = versionCode
}
package_name = packageName
version_code = versionCode
}
)
else db.lockDao.delete(packageName)
}

View File

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

View File

@ -99,9 +99,11 @@ class SyncService : ConnectionService<SyncService.Binder>() {
}
fun sync(request: SyncRequest) {
val ids = db.repositoryDao.all.mapNotNull { it.trueData }
.asSequence().filter { it.enabled }.map { it.id }.toList()
sync(ids, request)
GlobalScope.launch {
val ids = db.repositoryDao.all.mapNotNull { it.trueData }
.asSequence().filter { it.enabled }.map { it.id }.toList()
sync(ids, request)
}
}
fun sync(repository: Repository) {
@ -332,92 +334,94 @@ class SyncService : ConnectionService<SyncService.Binder>() {
private fun handleNextTask(hasUpdates: Boolean) {
if (currentTask == null) {
if (tasks.isNotEmpty()) {
val task = tasks.removeAt(0)
val repository = db.repositoryDao.get(task.repositoryId)?.trueData
if (repository != null && repository.enabled) {
val lastStarted = started
val newStarted =
if (task.manual || lastStarted == Started.MANUAL) Started.MANUAL else Started.AUTO
started = newStarted
if (newStarted == Started.MANUAL && lastStarted != Started.MANUAL) {
startSelf()
handleSetStarted()
}
val initialState = State.Connecting(repository.name)
publishForegroundState(true, initialState)
val unstable = Preferences[Preferences.Key.UpdateUnstable]
lateinit var disposable: Disposable
disposable = RepositoryUpdater
.update(this, repository, unstable) { stage, progress, total ->
if (!disposable.isDisposed) {
scope.launch {
mutableStateSubject.emit(
State.Syncing(
repository.name,
stage,
progress,
total
GlobalScope.launch {
if (tasks.isNotEmpty()) {
val task = tasks.removeAt(0)
val repository = db.repositoryDao.get(task.repositoryId)?.trueData
if (repository != null && repository.enabled) {
val lastStarted = started
val newStarted =
if (task.manual || lastStarted == Started.MANUAL) Started.MANUAL else Started.AUTO
started = newStarted
if (newStarted == Started.MANUAL && lastStarted != Started.MANUAL) {
startSelf()
handleSetStarted()
}
val initialState = State.Connecting(repository.name)
publishForegroundState(true, initialState)
val unstable = Preferences[Preferences.Key.UpdateUnstable]
lateinit var disposable: Disposable
disposable = RepositoryUpdater
.update(this@SyncService, repository, unstable) { stage, progress, total ->
if (!disposable.isDisposed) {
scope.launch {
mutableStateSubject.emit(
State.Syncing(
repository.name,
stage,
progress,
total
)
)
)
}
}
}
.observeOn(AndroidSchedulers.mainThread())
.subscribe { result, throwable ->
currentTask = null
throwable?.printStackTrace()
if (throwable != null && task.manual) {
showNotificationError(repository, throwable as Exception)
}
handleNextTask(result == true || hasUpdates)
}
currentTask = CurrentTask(task, disposable, hasUpdates, initialState)
} else {
handleNextTask(hasUpdates)
}
} else if (started != Started.NO) {
val disposable = RxUtils
.querySingle { it ->
db.productDao
.query(
installed = true,
updates = true,
searchQuery = "",
section = ProductItem.Section.All,
order = ProductItem.Order.NAME,
signal = it
)
.use {
it.asSequence().map { it.getProductItem() }
.toList()
}
}
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe { result, throwable ->
currentTask = null
throwable?.printStackTrace()
if (throwable != null && task.manual) {
showNotificationError(repository, throwable as Exception)
currentTask = null
handleNextTask(false)
if (result.isNotEmpty()) {
if (Preferences[Preferences.Key.InstallAfterSync])
runAutoUpdate(result)
if (hasUpdates && Preferences[Preferences.Key.UpdateNotify] &&
updateNotificationBlockerFragment?.get()?.isAdded == true
)
displayUpdatesNotification(result)
}
handleNextTask(result == true || hasUpdates)
}
currentTask = CurrentTask(task, disposable, hasUpdates, initialState)
} else {
handleNextTask(hasUpdates)
}
} else if (started != Started.NO) {
val disposable = RxUtils
.querySingle { it ->
db.productDao
.query(
installed = true,
updates = true,
searchQuery = "",
section = ProductItem.Section.All,
order = ProductItem.Order.NAME,
signal = it
)
.use {
it.asSequence().map { it.getProductItem() }
.toList()
}
}
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe { result, throwable ->
throwable?.printStackTrace()
currentTask = null
handleNextTask(false)
if (result.isNotEmpty()) {
if (Preferences[Preferences.Key.InstallAfterSync])
runAutoUpdate(result)
if (hasUpdates && Preferences[Preferences.Key.UpdateNotify] &&
updateNotificationBlockerFragment?.get()?.isAdded == true
)
displayUpdatesNotification(result)
if (hasUpdates) {
currentTask = CurrentTask(null, disposable, true, State.Finishing)
} else {
scope.launch { mutableFinishState.emit(Unit) }
val needStop = started == Started.MANUAL
started = Started.NO
if (needStop) {
stopForeground(true)
stopSelf()
}
}
if (hasUpdates) {
currentTask = CurrentTask(null, disposable, true, State.Finishing)
} else {
scope.launch { mutableFinishState.emit(Unit) }
val needStop = started == Started.MANUAL
started = Started.NO
if (needStop) {
stopForeground(true)
stopSelf()
}
}
}
}

View File

@ -63,7 +63,7 @@ class ExploreFragment : MainNavFragmentX(), CursorOwner.Callback {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
viewModel.fillList(source)
//viewModel.fillList(source)
viewModel.db.repositoryDao.allFlowable
.observeOn(Schedulers.io())
.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?) {
super.onViewCreated(view, savedInstanceState)
viewModel.fillList(source)
//viewModel.fillList(source)
viewModel.db.repositoryDao.allFlowable
.observeOn(Schedulers.io())
.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?) {
super.onViewCreated(view, savedInstanceState)
viewModel.fillList(source)
//viewModel.fillList(source)
viewModel.db.repositoryDao.allFlowable
.observeOn(Schedulers.io())
.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) {
viewModel.setSearchQuery(searchQuery) {
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) {
viewModel.setSection(section) {
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) {
viewModel.setOrder(order) {
if (view != null) {
viewModel.fillList(source)
//viewModel.fillList(source)
}
}
}