Browse Source

Fix Cancel Notification Appearance and Progress Bar Visibility

Signed-off-by: alperozturk <alper_ozturk@proton.me>
alperozturk 1 year ago
parent
commit
e9e74ba8fb

+ 9 - 4
app/src/main/java/com/nextcloud/client/files/downloader/DownloadNotificationManager.kt

@@ -102,14 +102,14 @@ class DownloadNotificationManager(
             val fileName: String = filePath.substring(filePath.lastIndexOf(FileUtils.PATH_SEPARATOR) + 1)
             val text =
                 String.format(context.getString(R.string.downloader_download_in_progress_content), percent, fileName)
-            updateNotificationText(text)
+            updateNotificationText(text, false)
         }
     }
 
     @Suppress("MagicNumber")
     fun showCompleteNotification(text: String) {
         Handler(Looper.getMainLooper()).postDelayed({
-            updateNotificationText(text)
+            updateNotificationText(text, true)
             dismissNotification()
         }, 3000)
     }
@@ -117,7 +117,7 @@ class DownloadNotificationManager(
     @Suppress("MagicNumber")
     fun dismissNotification() {
         Handler(Looper.getMainLooper()).postDelayed({
-            notificationManager.cancelAll()
+            notificationManager.cancel(id)
         }, 2000)
     }
 
@@ -125,13 +125,18 @@ class DownloadNotificationManager(
         val notifyId = SecureRandom().nextInt()
 
         notificationBuilder.run {
+            setProgress(0, 0, false)
             setContentText(text)
             notificationManager.notify(notifyId, this.build())
         }
     }
 
-    private fun updateNotificationText(text: String) {
+    private fun updateNotificationText(text: String, cancelProgressBar: Boolean) {
         notificationBuilder.run {
+            if (cancelProgressBar) {
+                setProgress(0, 0, false)
+            }
+
             setContentText(text)
             notificationManager.notify(id, this.build())
         }

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

@@ -114,6 +114,7 @@ class FileDownloadWorker(
     private var fileDataStorageManager: FileDataStorageManager? = null
 
     private var folder: OCFile? = null
+    private var isAnyOperationFailed = false
 
     @Suppress("TooGenericExceptionCaught")
     override fun doWork(): Result {
@@ -126,8 +127,7 @@ class FileDownloadWorker(
                 downloadFile(it)
             }
 
-            showCompleteNotification()
-
+            showSuccessNotification()
             setIdleWorkerState()
 
             Log_OC.e(TAG, "FilesDownloadWorker successfully completed")
@@ -171,7 +171,12 @@ class FileDownloadWorker(
     }
 
     @Suppress("MagicNumber")
-    private fun showCompleteNotification() {
+    private fun showSuccessNotification() {
+        if (isAnyOperationFailed) {
+            notificationManager.dismissNotification()
+            return
+        }
+
         val successText = if (folder != null) {
             context.getString(R.string.downloader_folder_downloaded, folder?.fileName)
         } else if (currentDownload?.file != null) {
@@ -184,6 +189,7 @@ class FileDownloadWorker(
     }
 
     private fun getRequestDownloads(): AbstractList<String> {
+        isAnyOperationFailed = false
         setUser()
         setFolder()
         val files = getFiles()
@@ -387,6 +393,8 @@ class FileDownloadWorker(
             return
         }
 
+        isAnyOperationFailed = true
+
         val failMessage = if (result.isCancelled) {
             context.getString(
                 R.string.downloader_file_download_cancelled,