Kaynağa Gözat

Code IOS refactor

Ben Hsieh 8 yıl önce
ebeveyn
işleme
fa33d6990e

+ 0
- 1
src/index.js Dosyayı Görüntüle

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

+ 6
- 6
src/ios/RNFetchBlob.xcodeproj/project.pbxproj Dosyayı Görüntüle

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

+ 6
- 5
src/ios/RNFetchBlob/RNFetchBlob.m Dosyayı Görüntüle

@@ -10,7 +10,7 @@
10 10
 #import "RCTBridge.h"
11 11
 #import "RCTEventDispatcher.h"
12 12
 #import "RNFetchBlobFS.h"
13
-#import "RNFetchBlobResp.h"
13
+#import "RNFetchBlobNetwork.h"
14 14
 #import "RNFetchBlobConst.h"
15 15
 
16 16
 
@@ -67,7 +67,7 @@ RCT_EXPORT_METHOD(fetchBlobForm:(NSDictionary *)options
67 67
                                     initWithURL:[NSURL
68 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 73
     NSTimeInterval timeStamp = [[NSDate date] timeIntervalSince1970];
@@ -128,7 +128,7 @@ RCT_EXPORT_METHOD(fetchBlobForm:(NSDictionary *)options
128 128
         
129 129
         
130 130
         // send HTTP request
131
-        FetchBlobUtils * utils = [[FetchBlobUtils alloc] init];
131
+        RNFetchBlobNetwork * utils = [[RNFetchBlobNetwork alloc] init];
132 132
         [utils sendRequest:options bridge:self.bridge taskId:taskId withRequest:request withData:postData callback:callback];
133 133
     });
134 134
 }
@@ -146,7 +146,8 @@ RCT_EXPORT_METHOD(fetchBlob:(NSDictionary *)options
146 146
                                     initWithURL:[NSURL
147 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 151
     // move heavy task to another thread
151 152
     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
152 153
         NSMutableData * blobData;
@@ -173,7 +174,7 @@ RCT_EXPORT_METHOD(fetchBlob:(NSDictionary *)options
173 174
         [request setAllHTTPHeaderFields:mheaders];
174 175
         
175 176
         // send HTTP request
176
-        FetchBlobUtils * utils = [[FetchBlobUtils alloc] init];
177
+        RNFetchBlobNetwork * utils = [[RNFetchBlobNetwork alloc] init];
177 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 Dosyayı Görüntüle

@@ -1,9 +1,9 @@
1 1
 //
2
-//  RNFetchBlobResp.h
2
+//  RNFetchBlobNetwork.h
3 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 9
 #ifndef RNFetchBlobResp_h
@@ -12,7 +12,7 @@
12 12
 #import <Foundation/Foundation.h>
13 13
 #import "RCTBridgeModule.h"
14 14
 
15
-@interface FetchBlobUtils : NSObject  <NSURLSessionDelegate, NSURLSessionTaskDelegate, NSURLSessionDataDelegate> {
15
+@interface RNFetchBlobNetwork : NSObject  <NSURLSessionDelegate, NSURLSessionTaskDelegate, NSURLSessionDataDelegate> {
16 16
     
17 17
     NSString * taskId;
18 18
     int expectedBytes;

+ 25
- 11
src/ios/RNFetchBlobNetwork.m Dosyayı Görüntüle

@@ -1,9 +1,9 @@
1 1
 //
2
-//  RNFetchBlobResp.m
2
+//  RNFetchBlobNetwork.m
3 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 9
 #import "RCTConvert.h"
@@ -12,7 +12,7 @@
12 12
 #import "RCTBridge.h"
13 13
 #import "RCTEventDispatcher.h"
14 14
 #import "RNFetchBlobFS.h"
15
-#import "RNFetchBlobResp.h"
15
+#import "RNFetchBlobNetwork.h"
16 16
 #import "RNFetchBlobConst.h"
17 17
 
18 18
 ////////////////////////////////////////
@@ -21,7 +21,7 @@
21 21
 //
22 22
 ////////////////////////////////////////
23 23
 
24
-@implementation FetchBlobUtils
24
+@implementation RNFetchBlobNetwork
25 25
 
26 26
 
27 27
 @synthesize taskId;
@@ -32,6 +32,12 @@
32 32
 @synthesize bridge;
33 33
 @synthesize options;
34 34
 
35
+// constructor
36
+- (id)init {
37
+    self = [super init];
38
+    return self;
39
+}
40
+
35 41
 
36 42
 // removing case from headers
37 43
 + (NSMutableDictionary *) normalizeHeaders:(NSDictionary *)headers {
@@ -44,12 +50,7 @@
44 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 54
 - (void) sendRequest:(NSDictionary *)options bridge:(RCTBridge *)bridgeRef taskId:(NSString *)taskId withRequest:(NSURLRequest *)req withData:( NSData * _Nullable )data callback:(RCTResponseSenderBlock) callback {
54 55
     self.taskId = taskId;
55 56
     self.respData = [[NSMutableData alloc] initWithLength:0];
@@ -120,14 +121,22 @@
120 121
     }
121 122
 }
122 123
 
124
+////////////////////////////////////////
125
+//
126
+//  NSURLSession delegates
127
+//
128
+////////////////////////////////////////
129
+
123 130
 
124 131
 #pragma mark NSURLSession delegate methods
125 132
 
133
+// set expected content length on response received
126 134
 - (void) URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler
127 135
 {
128 136
     expectedBytes = [response expectedContentLength];
129 137
 }
130 138
 
139
+// download progress handler
131 140
 - (void) URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data
132 141
 {
133 142
     receivedBytes += [data length];
@@ -149,6 +158,11 @@
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 166
 - (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didSendBodyData:(int64_t)bytesSent totalBytesSent:(int64_t)totalBytesWritten totalBytesExpectedToSend:(int64_t)totalBytesExpectedToWrite
153 167
 {
154 168
     expectedBytes = totalBytesExpectedToWrite;