Skip to content

Commit

Permalink
fix: Automatically close sync offlines coroutine (#1553)
Browse files Browse the repository at this point in the history
  • Loading branch information
sirambd authored Mar 11, 2025
2 parents a3c8196 + 92aa311 commit 5cea169
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
11 changes: 5 additions & 6 deletions app/src/main/java/com/infomaniak/drive/ui/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ class MainViewModel(

private var rootFilesJob: Job = Job()
private var getFileDetailsJob = Job()
private var syncOfflineFilesJob = Job()
private var syncOfflineFilesJob: Job? = null
private var setCurrentFolderJob = Job()

val deleteFilesFromGallery = SingleLiveEvent<List<Int>>()
Expand Down Expand Up @@ -520,15 +520,14 @@ class MainViewModel(
}

fun syncOfflineFiles() {
syncOfflineFilesJob.cancel()
syncOfflineFilesJob = Job()
viewModelScope.launch(Dispatchers.IO + syncOfflineFilesJob) {
SyncOfflineUtils.startSyncOffline(getContext(), syncOfflineFilesJob)
syncOfflineFilesJob?.cancel()
syncOfflineFilesJob = viewModelScope.launch(Dispatchers.IO) {
SyncOfflineUtils.startSyncOffline(getContext())
}
}

fun cancelSyncOfflineFiles() {
syncOfflineFilesJob.cancel()
syncOfflineFilesJob?.cancel()
}

// Only for API 29 and below, otherwise use MediaStore.createDeleteRequest()
Expand Down
23 changes: 12 additions & 11 deletions app/src/main/java/com/infomaniak/drive/utils/SyncOfflineUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ import com.infomaniak.drive.utils.SyncUtils.syncImmediately
import com.infomaniak.lib.core.utils.SentryLog
import io.realm.Realm
import io.sentry.Sentry
import kotlinx.coroutines.CompletableJob
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.ensureActive
import java.util.Date

Expand All @@ -48,11 +49,11 @@ object SyncOfflineUtils {

private val renameActions = setOf(FILE_RENAME, FILE_RENAME_ALIAS, FILE_MOVE_OUT)

fun startSyncOffline(context: Context, syncOfflineFilesJob: CompletableJob) {
suspend fun startSyncOffline(context: Context) = coroutineScope {
// Delete all offline storage files prior to APIv3. For more info, see deleteLegacyOfflineFolder kDoc
deleteLegacyOfflineFolder(context)
DriveInfosController.getDrives(AccountUtils.currentUserId).forEach { drive ->
syncOfflineFilesJob.ensureActive()
ensureActive()
val userDrive = UserDrive(driveId = drive.id)

FileController.getRealmInstance(userDrive).use { realm ->
Expand All @@ -61,10 +62,10 @@ object SyncOfflineUtils {
// The api doesn't support sending a list of files that exceeds a certain limit,
// so we chunk the files in relation to this limit.
localFiles.chunked(API_LIMIT_FILES_ACTION_BODY).forEach {
syncOfflineFilesJob.ensureActive()
ensureActive()
processChunk(
context = context,
syncOfflineFilesJob = syncOfflineFilesJob,
coroutineScope = this,
userDrive = userDrive,
localFilesMap = it.associateBy { file -> file.id },
realm = realm,
Expand All @@ -89,7 +90,7 @@ object SyncOfflineUtils {

private fun processChunk(
context: Context,
syncOfflineFilesJob: CompletableJob,
coroutineScope: CoroutineScope,
userDrive: UserDrive,
localFilesMap: Map<Int, File>,
realm: Realm,
Expand All @@ -112,26 +113,26 @@ object SyncOfflineUtils {
val fileActionsIds = mutableSetOf<Int>()

lastFilesActions?.forEach { fileAction ->
syncOfflineFilesJob.ensureActive()
coroutineScope.ensureActive()
fileActionsIds.add(fileAction.fileId)
handleFileAction(context, fileAction, localFilesMap, userDrive, realm)
}

// Check if any of the files that don't have fileActions require synchronization.
handleFilesWithoutActions(context, localFilesMap, fileActionsIds, userDrive, realm, syncOfflineFilesJob)
handleFilesWithoutActions(context, localFilesMap, fileActionsIds, userDrive, realm, coroutineScope)
}
}

private fun handleFilesWithoutActions(
context: Context,
localFilesMap: Map<Int, File>,
fileActionsIds: MutableSet<Int>,
fileActionsIds: Set<Int>,
userDrive: UserDrive,
realm: Realm,
syncOfflineFilesJob: CompletableJob,
coroutineScope: CoroutineScope,
) {
for (file in localFilesMap.values) {
syncOfflineFilesJob.ensureActive()
coroutineScope.ensureActive()
if (fileActionsIds.contains(file.id)) continue
val ioFile = file.getOfflineFile(context, userDrive.userId) ?: continue

Expand Down

0 comments on commit 5cea169

Please sign in to comment.