Browse Source

IOS wip commit #140

Ben Hsieh 8 years ago
parent
commit
ca0fd9fda9

+ 6
- 0
src/ios/RNFetchBlob.xcodeproj/project.pbxproj View File

@@ -12,6 +12,7 @@
12 12
 		A158F4301D0539DB006FFD38 /* RNFetchBlobNetwork.m in Sources */ = {isa = PBXBuildFile; fileRef = A158F42F1D0539DB006FFD38 /* RNFetchBlobNetwork.m */; };
13 13
 		A15C30141CD25C330074CB35 /* RNFetchBlob.m in Sources */ = {isa = PBXBuildFile; fileRef = A15C30131CD25C330074CB35 /* RNFetchBlob.m */; };
14 14
 		A166D1AA1CE0647A00273590 /* RNFetchBlob.h in Sources */ = {isa = PBXBuildFile; fileRef = A15C30111CD25C330074CB35 /* RNFetchBlob.h */; };
15
+		A19B48251D98102400E6868A /* RNFetchBlobProgress.m in Sources */ = {isa = PBXBuildFile; fileRef = A19B48241D98102400E6868A /* RNFetchBlobProgress.m */; };
15 16
 		A1AAE2991D300E4D0051D11C /* RNFetchBlobReqBuilder.m in Sources */ = {isa = PBXBuildFile; fileRef = A1AAE2981D300E4D0051D11C /* RNFetchBlobReqBuilder.m */; };
16 17
 /* End PBXBuildFile section */
17 18
 
@@ -37,6 +38,8 @@
37 38
 		A15C300E1CD25C330074CB35 /* libRNFetchBlob.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libRNFetchBlob.a; sourceTree = BUILT_PRODUCTS_DIR; };
38 39
 		A15C30111CD25C330074CB35 /* RNFetchBlob.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = RNFetchBlob.h; path = RNFetchBlob/RNFetchBlob.h; sourceTree = "<group>"; };
39 40
 		A15C30131CD25C330074CB35 /* RNFetchBlob.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = RNFetchBlob.m; path = RNFetchBlob/RNFetchBlob.m; sourceTree = "<group>"; };
41
+		A19B48231D98100800E6868A /* RNFetchBlobProgress.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RNFetchBlobProgress.h; sourceTree = "<group>"; };
42
+		A19B48241D98102400E6868A /* RNFetchBlobProgress.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RNFetchBlobProgress.m; sourceTree = "<group>"; };
40 43
 		A1AAE2971D300E3E0051D11C /* RNFetchBlobReqBuilder.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RNFetchBlobReqBuilder.h; sourceTree = "<group>"; };
41 44
 		A1AAE2981D300E4D0051D11C /* RNFetchBlobReqBuilder.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RNFetchBlobReqBuilder.m; sourceTree = "<group>"; };
42 45
 		A1F950181D7E9134002A95A6 /* IOS7Polyfill.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = IOS7Polyfill.h; sourceTree = "<group>"; };
@@ -63,6 +66,8 @@
63 66
 		A15C30051CD25C330074CB35 = {
64 67
 			isa = PBXGroup;
65 68
 			children = (
69
+				A19B48241D98102400E6868A /* RNFetchBlobProgress.m */,
70
+				A19B48231D98100800E6868A /* RNFetchBlobProgress.h */,
66 71
 				A1F950181D7E9134002A95A6 /* IOS7Polyfill.h */,
67 72
 				A1AAE2981D300E4D0051D11C /* RNFetchBlobReqBuilder.m */,
68 73
 				A1AAE2971D300E3E0051D11C /* RNFetchBlobReqBuilder.h */,
@@ -147,6 +152,7 @@
147 152
 				A158F42D1D0535BB006FFD38 /* RNFetchBlobConst.m in Sources */,
148 153
 				A158F4271D052E49006FFD38 /* RNFetchBlobFS.m in Sources */,
149 154
 				A158F4301D0539DB006FFD38 /* RNFetchBlobNetwork.m in Sources */,
155
+				A19B48251D98102400E6868A /* RNFetchBlobProgress.m in Sources */,
150 156
 				A1AAE2991D300E4D0051D11C /* RNFetchBlobReqBuilder.m in Sources */,
