Эх сурвалжийг харах

Fix code analytics

Signed-off-by: alperozturk <alper_ozturk@proton.me>
alperozturk 1 жил өмнө
parent
commit
7bfe1ae164

+ 47 - 33
app/src/main/java/com/nextcloud/client/files/downloader/DownloadNotificationManager.kt

@@ -32,6 +32,7 @@ import androidx.core.app.NotificationCompat
 import com.nextcloud.client.account.User
 import com.owncloud.android.R
 import com.owncloud.android.authentication.AuthenticatorActivity
+import com.owncloud.android.datamodel.OCFile
 import com.owncloud.android.lib.common.operations.RemoteOperationResult
 import com.owncloud.android.lib.resources.files.FileUtils
 import com.owncloud.android.operations.DownloadFileOperation
@@ -87,7 +88,7 @@ class DownloadNotificationManager(private val context: Context, private val view
         downloadResult: RemoteOperationResult<*>,
         needsToUpdateCredentials: Boolean
     ) {
-        val tickerId = getTickerId(downloadResult.isSuccess, needsToUpdateCredentials)
+        val tickerId = getTickerId(downloadResult.isSuccess, needsToUpdateCredentials, null, null)
 
         notificationBuilder
             .setTicker(context.getString(tickerId))
@@ -98,12 +99,17 @@ class DownloadNotificationManager(private val context: Context, private val view
     }
 
     @Suppress("MagicNumber")
-    fun notifyForResult(result: RemoteOperationResult<*>, download: DownloadFileOperation) {
+    fun notifyForResult(
+        result: RemoteOperationResult<*>?,
+        download: DownloadFileOperation?,
+        folder: OCFile?,
+        isAnyOperationFailed: Boolean?
+    ) {
         dismissDownloadInProgressNotification()
 
-        val tickerId = getTickerId(result.isSuccess, null)
+        val tickerId = getTickerId(result?.isSuccess, null, folder, isAnyOperationFailed)
         val notifyId = SecureRandom().nextInt()
-        val resultText = getResultText(result, download)
+        val resultText = getResultText(result, download, folder, isAnyOperationFailed)
 
         notificationBuilder.run {
             setTicker(context.getString(tickerId))
@@ -118,47 +124,55 @@ class DownloadNotificationManager(private val context: Context, private val view
         )
     }
 
-    fun notifyForFolderResult(isAnyOperationFailed: Boolean, folderName: String) {
-        val notifyId = SecureRandom().nextInt()
-        val message = if (!isAnyOperationFailed) {
-            context.getString(R.string.downloader_folder_downloaded, folderName)
+    private fun getResultText(
+        result: RemoteOperationResult<*>?,
+        download: DownloadFileOperation?,
+        folder: OCFile?,
+        isAnyOperationFailed: Boolean?
+    ): String {
+        return folder?.let {
+            getFolderResultText(isAnyOperationFailed, it)
+        } ?: if (result?.isSuccess == true) {
+            download?.file?.fileName ?: ""
         } else {
-            context.getString(R.string.downloader_folder_download_failed, folderName)
+            ErrorMessageAdapter.getErrorCauseMessage(result, download, context.resources)
         }
+    }
 
-        notificationBuilder.run {
-            setContentText(message)
-            notificationManager.notify(notifyId, this.build())
+    private fun getFolderResultText(isAnyOperationFailed: Boolean?, folder: OCFile): String {
+        return if (isAnyOperationFailed == false) {
+            context.getString(R.string.downloader_folder_downloaded, folder.fileName)
+        } else {
+            context.getString(R.string.downloader_folder_download_failed, folder.fileName)
         }
+    }
 
-        NotificationUtils.cancelWithDelay(
-            notificationManager,
-            notifyId,
-            2000
-        )
+    private fun getTickerId(
+        isSuccess: Boolean?,
+        needsToUpdateCredentials: Boolean?,
+        folder: OCFile?,
+        isAnyOperationFailed: Boolean?
+    ): Int {
+        return if (needsToUpdateCredentials == true) {
+            R.string.downloader_download_failed_credentials_error
+        } else {
+            folder?.let { getFolderTickerId(isAnyOperationFailed) } ?: getFileTickerId(isSuccess)
+        }
     }
 
-    private fun getResultText(result: RemoteOperationResult<*>, download: DownloadFileOperation): String {
-        return if (result.isSuccess) {
-            download.file.fileName
+    private fun getFileTickerId(isSuccess: Boolean?): Int {
+        return if (isSuccess == true) {
+            R.string.downloader_download_succeeded_ticker
         } else {
-            ErrorMessageAdapter.getErrorCauseMessage(
-                result,
-                download,
-                context.resources
-            )
+            R.string.downloader_download_failed_ticker
         }
     }
 
-    private fun getTickerId(isSuccess: Boolean, needsToUpdateCredentials: Boolean?): Int {
-        return if (needsToUpdateCredentials == true) {
-            R.string.downloader_download_failed_credentials_error
+    private fun getFolderTickerId(isAnyOperationFailed: Boolean?): Int {
+        return if (isAnyOperationFailed == false) {
+            R.string.downloader_folder_downloaded
         } else {
-            if (isSuccess) {
-                R.string.downloader_download_succeeded_ticker
-            } else {
-                R.string.downloader_download_failed_ticker
-            }
+            R.string.downloader_folder_download_failed
         }
     }
 

+ 2 - 2
app/src/main/java/com/nextcloud/client/files/downloader/FileDownloadWorker.kt

@@ -145,7 +145,7 @@ class FileDownloadWorker(
     }
 
     private fun notifyForFolderResult(folder: OCFile) {
-        notificationManager.notifyForFolderResult(isAnyOperationFailed, folder.fileName)
+        notificationManager.notifyForResult(null, null, folder, isAnyOperationFailed)
     }
 
     private fun getRequestDownloads(): AbstractList<String> {
@@ -370,7 +370,7 @@ class FileDownloadWorker(
             }
 
             if (folder == null) {
-                notifyForResult(downloadResult, download)
+                notifyForResult(downloadResult, download, null, null)
             }
         }
     }