Browse Source

Code IOS refactor

Ben Hsieh 8 years ago
parent
commit
fa33d6990e

+ 0
- 1
src/index.js View File

104
   let options = this || {}
104
   let options = this || {}
105
 
105
 
106
   let promise = new Promise((resolve, reject) => {
106
   let promise = new Promise((resolve, reject) => {
107
-
108
     let [method, url, headers, body] = [...args]
107
     let [method, url, headers, body] = [...args]
109
     let nativeMethodName = Array.isArray(body) ? 'fetchBlobForm' : 'fetchBlob'
108
     let nativeMethodName = Array.isArray(body) ? 'fetchBlobForm' : 'fetchBlob'
110
 
109
 

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

9
 /* Begin PBXBuildFile section */
9
 /* Begin PBXBuildFile section */
10
 		A158F4271D052E49006FFD38 /* RNFetchBlobFS.m in Sources */ = {isa = PBXBuildFile; fileRef = A158F4261D052E49006FFD38 /* RNFetchBlobFS.m */; };
10
 		A158F4271D052E49006FFD38 /* RNFetchBlobFS.m in Sources */ = {isa = PBXBuildFile; fileRef = A158F4261D052E49006FFD38 /* RNFetchBlobFS.m */; };
11
 		A158F42D1D0535BB006FFD38 /* RNFetchBlobConst.m in Sources */ = {isa = PBXBuildFile; fileRef = A158F42C1D0535BB006FFD38 /* RNFetchBlobConst.m */; };
11
 		A158F42D1D0535BB006FFD38 /* RNFetchBlobConst.m in Sources */ = {isa = PBXBuildFile; fileRef = A158F42C1D0535BB006FFD38 /* RNFetchBlobConst.m */; };
12
-		A158F4301D0539DB006FFD38 /* RNFetchBlobResp.m in Sources */ = {isa = PBXBuildFile; fileRef = A158F42F1D0539DB006FFD38 /* RNFetchBlobResp.m */; };
12
+		A158F4301D0539DB006FFD38 /* RNFetchBlobNetwork.m in Sources */ = {isa = PBXBuildFile; fileRef = A158F42F1D0539DB006FFD38 /* RNFetchBlobNetwork.m */; };
13
 		A15C30141CD25C330074CB35 /* RNFetchBlob.m in Sources */ = {isa = PBXBuildFile; fileRef = A15C30131CD25C330074CB35 /* RNFetchBlob.m */; };
13
 		A15C30141CD25C330074CB35 /* RNFetchBlob.m in Sources */ = {isa = PBXBuildFile; fileRef = A15C30131CD25C330074CB35 /* RNFetchBlob.m */; };
14
 		A166D1AA1CE0647A00273590 /* RNFetchBlob.h in Sources */ = {isa = PBXBuildFile; fileRef = A15C30111CD25C330074CB35 /* RNFetchBlob.h */; };
14
 		A166D1AA1CE0647A00273590 /* RNFetchBlob.h in Sources */ = {isa = PBXBuildFile; fileRef = A15C30111CD25C330074CB35 /* RNFetchBlob.h */; };
15
 /* End PBXBuildFile section */
15
 /* End PBXBuildFile section */
31
 		A158F4281D052E57006FFD38 /* RNFetchBlobFS.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RNFetchBlobFS.h; sourceTree = "<group>"; };
31
 		A158F4281D052E57006FFD38 /* RNFetchBlobFS.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RNFetchBlobFS.h; sourceTree = "<group>"; };
32
 		A158F4291D0534A9006FFD38 /* RNFetchBlobConst.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RNFetchBlobConst.h; sourceTree = "<group>"; };
32
 		A158F4291D0534A9006FFD38 /* RNFetchBlobConst.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RNFetchBlobConst.h; sourceTree = "<group>"; };
33
 		A158F42C1D0535BB006FFD38 /* RNFetchBlobConst.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RNFetchBlobConst.m; sourceTree = "<group>"; };
33
 		A158F42C1D0535BB006FFD38 /* RNFetchBlobConst.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RNFetchBlobConst.m; sourceTree = "<group>"; };
34
-		A158F42E1D0539CE006FFD38 /* RNFetchBlobResp.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RNFetchBlobResp.h; sourceTree = "<group>"; };
35
-		A158F42F1D0539DB006FFD38 /* RNFetchBlobResp.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RNFetchBlobResp.m; sourceTree = "<group>"; };
34
+		A158F42E1D0539CE006FFD38 /* RNFetchBlobNetwork.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RNFetchBlobNetwork.h; sourceTree = "<group>"; };
35
+		A158F42F1D0539DB006FFD38 /* RNFetchBlobNetwork.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RNFetchBlobNetwork.m; sourceTree = "<group>"; };
36
 		A15C300E1CD25C330074CB35 /* libRNFetchBlob.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libRNFetchBlob.a; sourceTree = BUILT_PRODUCTS_DIR; };
36
 		A15C300E1CD25C330074CB35 /* libRNFetchBlob.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libRNFetchBlob.a; sourceTree = BUILT_PRODUCTS_DIR; };
37
 		A15C30111CD25C330074CB35 /* RNFetchBlob.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = RNFetchBlob.h; path = RNFetchBlob/RNFetchBlob.h; sourceTree = "<group>"; };
37
 		A15C30111CD25C330074CB35 /* RNFetchBlob.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = RNFetchBlob.h; path = RNFetchBlob/RNFetchBlob.h; sourceTree = "<group>"; };
38
 		A15C30131CD25C330074CB35 /* RNFetchBlob.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = RNFetchBlob.m; path = RNFetchBlob/RNFetchBlob.m; sourceTree = "<group>"; };
38
 		A15C30131CD25C330074CB35 /* RNFetchBlob.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = RNFetchBlob.m; path = RNFetchBlob/RNFetchBlob.m; sourceTree = "<group>"; };
59
 		A15C30051CD25C330074CB35 = {
59
 		A15C30051CD25C330074CB35 = {
60
 			isa = PBXGroup;
60
 			isa = PBXGroup;
61
 			children = (
61
 			children = (
62
-				A158F42F1D0539DB006FFD38 /* RNFetchBlobResp.m */,
63
-				A158F42E1D0539CE006FFD38 /* RNFetchBlobResp.h */,
62
+				A158F42F1D0539DB006FFD38 /* RNFetchBlobNetwork.m */,
63
+				A158F42E1D0539CE006FFD38 /* RNFetchBlobNetwork.h */,
64
 				A158F42C1D0535BB006FFD38 /* RNFetchBlobConst.m */,
64
 				A158F42C1D0535BB006FFD38 /* RNFetchBlobConst.m */,
65
 				A158F4291D0534A9006FFD38 /* RNFetchBlobConst.h */,
65
 				A158F4291D0534A9006FFD38 /* RNFetchBlobConst.h */,
66
 				A158F4281D052E57006FFD38 /* RNFetchBlobFS.h */,
66
 				A158F4281D052E57006FFD38 /* RNFetchBlobFS.h */,
139
 				A166D1AA1CE0647A00273590 /* RNFetchBlob.h in Sources */,
139
 				A166D1AA1CE0647A00273590 /* RNFetchBlob.h in Sources */,
140
 				A158F42D1D0535BB006FFD38 /* RNFetchBlobConst.m in Sources */,
140
 				A158F42D1D0535BB006FFD38 /* RNFetchBlobConst.m in Sources */,
141
 				A158F4271D052E49006FFD38 /* RNFetchBlobFS.m in Sources */,
141
 				A158F4271D052E49006FFD38 /* RNFetchBlobFS.m in Sources */,
142
-				A158F4301D0539DB006FFD38 /* RNFetchBlobResp.m in Sources */,
142
+				A158F4301D0539DB006FFD38 /* RNFetchBlobNetwork.m in Sources */,
143
 				A15C30141CD25C330074CB35 /* RNFetchBlob.m in Sources */,
143
 				A15C30141CD25C330074CB35 /* RNFetchBlob.m in Sources */,
144
 			);
144
 			);
145
 			runOnlyForDeploymentPostprocessing = 0;
145
 			runOnlyForDeploymentPostprocessing = 0;

+ 6
- 5
src/ios/RNFetchBlob/RNFetchBlob.m View File

10
 #import "RCTBridge.h"
10
 #import "RCTBridge.h"
11
 #import "RCTEventDispatcher.h"
11
 #import "RCTEventDispatcher.h"
12
 #import "RNFetchBlobFS.h"
12
 #import "RNFetchBlobFS.h"
13
-#import "RNFetchBlobResp.h"
13
+#import "RNFetchBlobNetwork.h"
14
 #import "RNFetchBlobConst.h"
14
 #import "RNFetchBlobConst.h"
15
 
15
 
16
 
16
 
67
                                     initWithURL:[NSURL
67
                                     initWithURL:[NSURL
68
                                                  URLWithString: url]];
68
                                                  URLWithString: url]];
