Bläddra i källkod

Fix nested folder creation

Signed-off-by: alperozturk <alper_ozturk@proton.me>
alperozturk 10 månader sedan
förälder
incheckning
0a87eeba88

+ 3 - 0
app/src/main/java/com/nextcloud/client/database/entity/OfflineOperationEntity.kt

@@ -22,6 +22,9 @@ data class OfflineOperationEntity(
     @ColumnInfo(name = ProviderTableMeta.OFFLINE_OPERATION_PARENT_OC_FILE_ID)
     var parentOCFileId: Long? = null,
 
+    @ColumnInfo(name = ProviderTableMeta.OFFLINE_OPERATION_PARENT_PATH)
+    var parentPath: String? = null,
+
     @ColumnInfo(name = ProviderTableMeta.OFFLINE_OPERATION_TYPE)
     var type: OfflineOperationType? = null,
 

+ 39 - 23
app/src/main/java/com/nextcloud/client/jobs/offlineOperations/OfflineOperationsWorker.kt

@@ -66,37 +66,33 @@ class OfflineOperationsWorker(
         }
 
         val client = clientFactory.create(user)
-        val offlineOperations = fileDataStorageManager.offlineOperationDao.getAll()
+        val operations = fileDataStorageManager.offlineOperationDao.getAll()
 
         notificationManager.start()
 
-        offlineOperations.forEachIndexed { index, operation ->
+        operations.forEachIndexed { index, operation ->
             val result = try {
-                when (operation.type) {
-                    OfflineOperationType.CreateFolder -> {
-                        val createFolderOperation = async(Dispatchers.IO) {
-                            CreateFolderOperation(
-                                operation.path,
-                                user,
-                                context,
-                                fileDataStorageManager
-                            )
-                        }.await()
-
-                        createFolderOperation.execute(client) to createFolderOperation
-                    }
-
-                    else -> {
-                        Log_OC.d(TAG, "Operation terminated, not supported operation type")
-                        null
-                    }
+                if (operation.type == OfflineOperationType.CreateFolder && operation.parentPath != null) {
+                    val createFolderOperation = async(Dispatchers.IO) {
+                        CreateFolderOperation(
+                            operation.path,
+                            user,
+                            context,
+                            fileDataStorageManager
+                        )
+                    }.await()
+
+                    createFolderOperation.execute(client) to createFolderOperation
+                } else {
+                    Log_OC.d(TAG, "Operation terminated, not supported or incomplete operation")
+                    null
                 }
             } catch (e: Exception) {
                 Log_OC.d(TAG, "Operation terminated, exception caught: $e")
                 null
             }
 
-            handleResult(operation, offlineOperations.size, index, result?.first, result?.second)
+            handleResult(operation, operations, index, result?.first, result?.second)
         }
 
         Log_OC.d(TAG, "OfflineOperationsWorker successfully completed")
@@ -107,7 +103,7 @@ class OfflineOperationsWorker(
 
     private fun handleResult(
         operation: OfflineOperationEntity,
-        operationSize: Int,
+        operations: List<OfflineOperationEntity>,
         currentOperationIndex: Int,
         result: RemoteOperationResult<*>?,
         remoteOperation: RemoteOperation<*>?
@@ -118,8 +114,9 @@ class OfflineOperationsWorker(
         Log_OC.d(TAG, "$logMessage path: ${operation.path}, type: ${operation.type}")
 
         if (result.isSuccess) {
+            updateNextOperationsParentPaths(operations, operation)
             fileDataStorageManager.offlineOperationDao.delete(operation)
-            notificationManager.update(operationSize, currentOperationIndex, operation.filename ?: "")
+            notificationManager.update(operations.size, currentOperationIndex, operation.filename ?: "")
         } else {
             val excludedErrorCodes = listOf(RemoteOperationResult.ResultCode.FOLDER_ALREADY_EXISTS)
 
@@ -128,4 +125,23 @@ class OfflineOperationsWorker(
             }
         }
     }
+
+    private fun updateNextOperationsParentPaths(
+        operations: List<OfflineOperationEntity>,
+        currentOperation: OfflineOperationEntity
+    ) {
+        operations.forEach { nextOperation ->
+            val nextOperationParentPath = getParentPath(nextOperation.path ?: "")
+            if (nextOperationParentPath == currentOperation.path) {
+                nextOperation.parentPath = currentOperation.path
+                fileDataStorageManager.offlineOperationDao.update(nextOperation)
+            }
+        }
+    }
+
+    private fun getParentPath(path: String): String {
+        val trimmedPath = path.trim('/')
+        val firstDir = trimmedPath.split('/').firstOrNull() ?: ""
+        return "/$firstDir/"
+    }
 }

+ 0 - 41
app/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java

@@ -68,7 +68,6 @@ import org.apache.commons.io.FileUtils;
 
 import java.io.File;
 import java.io.IOException;
-import java.text.SimpleDateFormat;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
@@ -217,46 +216,6 @@ public class FileDataStorageManager {
         saveFileWithParent(file, MainApp.getAppContext());
     }
 
-    public void deleteOfflineOperation(OCFile file) {
-        offlineOperationDao.deleteByPath(file.getDecryptedRemotePath());
-        removeFile(file, true, true);
-    }
-
-    public void renameCreateFolderOfflineOperation(OCFile file, String newFolderName) {
-        deleteOfflineOperation(file);
-
-        OCFile parentFolder = getFileById(file.getParentId());
-        if (parentFolder == null) {
-            return;
-        }
-
-        String newPath = parentFolder.getDecryptedRemotePath() + newFolderName + OCFile.PATH_SEPARATOR;
-        addCreateFolderOfflineOperation(newPath, newFolderName, file.getParentId());
-    }
-
-    @SuppressLint("SimpleDateFormat")
-    public void keepOfflineOperationAndServerFile(OfflineOperationEntity entity) {
-        Long parentOCFileId = entity.getParentOCFileId();
-        if (parentOCFileId == null) return;
-
-        OCFile parentFolder = getFileById(parentOCFileId);
-        if (parentFolder == null) return;
-
-        SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy - HH:mm");
-        String currentDateTime = sdf.format(new Date());
-
-        String newFolderName = entity.getFilename() + " - " + currentDateTime;
-        String newPath = parentFolder.getDecryptedRemotePath() + newFolderName + OCFile.PATH_SEPARATOR;
-        entity.setPath(newPath);
-        entity.setFilename(newFolderName);
-
-        offlineOperationDao.update(entity);
-
-        OCFile file = new OCFile(entity.getPath());
-        file.setMimeType(MimeType.DIRECTORY);
-        saveFileWithParent(file, MainApp.getAppContext());
-    }
-
     private @Nullable
     OCFile getFileByPath(String type, String path) {
         final boolean shouldUseEncryptedPath = ProviderTableMeta.FILE_PATH.equals(type);

+ 1 - 1
app/src/main/java/com/owncloud/android/ui/activity/ConflictsResolveActivity.kt

@@ -40,6 +40,7 @@ import com.owncloud.android.ui.dialog.ConflictsResolveDialog.Decision
 import com.owncloud.android.ui.dialog.ConflictsResolveDialog.OnConflictDecisionMadeListener
 import com.owncloud.android.utils.FileStorageUtils
 import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.delay
 import kotlinx.coroutines.launch
 import javax.inject.Inject
 
@@ -115,7 +116,6 @@ class ConflictsResolveActivity : FileActivity(), OnConflictDecisionMadeListener
                 Decision.KEEP_OFFLINE_FOLDER -> keepOfflineFolder(newFile, offlineOperation)
                 Decision.KEEP_SERVER_FOLDER -> keepServerFile(offlineOperation)
                 Decision.KEEP_BOTH_FOLDER -> keepBothFolder(offlineOperation)
-                Decision.CANCEL -> Unit
                 else -> Unit
             }