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

Commit ca0fd9f

Browse files
committed
IOS wip commit #140
1 parent 389eda8 commit ca0fd9f

File tree

6 files changed

+117
-7
lines changed

6 files changed

+117
-7
lines changed

src/ios/RNFetchBlob.xcodeproj/project.pbxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
A158F4301D0539DB006FFD38 /* RNFetchBlobNetwork.m in Sources */ = {isa = PBXBuildFile; fileRef = A158F42F1D0539DB006FFD38 /* RNFetchBlobNetwork.m */; };
1313
A15C30141CD25C330074CB35 /* RNFetchBlob.m in Sources */ = {isa = PBXBuildFile; fileRef = A15C30131CD25C330074CB35 /* RNFetchBlob.m */; };
1414
A166D1AA1CE0647A00273590 /* RNFetchBlob.h in Sources */ = {isa = PBXBuildFile; fileRef = A15C30111CD25C330074CB35 /* RNFetchBlob.h */; };
15+
A19B48251D98102400E6868A /* RNFetchBlobProgress.m in Sources */ = {isa = PBXBuildFile; fileRef = A19B48241D98102400E6868A /* RNFetchBlobProgress.m */; };
1516
A1AAE2991D300E4D0051D11C /* RNFetchBlobReqBuilder.m in Sources */ = {isa = PBXBuildFile; fileRef = A1AAE2981D300E4D0051D11C /* RNFetchBlobReqBuilder.m */; };
1617
/* End PBXBuildFile section */
1718

@@ -37,6 +38,8 @@
3738
A15C300E1CD25C330074CB35 /* libRNFetchBlob.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libRNFetchBlob.a; sourceTree = BUILT_PRODUCTS_DIR; };
3839
A15C30111CD25C330074CB35 /* RNFetchBlob.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = RNFetchBlob.h; path = RNFetchBlob/RNFetchBlob.h; sourceTree = "<group>"; };
3940
A15C30131CD25C330074CB35 /* RNFetchBlob.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = RNFetchBlob.m; path = RNFetchBlob/RNFetchBlob.m; sourceTree = "<group>"; };
41+
A19B48231D98100800E6868A /* RNFetchBlobProgress.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RNFetchBlobProgress.h; sourceTree = "<group>"; };
42+
A19B48241D98102400E6868A /* RNFetchBlobProgress.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RNFetchBlobProgress.m; sourceTree = "<group>"; };
4043
A1AAE2971D300E3E0051D11C /* RNFetchBlobReqBuilder.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RNFetchBlobReqBuilder.h; sourceTree = "<group>"; };
4144
A1AAE2981D300E4D0051D11C /* RNFetchBlobReqBuilder.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RNFetchBlobReqBuilder.m; sourceTree = "<group>"; };
4245
A1F950181D7E9134002A95A6 /* IOS7Polyfill.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = IOS7Polyfill.h; sourceTree = "<group>"; };
@@ -63,6 +66,8 @@
6366
A15C30051CD25C330074CB35 = {
6467
isa = PBXGroup;
6568
children = (
69+
A19B48241D98102400E6868A /* RNFetchBlobProgress.m */,
70+
A19B48231D98100800E6868A /* RNFetchBlobProgress.h */,
6671
A1F950181D7E9134002A95A6 /* IOS7Polyfill.h */,
6772
A1AAE2981D300E4D0051D11C /* RNFetchBlobReqBuilder.m */,
6873
A1AAE2971D300E3E0051D11C /* RNFetchBlobReqBuilder.h */,
@@ -147,6 +152,7 @@
147152
A158F42D1D0535BB006FFD38 /* RNFetchBlobConst.m in Sources */,
148153
A158F4271D052E49006FFD38 /* RNFetchBlobFS.m in Sources */,
149154
A158F4301D0539DB006FFD38 /* RNFetchBlobNetwork.m in Sources */,
155+
A19B48251D98102400E6868A /* RNFetchBlobProgress.m in Sources */,
150156
A1AAE2991D300E4D0051D11C /* RNFetchBlobReqBuilder.m in Sources */,
151157
A15C30141CD25C330074CB35 /* RNFetchBlob.m in Sources */,
152158
);

