Browse Source

Add network status indicator support

Ben Hsieh 8 years ago
parent
commit
c0edb71d7a
7 changed files with 53 additions and 38 deletions
  1. 1
    0
      README.md
  2. 1
    0
      src/ios/RNFetchBlobConst.h
  3. 1
    0
      src/ios/RNFetchBlobConst.m
  4. 30
    31
      src/ios/RNFetchBlobFS.m
  5. 16
    5
      src/ios/RNFetchBlobNetwork.m
  6. 1
    1
      src/package.json
  7. 3
    1
      src/types.js

+ 1
- 0
README.md View File

871
 
871
 
872
 | Version | |
872
 | Version | |
873
 |---|---|
873
 |---|---|
874
+| 0.5.6 | Add support for IOS network status indicator. Fix file stream ASCII reader bug. |
874
 | 0.5.5 | Remove work in progress code added in 0.5.2 which may cause memory leaks. |
875
 | 0.5.5 | Remove work in progress code added in 0.5.2 which may cause memory leaks. |
875
 | 0.5.4 | Fix #30 #31 build build error, and improve memory efficiency. |
876
 | 0.5.4 | Fix #30 #31 build build error, and improve memory efficiency. |
876
 | 0.5.3 | Add API for access untrusted SSL server |
877
 | 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";

+ 30
- 31
src/ios/RNFetchBlobFS.m View File

37
 
37
 
38
 // static member getter
38
 // static member getter
39
 + (NSArray *) getFileStreams {
39
 + (NSArray *) getFileStreams {
40
-    
40
+
41
     if(fileStreams == nil)
41
     if(fileStreams == nil)
42
         fileStreams = [[NSMutableDictionary alloc] init];
42
         fileStreams = [[NSMutableDictionary alloc] init];
43
     return fileStreams;
43
     return fileStreams;
70
 }
70
 }
71
 
71
 
72
 + (NSString *) getTempPath {
72
 + (NSString *) getTempPath {
73
-    
73
+
74
     return [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingString:@"/RNFetchBlob_tmp"];
74
     return [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingString:@"/RNFetchBlob_tmp"];
75
 }
75
 }
76
 
76
 
77
 + (NSString *) getTempPath:(NSString*)taskId withExtension:(NSString *)ext {
77
 + (NSString *) getTempPath:(NSString*)taskId withExtension:(NSString *)ext {
78
-    
78
+
79
     NSString * documentDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
79
     NSString * documentDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
80
     NSString * filename = [NSString stringWithFormat:@"/RNFetchBlob_tmp/RNFetchBlobTmp_%@", taskId];
80
     NSString * filename = [NSString stringWithFormat:@"/RNFetchBlob_tmp/RNFetchBlobTmp_%@", taskId];
81
     if(ext != nil)
81
     if(ext != nil)
173
             }
173
             }
174
             resolve(resultArray);
174
             resolve(resultArray);
175
         }
175
         }
176
-        
176
+
177
     }
177
     }
178
     @catch(NSException * e)
178
     @catch(NSException * e)
179
     {
179
     {
289
         [self.outStream close];
289
         [self.outStream close];
290
         self.outStream = nil;
290
         self.outStream = nil;
291
     }
291
     }
292
-    
292
+
293
 }
293
 }
294
 
294
 
295
 - (void)readWithPath:(NSString *)path useEncoding:(NSString *)encoding bufferSize:(int) bufferSize {
295
 - (void)readWithPath:(NSString *)path useEncoding:(NSString *)encoding bufferSize:(int) bufferSize {
296
-    
296
+
297
     self.inStream = [[NSInputStream alloc] initWithFileAtPath:path];
297
     self.inStream = [[NSInputStream alloc] initWithFileAtPath:path];
298
     self.inStream.delegate = self;
298
     self.inStream.delegate = self;
299
     self.encoding = encoding;
299
     self.encoding = encoding;
300
     self.path = path;
300
     self.path = path;
301
     self.bufferSize = bufferSize;
301
     self.bufferSize = bufferSize;
302
-    
302
+
303
     // NSStream needs a runloop so let's create a run loop for it
303
     // NSStream needs a runloop so let's create a run loop for it
304
     dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
304
     dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
305
     // start NSStream is a runloop
305
     // start NSStream is a runloop
308
                             forMode:NSDefaultRunLoopMode];
308
                             forMode:NSDefaultRunLoopMode];
309
         [inStream open];
309
         [inStream open];
310
         [[NSRunLoop currentRunLoop] run];
310
         [[NSRunLoop currentRunLoop] run];
311
-        
311
+
312
     });
312
     });
313
 }
313
 }
314
 
314
 
320
         [[RNFetchBlobFS getFileStreams] setValue:nil forKey:self.streamId];
320
         [[RNFetchBlobFS getFileStreams] setValue:nil forKey:self.streamId];
