Skip to content
This repository was archived by the owner on Mar 16, 2019. It is now read-only.

Commit c0edb71

Browse files
committed
Add network status indicator support
1 parent b7b1969 commit c0edb71

File tree

7 files changed

+53
-38
lines changed

7 files changed

+53
-38
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -871,6 +871,7 @@ A `session` is an object that helps you manage files. It simply maintains a list
871871

872872
| Version | |
873873
|---|---|
874+
| 0.5.6 | Add support for IOS network status indicator. Fix file stream ASCII reader bug. |
874875
| 0.5.5 | Remove work in progress code added in 0.5.2 which may cause memory leaks. |
875876
| 0.5.4 | Fix #30 #31 build build error, and improve memory efficiency. |
876877
| 0.5.3 | Add API for access untrusted SSL server |

src/ios/RNFetchBlobConst.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ extern NSString *const CONFIG_USE_TEMP;
2323
extern NSString *const CONFIG_FILE_PATH;
2424
extern NSString *const CONFIG_FILE_EXT;
2525
extern NSString *const CONFIG_TRUSTY;
26+
extern NSString *const CONFIG_INDICATOR;
2627

2728
// fs events
2829
extern NSString *const FS_EVENT_DATA;

src/ios/RNFetchBlobConst.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
extern NSString *const CONFIG_FILE_PATH = @"path";
1515
extern NSString *const CONFIG_FILE_EXT = @"appendExt";
1616
extern NSString *const CONFIG_TRUSTY = @"trusty";
17+
extern NSString *const CONFIG_INDICATOR = @"indicator";
1718

1819

1920
extern NSString *const MSG_EVENT = @"RNFetchBlobMessage";

src/ios/RNFetchBlobFS.m

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ @implementation RNFetchBlobFS
3737

3838
// static member getter
3939
+ (NSArray *) getFileStreams {
40-
40+
4141
if(fileStreams == nil)
4242
fileStreams = [[NSMutableDictionary alloc] init];
4343
return fileStreams;
@@ -70,12 +70,12 @@ + (NSString *) getPictureDir {
7070
}
7171

7272
+ (NSString *) getTempPath {
73-
73+
7474
return [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingString:@"/RNFetchBlob_tmp"];
7575
}
7676

7777
+ (NSString *) getTempPath:(NSString*)taskId withExtension:(NSString *)ext {
78-
78+
7979
NSString * documentDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
8080
NSString * filename = [NSString stringWithFormat:@"/RNFetchBlob_tmp/RNFetchBlobTmp_%@", taskId];
8181
if(ext != nil)
@@ -173,7 +173,7 @@ + (void) readFile:(NSString *)path encoding:(NSString *)encoding resolver:(RCTPr
173173
}
174174
resolve(resultArray);
175175
}
176-
176+
177177
}
178178
@catch(NSException * e)
179179
{
@@ -289,17 +289,17 @@ - (void)closeOutStream {
289289
[self.outStream close];
290290
self.outStream = nil;
291291
}
292-
292+
293293
}
294294

295295
- (void)readWithPath:(NSString *)path useEncoding:(NSString *)encoding bufferSize:(int) bufferSize {
296-
296+
297297
self.inStream = [[NSInputStream alloc] initWithFileAtPath:path];
298298
self.inStream.delegate = self;
299299
self.encoding = encoding;
300300
self.path = path;
301301
self.bufferSize = bufferSize;
302-
302+
303303
// NSStream needs a runloop so let's create a run loop for it
304304
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
305305
// start NSStream is a runloop
@@ -308,7 +308,7 @@ - (void)readWithPath:(NSString *)path useEncoding:(NSString *)encoding bufferSiz
308308
forMode:NSDefaultRunLoopMode];
309309
[inStream open];
310310
[[NSRunLoop currentRunLoop] run];
311-
311+
312312
});
313313
}
314314

@@ -320,7 +320,7 @@ - (void)closeInStream {
320320
[[RNFetchBlobFS getFileStreams] setValue:nil forKey:self.streamId];
321321
self.streamId = nil;
322322
}
323-
323+
324324
}
325325

326326
void runOnMainQueueWithoutDeadlocking(void (^block)(void))
@@ -339,18 +339,18 @@ void runOnMainQueueWithoutDeadlocking(void (^block)(void))
339339
#pragma mark RNFetchBlobFS read stream delegate
340340

