Sin descripción

RNFetchBlobProgress.m 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. #import "RNFetchBlobProgress.h"
  9. @interface RNFetchBlobProgress ()
  10. {
  11. float progress;
  12. int tick;
  13. double lastTick;
  14. }
  15. @end
  16. @implementation RNFetchBlobProgress
  17. -(id)initWithType:(ProgressType)type interval:(NSNumber *)interval count:(NSNumber *)count
  18. {
  19. self = [super init];
  20. self.count = count;
  21. self.interval = [NSNumber numberWithFloat:[interval floatValue] /1000];
  22. self.type = type;
  23. self.enable = YES;
  24. lastTick = 0;
  25. tick = 1;
  26. return self;
  27. }
  28. -(BOOL)shouldReport:(NSNumber *)nextProgress
  29. {
  30. BOOL * result = YES;
  31. float countF = [self.count floatValue];
  32. if(countF > 0 && [nextProgress floatValue] > 0)
  33. {
  34. result = (int)(floorf([nextProgress floatValue]*countF)) >= tick;
  35. }
  36. NSTimeInterval timeStamp = [[NSDate date] timeIntervalSince1970];
  37. // NSTimeInterval is defined as double
  38. NSNumber *timeStampObj = [NSNumber numberWithDouble: timeStamp];
  39. float delta = [timeStampObj doubleValue] - lastTick;
  40. BOOL * shouldReport = delta > [self.interval doubleValue] && self.enable && result;
  41. if(shouldReport)
  42. {
  43. tick++;
  44. lastTick = [timeStampObj doubleValue];
  45. }
  46. return shouldReport;
  47. }
  48. @end