浏览代码

FilesExportWork: extract method to reduce complexity of doWork

Signed-off-by: Álvaro Brey <alvaro.brey@nextcloud.com>
Álvaro Brey 3 年之前
父节点
当前提交
e400d3c0b8
共有 1 个文件被更改,包括 36 次插入25 次删除
  1. 36 25
      app/src/main/java/com/nextcloud/client/jobs/FilesExportWork.kt

+ 36 - 25
app/src/main/java/com/nextcloud/client/jobs/FilesExportWork.kt

@@ -54,9 +54,8 @@ class FilesExportWork(
     private val themeColorUtils: ThemeColorUtils,
     params: WorkerParameters
 ) : Worker(appContext, params) {
-    companion object {
-        const val FILES_TO_DOWNLOAD = "files_to_download"
-    }
+
+    private lateinit var storageManager: FileDataStorageManager
 
     override fun doWork(): Result {
         val fileIDs = inputData.getLongArray(FILES_TO_DOWNLOAD) ?: LongArray(0)
@@ -66,35 +65,42 @@ class FilesExportWork(
             return Result.success()
         }
 
-        val storageManager = FileDataStorageManager(user, contentResolver)
+        storageManager = FileDataStorageManager(user, contentResolver)
 
-        var successfulExports = 0
-        for (fileID in fileIDs) {
-            val ocFile = storageManager.getFileById(fileID) ?: continue
+        val successfulExports = exportFiles(fileIDs)
 
-            // check if storage is left
-            if (!FileStorageUtils.checkIfEnoughSpace(ocFile)) {
-                showErrorNotification(successfulExports)
-                break
-            }
+        // show notification
+        showSuccessNotification(successfulExports)
 
-            if (ocFile.isDown) {
-                try {
-                    exportFile(ocFile)
-                } catch (e: java.lang.RuntimeException) {
+        return Result.success()
+    }
+
+    private fun exportFiles(fileIDs: LongArray): Int {
+        var successfulExports = 0
+        for (fileID in fileIDs) {
+            val ocFile = storageManager.getFileById(fileID)
+            if (ocFile != null) {
+                // check if storage is left
+                if (!FileStorageUtils.checkIfEnoughSpace(ocFile)) {
                     showErrorNotification(successfulExports)
+                    break
                 }
-            } else {
-                downloadFile(ocFile)
-            }
-
-            successfulExports++
-        }
 
-        // show notification
-        showSuccessNotification(successfulExports)
+                if (ocFile.isDown) {
+                    try {
+                        exportFile(ocFile)
+                    } catch (e: RuntimeException) {
+                        Log_OC.e(TAG, "Error exporting file", e)
+                        showErrorNotification(successfulExports)
+                    }
+                } else {
+                    downloadFile(ocFile)
+                }
 
-        return Result.success()
+                successfulExports++
+            }
+        }
+        return successfulExports
     }
 
     @Throws(IllegalStateException::class)
@@ -172,4 +178,9 @@ class FilesExportWork(
         val notificationManager = appContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
         notificationManager.notify(notificationId, notificationBuilder.build())
     }
+
+    companion object {
+        const val FILES_TO_DOWNLOAD = "files_to_download"
+        private val TAG = FilesExportWork::class.simpleName
+    }
 }