Skip to content

Commit

Permalink
updated Fetch.updateRequest method
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyofrancis committed Jun 29, 2018
1 parent 5e4fcb0 commit da8e2df
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 57 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
Version 2.1.0-RC10
- Updated Fetch.updateRequest method.

Version 2.1.0-RC9
- Renamed enqueue option EnqueueAction.THROW_ERROR_IF_EXISTING to EnqueueAction.DO_NOT_ENQUEUE_IF_EXISTING
to better reflect the action intention.
Expand Down
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

[![Build Status](https://travis-ci.org/tonyofrancis/Fetch.svg?branch=v2)](https://travis-ci.org/tonyofrancis/Fetch)
[ ![Download](https://api.bintray.com/packages/tonyofrancis/maven/fetch2/images/download.svg?version=2.1.0-RC9) ](https://bintray.com/tonyofrancis/maven/fetch2/2.1.0-RC9/link)
[ ![Download](https://api.bintray.com/packages/tonyofrancis/maven/fetch2/images/download.svg?version=2.1.0-RC10) ](https://bintray.com/tonyofrancis/maven/fetch2/2.1.0-RC10/link)
[![Android Arsenal](https://img.shields.io/badge/Android%20Arsenal-Android%20Networking-blue.svg?style=flat)](https://android-arsenal.com/details/1/5196)
[![License](https://img.shields.io/badge/license-Apache%202.0-blue.svg)](https://github.com/tonyofrancis/Fetch/blob/master/LICENSE)

Expand Down Expand Up @@ -48,7 +48,7 @@ How to use Fetch
Using Fetch is easy! Just add the Gradle dependency to your application's build.gradle file.

```java
implementation "com.tonyodev.fetch2:fetch2:2.1.0-RC9"
implementation "com.tonyodev.fetch2:fetch2:2.1.0-RC10"
```

Next, get an instance of Fetch and request a download.
Expand Down Expand Up @@ -214,7 +214,7 @@ to use the OkHttp Downloader instead. You can create your own custom downloaders
if necessary. See the Java docs for details.

```java
implementation "com.tonyodev.fetch2okhttp:fetch2okhttp:2.1.0-RC9"
implementation "com.tonyodev.fetch2okhttp:fetch2okhttp:2.1.0-RC10"
```
Set the OkHttp Downloader for Fetch to use.
```java
Expand All @@ -235,7 +235,7 @@ If you would like to take advantage of RxJava2 features when using Fetch,
add the following gradle dependency to your application's build.gradle file.

```java
implementation "com.tonyodev.fetch2rx:fetch2rx:2.1.0-RC9"
implementation "com.tonyodev.fetch2rx:fetch2rx:2.1.0-RC10"
```

RxFetch makes it super easy to enqueue download requests and query downloads using rxJava2 functional methods.
Expand Down Expand Up @@ -271,7 +271,7 @@ added in the coming days.

Start using FetchFileServer by adding the gradle dependency to your application's build.gradle file.
```java
implementation "com.tonyodev.fetch2fileserver:fetch2fileserver:2.1.0-RC9"
implementation "com.tonyodev.fetch2fileserver:fetch2fileserver:2.1.0-RC10"
```

Start a FetchFileServer instance and add resource files that it can server to connected clients.
Expand Down Expand Up @@ -309,7 +309,7 @@ public class TestActivity extends AppCompatActivity {
Download a file from a FetchFileServer using the Fetch. Add the FetchFileServerDownloader
dependency to you app's build.gradle file.
```java
implementation "com.tonyodev.fetch2downloaders:fetch2downloaders:2.1.0-RC9"
implementation "com.tonyodev.fetch2downloaders:fetch2downloaders:2.1.0-RC10"
```

Then create an instance of Fetch and enqueue the download.
Expand Down Expand Up @@ -463,7 +463,7 @@ Fetch1 Migration

Migrate downloads from Fetch1 to Fetch2 using the migration assistant. Add the following gradle dependency to your application's build.gradle file.
```java
implementation "com.tonyodev.fetchmigrator:fetchmigrator:2.1.0-RC9"
implementation "com.tonyodev.fetchmigrator:fetchmigrator:2.1.0-RC10"
```
Then run the Migrator.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -430,10 +430,10 @@ public void updateRequest() throws Exception {

final int groupId = 1245;
final Priority priority = Priority.LOW;
final RequestInfo requestInfo = new RequestInfo();
requestInfo.setGroupId(groupId);
requestInfo.setPriority(priority);
fetchHandler.updateRequest(download.getId(), requestInfo);
final Request request1 = new Request(request.getUrl(), request.getFile());
request1.setGroupId(groupId);
request1.setPriority(priority);
fetchHandler.updateRequest(download.getId(), request1);

final Download downloadInfo = fetchHandler.getDownload(download.getId());
assertNotNull(downloadInfo);
Expand Down
11 changes: 5 additions & 6 deletions fetch2/src/main/java/com/tonyodev/fetch2/Fetch.kt
Original file line number Diff line number Diff line change
Expand Up @@ -217,17 +217,16 @@ interface Fetch {
* */
fun retry(vararg ids: Int): Fetch

/** Updates and replaces an existing download's groupId, headers, priority and network
* type information.
* @see com.tonyodev.fetch2.RequestInfo for more details.
* @param id Id of existing download
* @param requestInfo Request Info object
/** Updates an existing request.
* @see com.tonyodev.fetch2.Request for more details.
* @param oldRequestId Id of existing request/download
* @param newRequest Request object
* @param func Successful callback that the download will be returned on.
* @param func2 Failed callback that the error will be returned on.
* @throws FetchException if this instance of Fetch has been closed.
* @return Instance
* */
fun updateRequest(id: Int, requestInfo: RequestInfo, func: Func<Download>? = null,
fun updateRequest(oldRequestId: Int, newRequest: Request, func: Func<Download>? = null,
func2: Func<Error>? = null): Fetch

/**
Expand Down
13 changes: 6 additions & 7 deletions fetch2/src/main/java/com/tonyodev/fetch2/FetchExtentions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,18 @@ fun Fetch.enqueue(requests: List<Request>, func: ((List<Request>) -> Unit)?, fun
})
}

/** Updates and replaces an existing download's groupId, headers, priority and network
* type information.
* @see com.tonyodev.fetch2.RequestInfo for more details.
* @param id Id of existing download
* @param requestInfo Request Info object
/** Updates an existing request.
* @see com.tonyodev.fetch2.Request for more details.
* @param oldRequestId Id of existing download
* @param newRequest Request Info object
* @param func Successful callback that the download will be returned on.
* @param func2 Failed callback that the error will be returned on.
* @throws FetchException if this instance of Fetch has been closed.
* @return Instance
* */
fun Fetch.updateRequest(id: Int, requestInfo: RequestInfo, func: ((Download) -> Unit)?,
fun Fetch.updateRequest(oldRequestId: Int, newRequest: Request, func: ((Download) -> Unit)?,
func2: ((Error) -> Unit)?): Fetch {
return updateRequest(id, requestInfo, object : Func<Download> {
return updateRequest(oldRequestId, newRequest, object : Func<Download> {
override fun call(t: Download) {
func?.invoke(t)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ interface FetchHandler : Closeable {
fun cancelGroup(id: Int): List<Download>
fun cancelAll(): List<Download>
fun retry(ids: IntArray): List<Download>
fun updateRequest(id: Int, requestInfo: RequestInfo): Download?
fun updateRequest(oldRequestId: Int, newRequest: Request): Download?
fun getDownloads(): List<Download>
fun getDownload(id: Int): Download?
fun getDownloads(idList: List<Int>): List<Download>
Expand Down
35 changes: 19 additions & 16 deletions fetch2/src/main/java/com/tonyodev/fetch2/fetch/FetchHandlerImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -471,23 +471,26 @@ class FetchHandlerImpl(private val namespace: String,
}
}

override fun updateRequest(id: Int, requestInfo: RequestInfo): Download? {
startPriorityQueueIfNotStarted()
var downloadInfo = databaseManager.get(id)
if (downloadInfo != null) {
if (isDownloading(id)) {
cancelDownload(id)
}
downloadInfo = databaseManager.get(id)
if (downloadInfo != null) {
downloadInfo.group = requestInfo.groupId
downloadInfo.headers = requestInfo.headers
downloadInfo.priority = requestInfo.priority
downloadInfo.networkType = requestInfo.networkType
downloadInfo.enqueueAction = requestInfo.enqueueAction
databaseManager.update(downloadInfo)
return downloadInfo
override fun updateRequest(oldRequestId: Int, newRequest: Request): Download? {
val oldDownloadInfo = databaseManager.get(oldRequestId)
if (oldDownloadInfo != null) {
val newDownloadInfo = newRequest.toDownloadInfo()
newDownloadInfo.status = Status.QUEUED
newDownloadInfo.namespace = namespace
val enqueueAction = newDownloadInfo.enqueueAction
if (enqueueAction == EnqueueAction.REPLACE_EXISTING) {
if (newRequest.file == oldDownloadInfo.file) {
newDownloadInfo.downloaded = oldDownloadInfo.downloaded
newDownloadInfo.total = oldDownloadInfo.total
}
remove(intArrayOf(oldRequestId))
} else {
delete(intArrayOf(oldRequestId))
}
prepareDownloadInfoForEnqueue(newDownloadInfo)
startPriorityQueueIfNotStarted()
databaseManager.insert(newDownloadInfo)
return newDownloadInfo
}
return null
}
Expand Down
6 changes: 3 additions & 3 deletions fetch2/src/main/java/com/tonyodev/fetch2/fetch/FetchImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -441,13 +441,13 @@ open class FetchImpl constructor(override val namespace: String,
}
}

override fun updateRequest(id: Int, requestInfo: RequestInfo, func: Func<Download>?,
override fun updateRequest(oldRequestId: Int, newRequest: Request, func: Func<Download>?,
func2: Func<Error>?): Fetch {
synchronized(lock) {
throwExceptionIfClosed()
handlerWrapper.post {
try {
val download = fetchHandler.updateRequest(id, requestInfo)
val download = fetchHandler.updateRequest(oldRequestId, newRequest)
if (download != null && func != null) {
uiHandler.post {
func.call(download)
Expand All @@ -459,7 +459,7 @@ open class FetchImpl constructor(override val namespace: String,
}
}
} catch (e: Exception) {
logger.e("Failed to update request with id $id", e)
logger.e("Failed to update request with id $oldRequestId", e)
val error = getErrorFromMessage(e.message)
if (func2 != null) {
uiHandler.post {
Expand Down
18 changes: 9 additions & 9 deletions fetch2rx/src/main/java/com/tonyodev/fetch2rx/RxFetch.kt
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,17 @@ interface RxFetch : Fetch {
* */
fun addCompletedDownloads(completedDownloads: List<CompletedDownload>): Convertible<List<Download>>

/** Updates and replaces an existing download's groupId, headers, priority and network type information.
* If the download does not exist and the returned convertible object when converted
* to an observable or flowable and subscribed to, will throw an exception indicating the
* error.
* @see com.tonyodev.fetch2.RequestInfo for more details.
* @param id Id of existing download
* @param requestInfo Request Info object
/** Updates an existing request.
* @see com.tonyodev.fetch2.Request for more details.
* @param oldRequestId Id of existing request/download
* @param newRequest Request object
* @param func Successful callback that the download will be returned on.
* @param func2 Failed callback that the error will be returned on.
* @throws FetchException if this instance of Fetch has been closed.
* @return A Convertible object that allows you to get the results as on observable or
* flowable*/
fun updateRequest(id: Int, requestInfo: RequestInfo): Convertible<Download>
* flowable
* */
fun updateRequest(oldRequestId: Int, newRequest: Request): Convertible<Download>

/**
* Gets all downloads managed by this instance of Fetch.
Expand Down
4 changes: 2 additions & 2 deletions fetch2rx/src/main/java/com/tonyodev/fetch2rx/RxFetchImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ open class RxFetchImpl(namespace: String,
}
}

override fun updateRequest(id: Int, requestInfo: RequestInfo): Convertible<Download> {
override fun updateRequest(oldRequestId: Int, newRequest: Request): Convertible<Download> {
synchronized(lock) {
throwExceptionIfClosed()
val flowable = Flowable.just(Object())
Expand All @@ -137,7 +137,7 @@ open class RxFetchImpl(namespace: String,
throwExceptionIfClosed()
val download: Download?
try {
download = fetchHandler.updateRequest(id, requestInfo)
download = fetchHandler.updateRequest(oldRequestId, newRequest)
} catch (e: Exception) {
throw FetchException(e.message ?: FAILED_TO_ENQUEUE_REQUEST)
}
Expand Down
4 changes: 2 additions & 2 deletions versions.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ ext {
rxAndroid2_version = "2.0.2"
timber_version = "4.7.0"
novoda_bintray_version = "0.8.0"
library_version = "2.1.0-RC9"
library_version_code = 32
library_version = "2.1.0-RC10"
library_version_code = 33
}

0 comments on commit da8e2df

Please sign in to comment.