341341
- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode {
342-
342+
343343
NSString * streamEventCode = [NSString stringWithFormat:@"RNFetchBlobStream+%@", self.path];
344-
344+
345345
switch(eventCode) {
346-
346+
347347
// write stream event
348348
case NSStreamEventHasSpaceAvailable:
349349
{
350-
351-
350+
351+
352352
}
353-
353+
354354
// read stream incoming chunk
355355
case NSStreamEventHasBytesAvailable:
356356
{
@@ -360,7 +360,8 @@ - (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode {
360360
chunkSize = 4095;
361361
if(self.bufferSize > 0)
362362
chunkSize = self.bufferSize;
363-
uint8_t * buf = (uint8_t *)malloc(chunkSize);
363+
// uint8_t * buf = (uint8_t *)malloc(chunkSize);
364+
uint8_t buf[chunkSize];
364365
unsigned int len = 0;
365366
len = [(NSInputStream *)stream read:buf maxLength:chunkSize];
366367
// still have data in stream
@@ -385,19 +386,18 @@ - (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode {
385386
[asciiArray addObject:[NSNumber numberWithChar:bytePtr[i]]];
386387
}
387388
}
388-
389+
389390
[self.bridge.eventDispatcher
390391
sendDeviceEventWithName:streamEventCode
391392
body: @{
392393
@"event": FS_EVENT_DATA,
393394
@"detail": asciiArray
394395
}
395396
];
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;
401401
return;
402402
}
403403
// convert byte array to base64 data chunks
@@ -415,16 +415,16 @@ - (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode {
415415
];
416416
return;
417417
}
418-
418+
419419
[self.bridge.eventDispatcher
420420
sendDeviceEventWithName:streamEventCode
421421
body:@{
422422
@"event": FS_EVENT_DATA,
423423
@"detail": encodedChunk
424424
}
425425
];
426-
chunkData = nil;
427-
free(buf);
426+
// chunkData = nil;
427+
// free(buf);
428428
}
429429
// end of stream
430430
else {
@@ -435,12 +435,12 @@ - (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode {
435435
@"detail": @""
436436
}
437437
];
438-
chunkData = nil;
439-
free(buf);
438+
// chunkData = nil;
439+
// free(buf);
440440
}
441441
break;
442442
}
443-
443+
444444
// stream error
445445
case NSStreamEventErrorOccurred:
446446
{
@@ -453,10 +453,9 @@ - (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode {
453453
];
454454
break;
455455
}
456-
456+
457457
}
458-
458+
459459
}
460460

461461
@end
462-

src/ios/RNFetchBlobNetwork.m

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ - (void) sendRequest:(NSDictionary * _Nullable )options bridge:(RCTBridge * _Nu
7777
if([options valueForKey:CONFIG_TRUSTY] != nil)
7878
{
7979
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
80-
session = [NSURLSession sessionWithConfiguration:defaultConfigObject delegate:self delegateQueue:taskQueue];
80+
session = [NSURLSession sessionWithConfiguration:defaultConfigObject delegate:self delegateQueue:[NSOperationQueue mainQueue]];
8181
}
8282
// the session validates SSL certification, self-signed certification will be aborted
8383
else
@@ -105,6 +105,7 @@ - (void) sendRequest:(NSDictionary * _Nullable )options bridge:(RCTBridge * _Nu
105105
callback(@[[NSNull null], path]);
106106
// prevent memory leaks
107107
self.respData = nil;
108+
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
108109
}];
109110
[task resume];
110111
}
@@ -127,6 +128,7 @@ - (void) sendRequest:(NSDictionary * _Nullable )options bridge:(RCTBridge * _Nu
127128
return;
128129
}
129130
callback(@[[NSNull null], tmpPath]);
131+
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
130132
// prevent memory leaks
131133
self.respData = nil;
132134
}];
@@ -143,9 +145,15 @@ - (void) sendRequest:(NSDictionary * _Nullable )options bridge:(RCTBridge * _Nu
143145
else {
144146
callback(@[[NSNull null], [resp base64EncodedStringWithOptions:0]]);
145147
}
148+
self.respData = nil;
149+
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
146150
}];
147151
[task resume];
148152
}
153+
154+
// network status indicator
155+
if([[options objectForKey:CONFIG_INDICATOR] boolValue] == YES)
156+
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
149157
}
150158

151159
////////////////////////////////////////
@@ -188,6 +196,7 @@ - (void) URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dat
188196
- (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {
189197
NSLog([error localizedDescription]);
190198
self.error = error;
199+
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
191200
}
192201

193202
// upload progress handler
@@ -205,9 +214,9 @@ - (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didSen
205214
];
206215
}
207216

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+
//}
211220

212221
//- (void) URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session
213222
//{
@@ -225,8 +234,10 @@ - (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didRec
225234
{
226235
if([options valueForKey:CONFIG_TRUSTY] != nil)
227236
completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);
228-
else
237+
else {
229238
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+
}
230241
}
231242

232243
@end

src/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-fetch-blob",
3-
"version": "0.5.5",
3+
"version": "0.5.6",
44
"description": "A module provides upload, download, and files access API. Supports file stream read/write for process large files.",
55
"main": "index.js",
66
"scripts": {

src/types.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ type RNFetchBlobConfig = {
33
fileCache : bool,
44
path : string,
55
appendExt : string,
6-
session : string
6+
session : string,
7+
addAndroidDownloads : any,
8+
indicator : bool
79
};
810

911
type RNFetchBlobNative = {

0 commit comments

Comments
 (0)