Skip to content

Commit

Permalink
keep silent when enqueue
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Pham committed Oct 5, 2022
1 parent 58316ba commit 3b27215
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 28 deletions.
9 changes: 7 additions & 2 deletions fetch2/src/main/java/com/tonyodev/fetch2/Fetch.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<Request>, func: Func<List<Pair<Request, Error>>>? = null): Fetch
fun enqueue(
requests: List<Request>,
func: Func<List<Pair<Request, Error>>>? = null,
isNotify: Boolean = true
): Fetch


/**
Expand Down Expand Up @@ -740,7 +745,7 @@ interface Fetch {
func2: Func<Error>? = null
): Fetch


/**
* Resets the autoRetryAttempts value for a download back to 0.
* @param downloadId Id of existing request/download
Expand Down
56 changes: 32 additions & 24 deletions fetch2/src/main/java/com/tonyodev/fetch2/fetch/FetchImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,12 @@ open class FetchImpl constructor(
return this
}

override fun enqueue(requests: List<Request>, func: Func<List<Pair<Request, Error>>>?): Fetch {
enqueueRequest(requests, func, null)
override fun enqueue(
requests: List<Request>,
func: Func<List<Pair<Request, Error>>>?,
isNotify: Boolean
): Fetch {
enqueueRequest(requests, func, null, isNotify = isNotify)
return this
}

Expand Down Expand Up @@ -123,7 +127,8 @@ open class FetchImpl constructor(
private fun enqueueRequest(
requests: List<Request>,
func: Func<List<Pair<Request, Error>>>?,
func2: Func<Error>?
func2: Func<Error>?,
isNotify: Boolean = true
) {
synchronized(lock) {
throwExceptionIfClosed()
Expand All @@ -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) })
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ private void enqueueDownloads() {
final List<Request> requests = Data.getFetchRequestWithGroupId(GROUP_ID, this);
fetch.enqueue(requests, updatedRequests -> {

});
}, true);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 3b27215

Please sign in to comment.