瀏覽代碼

Add FileOperationHelper

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

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

@@ -12,12 +12,12 @@ import android.content.Context
 import androidx.core.app.NotificationCompat
 import com.nextcloud.client.database.entity.OfflineOperationEntity
 import com.nextcloud.client.jobs.notification.WorkerNotificationManager
+import com.nextcloud.utils.extensions.getErrorMessage
 import com.owncloud.android.R
 import com.owncloud.android.datamodel.OCFile
 import com.owncloud.android.lib.common.operations.RemoteOperation
 import com.owncloud.android.lib.common.operations.RemoteOperationResult
 import com.owncloud.android.ui.activity.ConflictsResolveActivity
-import com.owncloud.android.utils.ErrorMessageAdapter
 import com.owncloud.android.utils.theme.ViewThemeUtils
 
 class OfflineOperationsNotificationManager(private val context: Context, viewThemeUtils: ViewThemeUtils) :
@@ -67,7 +67,7 @@ class OfflineOperationsNotificationManager(private val context: Context, viewThe
     }
 
     fun showNewNotification(result: RemoteOperationResult<*>, operation: RemoteOperation<*>) {
-        val reason = ErrorMessageAdapter.getErrorCauseMessage(result, operation, context.resources)
+        val reason = (result to operation).getErrorMessage()
         val text = context.getString(R.string.offline_operations_worker_notification_error_text, reason)
 
         notificationBuilder.run {

+ 32 - 16
app/src/main/java/com/nextcloud/client/jobs/operation/FileOperationHelper.kt

@@ -10,6 +10,7 @@ package com.nextcloud.client.jobs.operation
 import android.content.Context
 import com.nextcloud.client.account.User
 import com.nextcloud.client.network.ClientFactoryImpl
+import com.nextcloud.utils.extensions.getErrorMessage
 import com.owncloud.android.datamodel.FileDataStorageManager
 import com.owncloud.android.datamodel.OCFile
 import com.owncloud.android.lib.common.utils.Log_OC
@@ -32,21 +33,36 @@ class FileOperationHelper(
     private val client = clientFactory.create(user)
 
     @Suppress("TooGenericExceptionCaught", "Deprecation")
-    suspend fun removeFile(file: OCFile, onlyLocalCopy: Boolean, inBackground: Boolean): Boolean = coroutineScope {
-        try {
-            async(Dispatchers.IO) {
-                RemoveFileOperation(
-                    file,
-                    onlyLocalCopy,
-                    user,
-                    inBackground,
-                    context,
-                    fileDataStorageManager
-                )
-            }.await().execute(client).isSuccess
-        } catch (e: Exception) {
-            Log_OC.e(TAG, "Error occurred while removing file: $e")
-            false
+    suspend fun removeFile(
+        file: OCFile,
+        onlyLocalCopy: Boolean,
+        inBackground: Boolean
+    ): Boolean =
+        coroutineScope {
+            try {
+                val operation = async(Dispatchers.IO) {
+                    RemoveFileOperation(
+                        file,
+                        onlyLocalCopy,
+                        user,
+                        inBackground,
+                        context,
+                        fileDataStorageManager
+                    )
+                }
+                val operationResult = operation.await()
+                val result = operationResult.execute(client)
+
+                return@coroutineScope if (result.isSuccess) {
+                    true
+                } else {
+                    val reason = (result to operationResult).getErrorMessage()
+                    Log_OC.e(TAG, "Error occurred while removing file: $reason")
+                    false
+                }
+            } catch (e: Exception) {
+                Log_OC.e(TAG, "Error occurred while removing file: $e")
+                false
+            }
         }
-    }
 }

+ 11 - 1
app/src/main/java/com/nextcloud/utils/extensions/RemoteOperationResultExtensions.kt

@@ -8,11 +8,21 @@
 package com.nextcloud.utils.extensions
 
 import com.nextcloud.client.database.entity.OfflineOperationEntity
+import com.owncloud.android.MainApp
+import com.owncloud.android.R
 import com.owncloud.android.datamodel.OCFile
+import com.owncloud.android.lib.common.operations.RemoteOperation
 import com.owncloud.android.lib.common.operations.RemoteOperationResult
 import com.owncloud.android.lib.resources.files.model.RemoteFile
+import com.owncloud.android.utils.ErrorMessageAdapter
 import com.owncloud.android.utils.FileStorageUtils
 
+fun Pair<RemoteOperationResult<*>?, RemoteOperation<*>?>?.getErrorMessage(): String {
+    val result = this?.first ?: return MainApp.string(R.string.unexpected_error_occurred)
+    val operation = this.second ?: return MainApp.string(R.string.unexpected_error_occurred)
+    return ErrorMessageAdapter.getErrorCauseMessage(result, operation, MainApp.getAppContext().resources)
+}
+
 fun RemoteOperationResult<*>?.getConflictedRemoteIdsWithOfflineOperations(
     offlineOperations: List<OfflineOperationEntity>
 ): HashMap<String, String?>? {
@@ -32,7 +42,7 @@ fun RemoteOperationResult<*>?.getConflictedRemoteIdsWithOfflineOperations(
 @Suppress("Deprecation")
 fun RemoteOperationResult<*>?.toOCFile(): List<OCFile>? {
     return if (this?.isSuccess == true) {
-       data?.toOCFileList()
+        data?.toOCFileList()
     } else {
         null
     }

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

@@ -38,7 +38,6 @@ import com.owncloud.android.lib.resources.files.model.RemoteFile
 import com.owncloud.android.ui.dialog.ConflictsResolveDialog
 import com.owncloud.android.ui.dialog.ConflictsResolveDialog.Decision
 import com.owncloud.android.ui.dialog.ConflictsResolveDialog.OnConflictDecisionMadeListener
-import com.owncloud.android.utils.DisplayUtils
 import com.owncloud.android.utils.FileStorageUtils
 import kotlinx.coroutines.Dispatchers
 import kotlinx.coroutines.launch
@@ -144,15 +143,10 @@ class ConflictsResolveActivity : FileActivity(), OnConflictDecisionMadeListener
         offlineOperation ?: return
 
         lifecycleScope.launch {
-            val success = fileOperationHelper.removeFile(serverFile, false, false)
-            if (success) {
+            val isSuccess = fileOperationHelper.removeFile(serverFile, false, false)
+            if (isSuccess) {
                 backgroundJobManager.startOfflineOperations()
                 offlineOperationNotificationManager.dismissNotification(offlineOperation.id)
-            } else {
-                DisplayUtils.showSnackMessage(
-                    this@ConflictsResolveActivity,
-                    R.string.conflict_resolver_activity_keep_offline_folder_error_message
-                )
             }
         }
     }

+ 0 - 1
app/src/main/res/values/strings.xml

@@ -1030,7 +1030,6 @@
     <string name="sync_not_enough_space_dialog_title">Not enough space</string>
     <string name="conflict_file_headline">Conflicting file %1$s</string>
     <string name="conflict_folder_headline">Folder Conflict</string>
-    <string name="conflict_resolver_activity_keep_offline_folder_error_message">Error occurred during deleting the remote file</string>
     <string name="conflict_message_description_for_folder">If you select both versions, the local folder will have a number appended to its name.</string>
     <string name="conflict_message_description">If you select both versions, the local file will have a number appended to its name.</string>
     <string name="conflict_local_file">Local file</string>