Browse Source

Add network status indicator support

Ben Hsieh 8 years ago
parent
commit
0bcf47ea27
7 changed files with 36 additions and 18 deletions
  1. 2
    1
      README.md
  2. 1
    0
      src/ios/RNFetchBlobConst.h
  3. 1
    0
      src/ios/RNFetchBlobConst.m
  4. 12
    10
      src/ios/RNFetchBlobFS.m
  5. 16
    5
      src/ios/RNFetchBlobNetwork.m
  6. 1
    1
      src/package.json
  7. 3
    1
      src/types.js

+ 2
- 1
README.md View File

1
-# react-native-fetch-blob [![npm version](https://img.shields.io/badge/npm package-0.5.5-brightgreen.svg)](https://badge.fury.io/js/react-native-fetch-blob) ![](https://img.shields.io/badge/PR-Welcome-brightgreen.svg) ![](https://img.shields.io/badge/in progress-0.6.0-yellow.svg)
1
+# react-native-fetch-blob [![npm version](https://img.shields.io/badge/npm package-0.5.6-brightgreen.svg)](https://badge.fury.io/js/react-native-fetch-blob) ![](https://img.shields.io/badge/PR-Welcome-brightgreen.svg) ![](https://img.shields.io/badge/in progress-0.6.0-yellow.svg)
2
 
2
 
3
 A module provides upload, download, and files access API. Supports file stream read/write for process large files.
3
 A module provides upload, download, and files access API. Supports file stream read/write for process large files.
4
 
4
 
847
 
847
 
848
 | Version | |
848
 | Version | |
849
 |---|---|
849
 |---|---|
850
+| 0.5.6 | Add support for IOS network status indicator. Fix file stream ASCII reader bug. |
850
 | 0.5.5 | Remove work in progress code added in 0.5.2 which may cause memory leaks. |
851
 | 0.5.5 | Remove work in progress code added in 0.5.2 which may cause memory leaks. |
851
 | 0.5.4 | Fix #30 #31 build build error, and improve memory efficiency. |
852
 | 0.5.4 | Fix #30 #31 build build error, and improve memory efficiency. |
852
 | 0.5.3 | Add API for access untrusted SSL server |
853
 | 0.5.3 | Add API for access untrusted SSL server |

+ 1
- 0
src/ios/RNFetchBlobConst.h View File

23
 extern NSString *const CONFIG_FILE_PATH;
23
 extern NSString *const CONFIG_FILE_PATH;
24
 extern NSString *const CONFIG_FILE_EXT;
24
 extern NSString *const CONFIG_FILE_EXT;
25
 extern NSString *const CONFIG_TRUSTY;
25
 extern NSString *const CONFIG_TRUSTY;
26
+extern NSString *const CONFIG_INDICATOR;
26
 
27
 
27
 // fs events
28
 // fs events
28
 extern NSString *const FS_EVENT_DATA;
29
 extern NSString *const FS_EVENT_DATA;

+ 1
- 0
src/ios/RNFetchBlobConst.m View File

14
 extern NSString *const CONFIG_FILE_PATH = @"path";
14
 extern NSString *const CONFIG_FILE_PATH = @"path";
15
 extern NSString *const CONFIG_FILE_EXT = @"appendExt";
15
 extern NSString *const CONFIG_FILE_EXT = @"appendExt";
16
 extern NSString *const CONFIG_TRUSTY = @"trusty";
16
 extern NSString *const CONFIG_TRUSTY = @"trusty";
17
+extern NSString *const CONFIG_INDICATOR = @"indicator";
17
 
18
 
18
 
19
 
19
 extern NSString *const MSG_EVENT = @"RNFetchBlobMessage";
20
 extern NSString *const MSG_EVENT = @"RNFetchBlobMessage";

+ 12
- 10
src/ios/RNFetchBlobFS.m View File

264
                 chunkSize = 4095;
264
                 chunkSize = 4095;
265
             if(self.bufferSize > 0)
265
             if(self.bufferSize > 0)
266
                 chunkSize = self.bufferSize;
266
                 chunkSize = self.bufferSize;
267
-            uint8_t * buf = (uint8_t *)malloc(chunkSize);
267
+//            uint8_t * buf = (uint8_t *)malloc(chunkSize);
268
+            uint8_t buf[chunkSize];
268
             unsigned int len = 0;
269
             unsigned int len = 0;
269
             len = [(NSInputStream *)stream read:buf maxLength:chunkSize];
270
             len = [(NSInputStream *)stream read:buf maxLength:chunkSize];
270
             // still have data in stream
271
             // still have data in stream
281
                     NSString * asciiStr = @"[";
282
                     NSString * asciiStr = @"[";
282
                     if (chunkData.length > 0)
283
                     if (chunkData.length > 0)
283
                     {
284
                     {
285
+//                        unsigned char *bytePtr = (unsigned char *)[chunkData bytes];
284
                         unsigned char *bytePtr = (unsigned char *)[chunkData bytes];
286
                         unsigned char *bytePtr = (unsigned char *)[chunkData bytes];
285
                         NSInteger byteLen = chunkData.length/sizeof(uint8_t);
287
                         NSInteger byteLen = chunkData.length/sizeof(uint8_t);
286
                         for (int i = 0; i < byteLen; i++)
288
                         for (int i = 0; i < byteLen; i++)
291
                             else
293
                             else
292
                                 asciiStr = [asciiStr stringByAppendingFormat:@"%d", val];
294
                                 asciiStr = [asciiStr stringByAppendingFormat:@"%d", val];
293
                         }
295
                         }
294
-                        free(bytePtr);
296
+//                        free(bytePtr);
295
                     }
297
                     }
296
                     asciiStr = [asciiStr stringByAppendingString:@"]"];
