Skip to content

Commit

Permalink
handle while loop better
Browse files Browse the repository at this point in the history
Signed-off-by: alperozturk <[email protected]>
  • Loading branch information
alperozturk96 committed Sep 2, 2024
1 parent c42ccaf commit 723177e
Showing 1 changed file with 19 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class OfflineOperationsWorker(

companion object {
private val TAG = OfflineOperationsWorker::class.java.simpleName
const val JOB_NAME = "job_name"
const val JOB_NAME = "JOB_NAME"
}

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

var operations = fileDataStorageManager.offlineOperationDao.getAll()
val totalOperations = operations.size
var currentOperationIndex = 0
var currentSuccessfulOperationIndex = 0

return@coroutineScope try {
while (operations.isNotEmpty()) {
val operation = operations.first()
val result = executeOperation(operation, client)
handleResult(operation, totalOperations, currentOperationIndex, result?.first, result?.second)
val isSuccess = handleResult(operation, totalOperations, currentSuccessfulOperationIndex, result?.first, result?.second)

currentOperationIndex++
operations = fileDataStorageManager.offlineOperationDao.getAll()
operations = if (isSuccess) {
currentSuccessfulOperationIndex++
fileDataStorageManager.offlineOperationDao.getAll()
} else {
operations.filter { it != operation }
}
}

Log_OC.d(TAG, "OfflineOperationsWorker successfully completed")
Expand Down Expand Up @@ -122,25 +126,30 @@ class OfflineOperationsWorker(
private fun handleResult(
operation: OfflineOperationEntity,
totalOperations: Int,
currentOperationIndex: Int,
currentSuccessfulOperationIndex: Int,
result: RemoteOperationResult<*>?,
remoteOperation: RemoteOperation<*>?
) {
result ?: return Log_OC.d(TAG, "Operation not completed, result is null")
remoteOperation: RemoteOperation<*>?,
): Boolean {
if (result == null) {
Log_OC.d(TAG, "Operation not completed, result is null")
return false
}

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

if (result.isSuccess) {
repository.updateNextOperations(operation)
fileDataStorageManager.offlineOperationDao.delete(operation)
notificationManager.update(totalOperations, currentOperationIndex, operation.filename ?: "")
notificationManager.update(totalOperations, currentSuccessfulOperationIndex, operation.filename ?: "")
} else {
val excludedErrorCodes = listOf(RemoteOperationResult.ResultCode.FOLDER_ALREADY_EXISTS)

if (remoteOperation != null && !excludedErrorCodes.contains(result.code)) {
notificationManager.showNewNotification(result, remoteOperation)
}
}

return result.isSuccess
}
}

0 comments on commit 723177e

Please sign in to comment.