From 8f32f15b4691d058e5791fc3582c88f93a1d6a87 Mon Sep 17 00:00:00 2001 From: Tonyo Francis Date: Wed, 22 Aug 2018 21:25:08 -0400 Subject: [PATCH] added new param to addCompletedDownload methods --- fetch2/src/main/java/com/tonyodev/fetch2/Fetch.kt | 12 ++++++------ .../java/com/tonyodev/fetch2/fetch/FetchImpl.kt | 14 ++++++++------ .../src/main/java/com/tonyodev/fetch2rx/RxFetch.kt | 6 ++++-- .../main/java/com/tonyodev/fetch2rx/RxFetchImpl.kt | 10 ++++++---- 4 files changed, 24 insertions(+), 18 deletions(-) diff --git a/fetch2/src/main/java/com/tonyodev/fetch2/Fetch.kt b/fetch2/src/main/java/com/tonyodev/fetch2/Fetch.kt index 6996e0c9..35592bbd 100644 --- a/fetch2/src/main/java/com/tonyodev/fetch2/Fetch.kt +++ b/fetch2/src/main/java/com/tonyodev/fetch2/Fetch.kt @@ -564,8 +564,7 @@ interface Fetch { * @throws FetchException if this instance of Fetch has been closed. * @return Instance * */ - fun updateRequest(requestId: Int, updatedRequest: Request, func: Func? = null, - func2: Func? = null): Fetch + fun updateRequest(requestId: Int, updatedRequest: Request, func: Func? = null, func2: Func? = null): Fetch /** Replaces the existing extras object associated with an existing download/request with the newly passed in extras object. * @param id Id of existing request/download @@ -575,8 +574,7 @@ interface Fetch { * @throws FetchException if this instance of Fetch has been closed. * @return Instance * */ - fun replaceExtras(id: Int, extras: Extras, func: Func? = null, - func2: Func? = null): Fetch + fun replaceExtras(id: Int, extras: Extras, func: Func? = null, func2: Func? = null): Fetch /** * Gets all downloads managed by this instance of Fetch. @@ -682,23 +680,25 @@ interface Fetch { * Adds a completed download to Fetch for management. If Fetch is already managing another download with the same file as this completed download's * file, Fetch will replace the already managed download with this completed download. * @param completedDownload Completed Download + * @param alertListeners boolean indicating whether to alert all listeners attached to this fetch's namespace of the downloads completed status. * @param func Callback that the added download will be returned on. * @param func2 Callback that is called when adding the completed download fails. An error is returned. * @throws FetchException if this instance of Fetch has been closed. * @return Instance * */ - fun addCompletedDownload(completedDownload: CompletedDownload, func: Func? = null, func2: Func? = null): Fetch + fun addCompletedDownload(completedDownload: CompletedDownload, alertListeners: Boolean = true, func: Func? = null, func2: Func? = null): Fetch /** * Adds a list of completed downloads to Fetch for management. If Fetch is already managing another download with the same file as this completed download's * file, Fetch will replace the already managed download with this completed download. * @param completedDownloads Completed Downloads list + * @param alertListeners boolean indicating whether to alert all listeners attached to this fetch's namespace of the downloads completed status. * @param func Callback that the added downloads list will be returned on. * @param func2 Callback that is called when adding the completed downloads fails. An error is returned. * @throws FetchException if this instance of Fetch has been closed. * @return Instance * */ - fun addCompletedDownloads(completedDownloads: List, func: Func>? = null, func2: Func? = null): Fetch + fun addCompletedDownloads(completedDownloads: List, alertListeners: Boolean = true, func: Func>? = null, func2: Func? = null): Fetch /** * Gets the list of download blocks belonging to a download. List may be empty if 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 2777b8ae..50719e2d 100644 --- a/fetch2/src/main/java/com/tonyodev/fetch2/fetch/FetchImpl.kt +++ b/fetch2/src/main/java/com/tonyodev/fetch2/fetch/FetchImpl.kt @@ -701,8 +701,8 @@ open class FetchImpl constructor(override val namespace: String, } } - override fun addCompletedDownload(completedDownload: CompletedDownload, func: Func?, func2: Func?): Fetch { - return addCompletedDownloads(listOf(completedDownload), Func { downloads -> + override fun addCompletedDownload(completedDownload: CompletedDownload, alertListeners: Boolean, func: Func?, func2: Func?): Fetch { + return addCompletedDownloads(listOf(completedDownload), alertListeners, Func { downloads -> if (downloads.isNotEmpty()) { func?.call(downloads.first()) } else { @@ -711,16 +711,18 @@ open class FetchImpl constructor(override val namespace: String, }, func2) } - override fun addCompletedDownloads(completedDownloads: List, func: Func>?, func2: Func?): Fetch { + override fun addCompletedDownloads(completedDownloads: List, alertListeners: Boolean, func: Func>?, func2: Func?): Fetch { synchronized(lock) { throwExceptionIfClosed() handlerWrapper.post { try { val downloads = fetchHandler.enqueueCompletedDownloads(completedDownloads) uiHandler.post { - downloads.forEach { - listenerCoordinator.mainListener.onCompleted(it) - logger.d("Added CompletedDownload $it") + if (alertListeners) { + downloads.forEach { + listenerCoordinator.mainListener.onCompleted(it) + logger.d("Added CompletedDownload $it") + } } func?.call(downloads) } diff --git a/fetch2rx/src/main/java/com/tonyodev/fetch2rx/RxFetch.kt b/fetch2rx/src/main/java/com/tonyodev/fetch2rx/RxFetch.kt index b2a9e56e..af04e0d3 100644 --- a/fetch2rx/src/main/java/com/tonyodev/fetch2rx/RxFetch.kt +++ b/fetch2rx/src/main/java/com/tonyodev/fetch2rx/RxFetch.kt @@ -399,19 +399,21 @@ interface RxFetch { * Adds a completed download to Fetch for management. If Fetch is already managing another download with the same file as this completed download's * file, Fetch will replace the already managed download with this completed download. * @param completedDownload Completed Download + * @param alertListeners boolean indicating whether to alert all listeners attached to this fetch's namespace of the downloads completed status. * @throws FetchException if this instance of Fetch has been closed. * @return Instance * */ - fun addCompletedDownload(completedDownload: CompletedDownload): Convertible + fun addCompletedDownload(completedDownload: CompletedDownload, alertListeners: Boolean = true): Convertible /** * Adds a list of completed downloads to Fetch for management. If Fetch is already managing another download with the same file as this completed download's * file, Fetch will replace the already managed download with this completed download. * @param completedDownloads Completed Downloads list + * @param alertListeners boolean indicating whether to alert all listeners attached to this fetch's namespace of the downloads completed status. * @throws FetchException if this instance of Fetch has been closed. * @return Convertible * */ - fun addCompletedDownloads(completedDownloads: List): Convertible> + fun addCompletedDownloads(completedDownloads: List, alertListeners: Boolean = true): Convertible> /** * Gets the list of download blocks belonging to a download. List may be empty if diff --git a/fetch2rx/src/main/java/com/tonyodev/fetch2rx/RxFetchImpl.kt b/fetch2rx/src/main/java/com/tonyodev/fetch2rx/RxFetchImpl.kt index eeb75ecf..9a33b863 100644 --- a/fetch2rx/src/main/java/com/tonyodev/fetch2rx/RxFetchImpl.kt +++ b/fetch2rx/src/main/java/com/tonyodev/fetch2rx/RxFetchImpl.kt @@ -747,8 +747,8 @@ open class RxFetchImpl(override val namespace: String, } } - override fun addCompletedDownload(completedDownload: CompletedDownload): Convertible { - return addCompletedDownloads(listOf(completedDownload)) + override fun addCompletedDownload(completedDownload: CompletedDownload, alertListeners: Boolean): Convertible { + return addCompletedDownloads(listOf(completedDownload), alertListeners) .flowable .subscribeOn(scheduler) .flatMap { @@ -762,7 +762,7 @@ open class RxFetchImpl(override val namespace: String, .toConvertible() } - override fun addCompletedDownloads(completedDownloads: List): Convertible> { + override fun addCompletedDownloads(completedDownloads: List, alertListeners: Boolean): Convertible> { return synchronized(lock) { throwExceptionIfClosed() Flowable.just(completedDownloads) @@ -772,7 +772,9 @@ open class RxFetchImpl(override val namespace: String, val downloads = fetchHandler.enqueueCompletedDownloads(completedDownloads) uiHandler.post { downloads.forEach { download -> - listenerCoordinator.mainListener.onCompleted(download) + if (alertListeners) { + listenerCoordinator.mainListener.onCompleted(download) + } logger.d("Added CompletedDownload $download") } }