mirror of
https://github.com/status-im/react-native-fs.git
synced 2025-02-28 23:10:29 +00:00
Added progressDivider
This commit is contained in:
parent
39e356d1f8
commit
1bc9481a47
@ -9,6 +9,7 @@ typedef void (^ProgressCallback)(NSNumber*, NSNumber*);
|
|||||||
|
|
||||||
@property (copy) NSString* fromUrl;
|
@property (copy) NSString* fromUrl;
|
||||||
@property (copy) NSString* toFile;
|
@property (copy) NSString* toFile;
|
||||||
|
@property (copy) NSNumber* progressDivider;
|
||||||
@property (copy) DownloaderCallback callback; // Download has finished (data written)
|
@property (copy) DownloaderCallback callback; // Download has finished (data written)
|
||||||
@property (copy) ErrorCallback errorCallback; // Something went wrong
|
@property (copy) ErrorCallback errorCallback; // Something went wrong
|
||||||
@property (copy) BeginCallback beginCallback; // Download has started (headers received)
|
@property (copy) BeginCallback beginCallback; // Download has started (headers received)
|
||||||
|
15
Downloader.m
15
Downloader.m
@ -10,6 +10,7 @@
|
|||||||
|
|
||||||
@property (retain) NSURLConnection* connection;
|
@property (retain) NSURLConnection* connection;
|
||||||
@property (retain) NSNumber* statusCode;
|
@property (retain) NSNumber* statusCode;
|
||||||
|
@property (retain) NSNumber* lastProgressValue;
|
||||||
@property (retain) NSNumber* contentLength;
|
@property (retain) NSNumber* contentLength;
|
||||||
@property (retain) NSNumber* bytesWritten;
|
@property (retain) NSNumber* bytesWritten;
|
||||||
|
|
||||||
@ -72,7 +73,19 @@
|
|||||||
|
|
||||||
_bytesWritten = [NSNumber numberWithUnsignedInteger:[_bytesWritten unsignedIntegerValue] + data.length];
|
_bytesWritten = [NSNumber numberWithUnsignedInteger:[_bytesWritten unsignedIntegerValue] + data.length];
|
||||||
|
|
||||||
return _params.progressCallback(_contentLength, _bytesWritten);
|
if (_params.progressDivider <= 1) {
|
||||||
|
return _params.progressCallback(_contentLength, _bytesWritten);
|
||||||
|
} else {
|
||||||
|
NSLog(@"---Progress callback---");
|
||||||
|
long double progress = Math.round(((double) _bytesWritten * 100) / _contentLength);
|
||||||
|
if (progress % param.progressDivider == 0) {
|
||||||
|
if ((progress != _lastProgressValue) || (_bytesWritten == _contentLength)) {
|
||||||
|
NSLog(@"---Progress callback EMIT--- %zu", progress);
|
||||||
|
_lastProgressValue = [NSNumber numberWithLong:progress];
|
||||||
|
return _params.progressCallback(_contentLength, _bytesWritten);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -155,7 +155,7 @@ var RNFS = {
|
|||||||
.catch(convertError);
|
.catch(convertError);
|
||||||
},
|
},
|
||||||
|
|
||||||
downloadFile(fromUrl, toFile, begin, progress) {
|
downloadFile(fromUrl, toFile, begin, progress, progressDivider = 1) {
|
||||||
var jobId = getJobId();
|
var jobId = getJobId();
|
||||||
var subscriptions = [];
|
var subscriptions = [];
|
||||||
|
|
||||||
@ -167,7 +167,7 @@ var RNFS = {
|
|||||||
subscriptions.push(NativeAppEventEmitter.addListener('DownloadProgress-' + jobId, progress));
|
subscriptions.push(NativeAppEventEmitter.addListener('DownloadProgress-' + jobId, progress));
|
||||||
}
|
}
|
||||||
|
|
||||||
return _downloadFile(fromUrl, toFile, jobId)
|
return _downloadFile(fromUrl, toFile, jobId, progressDivider)
|
||||||
.then(res => {
|
.then(res => {
|
||||||
subscriptions.forEach(sub => sub.remove());
|
subscriptions.forEach(sub => sub.remove());
|
||||||
return res;
|
return res;
|
||||||
|
@ -175,6 +175,7 @@ RCT_EXPORT_METHOD(moveFile:(NSString *)filepath
|
|||||||
RCT_EXPORT_METHOD(downloadFile:(NSString *)urlStr
|
RCT_EXPORT_METHOD(downloadFile:(NSString *)urlStr
|
||||||
filepath:(NSString *)filepath
|
filepath:(NSString *)filepath
|
||||||
jobId:(nonnull NSNumber *)jobId
|
jobId:(nonnull NSNumber *)jobId
|
||||||
|
progressDivider:(nonnull NSNumber *)progressDivider
|
||||||
callback:(RCTResponseSenderBlock)callback)
|
callback:(RCTResponseSenderBlock)callback)
|
||||||
{
|
{
|
||||||
|
|
||||||
@ -182,6 +183,7 @@ RCT_EXPORT_METHOD(downloadFile:(NSString *)urlStr
|
|||||||
|
|
||||||
params.fromUrl = urlStr;
|
params.fromUrl = urlStr;
|
||||||
params.toFile = filepath;
|
params.toFile = filepath;
|
||||||
|
params.progressDivider = progressDivider;
|
||||||
|
|
||||||
params.callback = ^(NSNumber* statusCode, NSNumber* bytesWritten) {
|
params.callback = ^(NSNumber* statusCode, NSNumber* bytesWritten) {
|
||||||
NSMutableDictionary* result = [[NSMutableDictionary alloc] initWithDictionary: @{@"jobId": jobId,
|
NSMutableDictionary* result = [[NSMutableDictionary alloc] initWithDictionary: @{@"jobId": jobId,
|
||||||
|
@ -19,6 +19,7 @@ public class DownloadParams {
|
|||||||
|
|
||||||
public URL src;
|
public URL src;
|
||||||
public File dest;
|
public File dest;
|
||||||
|
public float progressDivider;
|
||||||
public OnTaskCompleted onTaskCompleted;
|
public OnTaskCompleted onTaskCompleted;
|
||||||
public OnDownloadBegin onDownloadBegin;
|
public OnDownloadBegin onDownloadBegin;
|
||||||
public OnDownloadProgress onDownloadProgress;
|
public OnDownloadProgress onDownloadProgress;
|
||||||
|
@ -81,12 +81,16 @@ public class Downloader extends AsyncTask<DownloadParams, int[], DownloadResult>
|
|||||||
}
|
}
|
||||||
|
|
||||||
total += count;
|
total += count;
|
||||||
double progress = Math.round(((double) total * 100) / lengthOfFile);
|
if (param.progressDivider <= 1) {
|
||||||
if (progress % 10 == 0) {
|
publishProgress(new int[]{lengthOfFile, total});
|
||||||
if ((progress != lastProgressValue) || (total == lengthOfFile)) {
|
} else {
|
||||||
Log.d("Downloader", "EMIT: " + String.valueOf(progress) + ", TOTAL:" + String.valueOf(total));
|
double progress = Math.round(((double) total * 100) / lengthOfFile);
|
||||||
lastProgressValue = progress;
|
if (progress % param.progressDivider == 0) {
|
||||||
publishProgress(new int[]{lengthOfFile, total});
|
if ((progress != lastProgressValue) || (total == lengthOfFile)) {
|
||||||
|
Log.d("Downloader", "EMIT: " + String.valueOf(progress) + ", TOTAL:" + String.valueOf(total));
|
||||||
|
lastProgressValue = progress;
|
||||||
|
publishProgress(new int[]{lengthOfFile, total});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
output.write(data, 0, count);
|
output.write(data, 0, count);
|
||||||
|
@ -220,7 +220,7 @@ public class RNFSManager extends ReactContextBaseJavaModule {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@ReactMethod
|
@ReactMethod
|
||||||
public void downloadFile(String urlStr, final String filepath, final int jobId, final Callback callback) {
|
public void downloadFile(String urlStr, final String filepath, final int jobId, float progressDivider, final Callback callback) {
|
||||||
try {
|
try {
|
||||||
File file = new File(filepath);
|
File file = new File(filepath);
|
||||||
URL url = new URL(urlStr);
|
URL url = new URL(urlStr);
|
||||||
@ -229,6 +229,7 @@ public class RNFSManager extends ReactContextBaseJavaModule {
|
|||||||
|
|
||||||
params.src = url;
|
params.src = url;
|
||||||
params.dest = file;
|
params.dest = file;
|
||||||
|
params.progressDivider = progressDivider;
|
||||||
|
|
||||||
params.onTaskCompleted = new DownloadParams.OnTaskCompleted() {
|
params.onTaskCompleted = new DownloadParams.OnTaskCompleted() {
|
||||||
public void onTaskCompleted(DownloadResult res) {
|
public void onTaskCompleted(DownloadResult res) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user