321
         self.streamId = nil;
321
         self.streamId = nil;
322
     }
322
     }
323
-    
323
+
324
 }
324
 }
325
 
325
 
326
 void runOnMainQueueWithoutDeadlocking(void (^block)(void))
326
 void runOnMainQueueWithoutDeadlocking(void (^block)(void))
339
 #pragma mark RNFetchBlobFS read stream delegate
339
 #pragma mark RNFetchBlobFS read stream delegate
340
 
340
 
341
 - (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode {
341
 - (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode {
342
-    
342
+
343
     NSString * streamEventCode = [NSString stringWithFormat:@"RNFetchBlobStream+%@", self.path];
343
     NSString * streamEventCode = [NSString stringWithFormat:@"RNFetchBlobStream+%@", self.path];
344
-    
344
+
345
     switch(eventCode) {
345
     switch(eventCode) {
346
-            
346
+
347
             // write stream event
347
             // write stream event
348
         case NSStreamEventHasSpaceAvailable:
348
         case NSStreamEventHasSpaceAvailable:
349
         {
349
         {
350
-            
351
-            
350
+
351
+
352
         }
352
         }
353
-            
353
+
354
             // read stream incoming chunk
354
             // read stream incoming chunk
355
         case NSStreamEventHasBytesAvailable:
355
         case NSStreamEventHasBytesAvailable:
356
         {
356
         {
360
                 chunkSize = 4095;
360
                 chunkSize = 4095;
361
             if(self.bufferSize > 0)
361
             if(self.bufferSize > 0)
362
                 chunkSize = self.bufferSize;
362
                 chunkSize = self.bufferSize;
363
-            uint8_t * buf = (uint8_t *)malloc(chunkSize);
363
+//            uint8_t * buf = (uint8_t *)malloc(chunkSize);
364
+            uint8_t buf[chunkSize];
364
             unsigned int len = 0;
365
             unsigned int len = 0;
365
             len = [(NSInputStream *)stream read:buf maxLength:chunkSize];
366
             len = [(NSInputStream *)stream read:buf maxLength:chunkSize];
366
             // still have data in stream
367
             // still have data in stream
385
                             [asciiArray addObject:[NSNumber numberWithChar:bytePtr[i]]];
386
                             [asciiArray addObject:[NSNumber numberWithChar:bytePtr[i]]];
386
                         }
387
                         }
387
                     }
388
                     }
388
-                    
389
+
389
                     [self.bridge.eventDispatcher
390
                     [self.bridge.eventDispatcher
390
                      sendDeviceEventWithName:streamEventCode
391
                      sendDeviceEventWithName:streamEventCode
391
                      body: @{
392
                      body: @{
393
                              @"detail": asciiArray
394
                              @"detail": asciiArray
394
                             }
395
                             }
395
                      ];
396
                      ];
396
-                    free(buf);
397
-                    bytePtr = nil;
398
-                    asciiArray = nil;
399
-                    buf = nil;
400
-                    chunkData = nil;
397
+//                    free(buf);
398
+//                    asciiStr = nil;
399
+//                    buf = nil;
400
+//                    chunkData = nil;
401
                     return;
401
                     return;
402
                 }
402
                 }
403
                 // convert byte array to base64 data chunks
403
                 // convert byte array to base64 data chunks
415
                      ];
415
                      ];
416
                     return;
416
                     return;
417
                 }
417
                 }
418
-                
418
+
419
                 [self.bridge.eventDispatcher
419
                 [self.bridge.eventDispatcher
420
                  sendDeviceEventWithName:streamEventCode
420
                  sendDeviceEventWithName:streamEventCode
421
                  body:@{
421
                  body:@{
423
                         @"detail": encodedChunk
423
                         @"detail": encodedChunk
424
                         }
424
                         }
425
                  ];
425
                  ];
426
-                chunkData = nil;
427
-                free(buf);
426
+//                chunkData = nil;
427
+//                free(buf);
428
             }
428
             }
429
             // end of stream
429
             // end of stream
430
             else {
430
             else {
435
                         @"detail": @""
435
                         @"detail": @""
436
                         }
436
                         }
437
                  ];
437
                  ];
438
-                chunkData = nil;
439
-                free(buf);
438
+//                chunkData = nil;
439
+//                free(buf);
440
             }
440
             }
441
             break;
441
             break;
442
         }
442
         }
443
-            
443
+
444
             // stream error
444
             // stream error
445
         case NSStreamEventErrorOccurred:
445
         case NSStreamEventErrorOccurred:
446
         {
446
         {
453
              ];
453
              ];
454
             break;
454
             break;
455
         }
455
         }
456
-            
456
+
457
     }
457
     }
458
-    
458
+
459
 }
459
 }
460
 
460
 
461
 @end
461
 @end
462
-

+ 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 = {