69
     
69
     
70
-    NSMutableDictionary *mheaders = [[NSMutableDictionary alloc] initWithDictionary:[ FetchBlobUtils normalizeHeaders:headers]];
70
+    NSMutableDictionary *mheaders = [[NSMutableDictionary alloc] initWithDictionary:[ RNFetchBlobNetwork normalizeHeaders:headers]];
71
     
71
     
72
     
72
     
73
     NSTimeInterval timeStamp = [[NSDate date] timeIntervalSince1970];
73
     NSTimeInterval timeStamp = [[NSDate date] timeIntervalSince1970];
128
         
128
         
129
         
129
         
130
         // send HTTP request
130
         // send HTTP request
131
-        FetchBlobUtils * utils = [[FetchBlobUtils alloc] init];
131
+        RNFetchBlobNetwork * utils = [[RNFetchBlobNetwork alloc] init];
132
         [utils sendRequest:options bridge:self.bridge taskId:taskId withRequest:request withData:postData callback:callback];
132
         [utils sendRequest:options bridge:self.bridge taskId:taskId withRequest:request withData:postData callback:callback];
133
     });
133
     });
134
 }
134
 }
146
                                     initWithURL:[NSURL
146
                                     initWithURL:[NSURL
147
                                                  URLWithString: url]];
