浏览代码

Fix Fetch Replacement progress and cancel task issue #370

Ben Hsieh 7 年前
父节点
当前提交
5b43136beb
共有 2 个文件被更改,包括 56 次插入14 次删除
  1. 28
    0
      android/src/main/java/com/RNFetchBlob/RNFetchBlob.java
  2. 28
    14
      polyfill/Fetch.js

+ 28
- 0
android/src/main/java/com/RNFetchBlob/RNFetchBlob.java 查看文件

@@ -1,6 +1,7 @@
1 1
 package com.RNFetchBlob;
2 2
 
3 3
 import android.app.Activity;
4
+import android.app.DownloadManager;
4 5
 import android.content.Intent;
5 6
 import android.net.Uri;
6 7
 
@@ -341,4 +342,31 @@ public class RNFetchBlob extends ReactContextBaseJavaModule {
341 342
 
342 343
     }
343 344
 
345
+    @ReactMethod
346
+    public void addCompleteDownload (ReadableMap config, Promise promise) {
347
+        DownloadManager dm = (DownloadManager) RNFetchBlob.RCTContext.getSystemService(RNFetchBlob.RCTContext.DOWNLOAD_SERVICE);
348
+        String path = RNFetchBlobFS.normalizePath(config.getString("path"));
349
+        if(path == null) {
350
+            promise.reject("RNFetchblob.addCompleteDownload can not resolve URI:" + config.getString("path"), "RNFetchblob.addCompleteDownload can not resolve URI:" + path);
351
+            return;
352
+        }
353
+        try {
354
+            WritableMap stat = RNFetchBlobFS.statFile(path);
355
+            dm.addCompletedDownload(
356
+                    config.hasKey("title") ? config.getString("title") : "",
357
+                    config.hasKey("description") ? config.getString("description") : "",
358
+                    true,
359
+                    config.hasKey("mime") ? config.getString("mime") : null,
360
+                    path,
361
+                    Long.valueOf(stat.getString("size")),
362
+                    config.hasKey("showNotification") && config.getBoolean("showNotification")
363
+            );
364
+            promise.resolve(null);
365
+        }
366
+        catch(Exception ex) {
367
+            promise.reject("RNFetchblob.addCompleteDownload failed", ex.getStackTrace().toString());
368
+        }
369
+
370
+    }
371
+
344 372
 }

+ 28
- 14
polyfill/Fetch.js 查看文件

@@ -57,24 +57,38 @@ class RNFetchBlobFetchPolyfill {
57 57
       // task is a progress reportable and cancellable Promise, however,
58 58
       // task.then is not, so we have to extend task.then with progress and
59 59
       // cancel function
60
-      let task = promise
60
+      let progressHandler, uploadHandler, cancelHandler
61
+      let statefulPromise = promise
61 62
           .then((body) => {
62
-            return RNFetchBlob.config(config)
63
-            .fetch(options.method, url, options.headers, body)
63
+            let task = RNFetchBlob.config(config)
64
+              .fetch(options.method, url, options.headers, body)
65
+            if(progressHandler)
66
+              task.progress(progressHandler)
67
+            if(uploadHandler)
68
+              task.uploadProgress(uploadHandler)
69
+            if(cancelHandler)
70
+              task.cancel()
71
+            return task.then((resp) => {
72
+              log.verbose('response', resp)
73
+              // release blob cache created when sending request
74
+              if(blobCache !== null && blobCache instanceof Blob)
75
+                blobCache.close()
76
+              return Promise.resolve(new RNFetchBlobFetchRepsonse(resp))
77
+            })
64 78
           })
65 79
 
66
-      let statefulPromise = task.then((resp) => {
67
-        log.verbose('response', resp)
68
-        // release blob cache created when sending request
69
-        if(blobCache !== null && blobCache instanceof Blob)
70
-          blobCache.close()
71
-        return Promise.resolve(new RNFetchBlobFetchRepsonse(resp))
72
-      })
73
-
74 80
       // extend task.then progress with report and cancelling functions
75
-      statefulPromise.cancel = task.cancel
76
-      statefulPromise.progress = task.progress
77
-      statefulPromise.uploadProgress = task.uploadProgress
81
+      statefulPromise.progress = (fn) => {
82
+        progressHandler = fn
83
+      }
84
+      statefulPromise.uploadProgress = (fn) => {
85
+        uploadHandler = fn
86
+      }
87
+      statefulPromise.cancel = () => {
88
+        cancelHandler = true
89
+        if(task.cancel)
90
+          task.cancel()
91
+      }
78 92
 
79 93
       return statefulPromise
80 94