mirror of
https://github.com/status-im/react-native-fs.git
synced 2025-02-28 23:10:29 +00:00
Merge pull request #68 from charpeni/jobId-progress-callback
Add property jobId to progressCallback
This commit is contained in:
commit
23a85af821
@ -161,14 +161,14 @@ RCT_EXPORT_METHOD(moveFile:(NSString *)filepath
|
||||
callback:(RCTResponseSenderBlock)callback)
|
||||
{
|
||||
NSFileManager *manager = [NSFileManager defaultManager];
|
||||
|
||||
|
||||
NSError *error = nil;
|
||||
BOOL success = [manager moveItemAtPath:filepath toPath:destPath error:&error];
|
||||
|
||||
|
||||
if (!success) {
|
||||
return callback([self makeErrorPayload:error]);
|
||||
}
|
||||
|
||||
|
||||
callback(@[[NSNull null], [NSNumber numberWithBool:success], destPath]);
|
||||
}
|
||||
|
||||
@ -177,9 +177,9 @@ RCT_EXPORT_METHOD(downloadFile:(NSString *)urlStr
|
||||
jobId:(nonnull NSNumber *)jobId
|
||||
callback:(RCTResponseSenderBlock)callback)
|
||||
{
|
||||
|
||||
|
||||
DownloadParams* params = [DownloadParams alloc];
|
||||
|
||||
|
||||
params.fromUrl = urlStr;
|
||||
params.toFile = filepath;
|
||||
|
||||
@ -195,7 +195,7 @@ RCT_EXPORT_METHOD(downloadFile:(NSString *)urlStr
|
||||
params.errorCallback = ^(NSError* error) {
|
||||
return callback([self makeErrorPayload:error]);
|
||||
};
|
||||
|
||||
|
||||
params.beginCallback = ^(NSNumber* statusCode, NSNumber* contentLength, NSDictionary* headers) {
|
||||
[self.bridge.eventDispatcher sendAppEventWithName:[NSString stringWithFormat:@"DownloadBegin-%@", jobId]
|
||||
body:@{@"jobId": jobId,
|
||||
@ -203,26 +203,27 @@ RCT_EXPORT_METHOD(downloadFile:(NSString *)urlStr
|
||||
@"contentLength": contentLength,
|
||||
@"headers": headers}];
|
||||
};
|
||||
|
||||
|
||||
params.progressCallback = ^(NSNumber* contentLength, NSNumber* bytesWritten) {
|
||||
[self.bridge.eventDispatcher sendAppEventWithName:[NSString stringWithFormat:@"DownloadProgress-%@", jobId]
|
||||
body:@{@"contentLength": contentLength,
|
||||
body:@{@"jobId": jobId,
|
||||
@"contentLength": contentLength,
|
||||
@"bytesWritten": bytesWritten}];
|
||||
};
|
||||
|
||||
if (!self.downloaders) self.downloaders = [[NSMutableDictionary alloc] init];
|
||||
|
||||
|
||||
Downloader* downloader = [Downloader alloc];
|
||||
|
||||
[downloader downloadFile:params];
|
||||
|
||||
|
||||
[self.downloaders setValue:downloader forKey:[jobId stringValue]];
|
||||
}
|
||||
|
||||
RCT_EXPORT_METHOD(stopDownload:(nonnull NSNumber *)jobId)
|
||||
{
|
||||
Downloader* downloader = [self.downloaders objectForKey:[jobId stringValue]];
|
||||
|
||||
|
||||
if (downloader != nil) {
|
||||
[downloader stopDownload];
|
||||
}
|
||||
|
@ -44,7 +44,7 @@ public class RNFSManager extends ReactContextBaseJavaModule {
|
||||
|
||||
private static final String NSFileTypeRegular = "NSFileTypeRegular";
|
||||
private static final String NSFileTypeDirectory = "NSFileTypeDirectory";
|
||||
|
||||
|
||||
private SparseArray<Downloader> downloaders = new SparseArray<Downloader>();
|
||||
|
||||
public RNFSManager(ReactApplicationContext reactContext) {
|
||||
@ -71,7 +71,7 @@ public class RNFSManager extends ReactContextBaseJavaModule {
|
||||
callback.invoke(makeErrorPayload(ex));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ReactMethod
|
||||
public void exists(String filepath, Callback callback) {
|
||||
try {
|
||||
@ -223,48 +223,50 @@ public class RNFSManager extends ReactContextBaseJavaModule {
|
||||
URL url = new URL(urlStr);
|
||||
|
||||
DownloadParams params = new DownloadParams();
|
||||
|
||||
|
||||
params.src = url;
|
||||
params.dest = file;
|
||||
|
||||
|
||||
params.onTaskCompleted = new DownloadParams.OnTaskCompleted() {
|
||||
public void onTaskCompleted(DownloadResult res) {
|
||||
if (res.exception == null) {
|
||||
WritableMap infoMap = Arguments.createMap();
|
||||
|
||||
|
||||
infoMap.putInt("jobId", jobId);
|
||||
infoMap.putInt("statusCode", res.statusCode);
|
||||
infoMap.putInt("bytesWritten", res.bytesWritten);
|
||||
|
||||
|
||||
callback.invoke(null, infoMap);
|
||||
} else {
|
||||
callback.invoke(makeErrorPayload(res.exception));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
params.onDownloadBegin = new DownloadParams.OnDownloadBegin() {
|
||||
public void onDownloadBegin(int statusCode, int contentLength, Map<String, String> headers) {
|
||||
WritableMap headersMap = Arguments.createMap();
|
||||
|
||||
|
||||
for (Map.Entry<String, String> entry : headers.entrySet()) {
|
||||
headersMap.putString(entry.getKey(), entry.getValue());
|
||||
}
|
||||
|
||||
|
||||
WritableMap data = Arguments.createMap();
|
||||
|
||||
|
||||
data.putInt("jobId", jobId);
|
||||
data.putInt("statusCode", statusCode);
|
||||
data.putInt("contentLength", contentLength);
|
||||
data.putMap("headers", headersMap);
|
||||
|
||||
|
||||
sendEvent(getReactApplicationContext(), "DownloadBegin-" + jobId, data);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
params.onDownloadProgress = new DownloadParams.OnDownloadProgress() {
|
||||
public void onDownloadProgress(int contentLength, int bytesWritten) {
|
||||
WritableMap data = Arguments.createMap();
|
||||
|
||||
data.putInt("jobId", jobId);
|
||||
data.putInt("contentLength", contentLength);
|
||||
data.putInt("bytesWritten", bytesWritten);
|
||||
|
||||
@ -273,22 +275,22 @@ public class RNFSManager extends ReactContextBaseJavaModule {
|
||||
};
|
||||
|
||||
Downloader downloader = new Downloader();
|
||||
|
||||
|
||||
downloader.execute(params);
|
||||
|
||||
|
||||
this.downloaders.put(jobId, downloader);
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
callback.invoke(makeErrorPayload(ex));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ReactMethod
|
||||
public void stopDownload(int jobId) {
|
||||
Downloader downloader = this.downloaders.get(jobId);
|
||||
|
||||
|
||||
if (downloader != null) {
|
||||
downloader.stop();
|
||||
downloader.stop();
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user