Fix concurrent modification

Signed-off-by: alperozturk <alper_ozturk@proton.me>
This commit is contained in:
alperozturk 2024-09-03 14:01:21 +02:00
parent 7e035847a2
commit da2e083684
No known key found for this signature in database
GPG key ID: 4E577DC593B59BDF
3 changed files with 38 additions and 15 deletions

View file

@ -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<OCFile> =
dao.getAll().map { entity ->
override fun convertToOCFiles(fileId: Long): List<OCFile> =
getAllSubEntities(fileId).map { entity ->
OCFile(entity.path).apply {
mimeType = if (entity.type is OfflineOperationType.CreateFolder) {
MimeType.DIRECTORY

View file

@ -14,5 +14,5 @@ interface OfflineOperationsRepositoryType {
fun getAllSubEntities(fileId: Long): List<OfflineOperationEntity>
fun deleteOperation(file: OCFile)
fun updateNextOperations(operation: OfflineOperationEntity)
fun convertToOCFiles(): List<OCFile>
fun convertToOCFiles(fileId: Long): List<OCFile>
}

View file

@ -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<RecyclerView.ViewHol
mFiles = sortOrder.sortCloudFiles(mFiles);
prepareListOfHiddenFiles();
mergeOCFilesForLivePhoto();
addOfflineOperations();
addOfflineOperations(directory.getFileId());
mFilesAll.clear();
mFilesAll.addAll(mFiles);
currentDirectory = directory;
@ -795,13 +798,30 @@ public class OCFileListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHol
notifyDataSetChanged();
}
private void addOfflineOperations() {
List<OCFile> offlineOperations = mStorageManager.offlineOperationsRepository.convertToOCFiles();
private void addOfflineOperations(long fileId) {
List<OCFile> offlineOperations = mStorageManager.offlineOperationsRepository.convertToOCFiles(fileId);
List<OCFile> filesToAdd = new ArrayList<>();
for (OCFile offlineFile : offlineOperations) {
if (!mFiles.contains(offlineFile)) {
mFiles.add(offlineFile);
boolean shouldAdd = true;
Iterator<OCFile> 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<Object> objects,