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

Commit 1b18a01

Browse files
author
Elad Gil
committed
cossolidate progress report to one per id
1 parent 858be9d commit 1b18a01

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
package com.eko;
32

43
import android.annotation.SuppressLint;
@@ -63,7 +62,7 @@ public class RNBackgroundDownloadModule extends ReactContextBaseJavaModule imple
6362
private Map<Integer, RNBGDTaskConfig> requestIdToConfig = new HashMap<>();
6463
private DeviceEventManagerModule.RCTDeviceEventEmitter ee;
6564
private Date lastProgressReport = new Date();
66-
private WritableArray progressReports = Arguments.createArray();
65+
private HashMap<String, WritableMap> progressReports = new HashMap<>();
6766

6867
public RNBackgroundDownloadModule(ReactApplicationContext reactContext) {
6968
super(reactContext);
@@ -271,12 +270,16 @@ public void onProgress(Download download, long l, long l1) {
271270
params.putInt("written", (int)download.getDownloaded());
272271
params.putInt("total", (int)download.getTotal());
273272
params.putDouble("percent", ((double)download.getProgress()) / 100);
274-
progressReports.pushMap(params);
273+
progressReports.put(config.id, params);
275274
Date now = new Date();
276275
if (now.getTime() - lastProgressReport.getTime() > 1500) {
277-
ee.emit("downloadProgress", progressReports);
276+
WritableArray reportsArray = Arguments.createArray();
277+
for (WritableMap report : progressReports.values()) {
278+
reportsArray.pushMap(report);
279+
}
280+
ee.emit("downloadProgress", reportsArray);
278281
lastProgressReport = now;
279-
progressReports = Arguments.createArray();
282+
progressReports.clear();
280283
}
281284
}
282285

ios/RNBackgroundDownload.m

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ @implementation RNBackgroundDownload {
2121
NSMutableDictionary<NSString *, NSURLSessionDownloadTask *> *idToTaskMap;
2222
NSMutableDictionary<NSString *, NSData *> *idToResumeDataMap;
2323
NSMutableDictionary<NSString *, NSNumber *> *idToPercentMap;
24+
NSMutableDictionary<NSString *, NSDictionary *> *progressReports;
2425
NSOperationQueue *downloadOperationsQueue;
2526
NSDate *lastProgressReport;
26-
NSMutableArray<NSDictionary *> *progressReports;
2727
}
2828

2929
RCT_EXPORT_MODULE();
@@ -68,7 +68,7 @@ - (id) init {
6868
NSString *sessonIdentifier = [bundleIdentifier stringByAppendingString:@".backgrounddownloadtask"];
6969
sessionConfig = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:sessonIdentifier];
7070
downloadOperationsQueue = [[NSOperationQueue alloc] init];
71-
progressReports = [[NSMutableArray alloc] init];
71+
progressReports = [[NSMutableDictionary alloc] init];
7272
lastProgressReport = [[NSDate alloc] init];
7373
}
7474
return self;
@@ -220,14 +220,14 @@ - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTas
220220
NSNumber *prevPercent = idToPercentMap[taskCofig.id];
221221
NSNumber *percent = [NSNumber numberWithFloat:(float)totalBytesWritten/(float)totalBytesExpectedToWrite];
222222
if ([percent floatValue] - [prevPercent floatValue] > 0.01f) {
223-
[progressReports addObject:@{@"id": taskCofig.id, @"written": [NSNumber numberWithLongLong: totalBytesWritten], @"total": [NSNumber numberWithLongLong: totalBytesExpectedToWrite], @"percent": percent}];
223+
progressReports[taskCofig.id] = @{@"id": taskCofig.id, @"written": [NSNumber numberWithLongLong: totalBytesWritten], @"total": [NSNumber numberWithLongLong: totalBytesExpectedToWrite], @"percent": percent};
224224
idToPercentMap[taskCofig.id] = percent;
225225
}
226226

227227
NSDate *now = [[NSDate alloc] init];
228228
if ([now timeIntervalSinceDate:lastProgressReport] > 1.5 && progressReports.count > 0) {
229229
if (self.bridge) {
230-
[self sendEventWithName:@"downloadProgress" body:progressReports];
230+
[self sendEventWithName:@"downloadProgress" body:[progressReports allValues]];
231231
}
232232
lastProgressReport = now;
233233
[progressReports removeAllObjects];

0 commit comments

Comments
 (0)