298
                     asciiStr = [asciiStr stringByAppendingString:@"]"];
297
                     [self.bridge.eventDispatcher
299
                     [self.bridge.eventDispatcher
301
                              @"detail": asciiStr
303
                              @"detail": asciiStr
302
                             }
304
                             }
303
                      ];
305
                      ];
304
-                    free(buf);
305
-                    asciiStr = nil;
306
-                    buf = nil;
307
-                    chunkData = nil;
306
+//                    free(buf);
307
+//                    asciiStr = nil;
308
+//                    buf = nil;
309
+//                    chunkData = nil;
308
                     return;
310
                     return;
309
                 }
311
                 }
310
                 // convert byte array to base64 data chunks
312
                 // convert byte array to base64 data chunks
330
                         @"detail": encodedChunk
332
                         @"detail": encodedChunk
331
                         }
333
                         }
332
                  ];
334
                  ];
333
-                chunkData = nil;
334
-                free(buf);
335
+//                chunkData = nil;
336
+//                free(buf);
335
             }
337
             }
336
             // end of stream
338
             // end of stream
337
             else {
339
             else {
342
                         @"detail": @""
344
                         @"detail": @""
343
                         }
345
                         }
344
                  ];
346
                  ];
345
-                chunkData = nil;
346
-                free(buf);
347
+//                chunkData = nil;
348
+//                free(buf);
347
             }
349
             }
348
             break;
350
             break;
349
         }
351
         }

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

77
     if([options valueForKey:CONFIG_TRUSTY] != nil)
77
     if([options valueForKey:CONFIG_TRUSTY] != nil)
78
     {
78
     {
79
         NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
79
         NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
80
-        session = [NSURLSession sessionWithConfiguration:defaultConfigObject delegate:self delegateQueue:taskQueue];
80
+        session = [NSURLSession sessionWithConfiguration:defaultConfigObject delegate:self delegateQueue:[NSOperationQueue mainQueue]];
81
     }
81
     }
82
     // the session validates SSL certification, self-signed certification will be aborted
82
     // the session validates SSL certification, self-signed certification will be aborted
83
     else
83
     else
105
             callback(@[[NSNull null], path]);
105
             callback(@[[NSNull null], path]);
106
             // prevent memory leaks
106
             // prevent memory leaks
107
             self.respData = nil;
107
             self.respData = nil;
108
+            [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
108
         }];
109
         }];
109
         [task resume];
110
         [task resume];
110
     }
111
     }
127
                 return;
128
                 return;
128
             }
129
             }
129
             callback(@[[NSNull null], tmpPath]);
130
             callback(@[[NSNull null], tmpPath]);
131
+            [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
130
             // prevent memory leaks
132
             // prevent memory leaks
131
             self.respData = nil;
133
             self.respData = nil;
132
         }];
134
         }];
143
             else {
145
             else {
144
                 callback(@[[NSNull null], [resp base64EncodedStringWithOptions:0]]);
146
                 callback(@[[NSNull null], [resp base64EncodedStringWithOptions:0]]);
145
             }
147
             }
148
+            self.respData = nil;
149
+            [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
146
         }];
150
         }];
147
         [task resume];
151
         [task resume];
148
     }
152
     }
153
+    
154
+    // network status indicator
155
+    if([[options objectForKey:CONFIG_INDICATOR] boolValue] == YES)
156
+        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
149
 }
157
 }
150
 
158
 
151
 ////////////////////////////////////////
159
 ////////////////////////////////////////
188
 - (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {
196
 - (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {
189
     NSLog([error localizedDescription]);
197
     NSLog([error localizedDescription]);
190
     self.error = error;
198
     self.error = error;
199
+    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
191
 }
200
 }
192
 
201
 
193
 // upload progress handler
202
 // upload progress handler
205
      ];
214
      ];
206
 }
215
 }
207
 
216
 
208
-- (void) application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier completionHandler:(void (^)())completionHandler {
209
-    
210
-}
217
+//- (void) application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier completionHandler:(void (^)())completionHandler {
218
+//    
219
+//}
211
 
220
 
212
 //- (void) URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session
221
 //- (void) URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session
213
 //{
222
 //{
225
 {
234
 {
226
     if([options valueForKey:CONFIG_TRUSTY] != nil)
235
     if([options valueForKey:CONFIG_TRUSTY] != nil)
227
         completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);
236
         completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);
228
-    else
237
+    else {
229
         RCTLogWarn(@"counld not create connection with an unstrusted SSL certification, if you're going to create connection anyway, add `trusty:true` to RNFetchBlob.config");
238
         RCTLogWarn(@"counld not create connection with an unstrusted SSL certification, if you're going to create connection anyway, add `trusty:true` to RNFetchBlob.config");
239
+        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
240
+    }
230
 }
241
 }
231
 
242
 
232
 @end
243
 @end

+ 1
- 1
src/package.json View File

1
 {
1
 {
2
   "name": "react-native-fetch-blob",
2
   "name": "react-native-fetch-blob",
3
-  "version": "0.5.5",
3
+  "version": "0.5.6",
4
   "description": "A module provides upload, download, and files access API. Supports file stream read/write for process large files.",
4
   "description": "A module provides upload, download, and files access API. Supports file stream read/write for process large files.",
5
   "main": "index.js",
5
   "main": "index.js",
6
   "scripts": {
6
   "scripts": {

+ 3
- 1
src/types.js View File

3
   fileCache : bool,
3
   fileCache : bool,
4
   path : string,
4
   path : string,
5
   appendExt : string,
5
   appendExt : string,
6
-  session : string
6
+  session : string,
7
+  addAndroidDownloads : any,
8
+  indicator : bool
7
 };
9
 };
8
 
10
 
9
 type RNFetchBlobNative = {
11
 type RNFetchBlobNative = {