@@ -36,7 +36,8 @@ import com.infomaniak.drive.utils.SyncUtils.syncImmediately
3636import  com.infomaniak.lib.core.utils.SentryLog 
3737import  io.realm.Realm 
3838import  io.sentry.Sentry 
39- import  kotlinx.coroutines.CompletableJob 
39+ import  kotlinx.coroutines.CoroutineScope 
40+ import  kotlinx.coroutines.coroutineScope 
4041import  kotlinx.coroutines.ensureActive 
4142import  java.util.Date 
4243
@@ -48,11 +49,11 @@ object SyncOfflineUtils {
4849
4950    private  val  renameActions =  setOf (FILE_RENAME , FILE_RENAME_ALIAS , FILE_MOVE_OUT )
5051
51-     fun  startSyncOffline (context :  Context ,  syncOfflineFilesJob :   CompletableJob )  {
52+     suspend   fun  startSyncOffline (context :  Context )  =  coroutineScope  {
5253        //  Delete all offline storage files prior to APIv3. For more info, see deleteLegacyOfflineFolder kDoc
5354        deleteLegacyOfflineFolder(context)
5455        DriveInfosController .getDrives(AccountUtils .currentUserId).forEach { drive -> 
55-             syncOfflineFilesJob. ensureActive()
56+             ensureActive()
5657            val  userDrive =  UserDrive (driveId =  drive.id)
5758
5859            FileController .getRealmInstance(userDrive).use { realm -> 
@@ -61,10 +62,10 @@ object SyncOfflineUtils {
6162                //  The api doesn't support sending a list of files that exceeds a certain limit,
6263                //  so we chunk the files in relation to this limit.
6364                localFiles.chunked(API_LIMIT_FILES_ACTION_BODY ).forEach {
64-                     syncOfflineFilesJob. ensureActive()
65+                     ensureActive()
6566                    processChunk(
6667                        context =  context,
67-                         syncOfflineFilesJob  =  syncOfflineFilesJob ,
68+                         coroutineScope  =  this ,
6869                        userDrive =  userDrive,
6970                        localFilesMap =  it.associateBy { file ->  file.id },
7071                        realm =  realm,
@@ -89,7 +90,7 @@ object SyncOfflineUtils {
8990
9091    private  fun  processChunk (
9192        context :  Context ,
92-         syncOfflineFilesJob :   CompletableJob ,
93+         coroutineScope :   CoroutineScope ,
9394        userDrive :  UserDrive ,
9495        localFilesMap :  Map <Int , File >,
9596        realm :  Realm ,
@@ -112,26 +113,26 @@ object SyncOfflineUtils {
112113            val  fileActionsIds =  mutableSetOf<Int >()
113114
114115            lastFilesActions?.forEach { fileAction -> 
115-                 syncOfflineFilesJob .ensureActive()
116+                 coroutineScope .ensureActive()
116117                fileActionsIds.add(fileAction.fileId)
117118                handleFileAction(context, fileAction, localFilesMap, userDrive, realm)
118119            }
119120
120121            //  Check if any of the files that don't have fileActions require synchronization.
121-             handleFilesWithoutActions(context, localFilesMap, fileActionsIds, userDrive, realm, syncOfflineFilesJob )
122+             handleFilesWithoutActions(context, localFilesMap, fileActionsIds, userDrive, realm, coroutineScope )
122123        }
123124    }
124125
125126    private  fun  handleFilesWithoutActions (
126127        context :  Context ,
127128        localFilesMap :  Map <Int , File >,
128-         fileActionsIds :  MutableSet <Int >,
129+         fileActionsIds :  Set <Int >,
129130        userDrive :  UserDrive ,
130131        realm :  Realm ,
131-         syncOfflineFilesJob :   CompletableJob ,
132+         coroutineScope :   CoroutineScope ,
132133    ) {
133134        for  (file in  localFilesMap.values) {
134-             syncOfflineFilesJob .ensureActive()
135+             coroutineScope .ensureActive()
135136            if  (fileActionsIds.contains(file.id)) continue 
136137            val  ioFile =  file.getOfflineFile(context, userDrive.userId) ? :  continue 
137138
0 commit comments