Skip to content

Commit

Permalink
Add showNewNotification
Browse files Browse the repository at this point in the history
Signed-off-by: alperozturk <[email protected]>
  • Loading branch information
alperozturk96 committed Jan 8, 2024
1 parent f0487a6 commit a17933f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ class FileDownloadWorker(
private var fileDataStorageManager: FileDataStorageManager? = null

private var folder: OCFile? = null
private var failedFileNames: ArrayList<String> = arrayListOf()

@Suppress("TooGenericExceptionCaught")
override fun doWork(): Result {
Expand Down Expand Up @@ -164,7 +163,6 @@ class FileDownloadWorker(
}

private fun setIdleWorkerState() {
failedFileNames.clear()
pendingDownloads.all.clear()
pendingDownloadFileIds.clear()
currentDownload = null
Expand All @@ -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)
}

Expand Down Expand Up @@ -377,7 +370,7 @@ class FileDownloadWorker(

private fun cleanupDownloadProcess(result: RemoteOperationResult<*>?) {
result?.let {
checkOperationFailures(it)
showFailedDownloadNotifications(it)
}

val removeResult = pendingDownloads.removePayload(
Expand All @@ -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
)
)
}
}

Expand All @@ -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
Expand Down
3 changes: 1 addition & 2 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,7 @@
<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_files_download_failed">Error occurred while downloading %s files</string>
<string name="downloader_folder_download_failed">Error occurred while downloading %s folder</string>
<string name="downloader_file_download_failed">Error occurred while downloading %s file</string>
<string name="downloader_folder_downloaded">%s folder successfully downloaded</string>
<string name="downloader_file_downloaded">%s file successfully downloaded</string>
<string name="downloader_unexpected_error">Unexpected error occurred while downloading files</string>
Expand Down

0 comments on commit a17933f

Please sign in to comment.