From 3b272159e8323d27ae815a484071bb15beb659e4 Mon Sep 17 00:00:00 2001 From: Nathan Pham Date: Wed, 5 Oct 2022 13:42:10 +0700 Subject: [PATCH] keep silent when enqueue --- .../main/java/com/tonyodev/fetch2/Fetch.kt | 9 ++- .../com/tonyodev/fetch2/fetch/FetchImpl.kt | 56 +++++++++++-------- .../fetchapp/DownloadListActivity.java | 2 +- .../fetchapp/FailedMultiEnqueueActivity.java | 2 +- 4 files changed, 41 insertions(+), 28 deletions(-) diff --git a/fetch2/src/main/java/com/tonyodev/fetch2/Fetch.kt b/fetch2/src/main/java/com/tonyodev/fetch2/Fetch.kt index c057c59e..ad71a10d 100644 --- a/fetch2/src/main/java/com/tonyodev/fetch2/Fetch.kt +++ b/fetch2/src/main/java/com/tonyodev/fetch2/Fetch.kt @@ -69,11 +69,16 @@ interface Fetch { * that the request was not enqueued for the specified reason. * Fetch may update a request depending on the initial request's Enqueue Action. * Update old request references with this request. + * @param isNotify return True if you want to fetch notify to target. False otherwise * * @throws FetchException if this instance of Fetch has been closed. * @return Instance * */ - fun enqueue(requests: List, func: Func>>? = null): Fetch + fun enqueue( + requests: List, + func: Func>>? = null, + isNotify: Boolean = true + ): Fetch /** @@ -740,7 +745,7 @@ interface Fetch { func2: Func? = null ): Fetch - + /** * Resets the autoRetryAttempts value for a download back to 0. * @param downloadId Id of existing request/download diff --git a/fetch2/src/main/java/com/tonyodev/fetch2/fetch/FetchImpl.kt b/fetch2/src/main/java/com/tonyodev/fetch2/fetch/FetchImpl.kt index 96806c52..7a3b1cf8 100644 --- a/fetch2/src/main/java/com/tonyodev/fetch2/fetch/FetchImpl.kt +++ b/fetch2/src/main/java/com/tonyodev/fetch2/fetch/FetchImpl.kt @@ -94,8 +94,12 @@ open class FetchImpl constructor( return this } - override fun enqueue(requests: List, func: Func>>?): Fetch { - enqueueRequest(requests, func, null) + override fun enqueue( + requests: List, + func: Func>>?, + isNotify: Boolean + ): Fetch { + enqueueRequest(requests, func, null, isNotify = isNotify) return this } @@ -123,7 +127,8 @@ open class FetchImpl constructor( private fun enqueueRequest( requests: List, func: Func>>?, - func2: Func? + func2: Func?, + isNotify: Boolean = true ) { synchronized(lock) { throwExceptionIfClosed() @@ -134,31 +139,34 @@ open class FetchImpl constructor( throw FetchException(ENQUEUED_REQUESTS_ARE_NOT_DISTINCT) } val downloadPairs = fetchHandler.enqueue(requests) - downloadPairs.forEach { downloadPair -> - val download = downloadPair.first - when (download.status) { - Status.ADDED -> { - listenerCoordinator.mainListener.onAdded(download) - logger.d("Added $download") - } - Status.QUEUED -> { - val downloadCopy = - download.toDownloadInfo(fetchDatabaseManagerWrapper.getNewDownloadInfoInstance()) - downloadCopy.status = Status.ADDED - listenerCoordinator.mainListener.onAdded(downloadCopy) - logger.d("Added $download") - listenerCoordinator.mainListener.onQueued(download, false) - logger.d("Queued $download for download") - } - Status.COMPLETED -> { - listenerCoordinator.mainListener.onCompleted(download) - logger.d("Completed download $download") - } - else -> { + if (isNotify) { + downloadPairs.forEach { downloadPair -> + val download = downloadPair.first + when (download.status) { + Status.ADDED -> { + listenerCoordinator.mainListener.onAdded(download) + logger.d("Added $download") + } + Status.QUEUED -> { + val downloadCopy = + download.toDownloadInfo(fetchDatabaseManagerWrapper.getNewDownloadInfoInstance()) + downloadCopy.status = Status.ADDED + listenerCoordinator.mainListener.onAdded(downloadCopy) + logger.d("Added $download") + listenerCoordinator.mainListener.onQueued(download, false) + logger.d("Queued $download for download") + } + Status.COMPLETED -> { + listenerCoordinator.mainListener.onCompleted(download) + logger.d("Completed download $download") + } + else -> { + } } } } + uiHandler.post { func?.call(downloadPairs.map { Pair(it.first.request, it.second) }) } diff --git a/sampleApp/src/main/java/com/tonyodev/fetchapp/DownloadListActivity.java b/sampleApp/src/main/java/com/tonyodev/fetchapp/DownloadListActivity.java index 451501b4..d38283b1 100644 --- a/sampleApp/src/main/java/com/tonyodev/fetchapp/DownloadListActivity.java +++ b/sampleApp/src/main/java/com/tonyodev/fetchapp/DownloadListActivity.java @@ -178,7 +178,7 @@ private void enqueueDownloads() { final List requests = Data.getFetchRequestWithGroupId(GROUP_ID, this); fetch.enqueue(requests, updatedRequests -> { - }); + }, true); } diff --git a/sampleApp/src/main/java/com/tonyodev/fetchapp/FailedMultiEnqueueActivity.java b/sampleApp/src/main/java/com/tonyodev/fetchapp/FailedMultiEnqueueActivity.java index 51f04992..27a641b3 100644 --- a/sampleApp/src/main/java/com/tonyodev/fetchapp/FailedMultiEnqueueActivity.java +++ b/sampleApp/src/main/java/com/tonyodev/fetchapp/FailedMultiEnqueueActivity.java @@ -37,7 +37,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) { final Request request = new Request(url, file); requests.add(request); } - fetch.enqueue(requests, null); + fetch.enqueue(requests, null, true); Snackbar.make(mainView, "Enqueued " + size + " requests. Check Logcat for " + "failed status", Snackbar.LENGTH_INDEFINITE) .show();