瀏覽代碼

Fix nested folder creation inside the existed folder

Signed-off-by: alperozturk <alper_ozturk@proton.me>
alperozturk 10 月之前
父節點
當前提交
e470b5577d

+ 0 - 3
app/src/main/java/com/nextcloud/client/database/dao/OfflineOperationDao.kt

@@ -36,7 +36,4 @@ interface OfflineOperationDao {
 
     @Query("SELECT * FROM offline_operations WHERE offline_operations_path = :path LIMIT 1")
     fun getByPath(path: String): OfflineOperationEntity?
-
-    @Query("UPDATE offline_operations SET offline_operations_parent_path = :parentPath WHERE _id = :id")
-    fun updateParentPathById(id: Int, parentPath: String)
 }

+ 2 - 6
app/src/main/java/com/nextcloud/client/jobs/offlineOperations/OfflineOperationsWorker.kt

@@ -17,6 +17,7 @@ import com.nextcloud.client.network.ConnectivityService
 import com.nextcloud.model.OfflineOperationType
 import com.nextcloud.model.WorkerState
 import com.nextcloud.model.WorkerStateLiveData
+import com.nextcloud.utils.extensions.updateNextOperationsParentPaths
 import com.owncloud.android.datamodel.FileDataStorageManager
 import com.owncloud.android.lib.common.operations.RemoteOperation
 import com.owncloud.android.lib.common.operations.RemoteOperationResult
@@ -115,12 +116,7 @@ class OfflineOperationsWorker(
 
         if (result.isSuccess) {
             fileDataStorageManager.offlineOperationDao.run {
-                operation.id?.let { id ->
-                    operation.path?.let { path ->
-                        updateParentPathById(operation.id, path)
-                    }
-                }
-
+                updateNextOperationsParentPaths(operation)
                 delete(operation)
             }
 

+ 19 - 8
app/src/main/java/com/nextcloud/utils/extensions/OfflineOperationExtensions.kt

@@ -10,6 +10,8 @@ package com.nextcloud.utils.extensions
 import com.nextcloud.client.database.dao.OfflineOperationDao
 import com.nextcloud.client.database.entity.OfflineOperationEntity
 
+private const val DELIMITER = '/'
+
 fun OfflineOperationDao.updatePathsIfParentPathMatches(oldPath: String?, newTopDir: String?, parentPath: String?) {
     if (oldPath.isNullOrEmpty() || newTopDir.isNullOrEmpty()) return
 
@@ -23,6 +25,17 @@ fun OfflineOperationDao.updatePathsIfParentPathMatches(oldPath: String?, newTopD
     }
 }
 
+fun OfflineOperationDao.updateNextOperationsParentPaths(currentOperation: OfflineOperationEntity) {
+    getAll().forEach { nextOperation ->
+        val nextOperationParentPath = nextOperation.getParentPathFromPath()
+        val currentOperationParentPath = currentOperation.getParentPathFromPath()
+        if (nextOperationParentPath == currentOperationParentPath) {
+            nextOperation.parentPath = currentOperationParentPath
+            update(nextOperation)
+        }
+    }
+}
+
 fun OfflineOperationEntity.updatePathsIfParentPathMatches(oldPath: String?, newTopDir: String?): String? {
     if (oldPath.isNullOrEmpty() || newTopDir.isNullOrEmpty()) return null
 
@@ -38,7 +51,7 @@ fun OfflineOperationEntity.updatePathsIfParentPathMatches(oldPath: String?, newT
 fun OfflineOperationEntity.updatePath(newParentPath: String?): String? {
     if (newParentPath.isNullOrEmpty() || path.isNullOrEmpty()) return null
 
-    val segments = path!!.trim('/').split('/').toMutableList()
+    val segments = path!!.trim(DELIMITER).split(DELIMITER).toMutableList()
 
     if (segments.size == 1) {
         return newParentPath
@@ -46,15 +59,13 @@ fun OfflineOperationEntity.updatePath(newParentPath: String?): String? {
 
     segments.removeAt(0)
 
-    return newParentPath + segments.joinToString(separator = "/") + "/"
+    return newParentPath + segments.joinToString(separator = DELIMITER.toString()) + DELIMITER
 }
 
-fun OfflineOperationEntity.getParentPathFromPath(): String? {
-    return path?.getParentPathFromPath()
-}
+fun OfflineOperationEntity.getParentPathFromPath(): String? = path?.getParentPathFromPath()
 
 private fun String?.getParentPathFromPath(): String {
-    val trimmedPath = this?.trim('/')
-    val firstDir = trimmedPath?.split('/')?.firstOrNull() ?: ""
-    return "/$firstDir/"
+    val trimmedPath = this?.trim(DELIMITER)
+    val firstDir = trimmedPath?.split(DELIMITER)?.firstOrNull() ?: ""
+    return DELIMITER + firstDir + DELIMITER
 }

+ 13 - 0
app/src/main/java/com/owncloud/android/datamodel/OCFile.java

@@ -35,6 +35,7 @@ import com.owncloud.android.utils.MimeType;
 import java.io.File;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Objects;
 
 import androidx.annotation.NonNull;
 import androidx.annotation.Nullable;
@@ -783,6 +784,18 @@ public class OCFile implements Parcelable, Comparable<OCFile>, ServerFileInterfa
         return getRemoteId() == null;
     }
 
+    public String getOfflineOperationParentPath() {
+        if (isOfflineOperation()) {
+            if (Objects.equals(remotePath, OCFile.PATH_SEPARATOR)) {
+                return OCFile.PATH_SEPARATOR;
+            } else {
+                return null;
+            }
+        } else {
+            return getDecryptedRemotePath();
+        }
+    }
+
     public String getEtagInConflict() {
         return this.etagInConflict;
     }