From a17933fe44f3063709aadbc47e9563d92af2ed64 Mon Sep 17 00:00:00 2001 From: alperozturk Date: Mon, 8 Jan 2024 13:46:43 +0100 Subject: [PATCH] Add showNewNotification Signed-off-by: alperozturk --- .../downloader/DownloadNotificationManager.kt | 12 ++++++++- .../files/downloader/FileDownloadWorker.kt | 27 +++++++++---------- app/src/main/res/values/strings.xml | 3 +-- 3 files changed, 24 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/com/nextcloud/client/files/downloader/DownloadNotificationManager.kt b/app/src/main/java/com/nextcloud/client/files/downloader/DownloadNotificationManager.kt index 451aaf2235eb..11a12144167e 100644 --- a/app/src/main/java/com/nextcloud/client/files/downloader/DownloadNotificationManager.kt +++ b/app/src/main/java/com/nextcloud/client/files/downloader/DownloadNotificationManager.kt @@ -37,6 +37,7 @@ import com.owncloud.android.operations.DownloadFileOperation import com.owncloud.android.ui.notifications.NotificationUtils import com.owncloud.android.utils.theme.ViewThemeUtils import java.io.File +import java.security.SecureRandom @Suppress("TooManyFunctions") class DownloadNotificationManager( @@ -120,7 +121,16 @@ class DownloadNotificationManager( }, 2000) } - fun updateNotificationText(text: String) { + fun showNewNotification(text: String) { + val notifyId = SecureRandom().nextInt() + + notificationBuilder.run { + setContentText(text) + notificationManager.notify(notifyId, this.build()) + } + } + + private fun updateNotificationText(text: String) { notificationBuilder.run { setContentText(text) notificationManager.notify(id, this.build()) diff --git a/app/src/main/java/com/nextcloud/client/files/downloader/FileDownloadWorker.kt b/app/src/main/java/com/nextcloud/client/files/downloader/FileDownloadWorker.kt index 6328315ecd6c..2f2404dfd6f5 100644 --- a/app/src/main/java/com/nextcloud/client/files/downloader/FileDownloadWorker.kt +++ b/app/src/main/java/com/nextcloud/client/files/downloader/FileDownloadWorker.kt @@ -122,7 +122,6 @@ class FileDownloadWorker( private var fileDataStorageManager: FileDataStorageManager? = null private var folder: OCFile? = null - private var failedFileNames: ArrayList = arrayListOf() @Suppress("TooGenericExceptionCaught") override fun doWork(): Result { @@ -164,7 +163,6 @@ class FileDownloadWorker( } private fun setIdleWorkerState() { - failedFileNames.clear() pendingDownloads.all.clear() pendingDownloadFileIds.clear() currentDownload = null @@ -181,14 +179,9 @@ class FileDownloadWorker( pendingDownloads.remove(accountName) } + @Suppress("MagicNumber") private fun showCompleteNotification() { - val result = if (failedFileNames.isEmpty()) { - getSuccessNotificationText() - } else { - val fileNames = failedFileNames.joinToString() - context.getString(R.string.downloader_files_download_failed, fileNames) - } - + val result = getSuccessNotificationText() notificationManager.showCompleteNotification(result) } @@ -377,7 +370,7 @@ class FileDownloadWorker( private fun cleanupDownloadProcess(result: RemoteOperationResult<*>?) { result?.let { - checkOperationFailures(it) + showFailedDownloadNotifications(it) } val removeResult = pendingDownloads.removePayload( @@ -401,11 +394,15 @@ class FileDownloadWorker( } } - private fun checkOperationFailures(result: RemoteOperationResult<*>) { + private fun showFailedDownloadNotifications(result: RemoteOperationResult<*>) { if (!result.isSuccess) { - currentDownload?.file?.fileName?.let { fileName -> - failedFileNames.add(fileName) - } + val fileName = currentDownload?.file?.fileName ?: "" + notificationManager.showNewNotification( + context.getString( + R.string.downloader_file_download_failed, + fileName + ) + ) } } @@ -422,7 +419,7 @@ class FileDownloadWorker( prepareForResult() if (needsToUpdateCredentials) { - updateNotificationText(context.getString(R.string.downloader_download_failed_credentials_error)) + showNewNotification(context.getString(R.string.downloader_download_failed_credentials_error)) setContentIntent( intents.credentialContentIntent(download.user), PendingIntent.FLAG_ONE_SHOT or PendingIntent.FLAG_IMMUTABLE diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 54a959bc739f..ac2c5f77fb05 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -175,8 +175,7 @@ Download failed Could not download %1$s Not downloaded yet - Error occurred while downloading %s files - Error occurred while downloading %s folder + Error occurred while downloading %s file %s folder successfully downloaded %s file successfully downloaded Unexpected error occurred while downloading files