From da2e083684851ea04efedde16178f14be1f4abf1 Mon Sep 17 00:00:00 2001 From: alperozturk Date: Tue, 3 Sep 2024 14:01:21 +0200 Subject: [PATCH] Fix concurrent modification Signed-off-by: alperozturk --- .../repository/OfflineOperationsRepository.kt | 21 +++++++------ .../OfflineOperationsRepositoryType.kt | 2 +- .../android/ui/adapter/OCFileListAdapter.java | 30 +++++++++++++++---- 3 files changed, 38 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/com/nextcloud/client/jobs/offlineOperations/repository/OfflineOperationsRepository.kt b/app/src/main/java/com/nextcloud/client/jobs/offlineOperations/repository/OfflineOperationsRepository.kt index 47767683c3..d6c3793aa2 100644 --- a/app/src/main/java/com/nextcloud/client/jobs/offlineOperations/repository/OfflineOperationsRepository.kt +++ b/app/src/main/java/com/nextcloud/client/jobs/offlineOperations/repository/OfflineOperationsRepository.kt @@ -79,14 +79,17 @@ class OfflineOperationsRepository( if (newParentPath != nextOperation.parentPath || newPath != nextOperation.path) { nextOperation.apply { - if (type is OfflineOperationType.CreateFile) { - val updatedType = type as OfflineOperationType.CreateFile - updatedType.remotePath = newPath - } else if (type is OfflineOperationType.CreateFolder) { - val updatedType = type as OfflineOperationType.CreateFolder - updatedType.path = newPath - } + type = when (type) { + is OfflineOperationType.CreateFile -> (type as OfflineOperationType.CreateFile).copy( + remotePath = newPath + ) + is OfflineOperationType.CreateFolder -> (type as OfflineOperationType.CreateFolder).copy( + path = newPath + ) + + else -> type + } parentPath = newParentPath path = newPath } @@ -100,8 +103,8 @@ class OfflineOperationsRepository( .forEach { dao.update(it) } } - override fun convertToOCFiles(): List = - dao.getAll().map { entity -> + override fun convertToOCFiles(fileId: Long): List = + getAllSubEntities(fileId).map { entity -> OCFile(entity.path).apply { mimeType = if (entity.type is OfflineOperationType.CreateFolder) { MimeType.DIRECTORY diff --git a/app/src/main/java/com/nextcloud/client/jobs/offlineOperations/repository/OfflineOperationsRepositoryType.kt b/app/src/main/java/com/nextcloud/client/jobs/offlineOperations/repository/OfflineOperationsRepositoryType.kt index 1c9048fc8b..b6509093fa 100644 --- a/app/src/main/java/com/nextcloud/client/jobs/offlineOperations/repository/OfflineOperationsRepositoryType.kt +++ b/app/src/main/java/com/nextcloud/client/jobs/offlineOperations/repository/OfflineOperationsRepositoryType.kt @@ -14,5 +14,5 @@ interface OfflineOperationsRepositoryType { fun getAllSubEntities(fileId: Long): List fun deleteOperation(file: OCFile) fun updateNextOperations(operation: OfflineOperationEntity) - fun convertToOCFiles(): List + fun convertToOCFiles(fileId: Long): List } diff --git a/app/src/main/java/com/owncloud/android/ui/adapter/OCFileListAdapter.java b/app/src/main/java/com/owncloud/android/ui/adapter/OCFileListAdapter.java index 0068d3d39f..13f5bf4fd3 100644 --- a/app/src/main/java/com/owncloud/android/ui/adapter/OCFileListAdapter.java +++ b/app/src/main/java/com/owncloud/android/ui/adapter/OCFileListAdapter.java @@ -31,6 +31,7 @@ import android.widget.LinearLayout; import com.elyeproj.loaderviewlibrary.LoaderImageView; import com.nextcloud.android.common.ui.theme.utils.ColorRole; import com.nextcloud.client.account.User; +import com.nextcloud.client.database.entity.OfflineOperationEntity; import com.nextcloud.client.jobs.upload.FileUploadHelper; import com.nextcloud.client.preferences.AppPreferences; import com.nextcloud.model.OCFileFilterType; @@ -79,8 +80,10 @@ import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Collections; import java.util.Date; +import java.util.Iterator; import java.util.List; import java.util.Locale; +import java.util.Objects; import java.util.Set; import java.util.UUID; @@ -781,7 +784,7 @@ public class OCFileListAdapter extends RecyclerView.Adapter offlineOperations = mStorageManager.offlineOperationsRepository.convertToOCFiles(); + private void addOfflineOperations(long fileId) { + List offlineOperations = mStorageManager.offlineOperationsRepository.convertToOCFiles(fileId); + List filesToAdd = new ArrayList<>(); + for (OCFile offlineFile : offlineOperations) { - if (!mFiles.contains(offlineFile)) { - mFiles.add(offlineFile); + boolean shouldAdd = true; + Iterator iterator = mFiles.iterator(); + + if (iterator.hasNext()) { + do { + OCFile file = iterator.next(); + if (Objects.equals(file.getDecryptedRemotePath(), offlineFile.getDecryptedRemotePath())) { + shouldAdd = false; + break; + } + } while (iterator.hasNext()); + } + + if (shouldAdd) { + filesToAdd.add(offlineFile); } } + + mFiles.addAll(filesToAdd); } public void setData(List objects,