diff --git a/app/src/main/java/com/nextcloud/client/database/typeAdapter/OfflineOperationTypeAdapter.kt b/app/src/main/java/com/nextcloud/client/database/typeAdapter/OfflineOperationTypeAdapter.kt index a34c8d7738..32a28eacca 100644 --- a/app/src/main/java/com/nextcloud/client/database/typeAdapter/OfflineOperationTypeAdapter.kt +++ b/app/src/main/java/com/nextcloud/client/database/typeAdapter/OfflineOperationTypeAdapter.kt @@ -40,8 +40,15 @@ class OfflineOperationTypeAdapter : JsonSerializer, JsonDe jsonObject.addProperty("mimeType", src.mimeType) } + is OfflineOperationType.RenameFile -> { + jsonObject.addProperty("type", src.type) + jsonObject.addProperty("ocFileId", src.ocFileId) + jsonObject.addProperty("newName", src.newName) + } + null -> Unit } + return jsonObject } @@ -65,6 +72,12 @@ class OfflineOperationTypeAdapter : JsonSerializer, JsonDe jsonObject.get("mimeType").asString ) + OfflineOperationRawType.RenameFile.name -> OfflineOperationType.RenameFile( + jsonObject.get("type").asString, + jsonObject.get("ocFileId").asLong, + jsonObject.get("newName").asString + ) + else -> null } } diff --git a/app/src/main/java/com/nextcloud/client/jobs/offlineOperations/OfflineOperationsWorker.kt b/app/src/main/java/com/nextcloud/client/jobs/offlineOperations/OfflineOperationsWorker.kt index 606ebe3969..db471b79e2 100644 --- a/app/src/main/java/com/nextcloud/client/jobs/offlineOperations/OfflineOperationsWorker.kt +++ b/app/src/main/java/com/nextcloud/client/jobs/offlineOperations/OfflineOperationsWorker.kt @@ -25,6 +25,7 @@ import com.owncloud.android.lib.common.operations.RemoteOperationResult import com.owncloud.android.lib.common.utils.Log_OC import com.owncloud.android.lib.resources.files.UploadFileRemoteOperation import com.owncloud.android.operations.CreateFolderOperation +import com.owncloud.android.operations.RenameFileOperation import com.owncloud.android.utils.theme.ViewThemeUtils import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.NonCancellable @@ -98,6 +99,7 @@ class OfflineOperationsWorker( Result.success() } catch (e: Exception) { Log_OC.d(TAG, "OfflineOperationsWorker terminated: $e") + notificationManager.dismissNotification() Result.failure() } } @@ -146,6 +148,21 @@ class OfflineOperationsWorker( createFileOperation.execute(client) to createFileOperation } + is OfflineOperationType.RenameFile -> { + val renameFileOperation = withContext(NonCancellable) { + val operationType = (operation.type as OfflineOperationType.RenameFile) + fileDataStorageManager.getFileById(operationType.ocFileId)?.remotePath?.let { updatedRemotePath -> + RenameFileOperation( + updatedRemotePath, + operationType.newName, + fileDataStorageManager + ) + } + } + + renameFileOperation?.execute(client) to renameFileOperation + } + else -> { Log_OC.d(TAG, "Unsupported operation type: ${operation.type}") null diff --git a/app/src/main/java/com/nextcloud/model/OfflineOperationType.kt b/app/src/main/java/com/nextcloud/model/OfflineOperationType.kt index 65bb25b608..028c8ac3dc 100644 --- a/app/src/main/java/com/nextcloud/model/OfflineOperationType.kt +++ b/app/src/main/java/com/nextcloud/model/OfflineOperationType.kt @@ -11,15 +11,23 @@ sealed class OfflineOperationType { abstract val type: String data class CreateFolder(override val type: String, var path: String) : OfflineOperationType() + data class CreateFile( override val type: String, val localPath: String, var remotePath: String, val mimeType: String ) : OfflineOperationType() + + data class RenameFile( + override val type: String, + var ocFileId: Long, + val newName: String + ) : OfflineOperationType() } enum class OfflineOperationRawType { CreateFolder, - CreateFile + CreateFile, + RenameFile } diff --git a/app/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java b/app/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java index 1dbc31ded2..f2f0666a73 100644 --- a/app/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java +++ b/app/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java @@ -217,6 +217,35 @@ public class FileDataStorageManager { offlineOperationsRepository.deleteOperation(file); } + public void addRenameFileOfflineOperation(OCFile file, String newName) { + OfflineOperationEntity entity = new OfflineOperationEntity(); + + entity.setFilename(newName); + entity.setParentOCFileId(file.getParentId()); + + OfflineOperationType operationType = new OfflineOperationType.RenameFile(OfflineOperationRawType.RenameFile.name(), file.getFileId(), newName); + entity.setType(operationType); + entity.setPath(file.getDecryptedRemotePath()); + + long createdAt = System.currentTimeMillis(); + long modificationTimestamp = System.currentTimeMillis(); + + entity.setCreatedAt(createdAt); + entity.setModifiedAt(modificationTimestamp / 1000); + + offlineOperationDao.insert(entity); + } + + public String getFilenameConsideringOfflineOperation(OCFile file) { + String filename = file.getDecryptedFileName(); + OfflineOperationEntity renameEntity = offlineOperationDao.getByPath(file.getDecryptedRemotePath()); + if (renameEntity != null && renameEntity.getType() instanceof OfflineOperationType.RenameFile renameFile) { + filename = renameFile.getNewName(); + } + + return filename; + } + public void renameOfflineOperation(OCFile file, String newFolderName) { var entity = offlineOperationDao.getByPath(file.getDecryptedRemotePath()); if (entity == null) { diff --git a/app/src/main/java/com/owncloud/android/ui/activity/ToolbarActivity.java b/app/src/main/java/com/owncloud/android/ui/activity/ToolbarActivity.java index a0f69612f3..0e56749caa 100644 --- a/app/src/main/java/com/owncloud/android/ui/activity/ToolbarActivity.java +++ b/app/src/main/java/com/owncloud/android/ui/activity/ToolbarActivity.java @@ -142,7 +142,7 @@ public abstract class ToolbarActivity extends BaseActivity implements Injectable String title; boolean isRoot = isRoot(chosenFile); - title = isRoot ? themeUtils.getDefaultDisplayNameForRootFolder(this) : chosenFile.getFileName(); + title = isRoot ? themeUtils.getDefaultDisplayNameForRootFolder(this) : fileDataStorageManager.getFilenameConsideringOfflineOperation(chosenFile); updateActionBarTitleAndHomeButtonByString(title); if (mAppBar != null) { 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 eba470e369..6c290c5548 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 @@ -518,7 +518,7 @@ public class OCFileListAdapter extends RecyclerView.Adapter()?.refreshCurrentDirectory() } else { - (requireActivity() as ComponentsGetter).fileOperationsHelper.renameFile(mTargetFile, newFileName) + typedActivity()?.connectivityService?.isNetworkAndServerAvailable { result -> + if (result) { + typedActivity()?.fileOperationsHelper?.renameFile(mTargetFile, newFileName) + } else { + fileDataStorageManager.addRenameFileOfflineOperation(mTargetFile, newFileName) + typedActivity()?.refreshCurrentDirectory() + } + } } } }