UpdateHelper: Implement logic to see if updates check is ongoing

Signed-off-by: Aayush Gupta <aayushgupta219@gmail.com>
This commit is contained in:
Aayush Gupta
2024-10-21 10:43:08 +05:30
parent 0f1b7f2602
commit b0e19c7446
2 changed files with 8 additions and 15 deletions

View File

@@ -11,6 +11,7 @@ import androidx.work.OutOfQuotaPolicy
import androidx.work.OneTimeWorkRequestBuilder
import androidx.work.PeriodicWorkRequest
import androidx.work.PeriodicWorkRequestBuilder
import androidx.work.WorkInfo
import androidx.work.WorkManager
import com.aurora.extensions.isMAndAbove
import com.aurora.store.AuroraApp
@@ -57,6 +58,11 @@ class UpdateHelper @Inject constructor(
.map { list -> if (!isExtendedUpdateEnabled) list.filter { it.hasValidCert } else list }
.stateIn(AuroraApp.scope, SharingStarted.WhileSubscribed(), null)
val isCheckingUpdates = WorkManager.getInstance(context)
.getWorkInfosForUniqueWorkFlow(EXPEDITED_UPDATE_WORKER)
.map { list -> !list.all { it.state.isFinished } }
.stateIn(AuroraApp.scope, SharingStarted.WhileSubscribed(), false)
/**
* Deletes invalid updates from database and starts observing events
*/

View File

@@ -19,16 +19,12 @@
package com.aurora.store.viewmodel.all
import android.util.Log
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.aurora.store.data.room.update.Update
import com.aurora.store.data.helper.UpdateHelper
import com.aurora.store.data.helper.DownloadHelper
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import javax.inject.Inject
@@ -37,25 +33,16 @@ class UpdatesViewModel @Inject constructor(
private val updateHelper: UpdateHelper,
private val downloadHelper: DownloadHelper,
) : ViewModel() {
private val TAG = UpdatesViewModel::class.java.simpleName
var updateAllEnqueued: Boolean = false
val downloadsList get() = downloadHelper.downloadsList
val updates get() = updateHelper.updates
private val _fetchingUpdates = MutableStateFlow(false)
val fetchingUpdates = _fetchingUpdates.asStateFlow()
val fetchingUpdates = updateHelper.isCheckingUpdates
fun fetchUpdates() {
_fetchingUpdates.value = true
viewModelScope.launch(Dispatchers.IO) {
try {
updateHelper.checkUpdatesNow()
} catch (exception: Exception) {
Log.d(TAG, "Failed to get updates", exception)
}
}.invokeOnCompletion { _fetchingUpdates.value = false }
updateHelper.checkUpdatesNow()
}
fun download(update: Update) {