Skip to content

Commit

Permalink
Added bew FetchConifguration option for FetchListeners
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyofrancis committed Jun 13, 2018
1 parent a12f3b6 commit 7a1a5cd
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 14 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG
@@ -1,3 +1,8 @@
Version 2.1.0-RC4
- Added new method enableListenerNotifyOnAttached(enabled) to FetchConfiguration.Builder class.
This option allows Fetch to notify the newly attached listeners of the download status
of all downloads managed by the namespace. Default is false.

Version 2.1.0-RC3
- Removed used AppCompat Dependency on library modules.

Expand Down
15 changes: 8 additions & 7 deletions README.md
@@ -1,5 +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-RC3) ](https://bintray.com/tonyofrancis/maven/fetch2/2.1.0-RC3/link)
[ ![Download](https://api.bintray.com/packages/tonyofrancis/maven/fetch2/images/download.svg?version=2.1.0-RC4) ](https://bintray.com/tonyofrancis/maven/fetch2/2.1.0-RC4/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 @@ -47,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-RC3"
implementation "com.tonyodev.fetch2:fetch2:2.1.0-RC4"
```

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

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

Start a FetchFileServer instance and add resource files that it can server to connected clients.
Expand Down Expand Up @@ -308,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-RC3"
implementation "com.tonyodev.fetch2downloaders:fetch2downloaders:2.1.0-RC4"
```

Then create an instance of Fetch and enqueue the download.
Expand Down Expand Up @@ -462,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-RC3"
implementation "com.tonyodev.fetchmigrator:fetchmigrator:2.1.0-RC4"
```
Then run the Migrator.
Expand Down
Expand Up @@ -89,7 +89,7 @@ public void useAppContext() throws Exception {
listenerCoordinator);
fetchHandler = new FetchHandlerImpl(namespace, databaseManager, downloadManager,
priorityListProcessorImpl, fetchLogger, autoStart,
client, tempDir, listenerCoordinator);
client, tempDir, listenerCoordinator, false, uiHandler);
}

@Test
Expand Down
18 changes: 16 additions & 2 deletions fetch2/src/main/java/com/tonyodev/fetch2/FetchConfiguration.kt
Expand Up @@ -21,7 +21,8 @@ class FetchConfiguration private constructor(val appContext: Context,
val autoStart: Boolean,
val retryOnNetworkGain: Boolean,
val fileServerDownloader: FileServerDownloader?,
val md5CheckingEnabled: Boolean) {
val md5CheckingEnabled: Boolean,
val enableListenerNotifyOnAttached: Boolean) {

/** Used to create an instance of Fetch Configuration.*/
class Builder(context: Context) {
Expand All @@ -39,6 +40,7 @@ class FetchConfiguration private constructor(val appContext: Context,
private var retryOnNetworkGain = DEFAULT_RETRY_ON_NETWORK_GAIN
private var fileServerDownloader: FileServerDownloader? = null
private var md5CheckEnabled = DEFAULT_MD5_CHECK_ENABLED
private var enableListenerNotifyOnAttached = DEFAULT_ENABLE_LISTENER_NOTIFY_ON_ATTACHED

/** Sets the namespace which Fetch operates in. Fetch uses
* a namespace to create a database that the instance will use. Downloads
Expand Down Expand Up @@ -194,6 +196,17 @@ class FetchConfiguration private constructor(val appContext: Context,
return this
}

/**
* Allows Fetch to notify the newly attached listeners of the download status
* of all downloads managed by the namespace. Default is false.
* @param enabled true or false
* @return Builder
* */
fun enableListenerNotifyOnAttached(enabled: Boolean): Builder {
this.enableListenerNotifyOnAttached = enabled
return this
}

/**
* Build FetchConfiguration instance.
* @return new FetchConfiguration instance.
Expand All @@ -219,7 +232,8 @@ class FetchConfiguration private constructor(val appContext: Context,
autoStart = autoStart,
retryOnNetworkGain = retryOnNetworkGain,
fileServerDownloader = fileServerDownloader,
md5CheckingEnabled = md5CheckEnabled)
md5CheckingEnabled = md5CheckEnabled,
enableListenerNotifyOnAttached = enableListenerNotifyOnAttached)
}

}
Expand Down
@@ -1,5 +1,6 @@
package com.tonyodev.fetch2.fetch

import android.os.Handler
import com.tonyodev.fetch2.*
import com.tonyodev.fetch2.database.DatabaseManager
import com.tonyodev.fetch2.database.DownloadInfo
Expand All @@ -21,7 +22,9 @@ class FetchHandlerImpl(private val namespace: String,
private val autoStart: Boolean,
private val httpDownloader: Downloader,
private val fileTempDir: String,
private val listenerCoordinator: ListenerCoordinator) : FetchHandler {
private val listenerCoordinator: ListenerCoordinator,
private val enableListenerNotifyOnAttached: Boolean,
private val uiHandler: Handler) : FetchHandler {

private val listenerId = UUID.randomUUID().hashCode()
private val listenerSet = mutableSetOf<FetchListener>()
Expand Down Expand Up @@ -514,6 +517,40 @@ class FetchHandlerImpl(private val namespace: String,
startPriorityQueueIfNotStarted()
listenerSet.add(listener)
listenerCoordinator.addListener(listenerId, listener)
if (enableListenerNotifyOnAttached) {
val downloads = databaseManager.get()
downloads.forEach {
uiHandler.post {
when (it.status) {
Status.COMPLETED -> {
listener.onCompleted(it)
}
Status.FAILED -> {
listener.onError(it)
}
Status.CANCELLED -> {
listener.onCancelled(it)
}
Status.DELETED -> {
listener.onDeleted(it)
}
Status.PAUSED -> {
listener.onPaused(it)
}
Status.QUEUED -> {
listener.onQueued(it, false)
}
Status.REMOVED -> {
listener.onRemoved(it)
}
Status.DOWNLOADING -> {
}
Status.NONE -> {
}
}
}
}
}
logger.d("Added listener $listener")
}

Expand Down
Expand Up @@ -117,7 +117,9 @@ object FetchModulesBuilder {
autoStart = fetchConfiguration.autoStart,
httpDownloader = fetchConfiguration.httpDownloader,
fileTempDir = getFileTempDir(fetchConfiguration.appContext),
listenerCoordinator = listenerCoordinator)
listenerCoordinator = listenerCoordinator,
enableListenerNotifyOnAttached = fetchConfiguration.enableListenerNotifyOnAttached,
uiHandler = uiHandler)
}

}
Expand Down
1 change: 1 addition & 0 deletions fetch2/src/main/java/com/tonyodev/fetch2/util/Defaults.kt
Expand Up @@ -20,6 +20,7 @@ const val DEFAULT_RETRY_ON_NETWORK_GAIN = true
const val DEFAULT_FILE_SLICE_NO_LIMIT_SET = -1
const val DEFAULT_INSTANCE_NAMESPACE = "LibGlobalFetchLib"
const val DEFAULT_MD5_CHECK_ENABLED = false
const val DEFAULT_ENABLE_LISTENER_NOTIFY_ON_ATTACHED = false
val defaultEmptyHeaderMap = mapOf<String, String>()
val defaultNetworkType = NetworkType.ALL
val defaultGlobalNetworkType = NetworkType.GLOBAL_OFF
Expand Down
4 changes: 2 additions & 2 deletions versions.gradle
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-RC3"
library_version_code = 26
library_version = "2.1.0-RC4"
library_version_code = 27
}

0 comments on commit 7a1a5cd

Please sign in to comment.