mirror of
https://github.com/zoriya/react-native-background-downloader.git
synced 2026-06-04 10:44:44 +00:00
cossolidate progress report to one per id
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
|
||||
package com.eko;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
@@ -63,7 +62,7 @@ public class RNBackgroundDownloadModule extends ReactContextBaseJavaModule imple
|
||||
private Map<Integer, RNBGDTaskConfig> requestIdToConfig = new HashMap<>();
|
||||
private DeviceEventManagerModule.RCTDeviceEventEmitter ee;
|
||||
private Date lastProgressReport = new Date();
|
||||
private WritableArray progressReports = Arguments.createArray();
|
||||
private HashMap<String, WritableMap> progressReports = new HashMap<>();
|
||||
|
||||
public RNBackgroundDownloadModule(ReactApplicationContext reactContext) {
|
||||
super(reactContext);
|
||||
@@ -271,12 +270,16 @@ public class RNBackgroundDownloadModule extends ReactContextBaseJavaModule imple
|
||||
params.putInt("written", (int)download.getDownloaded());
|
||||
params.putInt("total", (int)download.getTotal());
|
||||
params.putDouble("percent", ((double)download.getProgress()) / 100);
|
||||
progressReports.pushMap(params);
|
||||
progressReports.put(config.id, params);
|
||||
Date now = new Date();
|
||||
if (now.getTime() - lastProgressReport.getTime() > 1500) {
|
||||
ee.emit("downloadProgress", progressReports);
|
||||
WritableArray reportsArray = Arguments.createArray();
|
||||
for (WritableMap report : progressReports.values()) {
|
||||
reportsArray.pushMap(report);
|
||||
}
|
||||
ee.emit("downloadProgress", reportsArray);
|
||||
lastProgressReport = now;
|
||||
progressReports = Arguments.createArray();
|
||||
progressReports.clear();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -21,9 +21,9 @@ static CompletionHandler storedCompletionHandler;
|
||||
NSMutableDictionary<NSString *, NSURLSessionDownloadTask *> *idToTaskMap;
|
||||
NSMutableDictionary<NSString *, NSData *> *idToResumeDataMap;
|
||||
NSMutableDictionary<NSString *, NSNumber *> *idToPercentMap;
|
||||
NSMutableDictionary<NSString *, NSDictionary *> *progressReports;
|
||||
NSOperationQueue *downloadOperationsQueue;
|
||||
NSDate *lastProgressReport;
|
||||
NSMutableArray<NSDictionary *> *progressReports;
|
||||
}
|
||||
|
||||
RCT_EXPORT_MODULE();
|
||||
@@ -68,7 +68,7 @@ RCT_EXPORT_MODULE();
|
||||
NSString *sessonIdentifier = [bundleIdentifier stringByAppendingString:@".backgrounddownloadtask"];
|
||||
sessionConfig = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:sessonIdentifier];
|
||||
downloadOperationsQueue = [[NSOperationQueue alloc] init];
|
||||
progressReports = [[NSMutableArray alloc] init];
|
||||
progressReports = [[NSMutableDictionary alloc] init];
|
||||
lastProgressReport = [[NSDate alloc] init];
|
||||
}
|
||||
return self;
|
||||
@@ -220,14 +220,14 @@ RCT_EXPORT_METHOD(checkForExistingDownloads: (RCTPromiseResolveBlock)resolve rej
|
||||
NSNumber *prevPercent = idToPercentMap[taskCofig.id];
|
||||
NSNumber *percent = [NSNumber numberWithFloat:(float)totalBytesWritten/(float)totalBytesExpectedToWrite];
|
||||
if ([percent floatValue] - [prevPercent floatValue] > 0.01f) {
|
||||
[progressReports addObject:@{@"id": taskCofig.id, @"written": [NSNumber numberWithLongLong: totalBytesWritten], @"total": [NSNumber numberWithLongLong: totalBytesExpectedToWrite], @"percent": percent}];
|
||||
progressReports[taskCofig.id] = @{@"id": taskCofig.id, @"written": [NSNumber numberWithLongLong: totalBytesWritten], @"total": [NSNumber numberWithLongLong: totalBytesExpectedToWrite], @"percent": percent};
|
||||
idToPercentMap[taskCofig.id] = percent;
|
||||
}
|
||||
|
||||
NSDate *now = [[NSDate alloc] init];
|
||||
if ([now timeIntervalSinceDate:lastProgressReport] > 1.5 && progressReports.count > 0) {
|
||||
if (self.bridge) {
|
||||
[self sendEventWithName:@"downloadProgress" body:progressReports];
|
||||
[self sendEventWithName:@"downloadProgress" body:[progressReports allValues]];
|
||||
}
|
||||
lastProgressReport = now;
|
||||
[progressReports removeAllObjects];
|
||||
|
||||
Reference in New Issue
Block a user