147
                                                  URLWithString: url]];
148
     
148
     
149
-    NSMutableDictionary *mheaders = [[NSMutableDictionary alloc] initWithDictionary:[FetchBlobUtils normalizeHeaders:headers]];
149
+    NSMutableDictionary *mheaders = [[NSMutableDictionary alloc] initWithDictionary:[RNFetchBlobNetwork normalizeHeaders:headers]];
150
+    
150
     // move heavy task to another thread
151
     // move heavy task to another thread
151
     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
152
     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
152
         NSMutableData * blobData;
153
         NSMutableData * blobData;
173
         [request setAllHTTPHeaderFields:mheaders];
174
         [request setAllHTTPHeaderFields:mheaders];
174
         
175
         
175
         // send HTTP request
176
         // send HTTP request
176
-        FetchBlobUtils * utils = [[FetchBlobUtils alloc] init];
177
+        RNFetchBlobNetwork * utils = [[RNFetchBlobNetwork alloc] init];
177
         [utils sendRequest:options bridge:self.bridge taskId:taskId withRequest:request withData:blobData callback:callback];
178
         [utils sendRequest:options bridge:self.bridge taskId:taskId withRequest:request withData:blobData callback:callback];
178
     });
179
     });
179
 }
180
 }

+ 4
- 4
src/ios/RNFetchBlobNetwork.h View File

1
 //
1
 //
2
-//  RNFetchBlobResp.h
2
+//  RNFetchBlobNetwork.h
3
 //  RNFetchBlob
3
 //  RNFetchBlob
4
 //
4
 //
5
-//  Created by Ben Hsieh on 2016/6/6.
6
-//  Copyright © 2016年 suzuri04x2. All rights reserved.
5
+//  Created by wkh237 on 2016/6/6.
6
+//  Copyright © 2016 wkh237. All rights reserved.
7
 //
7
 //
8
 
8
 
9
 #ifndef RNFetchBlobResp_h
9
 #ifndef RNFetchBlobResp_h
12
 #import <Foundation/Foundation.h>
12
 #import <Foundation/Foundation.h>
13
 #import "RCTBridgeModule.h"
13
 #import "RCTBridgeModule.h"
14
 
14
 