151 157
 				A15C30141CD25C330074CB35 /* RNFetchBlob.m in Sources */,
152 158
 			);

+ 7
- 4
src/ios/RNFetchBlob/RNFetchBlob.m View File

@@ -12,6 +12,7 @@
12 12
 #import "RNFetchBlobNetwork.h"
13 13
 #import "RNFetchBlobConst.h"
14 14
 #import "RNFetchBlobReqBuilder.h"
15
+#import "RNFetchBlobProgress.h"
15 16
 
16 17
 
17 18
 RCTBridge * bridgeRef;
@@ -413,13 +414,15 @@ RCT_EXPORT_METHOD(cancelRequest:(NSString *)taskId callback:(RCTResponseSenderBl
413 414
 }
414 415
 
415 416
 #pragma mark - net.enableProgressReport
416
-RCT_EXPORT_METHOD(enableProgressReport:(NSString *)taskId {
417
-    [RNFetchBlobNetwork enableProgressReport:taskId];
417
+RCT_EXPORT_METHOD(enableProgressReport:(NSString *)taskId interval:(NSNumber*)interval count:(NSNumber*)count  {
418
+    RNFetchBlobProgress * cfg = [[RNFetchBlobProgress alloc] initWithType:Download interval:interval count:count];
419
+    [RNFetchBlobNetwork enableProgressReport:taskId config:cfg];
418 420
 })
419 421
 
420 422
 #pragma mark - net.enableUploadProgressReport
421
-RCT_EXPORT_METHOD(enableUploadProgressReport:(NSString *)taskId {
422
-    [RNFetchBlobNetwork enableUploadProgress:taskId];
423
+RCT_EXPORT_METHOD(enableUploadProgressReport:(NSString *)taskId interval:(NSNumber*)interval count:(NSNumber*)count{
424
+    RNFetchBlobProgress * cfg = [[RNFetchBlobProgress alloc] initWithType:Upload interval:interval count:count];
425
+    [RNFetchBlobNetwork enableUploadProgress:taskId config:cfg];
423 426
 })
424 427
 
425 428
 #pragma mark - fs.slice

+ 3
- 2
src/ios/RNFetchBlobNetwork.h View File

@@ -11,6 +11,7 @@
11 11
 
12 12
 #import <Foundation/Foundation.h>
13 13
 #import "RCTBridgeModule.h"
14
+#import "RNFetchBlobProgress.h"
14 15
 #import "RNFetchBlobFS.h"
15 16
 
16 17
 typedef void(^CompletionHander)(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error);
@@ -36,8 +37,8 @@ typedef void(^DataTaskCompletionHander) (NSData * _Nullable resp, NSURLResponse
36 37
 
37 38
 + (NSMutableDictionary  * _Nullable ) normalizeHeaders:(NSDictionary * _Nullable)headers;
38 39
 + (void) cancelRequest:(NSString *)taskId;
39
-+ (void) enableProgressReport:(NSString *) taskId;
40
-+ (void) enableUploadProgress:(NSString *) taskId;
40
++ (void) enableProgressReport:(NSString *) taskId config:(RNFetchBlobProgress*)config;
41
++ (void) enableUploadProgress:(NSString *) taskId config:(RNFetchBlobProgress*)config;
41 42
 - (void) sendRequest:(NSDictionary  * _Nullable )options contentLength:(long)contentLength bridge:(RCTBridge * _Nullable)bridgeRef taskId:(NSString * _Nullable)taskId withRequest:(NSURLRequest * _Nullable)req callback:(_Nullable RCTResponseSenderBlock) callback;
42 43
 
43 44
 

+ 5
- 1
src/ios/RNFetchBlobNetwork.m View File

@@ -16,6 +16,7 @@
16 16
 #import "RNFetchBlobReqBuilder.h"
17 17
 #import "IOS7Polyfill.h"
18 18
 #import <CommonCrypto/CommonDigest.h>
19
+#import "RNFetchBlobProgress.h"
19 20
 
20 21
 ////////////////////////////////////////
21 22
 //
@@ -294,9 +295,12 @@ NSOperationQueue *taskQueue;
294 295
             BOOL appendToExistingFile = [destPath RNFBContainsString:@"?append=true"];
295 296
             // For solving #141 append response data if the file already exists
296 297
             // base on PR#139 @kejinliang
297
-            if (appendToExistingFile && ![fm fileExistsAtPath:destPath])
298
+            if(appendToExistingFile)
298 299
             {
299 300
                 destPath = [destPath stringByReplacingOccurrencesOfString:@"?append=true" withString:@""];
301
+            }
302
+            if (![fm fileExistsAtPath:destPath])
303
+            {
300 304
                 [fm createFileAtPath:destPath contents:[[NSData alloc] init] attributes:nil];
301 305
             }
302 306
             writeStream = [[NSOutputStream alloc] initToFileAtPath:destPath append:YES];

+ 40
- 0
src/ios/RNFetchBlobProgress.h View File

@@ -0,0 +1,40 @@
1
+//
2
+//  RNFetchBlobProgress.h
3
+//  RNFetchBlob
4
+//
5
+//  Created by Ben Hsieh on 2016/9/25.
6
+//  Copyright © 2016年 wkh237.github.io. All rights reserved.
7
+//
8
+
9
+#ifndef RNFetchBlobProgress_h
10
+#define RNFetchBlobProgress_h
11
+
12
+#import <Foundation/Foundation.h>
13
+
14
+typedef NS_ENUM(NSUInteger, ProgressType) {
15
+    Download,
16
+    Upload,
17
+};
18
+
19
+@interface RNFetchBlobProgress : NSObject
20
+{
21
+    NSNumber * count;
22
+    NSNumber * interval;
23
+    ProgressType type;
24
+    BOOL enable;
25
+    
26
+}
27
+
28
+@property (nonatomic) NSNumber * count;
29
+@property (nonatomic) NSNumber * interval;
30
+@property (nonatomic) NSInteger type;
31
+@property (nonatomic) BOOL enable;
32
+
33
+-(id)initWithType:(ProgressType)type interval:(NSNumber*)interval count:(NSNumber *)count;
34
+-(BOOL)shouldReport;
35
+-(void)tick;
36
+
37
+
38
+@end
39
+
40
+#endif /* RNFetchBlobProgress_h */

+ 56
- 0
src/ios/RNFetchBlobProgress.m View File

@@ -0,0 +1,56 @@
1
+//
2
+//  RNFetchBlobProgress.m
3
+//  RNFetchBlob
4
+//
5
+//  Created by Ben Hsieh on 2016/9/25.
6
+//  Copyright © 2016年 wkh237.github.io. All rights reserved.
7
+//
8
+
9
+#import "RNFetchBlobProgress.h"
10
+
11
+@interface RNFetchBlobProgress ()
12
+{
13
+    float progress;
14
+    int tick;
15
+    long lastTick;
16
+}
17
+@end
18
+
19
+@implementation RNFetchBlobProgress
20
+
21
+-(id)initWithType:(ProgressType)type interval:(NSNumber *)interval count:(NSNumber *)count
22
+{
23
+    self = [super init];
24
+    self.count = count;
25
+    self.interval = interval;
26
+    self.type = type;
27
+    self.enable = YES;
28
+    return self;
29
+}
30
+
31
+-(void)tick:(NSNumber *) nextProgress
32
+{
33
+    progress = [nextProgress floatValue];
34
+    tick++;
35
+    NSTimeInterval timeStamp = [[NSDate date] timeIntervalSince1970];
36
+    // NSTimeInterval is defined as double
37
+    NSNumber *timeStampObj = [NSNumber numberWithDouble: timeStamp];
38
+    lastTick = [timeStampObj longValue];
39
+}
40
+
41
+-(BOOL)shouldReport
42
+{
43
+    BOOL result = YES;
44
+    if(self.count > 0)
45
+    {
46
+        result = floorf(progress*100/[count floatValue]) > tick;
47
+    }
48
+    NSTimeInterval timeStamp = [[NSDate date] timeIntervalSince1970];
49
+    // NSTimeInterval is defined as double
50
+    NSNumber *timeStampObj = [NSNumber numberWithDouble: timeStamp];
51
+    return lastTick - [timeStampObj longValue] > [self.interval longValue] && self.enable && result;
52
+    
53
+}
54
+
55
+
56
+@end