Procházet zdrojové kódy

Add network status indicator support

Ben Hsieh před 8 roky
rodič
revize
c0edb71d7a

+ 1
- 0
README.md Zobrazit soubor

@@ -871,6 +871,7 @@ A `session` is an object that helps you manage files. It simply maintains a list
871 871
 
872 872
 | Version | |
873 873
 |---|---|
874
+| 0.5.6 | Add support for IOS network status indicator. Fix file stream ASCII reader bug. |
874 875
 | 0.5.5 | Remove work in progress code added in 0.5.2 which may cause memory leaks. |
875 876
 | 0.5.4 | Fix #30 #31 build build error, and improve memory efficiency. |
876 877
 | 0.5.3 | Add API for access untrusted SSL server |

+ 1
- 0
src/ios/RNFetchBlobConst.h Zobrazit soubor

@@ -23,6 +23,7 @@ extern NSString *const CONFIG_USE_TEMP;
23 23
 extern NSString *const CONFIG_FILE_PATH;
24 24
 extern NSString *const CONFIG_FILE_EXT;
25 25
 extern NSString *const CONFIG_TRUSTY;
26
+extern NSString *const CONFIG_INDICATOR;
26 27
 
27 28
 // fs events
28 29
 extern NSString *const FS_EVENT_DATA;

+ 1
- 0
src/ios/RNFetchBlobConst.m Zobrazit soubor

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

+ 30
- 31
src/ios/RNFetchBlobFS.m Zobrazit soubor

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

+ 16
- 5
src/ios/RNFetchBlobNetwork.m Zobrazit soubor

@@ -77,7 +77,7 @@ NSOperationQueue *taskQueue;
77 77
     if([options valueForKey:CONFIG_TRUSTY] != nil)
78 78
     {
79 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 82
     // the session validates SSL certification, self-signed certification will be aborted
83 83
     else
@@ -105,6 +105,7 @@ NSOperationQueue *taskQueue;
105 105
             callback(@[[NSNull null], path]);
106 106
             // prevent memory leaks
107 107
             self.respData = nil;
108
+            [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
108 109
         }];
109 110
         [task resume];
110 111
     }
@@ -127,6 +128,7 @@ NSOperationQueue *taskQueue;
127 128
                 return;
128 129
             }
129 130
             callback(@[[NSNull null], tmpPath]);
131
+            [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
130 132
             // prevent memory leaks
131 133
             self.respData = nil;
132 134
         }];
@@ -143,9 +145,15 @@ NSOperationQueue *taskQueue;
143 145
             else {
144 146
                 callback(@[[NSNull null], [resp base64EncodedStringWithOptions:0]]);
145 147
             }
148
+            self.respData = nil;
149
+            [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
146 150
         }];
147 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,6 +196,7 @@ NSOperationQueue *taskQueue;
188 196
 - (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {
189 197
     NSLog([error localizedDescription]);
190 198
     self.error = error;
199
+    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
191 200
 }
192 201
 
193 202
 // upload progress handler
@@ -205,9 +214,9 @@ NSOperationQueue *taskQueue;
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 221
 //- (void) URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session
213 222
 //{
@@ -225,8 +234,10 @@ NSOperationQueue *taskQueue;
225 234
 {
226 235
     if([options valueForKey:CONFIG_TRUSTY] != nil)
227 236
         completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);
228
-    else
237
+    else {
229 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 243
 @end

+ 1
- 1
src/package.json Zobrazit soubor

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

+ 3
- 1
src/types.js Zobrazit soubor

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