Skip to content

Commit e0ad0c7

Browse files
committed
Can now use your own unique id for a request.
Version 2.0.0-RC8 - Can now use your own unique id for a request. - Added new methods removeAllWithStatus(status: Status) and fun deleteAllWithStatus(status: Status) - RxJava library update
1 parent 8fa2e51 commit e0ad0c7

File tree

11 files changed

+139
-12
lines changed

11 files changed

+139
-12
lines changed

CHANGELOG

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
Version 2.0.0-RC8
2+
- Can now use your own unique id for a request.
3+
- Added new methods removeAllWithStatus(status: Status) and fun deleteAllWithStatus(status: Status)
4+
- RxJava library update
5+
16
Version 2.0.0-RC7
27
- Kotlin update.
38
- Fixed download priority issue.

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[![Build Status](https://travis-ci.org/tonyofrancis/Fetch.svg?branch=v2)](https://travis-ci.org/tonyofrancis/Fetch)
2-
[ ![Download](https://api.bintray.com/packages/tonyofrancis/maven/fetch2/images/download.svg?version=2.0.0-RC7) ](https://bintray.com/tonyofrancis/maven/fetch2/2.0.0-RC7/link)
2+
[ ![Download](https://api.bintray.com/packages/tonyofrancis/maven/fetch2/images/download.svg?version=2.0.0-RC8) ](https://bintray.com/tonyofrancis/maven/fetch2/2.0.0-RC8/link)
33
[![Android Arsenal](https://img.shields.io/badge/Android%20Arsenal-Android%20Networking-blue.svg?style=flat)](https://android-arsenal.com/details/1/5196)
44
[![License](https://img.shields.io/badge/license-Apache%202.0-blue.svg)](https://github.com/tonyofrancis/Fetch/blob/master/LICENSE)
55

@@ -44,7 +44,7 @@ How to use Fetch
4444
Using Fetch is easy! Just add the Gradle dependency to your application's build.gradle file.
4545

4646
```java
47-
implementation "com.tonyodev.fetch2:fetch2:2.0.0-RC7"
47+
implementation "com.tonyodev.fetch2:fetch2:2.0.0-RC8"
4848
```
4949

5050
Next, get an instance of Fetch using the builder, and request a download.
@@ -239,7 +239,7 @@ to use the OkHttp Downloader instead. You can create your own custom downloaders
239239
if necessary. See the Java docs for details.
240240
241241
```java
242-
implementation "com.tonyodev.fetch2downloaders:fetch2downloaders:2.0.0-RC7"
242+
implementation "com.tonyodev.fetch2downloaders:fetch2downloaders:2.0.0-RC8"
243243
```
244244
Set the OkHttp Downloader for Fetch to use.
245245
```java
@@ -260,7 +260,7 @@ If you would like to take advantage of RxJava2 features when using Fetch,
260260
add the following gradle dependency to your application's build.gradle file.
261261

262262
```java
263-
implementation "com.tonyodev.fetch2rx:fetch2rx:2.0.0-RC7"
263+
implementation "com.tonyodev.fetch2rx:fetch2rx:2.0.0-RC8"
264264
```
265265

266266
RxFetch makes it super easy to enqueue download requests and query downloads using rxJava2 functional methods.
@@ -292,7 +292,7 @@ Fetch1 Migration
292292

293293
Migrate downloads from Fetch1 to Fetch2 using the migration assistant. Add the following gradle dependency to your application's build.gradle file.
294294
```java
295-
implementation "com.tonyodev.fetchmigrator:fetchmigrator:2.0.0-RC7"
295+
implementation "com.tonyodev.fetchmigrator:fetchmigrator:2.0.0-RC8"
296296
```
297297
298298
Then run the Migrator.

fetch2/src/main/java/com/tonyodev/fetch2/Error.kt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,17 @@ enum class Error constructor(val value: Int) {
5353
* This error is thrown by the FetchBuilder.
5454
* @see com.tonyodev.fetch2.Fetch.Builder
5555
* */
56-
FETCH_ALREADY_EXIST(12);
56+
FETCH_ALREADY_EXIST(12),
57+
58+
/** Indicates that a request in the Fetch database already has this unique id.
59+
* Ids must be unique.*/
60+
REQUEST_WITH_ID_ALREADY_EXIST(13),
61+
62+
/** Indicates that a request in the Fetch database already has this file path. File Paths
63+
* have to be unique for each request. This limitation maintains consistency and prevents data lose.
64+
* Fetch cannot write data to two different downloads with the same file path.
65+
* */
66+
REQUEST_WITH_FILE_PATH_ALREADY_EXIST(14);
5767

5868
companion object {
5969

@@ -74,6 +84,8 @@ enum class Error constructor(val value: Int) {
7484
10 -> DOWNLOAD_NOT_FOUND
7585
11 -> FETCH_DATABASE_ERROR
7686
12 -> FETCH_ALREADY_EXIST
87+
13 -> REQUEST_WITH_ID_ALREADY_EXIST
88+
14 -> REQUEST_WITH_FILE_PATH_ALREADY_EXIST
7789
else -> UNKNOWN
7890
}
7991
}

fetch2/src/main/java/com/tonyodev/fetch2/Fetch.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,14 @@ interface Fetch {
117117
* */
118118
fun removeAll()
119119

120+
/**
121+
* Remove all downloads with the specified status in this instance of Fetch.
122+
* The downloaded files for removed downloads are not deleted.
123+
* @param status status
124+
* @throws FetchException if this instance of Fetch has been closed.
125+
* */
126+
fun removeAllWithStatus(status: Status)
127+
120128
/**
121129
* Delete a download managed by this instance of Fetch.
122130
* The downloaded file is deleted.
@@ -140,6 +148,14 @@ interface Fetch {
140148
* */
141149
fun deleteAll()
142150

151+
/**
152+
* Deletes all downloads with the specified status in this instance of Fetch.
153+
* The downloaded files are also deleted.
154+
* @param status status
155+
* @throws FetchException if this instance of Fetch has been closed.
156+
* */
157+
fun deleteAllWithStatus(status: Status)
158+
143159
/**
144160
* Cancel a non completed download managed by this instance of Fetch.
145161
* The downloaded file for the cancelled download is not deleted.

fetch2/src/main/java/com/tonyodev/fetch2/FetchErrorUtils.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ fun getErrorFromThrowable(throwable: Throwable): Error {
1111
fun getErrorFromMessage(message: String?): Error {
1212
return if (message == null) {
1313
Error.UNKNOWN
14+
} else if (message.contains(UNIQUE_ID_DATABASE)) {
15+
Error.REQUEST_WITH_ID_ALREADY_EXIST
16+
} else if (message.contains(UNIQUE_FILE_PATH_DATABASE)) {
17+
Error.REQUEST_WITH_FILE_PATH_ALREADY_EXIST
1418
} else if (message.equals(EMPTY_RESPONSE_BODY, true)) {
1519
Error.EMPTY_RESPONSE_FROM_SERVER
1620
} else if (message.equals(FNC, ignoreCase = true) || message.equals(ENOENT, ignoreCase = true)) {

fetch2/src/main/java/com/tonyodev/fetch2/Request.kt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,23 @@ package com.tonyodev.fetch2
55
* begin the download process.
66
* */
77
open class Request constructor(
8+
/** Used to identify a download. Must be UNIQUE.*/
9+
val id: Int,
10+
811
/** The url where the file will be downloaded from.*/
912
val url: String,
1013

1114
/** The file eg(/files/download.txt) where the file will be
1215
* downloaded to and saved on disk.*/
1316
val file: String) : RequestInfo() {
1417

15-
/** Used to identify a download.*/
16-
val id: Int = (url.hashCode() * 31) + file.hashCode()
18+
constructor(
19+
/** The url where the file will be downloaded from.*/
20+
url: String,
21+
22+
/** The file eg(/files/download.txt) where the file will be
23+
* downloaded to and saved on disk.*/
24+
file: String) : this((url.hashCode() * 31) + file.hashCode(), url, file)
1725

1826
override fun equals(other: Any?): Boolean {
1927
if (this === other) return true

fetch2/src/main/java/com/tonyodev/fetch2/fetch/FetchHandler.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@ interface FetchHandler : Closeable {
2424
fun remove(ids: IntArray): List<Download>
2525
fun removeGroup(id: Int): List<Download>
2626
fun removeAll(): List<Download>
27+
fun removeAllWithStatus(status: Status): List<Download>
2728
fun delete(ids: IntArray): List<Download>
2829
fun deleteGroup(id: Int): List<Download>
2930
fun deleteAll(): List<Download>
31+
fun deleteAllWithStatus(status: Status): List<Download>
3032
fun cancel(ids: IntArray): List<Download>
3133
fun cancelGroup(id: Int): List<Download>
3234
fun cancelAll(): List<Download>

fetch2/src/main/java/com/tonyodev/fetch2/fetch/FetchHandlerImpl.kt

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,22 @@ open class FetchHandlerImpl(val namespace: String,
202202
return downloadInfoList
203203
}
204204

205+
override fun removeAllWithStatus(status: Status): List<Download> {
206+
throwExceptionIfClosed()
207+
val downloadInfoList = databaseManager.getByStatus(status)
208+
downloadInfoList.forEach {
209+
if (isDownloading(it.id)) {
210+
cancelDownload(it.id)
211+
}
212+
}
213+
databaseManager.delete(downloadInfoList)
214+
val removedStatus = Status.REMOVED
215+
downloadInfoList.forEach {
216+
it.status = removedStatus
217+
}
218+
return downloadInfoList
219+
}
220+
205221
override fun delete(ids: IntArray): List<Download> {
206222
throwExceptionIfClosed()
207223
ids.forEach {
@@ -270,6 +286,30 @@ open class FetchHandlerImpl(val namespace: String,
270286
return downloadInfoList
271287
}
272288

289+
override fun deleteAllWithStatus(status: Status): List<Download> {
290+
throwExceptionIfClosed()
291+
val downloadInfoList = databaseManager.getByStatus(status)
292+
downloadInfoList.forEach {
293+
if (isDownloading(it.id)) {
294+
cancelDownload(it.id)
295+
}
296+
}
297+
databaseManager.delete(downloadInfoList)
298+
val deletedStatus = Status.DELETED
299+
downloadInfoList.forEach {
300+
it.status = deletedStatus
301+
try {
302+
val file = File(it.file)
303+
if (file.exists()) {
304+
file.delete()
305+
}
306+
} catch (e: Exception) {
307+
logger.d("Failed to delete file ${it.file}", e)
308+
}
309+
}
310+
return downloadInfoList
311+
}
312+
273313
override fun cancel(ids: IntArray): List<Download> {
274314
throwExceptionIfClosed()
275315
ids.forEach {

fetch2/src/main/java/com/tonyodev/fetch2/fetch/FetchImpl.kt

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,25 @@ open class FetchImpl constructor(override val namespace: String,
244244
}
245245
}
246246

247+
override fun removeAllWithStatus(status: Status) {
248+
synchronized(lock) {
249+
fetchHandler.throwExceptionIfClosed()
250+
handler.post {
251+
try {
252+
val downloads = fetchHandler.removeAllWithStatus(status)
253+
uiHandler.post {
254+
downloads.forEach {
255+
logger.d("Removed download $it")
256+
fetchListenerProvider.mainListener.onRemoved(it)
257+
}
258+
}
259+
} catch (e: FetchException) {
260+
logger.e("Fetch with namespace $namespace error", e)
261+
}
262+
}
263+
}
264+
}
265+
247266
override fun delete(vararg ids: Int) {
248267
synchronized(lock) {
249268
fetchHandler.throwExceptionIfClosed()
@@ -301,6 +320,25 @@ open class FetchImpl constructor(override val namespace: String,
301320
}
302321
}
303322

323+
override fun deleteAllWithStatus(status: Status) {
324+
synchronized(lock) {
325+
fetchHandler.throwExceptionIfClosed()
326+
handler.post {
327+
try {
328+
val downloads = fetchHandler.deleteAllWithStatus(status)
329+
uiHandler.post {
330+
downloads.forEach {
331+
logger.d("Deleted download $it")
332+
fetchListenerProvider.mainListener.onDeleted(it)
333+
}
334+
}
335+
} catch (e: FetchException) {
336+
logger.e("Fetch with namespace $namespace error", e)
337+
}
338+
}
339+
}
340+
}
341+
304342
override fun cancel(vararg ids: Int) {
305343
synchronized(lock) {
306344
fetchHandler.throwExceptionIfClosed()

fetch2/src/main/java/com/tonyodev/fetch2/util/ErrorStrings.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,6 @@ const val ENOSPC = "write failed: ENOSPC (No space left on device)"
1919
const val DATABASE_DISK_FULL = "database or disk is full (code 13)"
2020
const val FETCH_DATABASE_ERROR = "Fetch data base error"
2121
const val FETCH_ALREADY_EXIST = "already exists. You cannot have more than one active " +
22-
"instance of Fetch with the same namespace. Did your forget to call close the old instance?"
22+
"instance of Fetch with the same namespace. Did your forget to call close the old instance?"
23+
const val UNIQUE_ID_DATABASE = "UNIQUE constraint failed: requests._id"
24+
const val UNIQUE_FILE_PATH_DATABASE = "UNIQUE constraint failed: requests._file"

versions.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ ext {
1212
library_target_version = 27
1313
library_build_tools_version = "27.0.3"
1414
gradle_tools_version = "3.0.1"
15-
rxJava2_version = "2.1.8"
15+
rxJava2_version = "2.1.9"
1616
rxAndroid2_version = "2.0.1"
1717
timber_version = "4.6.0"
1818
novoda_bintray_version = "0.8.0"
19-
library_version = "2.0.0-RC7"
20-
library_version_code = 8
19+
library_version = "2.0.0-RC8"
20+
library_version_code = 9
2121
}

0 commit comments

Comments
 (0)