@@ -36,7 +36,8 @@ import com.infomaniak.drive.utils.SyncUtils.syncImmediately
36
36
import com.infomaniak.lib.core.utils.SentryLog
37
37
import io.realm.Realm
38
38
import io.sentry.Sentry
39
- import kotlinx.coroutines.CompletableJob
39
+ import kotlinx.coroutines.CoroutineScope
40
+ import kotlinx.coroutines.coroutineScope
40
41
import kotlinx.coroutines.ensureActive
41
42
import java.util.Date
42
43
@@ -48,11 +49,11 @@ object SyncOfflineUtils {
48
49
49
50
private val renameActions = setOf (FILE_RENAME , FILE_RENAME_ALIAS , FILE_MOVE_OUT )
50
51
51
- fun startSyncOffline (context : Context , syncOfflineFilesJob : CompletableJob ) {
52
+ suspend fun startSyncOffline (context : Context ) = coroutineScope {
52
53
// Delete all offline storage files prior to APIv3. For more info, see deleteLegacyOfflineFolder kDoc
53
54
deleteLegacyOfflineFolder(context)
54
55
DriveInfosController .getDrives(AccountUtils .currentUserId).forEach { drive ->
55
- syncOfflineFilesJob. ensureActive()
56
+ ensureActive()
56
57
val userDrive = UserDrive (driveId = drive.id)
57
58
58
59
FileController .getRealmInstance(userDrive).use { realm ->
@@ -61,10 +62,10 @@ object SyncOfflineUtils {
61
62
// The api doesn't support sending a list of files that exceeds a certain limit,
62
63
// so we chunk the files in relation to this limit.
63
64
localFiles.chunked(API_LIMIT_FILES_ACTION_BODY ).forEach {
64
- syncOfflineFilesJob. ensureActive()
65
+ ensureActive()
65
66
processChunk(
66
67
context = context,
67
- syncOfflineFilesJob = syncOfflineFilesJob ,
68
+ coroutineScope = this ,
68
69
userDrive = userDrive,
69
70
localFilesMap = it.associateBy { file -> file.id },
70
71
realm = realm,
@@ -89,7 +90,7 @@ object SyncOfflineUtils {
89
90
90
91
private fun processChunk (
91
92
context : Context ,
92
- syncOfflineFilesJob : CompletableJob ,
93
+ coroutineScope : CoroutineScope ,
93
94
userDrive : UserDrive ,
94
95
localFilesMap : Map <Int , File >,
95
96
realm : Realm ,
@@ -112,26 +113,26 @@ object SyncOfflineUtils {
112
113
val fileActionsIds = mutableSetOf<Int >()
113
114
114
115
lastFilesActions?.forEach { fileAction ->
115
- syncOfflineFilesJob .ensureActive()
116
+ coroutineScope .ensureActive()
116
117
fileActionsIds.add(fileAction.fileId)
117
118
handleFileAction(context, fileAction, localFilesMap, userDrive, realm)
118
119
}
119
120
120
121
// 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 )
122
123
}
123
124
}
124
125
125
126
private fun handleFilesWithoutActions (
126
127
context : Context ,
127
128
localFilesMap : Map <Int , File >,
128
- fileActionsIds : MutableSet <Int >,
129
+ fileActionsIds : Set <Int >,
129
130
userDrive : UserDrive ,
130
131
realm : Realm ,
131
- syncOfflineFilesJob : CompletableJob ,
132
+ coroutineScope : CoroutineScope ,
132
133
) {
133
134
for (file in localFilesMap.values) {
134
- syncOfflineFilesJob .ensureActive()
135
+ coroutineScope .ensureActive()
135
136
if (fileActionsIds.contains(file.id)) continue
136
137
val ioFile = file.getOfflineFile(context, userDrive.userId) ? : continue
137
138
0 commit comments