Skip to content

Commit c3298e4

Browse files
committed
fixes
Signed-off-by: alperozturk <[email protected]>
1 parent 683223e commit c3298e4

File tree

4 files changed

+59
-51
lines changed

4 files changed

+59
-51
lines changed

app/src/main/java/com/nextcloud/client/database/entity/OfflineOperationEntity.kt

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77

88
package com.nextcloud.client.database.entity
99

10+
import android.content.Context
1011
import androidx.room.ColumnInfo
1112
import androidx.room.Entity
1213
import androidx.room.PrimaryKey
1314
import com.nextcloud.model.OfflineOperationType
15+
import com.owncloud.android.R
1416
import com.owncloud.android.db.ProviderMeta.ProviderTableMeta
1517

1618
@Entity(tableName = ProviderTableMeta.OFFLINE_OPERATION_TABLE_NAME)
@@ -41,7 +43,15 @@ data class OfflineOperationEntity(
4143
return (type is OfflineOperationType.RenameFile || type is OfflineOperationType.RemoveFile)
4244
}
4345

44-
fun isCreate(): Boolean {
45-
return (type is OfflineOperationType.CreateFile || type is OfflineOperationType.CreateFolder)
46+
fun getConflictText(context: Context): String {
47+
return if (type is OfflineOperationType.RemoveFile) {
48+
context.getString(R.string.offline_operations_worker_notification_remove_conflict_text, filename)
49+
} else if (type is OfflineOperationType.RenameFile) {
50+
context.getString(R.string.offline_operations_worker_notification_rename_conflict_text, filename)
51+
} else if (type is OfflineOperationType.CreateFile) {
52+
context.getString(R.string.offline_operations_worker_notification_create_file_conflict_text, filename)
53+
} else {
54+
context.getString(R.string.offline_operations_worker_notification_create_folder_conflict_text, filename)
55+
}
4656
}
4757
}

app/src/main/java/com/nextcloud/client/jobs/offlineOperations/OfflineOperationsNotificationManager.kt

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,23 @@ class OfflineOperationsNotificationManager(private val context: Context, viewThe
7777
}
7878
}
7979

