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) NotificationUtil.createNotificationChannel(this)
// Initialize Download and Update helpers to observe and trigger downloads // Initialize Download and Update helpers to observe and trigger downloads
scope.launch { downloadHelper.init()
downloadHelper.init() updateHelper.init()
updateHelper.init()
}
//Register broadcast receiver for package install/uninstall //Register broadcast receiver for package install/uninstall
ContextCompat.registerReceiver( 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.data.work.DownloadWorker
import com.aurora.store.util.PathUtil import com.aurora.store.util.PathUtil
import dagger.hilt.android.qualifiers.ApplicationContext import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.SharingStarted import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.filter import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.firstOrNull import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.stateIn import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.withContext import kotlinx.coroutines.launch
import javax.inject.Inject 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. * Removes failed download from the queue and starts observing for newly enqueued apps.
*/ */
suspend fun init() { fun init() {
withContext(Dispatchers.IO) { AuroraApp.scope.launch {
cancelFailedDownloads(downloadDao.downloads().firstOrNull() ?: emptyList()) cancelFailedDownloads(downloadDao.downloads().firstOrNull() ?: emptyList())
}.invokeOnCompletion {
observeDownloads()
} }
observeDownloads()
} }
private suspend fun observeDownloads() { private fun observeDownloads() {
downloadDao.downloads().collect { list -> downloadDao.downloads().onEach { list ->
try { try {
if (list.none { it.downloadStatus == DownloadStatus.DOWNLOADING }) { if (list.none { it.downloadStatus == DownloadStatus.DOWNLOADING }) {
list.find { it.downloadStatus == DownloadStatus.QUEUED } list.find { it.downloadStatus == DownloadStatus.QUEUED }
@@ -70,7 +71,7 @@ class DownloadHelper @Inject constructor(
} catch (exception: Exception) { } catch (exception: Exception) {
Log.e(TAG, "Failed to enqueue download worker", 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.PREFERENCES_UPDATES_RESTRICTIONS_METERED
import com.aurora.store.util.Preferences.PREFERENCE_UPDATES_CHECK_INTERVAL import com.aurora.store.util.Preferences.PREFERENCE_UPDATES_CHECK_INTERVAL
import dagger.hilt.android.qualifiers.ApplicationContext import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.SharingStarted import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.firstOrNull import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.stateIn import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.withContext import kotlinx.coroutines.launch
import java.util.concurrent.TimeUnit.HOURS import java.util.concurrent.TimeUnit.HOURS
import java.util.concurrent.TimeUnit.MINUTES import java.util.concurrent.TimeUnit.MINUTES
import javax.inject.Inject import javax.inject.Inject
@@ -69,12 +68,12 @@ class UpdateHelper @Inject constructor(
/** /**
* Deletes invalid updates from database and starts observing events * Deletes invalid updates from database and starts observing events
*/ */
suspend fun init() { fun init() {
withContext(Dispatchers.IO) { AuroraApp.scope.launch {
deleteInvalidUpdates() deleteInvalidUpdates()
}.invokeOnCompletion {
observeUpdates()
} }
observeUpdates()
} }
private fun observeUpdates() { private fun observeUpdates() {