src/ios/RNFetchBlob/RNFetchBlob.m

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#import "RNFetchBlobNetwork.h"
1313
#import "RNFetchBlobConst.h"
1414
#import "RNFetchBlobReqBuilder.h"
15+
#import "RNFetchBlobProgress.h"
1516

1617

1718
RCTBridge * bridgeRef;
@@ -413,13 +414,15 @@ - (NSDictionary *)constantsToExport
413414
}
414415

415416
#pragma mark - net.enableProgressReport
416-
RCT_EXPORT_METHOD(enableProgressReport:(NSString *)taskId {
417-
[RNFetchBlobNetwork enableProgressReport:taskId];
417+
RCT_EXPORT_METHOD(enableProgressReport:(NSString *)taskId interval:(NSNumber*)interval count:(NSNumber*)count {
418+
RNFetchBlobProgress * cfg = [[RNFetchBlobProgress alloc] initWithType:Download interval:interval count:count];
419+
[RNFetchBlobNetwork enableProgressReport:taskId config:cfg];
418420
})
419421

420422
#pragma mark - net.enableUploadProgressReport
421-
RCT_EXPORT_METHOD(enableUploadProgressReport:(NSString *)taskId {
422-
[RNFetchBlobNetwork enableUploadProgress:taskId];
423+
RCT_EXPORT_METHOD(enableUploadProgressReport:(NSString *)taskId interval:(NSNumber*)interval count:(NSNumber*)count{
424+
RNFetchBlobProgress * cfg = [[RNFetchBlobProgress alloc] initWithType:Upload interval:interval count:count];
425+
[RNFetchBlobNetwork enableUploadProgress:taskId config:cfg];
423426
})
424427

425428
#pragma mark - fs.slice

src/ios/RNFetchBlobNetwork.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#import <Foundation/Foundation.h>
1313
#import "RCTBridgeModule.h"
14+
#import "RNFetchBlobProgress.h"
1415
#import "RNFetchBlobFS.h"
1516

1617
typedef void(^CompletionHander)(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error);
@@ -36,8 +37,8 @@ typedef void(^DataTaskCompletionHander) (NSData * _Nullable resp, NSURLResponse
3637

3738
+ (NSMutableDictionary * _Nullable ) normalizeHeaders:(NSDictionary * _Nullable)headers;
3839
+ (void) cancelRequest:(NSString *)taskId;
39-
+ (void) enableProgressReport:(NSString *) taskId;
40-
+ (void) enableUploadProgress:(NSString *) taskId;
40+
+ (void) enableProgressReport:(NSString *) taskId config:(RNFetchBlobProgress*)config;
41+
+ (void) enableUploadProgress:(NSString *) taskId config:(RNFetchBlobProgress*)config;
4142
- (void) sendRequest:(NSDictionary * _Nullable )options contentLength:(long)contentLength bridge:(RCTBridge * _Nullable)bridgeRef taskId:(NSString * _Nullable)taskId withRequest:(NSURLRequest * _Nullable)req callback:(_Nullable RCTResponseSenderBlock) callback;
4243

4344

src/ios/RNFetchBlobNetwork.m

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#import "RNFetchBlobReqBuilder.h"
1717
#import "IOS7Polyfill.h"
1818
#import <CommonCrypto/CommonDigest.h>
19+
#import "RNFetchBlobProgress.h"
1920

2021
////////////////////////////////////////
2122
//
@@ -294,9 +295,12 @@ - (void) URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dat
294295
BOOL appendToExistingFile = [destPath RNFBContainsString:@"?append=true"];
295296
// For solving #141 append response data if the file already exists
296297
// base on PR#139 @kejinliang
297-
if (appendToExistingFile && ![fm fileExistsAtPath:destPath])
298+
if(appendToExistingFile)
298299
{
299300
destPath = [destPath stringByReplacingOccurrencesOfString:@"?append=true" withString:@""];
301+
}
302+
if (![fm fileExistsAtPath:destPath])
303+
{
300304
[fm createFileAtPath:destPath contents:[[NSData alloc] init] attributes:nil];
301305
}
302306
writeStream = [[NSOutputStream alloc] initToFileAtPath:destPath append:YES];

src/ios/RNFetchBlobProgress.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//
2+
// RNFetchBlobProgress.h
3+
// RNFetchBlob
4+
//
5+
// Created by Ben Hsieh on 2016/9/25.
6+
// Copyright © 2016年 wkh237.github.io. All rights reserved.
7+
//
8+
9+
#ifndef RNFetchBlobProgress_h
10+
#define RNFetchBlobProgress_h
11+
12+
#import <Foundation/Foundation.h>
13+
14+
typedef NS_ENUM(NSUInteger, ProgressType) {
15+
Download,
16+
Upload,
17+
};
18+
19+
@interface RNFetchBlobProgress : NSObject
20+
{
21+
NSNumber * count;
22+
NSNumber * interval;
23+
ProgressType type;
24+
BOOL enable;
25+
26+
}
27+
28+
@property (nonatomic) NSNumber * count;
29+
@property (nonatomic) NSNumber * interval;
30+
@property (nonatomic) NSInteger type;
31+
@property (nonatomic) BOOL enable;
32+
33+
-(id)initWithType:(ProgressType)type interval:(NSNumber*)interval count:(NSNumber *)count;
34+
-(BOOL)shouldReport;
35+
-(void)tick;
36+
37+
38+
@end
39+
40+
#endif /* RNFetchBlobProgress_h */

src/ios/RNFetchBlobProgress.m

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//
2+
// RNFetchBlobProgress.m
3+
// RNFetchBlob
4+
//
5+
// Created by Ben Hsieh on 2016/9/25.
6+
// Copyright © 2016年 wkh237.github.io. All rights reserved.
7+
//
8+
9+
#import "RNFetchBlobProgress.h"
10+
11+
@interface RNFetchBlobProgress ()
12+
{
13+
float progress;
14+
int tick;
15+
long lastTick;
16+
}
17+
@end
18+
19+
@implementation RNFetchBlobProgress
20+
21+
-(id)initWithType:(ProgressType)type interval:(NSNumber *)interval count:(NSNumber *)count
22+
{
23+
self = [super init];
24+
self.count = count;
25+
self.interval = interval;
26+
self.type = type;
27+
self.enable = YES;
28+
return self;
29+
}
30+
31+
-(void)tick:(NSNumber *) nextProgress
32+
{
33+
progress = [nextProgress floatValue];
34+
tick++;
35+
NSTimeInterval timeStamp = [[NSDate date] timeIntervalSince1970];
36+
// NSTimeInterval is defined as double
37+
NSNumber *timeStampObj = [NSNumber numberWithDouble: timeStamp];
38+
lastTick = [timeStampObj longValue];
39+
}
40+
41+
-(BOOL)shouldReport
42+
{
43+
BOOL result = YES;
44+
if(self.count > 0)
45+
{
46+
result = floorf(progress*100/[count floatValue]) > tick;
47+
}
48+
NSTimeInterval timeStamp = [[NSDate date] timeIntervalSince1970];
49+
// NSTimeInterval is defined as double
50+
NSNumber *timeStampObj = [NSNumber numberWithDouble: timeStamp];
51+
return lastTick - [timeStampObj longValue] > [self.interval longValue] && self.enable && result;
52+
53+
}
54+
55+
56+
@end

0 commit comments

Comments
 (0)