80+
fun showConflictNotificationForDeleteOrRemoveOperation(entity: OfflineOperationEntity?) {
81+
val id = entity?.id
82+
if (id == null) {
83+
return
84+
}
85+
86+
val title = entity.getConflictText(context)
87+
88+
notificationBuilder
89+
.setProgress(0, 0, false)
90+
.setOngoing(false)
91+
.clearActions()
92+
.setContentTitle(title)
93+
94+
notificationManager.notify(id, notificationBuilder.build())
95+
}
96+
8097
fun showConflictResolveNotification(file: OCFile, entity: OfflineOperationEntity?) {
8198
val path = entity?.path
8299
val id = entity?.id
@@ -87,10 +104,7 @@ class OfflineOperationsNotificationManager(private val context: Context, viewThe
87104

88105
val resolveConflictAction = getResolveConflictAction(file, id, path)
89106

90-
val title = context.getString(
91-
R.string.offline_operations_worker_notification_conflict_text,
92-
file.fileName
93-
)
107+
val title = entity.getConflictText(context)
94108

95109
notificationBuilder
96110
.setProgress(0, 0, false)

app/src/main/java/com/nextcloud/client/jobs/offlineOperations/OfflineOperationsWorker.kt

Lines changed: 23 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import com.nextcloud.client.network.ConnectivityService
1818
import com.nextcloud.model.OfflineOperationType
1919
import com.nextcloud.model.WorkerState
2020
import com.nextcloud.model.WorkerStateLiveData
21-
import com.nextcloud.utils.extensions.toOCFile
2221
import com.owncloud.android.datamodel.FileDataStorageManager
2322
import com.owncloud.android.datamodel.OCFile
2423
import com.owncloud.android.lib.common.OwnCloudClient
@@ -31,6 +30,7 @@ import com.owncloud.android.lib.resources.files.UploadFileRemoteOperation
3130
import com.owncloud.android.operations.CreateFolderOperation
3231
import com.owncloud.android.operations.RemoveFileOperation
3332
import com.owncloud.android.operations.RenameFileOperation
33+
import com.owncloud.android.utils.MimeTypeUtil
3434
import com.owncloud.android.utils.theme.ViewThemeUtils
3535
import kotlinx.coroutines.Dispatchers
3636
import kotlinx.coroutines.NonCancellable
@@ -120,29 +120,30 @@ class OfflineOperationsWorker(
120120
client: OwnCloudClient
121121
): OfflineOperationResult? = withContext(Dispatchers.IO) {
122122
val ocFile = fileDataStorageManager.getFileByDecryptedRemotePath(operation.path)
123+
val path = (operation.path)
123124

124-
if (isCreateConflict(operation, ocFile)) {
125-
Log_OC.w(TAG, "Offline operation skipped, file already exists: $operation")
126-
notificationManager.showConflictResolveNotification(ocFile!!, operation)
125+
if (path == null) {
126+
Log_OC.w(TAG, "Offline operation skipped, file path is null: $operation")
127127
return@withContext null
128128
}
129129

130-
if (isNonExistentFileForRenameOrRemove(operation, ocFile)) {
131-
Log_OC.w(TAG, "Offline operation skipped, same file name used for create operation: $operation")
132-
fileDataStorageManager.offlineOperationDao.delete(operation)
133-
return@withContext null
134-
}
130+
if (isRemoteFileExists(path)) {
131+
Log_OC.w(TAG, "Offline operation skipped, file already exists: $operation")
132+
133+
if (operation.isRenameOrRemove()) {
134+
fileDataStorageManager.offlineOperationDao.delete(operation)
135+
notificationManager.showConflictNotificationForDeleteOrRemoveOperation(operation)
136+
} else {
137+
notificationManager.showConflictResolveNotification(ocFile!!, operation)
138+
}
135139

136-
if (isFileChangedForRenameOrRemove(operation, ocFile)) {
137-
Log_OC.w(TAG, "Offline operation skipped, file changed: $operation")
138-
notificationManager.showConflictResolveNotification(ocFile!!, operation)
139140
return@withContext null
140141
}
141142

142143
return@withContext when (val type = operation.type) {
143144
is OfflineOperationType.CreateFolder -> createFolder(operation, client)
144145
is OfflineOperationType.CreateFile -> createFile(operation, client)
145-
is OfflineOperationType.RenameFile -> ocFile?.let { renameFile(operation, it, client) }
146+
is OfflineOperationType.RenameFile -> renameFile(operation, client)
146147
is OfflineOperationType.RemoveFile -> ocFile?.let { removeFile(it, client) }
147148
else -> {
148149
Log_OC.d(TAG, "Unsupported operation type: $type")
@@ -151,19 +152,6 @@ class OfflineOperationsWorker(
151152
}
152153
}
153154

154-
private fun isCreateConflict(operation: OfflineOperationEntity, ocFile: OCFile?): Boolean {
155-
return ocFile != null && ocFile.remoteId != null && (operation.filename == ocFile.fileName)
156-
&& operation.isCreate()
157-
}
158-
159-
private fun isNonExistentFileForRenameOrRemove(operation: OfflineOperationEntity, ocFile: OCFile?): Boolean {
160-
return operation.isRenameOrRemove() && (ocFile == null || ocFile.remoteId == null)
161-
}
162-
163-
private fun isFileChangedForRenameOrRemove(operation: OfflineOperationEntity, ocFile: OCFile?): Boolean {
164-
return operation.isRenameOrRemove() && ocFile?.remoteId != null && isFileChanged(ocFile)
165-
}
166-
167155
@Suppress("DEPRECATION")
168156
private suspend fun createFolder(
169157
operation: OfflineOperationEntity,
@@ -199,20 +187,10 @@ class OfflineOperationsWorker(
199187
}
200188

201189
@Suppress("DEPRECATION")
202-
private suspend fun renameFile(
203-
operation: OfflineOperationEntity,
204-
ocFile: OCFile,
205-
client: OwnCloudClient
206-
): OfflineOperationResult {
207-
val ocFileRemotePath = ocFile.remotePath
208-
if (ocFileRemotePath == null) {
209-
Log_OC.w(TAG, "Rename offline operation is cancelled, remote path is null")
210-
return null
211-
}
212-
190+
private suspend fun renameFile(operation: OfflineOperationEntity, client: OwnCloudClient): OfflineOperationResult {
213191
val renameFileOperation = withContext(NonCancellable) {
214192
val operationType = (operation.type as OfflineOperationType.RenameFile)
215-
RenameFileOperation(ocFileRemotePath, operationType.newName, fileDataStorageManager)
193+
RenameFileOperation(operation.path, operationType.newName, fileDataStorageManager)
216194
}
217195

218196
return renameFileOperation.execute(client) to renameFileOperation
@@ -281,15 +259,16 @@ class OfflineOperationsWorker(
281259
}
282260

283261
@Suppress("DEPRECATION")
284-
private fun isFileChanged(file: OCFile): Boolean {
262+
private fun isRemoteFileExists(remotePath: String): Boolean {
263+
val mimeType = MimeTypeUtil.getMimeTypeFromPath(remotePath)
264+
val isFolder = MimeTypeUtil.isFolder(mimeType)
285265
val client = ClientFactoryImpl(context).create(user)
286-
val remoteFile = if (file.isFolder) {
287-
ReadFolderRemoteOperation(file.remotePath).execute(client)
266+
val remoteFile = if (isFolder) {
267+
ReadFolderRemoteOperation(remotePath).execute(client)
288268
} else {
289-
ReadFileRemoteOperation(file.remotePath).execute(client)
269+
ReadFileRemoteOperation(remotePath).execute(client)
290270
}
291271

292-
val remoteETag = remoteFile.toOCFile()?.get(0)?.etag
293-
return file.etag != remoteETag
272+
return remoteFile.isSuccess
294273
}
295274
}

app/src/main/res/values/strings.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,12 @@
4343
<string name="app_config_proxy_port_title">Proxy Port</string>
4444
<string name="app_config_enforce_protection_title">Enforce Protection</string>
4545
<string name="offline_operations_file_does_not_exists_yet">File does not exists, yet. Please upload the file first.</string>
46-
<string name="offline_operations_worker_notification_conflict_text">Conflicting folder: %s</string>
46+
47+
<string name="offline_operations_worker_notification_remove_conflict_text">Offline operation cancelled. Could not delete %s file exists on server.</string>
48+
<string name="offline_operations_worker_notification_rename_conflict_text">Offline operation cancelled. Could not rename %s file exists on server.</string>
49+
<string name="offline_operations_worker_notification_create_file_conflict_text">Cannot create file: %s already exists on server.</string>
50+
<string name="offline_operations_worker_notification_create_folder_conflict_text">Cannot create folder: %s already exists on server.</string>
51+
4752
<string name="offline_operations_worker_notification_start_text">Starting offline operations</string>
4853
<string name="offline_operations_worker_notification_error_text">The offline operation cannot be completed. %s</string>
4954
<string name="offline_operations_worker_notification_manager_ticker">Offline Operations</string>

0 commit comments

Comments
 (0)