Skip to content

Commit 212edbf

Browse files
committed
handle while loop better
Signed-off-by: alperozturk <alper_ozturk@proton.me>
1 parent c59a3ea commit 212edbf

1 file changed

Lines changed: 19 additions & 10 deletions

File tree

app/src/main/java/com/nextcloud/client/jobs/offlineOperations/OfflineOperationsWorker.kt

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class OfflineOperationsWorker(
3939

4040
companion object {
4141
private val TAG = OfflineOperationsWorker::class.java.simpleName
42-
const val JOB_NAME = "job_name"
42+
const val JOB_NAME = "JOB_NAME"
4343
}
4444

4545
private val fileDataStorageManager = FileDataStorageManager(user, context.contentResolver)
@@ -67,16 +67,20 @@ class OfflineOperationsWorker(
6767

6868
var operations = fileDataStorageManager.offlineOperationDao.getAll()
6969
val totalOperations = operations.size
70-
var currentOperationIndex = 0
70+
var currentSuccessfulOperationIndex = 0
7171

7272
return@coroutineScope try {
7373
while (operations.isNotEmpty()) {
7474
val operation = operations.first()
7575
val result = executeOperation(operation, client)
76-
handleResult(operation, totalOperations, currentOperationIndex, result?.first, result?.second)
76+
val isSuccess = handleResult(operation, totalOperations, currentSuccessfulOperationIndex, result?.first, result?.second)
7777

78-
currentOperationIndex++
79-
operations = fileDataStorageManager.offlineOperationDao.getAll()
78+
operations = if (isSuccess) {
79+
currentSuccessfulOperationIndex++
80+
fileDataStorageManager.offlineOperationDao.getAll()
81+
} else {
82+
operations.filter { it != operation }
83+
}
8084
}
8185

8286
Log_OC.d(TAG, "OfflineOperationsWorker successfully completed")
@@ -122,25 +126,30 @@ class OfflineOperationsWorker(
122126
private fun handleResult(
123127
operation: OfflineOperationEntity,
124128
totalOperations: Int,
125-
currentOperationIndex: Int,
129+
currentSuccessfulOperationIndex: Int,
126130
result: RemoteOperationResult<*>?,
127-
remoteOperation: RemoteOperation<*>?
128-
) {
129-
result ?: return Log_OC.d(TAG, "Operation not completed, result is null")
131+
remoteOperation: RemoteOperation<*>?,
132+
): Boolean {
133+
if (result == null) {
134+
Log_OC.d(TAG, "Operation not completed, result is null")
135+
return false
136+
}
130137

131138
val logMessage = if (result.isSuccess) "Operation completed" else "Operation failed"
132139
Log_OC.d(TAG, "$logMessage path: ${operation.path}, type: ${operation.type}")
133140

134141
if (result.isSuccess) {
135142
repository.updateNextOperations(operation)
136143
fileDataStorageManager.offlineOperationDao.delete(operation)
137-
notificationManager.update(totalOperations, currentOperationIndex, operation.filename ?: "")
144+
notificationManager.update(totalOperations, currentSuccessfulOperationIndex, operation.filename ?: "")
138145
} else {
139146
val excludedErrorCodes = listOf(RemoteOperationResult.ResultCode.FOLDER_ALREADY_EXISTS)
140147

141148
if (remoteOperation != null && !excludedErrorCodes.contains(result.code)) {
142149
notificationManager.showNewNotification(result, remoteOperation)
143150
}
144151
}
152+
153+
return result.isSuccess
145154
}
146155
}

0 commit comments

Comments
 (0)