15
-@interface FetchBlobUtils : NSObject  <NSURLSessionDelegate, NSURLSessionTaskDelegate, NSURLSessionDataDelegate> {
15
+@interface RNFetchBlobNetwork : NSObject  <NSURLSessionDelegate, NSURLSessionTaskDelegate, NSURLSessionDataDelegate> {
16
     
16
     
17
     NSString * taskId;
17
     NSString * taskId;
18
     int expectedBytes;
18
     int expectedBytes;

+ 25
- 11
src/ios/RNFetchBlobNetwork.m View File

1
 //
1
 //
2
-//  RNFetchBlobResp.m
2
+//  RNFetchBlobNetwork.m
3
 //  RNFetchBlob
3
 //  RNFetchBlob
4
 //
4
 //
5
-//  Created by Ben Hsieh on 2016/6/6.
6
-//  Copyright © 2016年 suzuri04x2. All rights reserved.
5
+//  Created by wkh237 on 2016/6/6.
6
+//  Copyright © 2016 wkh237. All rights reserved.
7
 //
7
 //
8
 
8
 
9
 #import "RCTConvert.h"
9
 #import "RCTConvert.h"
12
 #import "RCTBridge.h"
12
 #import "RCTBridge.h"
13
 #import "RCTEventDispatcher.h"
13
 #import "RCTEventDispatcher.h"
14
 #import "RNFetchBlobFS.h"
14
 #import "RNFetchBlobFS.h"
15
-#import "RNFetchBlobResp.h"
15
+#import "RNFetchBlobNetwork.h"
16
 #import "RNFetchBlobConst.h"
16
 #import "RNFetchBlobConst.h"
17
 
17
 
18
 ////////////////////////////////////////
18
 ////////////////////////////////////////
21
 //
21
 //
22
 ////////////////////////////////////////
22
 ////////////////////////////////////////
23
 
23
 
24
-@implementation FetchBlobUtils
24
+@implementation RNFetchBlobNetwork
25
 
25
 
26
 
26
 
27
 @synthesize taskId;
27
 @synthesize taskId;
32
 @synthesize bridge;
32
 @synthesize bridge;
33
 @synthesize options;
33
 @synthesize options;
34
 
34
 
35
+// constructor
36
+- (id)init {
37
+    self = [super init];
38
+    return self;
39
+}
40
+
35
 
41
 
36
 // removing case from headers
42
 // removing case from headers
37
 + (NSMutableDictionary *) normalizeHeaders:(NSDictionary *)headers {
43
 + (NSMutableDictionary *) normalizeHeaders:(NSDictionary *)headers {
44
     return mheaders;
50
     return mheaders;
45
 }
51
 }
46
 
52
 
47
-- (id)init {
48
-    self = [super init];
49
-    return self;
50
-}
51
-
52
-
53
+// send HTTP request
53
 - (void) sendRequest:(NSDictionary *)options bridge:(RCTBridge *)bridgeRef taskId:(NSString *)taskId withRequest:(NSURLRequest *)req withData:( NSData * _Nullable )data callback:(RCTResponseSenderBlock) callback {
54
 - (void) sendRequest:(NSDictionary *)options bridge:(RCTBridge *)bridgeRef taskId:(NSString *)taskId withRequest:(NSURLRequest *)req withData:( NSData * _Nullable )data callback:(RCTResponseSenderBlock) callback {
54
     self.taskId = taskId;
55
     self.taskId = taskId;
55
     self.respData = [[NSMutableData alloc] initWithLength:0];
56
     self.respData = [[NSMutableData alloc] initWithLength:0];
120
     }
121
     }
121
 }
122
 }
122
 
123
 
124
+////////////////////////////////////////
125
+//
126
+//  NSURLSession delegates
127
+//
128
+////////////////////////////////////////
129
+
123
 
130
 
124
 #pragma mark NSURLSession delegate methods
131
 #pragma mark NSURLSession delegate methods
125
 
132
 
133
+// set expected content length on response received
126
 - (void) URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler
134
 - (void) URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler
127
 {
135
 {
128
     expectedBytes = [response expectedContentLength];
136
     expectedBytes = [response expectedContentLength];
129
 }
137
 }
130
 
138
 
139
+// download progress handler
131
 - (void) URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data
140
 - (void) URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data
132
 {
141
 {
133
     receivedBytes += [data length];
142
     receivedBytes += [data length];
149
      ];
158
      ];
150
 }
159
 }
151
 
160
 
161
+- (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {
162
+    NSLog([error localizedDescription]);
163
+}
164
+
165
+// upload progress handler
152
 - (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didSendBodyData:(int64_t)bytesSent totalBytesSent:(int64_t)totalBytesWritten totalBytesExpectedToSend:(int64_t)totalBytesExpectedToWrite
166
 - (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didSendBodyData:(int64_t)bytesSent totalBytesSent:(int64_t)totalBytesWritten totalBytesExpectedToSend:(int64_t)totalBytesExpectedToWrite
153
 {
167
 {
154
     expectedBytes = totalBytesExpectedToWrite;
168
     expectedBytes = totalBytesExpectedToWrite;