Skip to content
This repository was archived by the owner on Feb 27, 2022. It is now read-only.

Commit 186d153

Browse files
author
Tsirikoglou Yiannis
committed
Uses okHttp Parallel/Chunk Downloader as described here:
tonyofrancis/Fetch#136
1 parent 13e4d4e commit 186d153

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

android/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ dependencies {
2020
implementation 'com.facebook.react:react-native:+'
2121
if (project.properties['android.useAndroidX'] == true) {
2222
implementation "androidx.tonyodev.fetch2:xfetch2:3.1.4"
23+
implementation "androidx.tonyodev.fetch2okhttp:xfetch2okhttp:3.1.4"
24+
2325
} else {
2426
implementation "com.tonyodev.fetch2:fetch2:3.0.10"
27+
implementation "com.tonyodev.fetch2okhttp:fetch2okhttp:3.0.10"
2528
}
2629
}

android/src/main/java/com/eko/RNBackgroundDownloaderModule.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
import com.facebook.react.bridge.WritableMap;
1515
import com.facebook.react.modules.core.DeviceEventManagerModule;
1616
import com.tonyodev.fetch2.Download;
17+
import com.tonyodev.fetch2core.Downloader;
18+
import com.tonyodev.fetch2okhttp.OkHttpDownloader;
1719
import com.tonyodev.fetch2.Error;
1820
import com.tonyodev.fetch2.Fetch;
1921
import com.tonyodev.fetch2.FetchConfiguration;
@@ -40,6 +42,8 @@
4042

4143
import javax.annotation.Nullable;
4244

45+
import okhttp3.OkHttpClient;
46+
4347
public class RNBackgroundDownloaderModule extends ReactContextBaseJavaModule implements FetchListener {
4448

4549
private static final int TASK_RUNNING = 0;
@@ -72,14 +76,17 @@ public class RNBackgroundDownloaderModule extends ReactContextBaseJavaModule imp
7276
private DeviceEventManagerModule.RCTDeviceEventEmitter ee;
7377
private Date lastProgressReport = new Date();
7478
private HashMap<String, WritableMap> progressReports = new HashMap<>();
75-
private static Object sharedLock = new Object();
79+
private static Object sharedLock = new Object();
7680

7781
public RNBackgroundDownloaderModule(ReactApplicationContext reactContext) {
7882
super(reactContext);
79-
83+
OkHttpClient okHttpClient = new OkHttpClient.Builder().build();
84+
final Downloader okHttpDownloader = new OkHttpDownloader(okHttpClient,
85+
Downloader.FileDownloaderType.PARALLEL);
8086
loadConfigMap();
8187
FetchConfiguration fetchConfiguration = new FetchConfiguration.Builder(this.getReactApplicationContext())
8288
.setDownloadConcurrentLimit(4)
89+
.setHttpDownloader(okHttpDownloader)
8390
.build();
8491
fetch = Fetch.Impl.getInstance(fetchConfiguration);
8592
fetch.addListener(this);
@@ -163,7 +170,7 @@ private void loadConfigMap() {
163170
e.printStackTrace();
164171
}
165172
}
166-
173+
167174
private int convertErrorCode(Error error) {
168175
if ((error == Error.FILE_NOT_CREATED)
169176
|| (error == Error.WRITE_PERMISSION_DENIED)) {
@@ -204,7 +211,7 @@ public void download(ReadableMap options) {
204211
}
205212
request.setPriority(options.hasKey("priority") ? Priority.valueOf(options.getInt("priority")) : Priority.NORMAL);
206213
request.setNetworkType(options.hasKey("network") ? NetworkType.valueOf(options.getInt("network")) : NetworkType.ALL);
207-
214+
208215
fetch.enqueue(request, new Func<Request>() {
209216
@Override
210217
public void call(Request download) {
@@ -213,7 +220,7 @@ public void call(Request download) {
213220
@Override
214221
public void call(Error error) {
215222
//An error occurred when enqueuing a request.
216-
223+
217224
WritableMap params = Arguments.createMap();
218225
params.putString("id", id);
219226
params.putString("error", error.toString());

0 commit comments

Comments
 (0)