Skip to content

Commit

Permalink
Added new method hasActiveDownloads(includeAddedDownloads: Boolean, f…
Browse files Browse the repository at this point in the history
…unc: Func<Boolean>)
  • Loading branch information
tonyofrancis committed Mar 3, 2019
1 parent 199d75a commit 666ecd0
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 17 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
Version 3.0.1
- Added new method hasActiveDownloads(includeAddedDownloads: Boolean, func: Func<Boolean>)


Version 3.0.0
*Version 3 comes with a lot of enhancements and fixes*

Expand Down
12 changes: 6 additions & 6 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=3.0.0) ](https://bintray.com/tonyofrancis/maven/fetch2/3.0.0/link)
[ ![Download](https://api.bintray.com/packages/tonyofrancis/maven/fetch2/images/download.svg?version=3.0.1) ](https://bintray.com/tonyofrancis/maven/fetch2/3.0.1/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 @@ -50,7 +50,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:3.0.0"
implementation "com.tonyodev.fetch2:fetch2:3.0.1"
```

Next, get an instance of Fetch and request a download.
Expand Down Expand Up @@ -216,7 +216,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:3.0.0"
implementation "com.tonyodev.fetch2okhttp:fetch2okhttp:3.0.1"
```
Set the OkHttp Downloader for Fetch to use.
```java
Expand All @@ -237,7 +237,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:3.0.0"
implementation "com.tonyodev.fetch2rx:fetch2rx:3.0.1"
```

RxFetch makes it super easy to enqueue download requests and query downloads using rxJava2 functional methods.
Expand Down Expand Up @@ -273,7 +273,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:3.0.0"
implementation "com.tonyodev.fetch2fileserver:fetch2fileserver:3.0.1"
```

Start a FetchFileServer instance and add resource files that it can server to connected clients.
Expand Down Expand Up @@ -382,7 +382,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:3.0.0"
implementation "com.tonyodev.fetchmigrator:fetchmigrator:3.0.1"
```

Then run the Migrator.
Expand Down
9 changes: 9 additions & 0 deletions fetch2/src/main/java/com/tonyodev/fetch2/Fetch.kt
Original file line number Diff line number Diff line change
Expand Up @@ -877,6 +877,15 @@ interface Fetch {
* */
fun removeFetchObserversForDownload(downloadId: Int, vararg fetchObservers: FetchObserver<Download>): Fetch

/** Indicates if this fetch namespace has active(Queued or Downloading) downloads. You can use this value to
* keep a background service ongoing until the callback function returns false.
* @param includeAddedDownloads To include downloads with a status of Added. Added downloads are not considered active.
* @param func the callback function
* @throws FetchException if accessed on ui thread
* @return instance
* */
fun hasActiveDownloads(includeAddedDownloads: Boolean, func: Func<Boolean>): Fetch

/**
* Fetch implementation class. Use this Singleton to get instances of Fetch.
* */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,11 @@ interface FetchDatabaseManager : Closeable {

/**
* Gets the count/sum of all downloads with the status of Queued and Downloading combined.
* @param includeAddedDownloads if to include downloads with the status of Added.
* Added downloads are not considered pending by default.
* @return the pending download count.
* */
fun getPendingCount(): Long
fun getPendingCount(includeAddedDownloads: Boolean): Long

/**
* Interface used for the DownloadManager's delegate.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,16 @@ class FetchDatabaseManagerImpl constructor(context: Context,
" WHERE ${DownloadDatabase.COLUMN_STATUS} = '${Status.QUEUED.value}'" +
" OR ${DownloadDatabase.COLUMN_STATUS} = '${Status.DOWNLOADING}'"

override fun getPendingCount(): Long {
private val pendingCountIncludeAddedQuery = "SELECT ${DownloadDatabase.COLUMN_ID} FROM ${DownloadDatabase.TABLE_NAME}" +
" WHERE ${DownloadDatabase.COLUMN_STATUS} = '${Status.QUEUED.value}'" +
" OR ${DownloadDatabase.COLUMN_STATUS} = '${Status.DOWNLOADING}'" +
" OR ${DownloadDatabase.COLUMN_STATUS} = '${Status.ADDED}'"

override fun getPendingCount(includeAddedDownloads: Boolean): Long {
synchronized(lock) {
return try {
val cursor: Cursor? = database.query(pendingCountQuery)
val query = if (includeAddedDownloads) pendingCountIncludeAddedQuery else pendingCountQuery
val cursor: Cursor? = database.query(query)
val count = cursor?.count?.toLong() ?: -1L
cursor?.close()
count
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ interface FetchHandler : Closeable {
fun getFetchFileServerCatalog(request: Request): List<FileResource>
fun setDownloadConcurrentLimit(downloadConcurrentLimit: Int)
fun replaceExtras(id: Int, extras: Extras): Download
fun hasActiveDownloads(): Boolean
fun hasActiveDownloads(includeAddedDownloads: Boolean): Boolean
fun getListenerSet(): Set<FetchListener>
fun getPendingCount(): Long
fun renameCompletedDownloadFile(id: Int, newFileName: String): Download
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -674,15 +674,15 @@ class FetchHandlerImpl(private val namespace: String,
}
}

override fun hasActiveDownloads(): Boolean {
override fun hasActiveDownloads(includeAddedDownloads: Boolean): Boolean {
if (Thread.currentThread() == Looper.getMainLooper().thread) {
throw FetchException(BLOCKING_CALL_ON_UI_THREAD)
}
return fetchDatabaseManager.getPendingCount() > 0
return fetchDatabaseManager.getPendingCount(includeAddedDownloads) > 0
}

override fun getPendingCount(): Long {
return fetchDatabaseManager.getPendingCount()
return fetchDatabaseManager.getPendingCount(false)
}

private fun cancelDownloadsIfDownloading(downloads: List<DownloadInfo>) {
Expand Down
15 changes: 14 additions & 1 deletion fetch2/src/main/java/com/tonyodev/fetch2/fetch/FetchImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ open class FetchImpl constructor(override val namespace: String,
override val hasActiveDownloads: Boolean
get() {
return try {
fetchHandler.hasActiveDownloads()
fetchHandler.hasActiveDownloads(false)
} catch (e: Exception) {
false
}
Expand Down Expand Up @@ -856,6 +856,19 @@ open class FetchImpl constructor(override val namespace: String,
return this
}

override fun hasActiveDownloads(includeAddedDownloads: Boolean, func: Func<Boolean>): Fetch {
synchronized(lock) {
throwExceptionIfClosed()
handlerWrapper.post {
val hasActiveDownloads = fetchHandler.hasActiveDownloads(includeAddedDownloads)
uiHandler.post {
func.call(hasActiveDownloads)
}
}
}
return this
}

override fun addListener(listener: FetchListener): Fetch {
return addListener(listener, DEFAULT_ENABLE_LISTENER_NOTIFY_ON_ATTACHED)
}
Expand Down
8 changes: 8 additions & 0 deletions fetch2rx/src/main/java/com/tonyodev/fetch2rx/RxFetch.kt
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,14 @@ interface RxFetch {
* */
fun removeFetchObserversForDownload(downloadId: Int, vararg fetchObservers: FetchObserver<Download>): RxFetch

/** Indicates if this fetch namespace has active(Queued or Downloading) downloads. You can use this value to
* keep a background service ongoing until the results returns false.
* @param includeAddedDownloads To include downloads with a status of Added. Added downloads are not considered active.
* @throws FetchException if accessed on ui thread
* @return Convertible with results.
* */
fun hasActiveDownloads(includeAddedDownloads: Boolean): Convertible<Boolean>

/**
* RX Fetch implementation class. Use this Singleton to get instances of RxFetch or Fetch.
* */
Expand Down
17 changes: 16 additions & 1 deletion fetch2rx/src/main/java/com/tonyodev/fetch2rx/RxFetchImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ open class RxFetchImpl(override val namespace: String,
override val hasActiveDownloads: Boolean
get() {
return try {
fetchHandler.hasActiveDownloads()
fetchHandler.hasActiveDownloads(false)
} catch (e: Exception) {
false
}
Expand Down Expand Up @@ -917,6 +917,21 @@ open class RxFetchImpl(override val namespace: String,
}
}

override fun hasActiveDownloads(includeAddedDownloads: Boolean): Convertible<Boolean> {
return synchronized(lock) {
throwExceptionIfClosed()
Flowable.just(includeAddedDownloads)
.subscribeOn(scheduler)
.flatMap {
throwExceptionIfClosed()
val hasActiveDownloads = fetchHandler.hasActiveDownloads(it)
Flowable.just(hasActiveDownloads)
}
.observeOn(uiScheduler)
.toConvertible()
}
}

override fun getContentLengthForRequest(request: Request, fromServer: Boolean): Convertible<Long> {
return synchronized(lock) {
throwExceptionIfClosed()
Expand Down
4 changes: 2 additions & 2 deletions versions.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ ext {
rxAndroid2_version = "2.1.1"
timber_version = "4.7.1"
novoda_bintray_version = "0.9"
library_version = "3.0.0"
library_version_code = 62
library_version = "3.0.1"
library_version_code = 63
}

0 comments on commit 666ecd0

Please sign in to comment.