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.OneTimeWorkRequestBuilder
import androidx.work.PeriodicWorkRequest import androidx.work.PeriodicWorkRequest
import androidx.work.PeriodicWorkRequestBuilder import androidx.work.PeriodicWorkRequestBuilder
import androidx.work.WorkInfo
import androidx.work.WorkManager import androidx.work.WorkManager
import com.aurora.extensions.isMAndAbove import com.aurora.extensions.isMAndAbove
import com.aurora.store.AuroraApp import com.aurora.store.AuroraApp
@@ -57,6 +58,11 @@ class UpdateHelper @Inject constructor(
.map { list -> if (!isExtendedUpdateEnabled) list.filter { it.hasValidCert } else list } .map { list -> if (!isExtendedUpdateEnabled) list.filter { it.hasValidCert } else list }
.stateIn(AuroraApp.scope, SharingStarted.WhileSubscribed(), null) .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 * Deletes invalid updates from database and starts observing events
*/ */

View File

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