fixup! Audit DownloadHelper & UpdateHelper scopes

Move back both UpdateHelper and DownloadHelper to mainscope as they
are only for observing changes to the data and don't need to be run
on a IO dispatcher

Signed-off-by: Aayush Gupta <aayushgupta219@gmail.com>
This commit is contained in:
Aayush Gupta
2025-03-12 16:06:08 +08:00
parent 38f6bb77fe
commit 2a2a0930c4
3 changed files with 17 additions and 19 deletions

View File

@@ -95,10 +95,8 @@ class AuroraApp : Application(), Configuration.Provider, SingletonImageLoader.Fa
NotificationUtil.createNotificationChannel(this)
// Initialize Download and Update helpers to observe and trigger downloads
scope.launch {
downloadHelper.init()
updateHelper.init()
}
downloadHelper.init()
updateHelper.init()
//Register broadcast receiver for package install/uninstall
ContextCompat.registerReceiver(

View File

@@ -16,12 +16,13 @@ import com.aurora.store.data.room.update.Update
import com.aurora.store.data.work.DownloadWorker
import com.aurora.store.util.PathUtil
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.withContext
import kotlinx.coroutines.launch
import javax.inject.Inject
/**
@@ -49,16 +50,16 @@ class DownloadHelper @Inject constructor(
/**
* Removes failed download from the queue and starts observing for newly enqueued apps.
*/
suspend fun init() {
withContext(Dispatchers.IO) {
fun init() {
AuroraApp.scope.launch {
cancelFailedDownloads(downloadDao.downloads().firstOrNull() ?: emptyList())
}.invokeOnCompletion {
observeDownloads()
}
observeDownloads()
}
private suspend fun observeDownloads() {
downloadDao.downloads().collect { list ->
private fun observeDownloads() {
downloadDao.downloads().onEach { list ->
try {
if (list.none { it.downloadStatus == DownloadStatus.DOWNLOADING }) {
list.find { it.downloadStatus == DownloadStatus.QUEUED }
@@ -70,7 +71,7 @@ class DownloadHelper @Inject constructor(
} catch (exception: Exception) {
Log.e(TAG, "Failed to enqueue download worker", exception)
}
}
}.launchIn(AuroraApp.scope)
}
/**

View File

@@ -25,14 +25,13 @@ import com.aurora.store.util.Preferences.PREFERENCES_UPDATES_RESTRICTIONS_IDLE
import com.aurora.store.util.Preferences.PREFERENCES_UPDATES_RESTRICTIONS_METERED
import com.aurora.store.util.Preferences.PREFERENCE_UPDATES_CHECK_INTERVAL
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.withContext
import kotlinx.coroutines.launch
import java.util.concurrent.TimeUnit.HOURS
import java.util.concurrent.TimeUnit.MINUTES
import javax.inject.Inject
@@ -69,12 +68,12 @@ class UpdateHelper @Inject constructor(
/**
* Deletes invalid updates from database and starts observing events
*/
suspend fun init() {
withContext(Dispatchers.IO) {
fun init() {
AuroraApp.scope.launch {
deleteInvalidUpdates()
}.invokeOnCompletion {
observeUpdates()
}
observeUpdates()
}
private fun observeUpdates() {