Skip to content

Commit

Permalink
added new param to addCompletedDownload methods
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyofrancis committed Aug 23, 2018
1 parent c85105a commit 8f32f15
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 18 deletions.
12 changes: 6 additions & 6 deletions fetch2/src/main/java/com/tonyodev/fetch2/Fetch.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<Download>? = null,
func2: Func<Error>? = null): Fetch
fun updateRequest(requestId: Int, updatedRequest: Request, func: Func<Download>? = null, func2: Func<Error>? = 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
Expand All @@ -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<Download>? = null,
func2: Func<Error>? = null): Fetch
fun replaceExtras(id: Int, extras: Extras, func: Func<Download>? = null, func2: Func<Error>? = null): Fetch

/**
* Gets all downloads managed by this instance of Fetch.
Expand Down Expand Up @@ -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<Download>? = null, func2: Func<Error>? = null): Fetch
fun addCompletedDownload(completedDownload: CompletedDownload, alertListeners: Boolean = true, func: Func<Download>? = null, func2: Func<Error>? = 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<CompletedDownload>, func: Func<List<Download>>? = null, func2: Func<Error>? = null): Fetch
fun addCompletedDownloads(completedDownloads: List<CompletedDownload>, alertListeners: Boolean = true, func: Func<List<Download>>? = null, func2: Func<Error>? = null): Fetch

/**
* Gets the list of download blocks belonging to a download. List may be empty if
Expand Down
14 changes: 8 additions & 6 deletions fetch2/src/main/java/com/tonyodev/fetch2/fetch/FetchImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -701,8 +701,8 @@ open class FetchImpl constructor(override val namespace: String,
}
}

override fun addCompletedDownload(completedDownload: CompletedDownload, func: Func<Download>?, func2: Func<Error>?): Fetch {
return addCompletedDownloads(listOf(completedDownload), Func { downloads ->
override fun addCompletedDownload(completedDownload: CompletedDownload, alertListeners: Boolean, func: Func<Download>?, func2: Func<Error>?): Fetch {
return addCompletedDownloads(listOf(completedDownload), alertListeners, Func { downloads ->
if (downloads.isNotEmpty()) {
func?.call(downloads.first())
} else {
Expand All @@ -711,16 +711,18 @@ open class FetchImpl constructor(override val namespace: String,
}, func2)
}

override fun addCompletedDownloads(completedDownloads: List<CompletedDownload>, func: Func<List<Download>>?, func2: Func<Error>?): Fetch {
override fun addCompletedDownloads(completedDownloads: List<CompletedDownload>, alertListeners: Boolean, func: Func<List<Download>>?, func2: Func<Error>?): 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)
}
Expand Down
6 changes: 4 additions & 2 deletions fetch2rx/src/main/java/com/tonyodev/fetch2rx/RxFetch.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<Download>
fun addCompletedDownload(completedDownload: CompletedDownload, alertListeners: Boolean = true): Convertible<Download>

/**
* 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<CompletedDownload>): Convertible<List<Download>>
fun addCompletedDownloads(completedDownloads: List<CompletedDownload>, alertListeners: Boolean = true): Convertible<List<Download>>

/**
* Gets the list of download blocks belonging to a download. List may be empty if
Expand Down
10 changes: 6 additions & 4 deletions fetch2rx/src/main/java/com/tonyodev/fetch2rx/RxFetchImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -747,8 +747,8 @@ open class RxFetchImpl(override val namespace: String,
}
}

override fun addCompletedDownload(completedDownload: CompletedDownload): Convertible<Download> {
return addCompletedDownloads(listOf(completedDownload))
override fun addCompletedDownload(completedDownload: CompletedDownload, alertListeners: Boolean): Convertible<Download> {
return addCompletedDownloads(listOf(completedDownload), alertListeners)
.flowable
.subscribeOn(scheduler)
.flatMap {
Expand All @@ -762,7 +762,7 @@ open class RxFetchImpl(override val namespace: String,
.toConvertible()
}

override fun addCompletedDownloads(completedDownloads: List<CompletedDownload>): Convertible<List<Download>> {
override fun addCompletedDownloads(completedDownloads: List<CompletedDownload>, alertListeners: Boolean): Convertible<List<Download>> {
return synchronized(lock) {
throwExceptionIfClosed()
Flowable.just(completedDownloads)
Expand All @@ -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")
}
}
Expand Down

0 comments on commit 8f32f15

Please sign in to comment.