Skip to content

Commit

Permalink
Better notifications
Browse files Browse the repository at this point in the history
Signed-off-by: alperozturk <[email protected]>
  • Loading branch information
alperozturk96 committed Jan 11, 2024
1 parent 04697a2 commit edab3e3
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class DownloadNotificationManager(

init {
notificationBuilder = NotificationUtils.newNotificationBuilder(context, viewThemeUtils).apply {
setContentTitle(context.resources.getString(R.string.app_name))
setContentTitle(context.getString(R.string.downloader_download_in_progress_ticker))
setSmallIcon(R.drawable.notification_icon)
setLargeIcon(BitmapFactory.decodeResource(context.resources, R.drawable.notification_icon))

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Nextcloud Android client application
*
* @author Alper Ozturk
* Copyright (C) 2023 Alper Ozturk
* Copyright (C) 2023 Nextcloud GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package com.nextcloud.client.files.downloader

enum class FileDownloadError {
Failed, Cancelled
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class FileDownloadWorker(
private var fileDataStorageManager: FileDataStorageManager? = null

private var workerId: Int? = null
private var isAnyOperationFailed = false
private var downloadError: FileDownloadError? = null

@Suppress("TooGenericExceptionCaught")
override fun doWork(): Result {
Expand All @@ -140,7 +140,8 @@ class FileDownloadWorker(
downloadFile(it)
}

if (isAnyOperationFailed) {
downloadError?.let {
showDownloadErrorNotification(it)
notificationManager.dismissNotification()
}

Expand Down Expand Up @@ -182,7 +183,6 @@ class FileDownloadWorker(
workerId = inputData.keyValueMap[WORKER_ID] as Int
Log_OC.e(TAG, "FilesDownloadWorker started for $workerId")

isAnyOperationFailed = false
setUser()
val files = getFiles()
val downloadType = getDownloadType()
Expand Down Expand Up @@ -339,7 +339,7 @@ class FileDownloadWorker(

private fun cleanupDownloadProcess(result: RemoteOperationResult<*>?, fileName: String?) {
result?.let {
showFailedDownloadNotifications(it, fileName)
checkDownloadError(it, fileName)
}

val removeResult = pendingDownloads.removePayload(
Expand All @@ -362,26 +362,29 @@ class FileDownloadWorker(
}
}

private fun showFailedDownloadNotifications(result: RemoteOperationResult<*>, fileName: String?) {
if (result.isSuccess) {
private fun checkDownloadError(result: RemoteOperationResult<*>, fileName: String?) {
if (result.isSuccess || downloadError != null) {
return
}

isAnyOperationFailed = true

val failMessage = if (result.isCancelled) {
context.getString(
R.string.downloader_file_download_cancelled,
fileName
)
downloadError = if (result.isCancelled) {
FileDownloadError.Cancelled
} else {
context.getString(
R.string.downloader_file_download_failed,
fileName
)
FileDownloadError.Failed
}
}

private fun showDownloadErrorNotification(downloadError: FileDownloadError) {
val text = when (downloadError) {
FileDownloadError.Cancelled -> {
context.getString(R.string.downloader_file_download_cancelled)
}
FileDownloadError.Failed -> {
context.getString(R.string.downloader_file_download_failed)
}
}

notificationManager.showNewNotification(failMessage)
notificationManager.showNewNotification(text)
}

private fun notifyDownloadResult(
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@
<string name="downloader_download_failed_ticker">Download failed</string>
<string name="downloader_download_failed_content">Could not download %1$s</string>
<string name="downloader_not_downloaded_yet">Not downloaded yet</string>
<string name="downloader_file_download_cancelled">%s file download cancelled</string>
<string name="downloader_file_download_failed">Error occurred while downloading %s file</string>
<string name="downloader_file_download_cancelled">Certain files were canceled during the download by user</string>
<string name="downloader_file_download_failed">Error occurred while downloading files</string>
<string name="downloader_unexpected_error">Unexpected error occurred while downloading files</string>
<string name="downloader_download_failed_credentials_error">Download failed, log in again</string>
<string name="common_choose_account">Choose account</string>
Expand Down

0 comments on commit edab3e3

Please sign in to comment.