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