Commits

José Manuel Sánchez Peñarroja  committed a5526e8

Fixed issues with progress. Changed estimated time to nstimeinterval. Fixed issues with average time and averagestats

  • Participants
  • Parent commits d25f431

Comments (0)

Files changed (13)

File Example/Example.xcworkspace/xcuserdata/josanchez.xcuserdatad/UserInterfaceState.xcuserstate

Binary file modified.

File bmf/ios/subspecs/m13/BMFM13NavigationBarProgressView.m

 			@strongify(self);
 			
 			if (runningNumber.boolValue) {
+				DDLogDebug(@"Fraction completed when starting: %f",self.progress.fractionCompleted);
+				
+				[self.viewController.navigationController setProgress:0 animated:NO];
 				[self.viewController.navigationController showProgress];
 			}
 			else {
 						}
 					}
 				}
-//				else {
-//					if (self.viewController.navigationController.isShowingProgressBar){
-//						[self.viewController.navigationController cancelProgress];	
-//					}
-//				}
 			}
 
 		}];

File bmf/ios/subspecs/m13/BMFM13ProgressView.m

 		[[RACObserve(self, progress.running) deliverOn:[RACScheduler mainThreadScheduler]] subscribeNext:^(NSNumber *runningNumber) {
 			@strongify(self);
 			if (runningNumber.boolValue) {
+				[self.progressView setProgress:self.progress.fractionCompleted animated:NO];
 				self.progressView.alpha = 1;
 			}
 			else {

File bmf/shared/data/BMFProgress.h

 
 @property (nonatomic,readonly) BOOL running;
 
-@property (assign, nonatomic) int64_t estimatedTime; // This allows to compare progress units accross objects
+@property (assign, nonatomic) NSTimeInterval estimatedTime; // This allows to compare progress units accross objects
 
 @property (assign, nonatomic) int64_t totalUnitCount;
 @property (assign, nonatomic) int64_t completedUnitCount;
 
 @property (nonatomic, readonly) BMFMutableWeakArray *children;
 
+/// This key is used to identify the task. The estimated time will be averaged based on it
+@property (nonatomic, strong) NSString *key;
+
+
 /// Clears completed, total unit counts and estimated time
 - (void) clear;
 

File bmf/shared/data/BMFProgress.m

 @property (nonatomic,assign) BOOL running;
 @property (nonatomic, strong) BMFMutableWeakArray *children;
 
-@property (nonatomic, strong) NSString *key;
-
 @end
 
 @implementation BMFProgress {
 		dispatch_sync(serialQueue, ^{
 			self.children = [BMFMutableWeakArray new];
 			
-			
-			_childrenEstimatedTimeSignal = [RACReplaySubject subject];
-			_childrenFractionCompletedSignal = [RACReplaySubject subject];
-			_childrenRunningSignal = [RACReplaySubject subject];
-			_childrenMessageSignal = [RACReplaySubject subject];
+			_childrenEstimatedTimeSignal = [RACSubject subject];
+			_childrenFractionCompletedSignal = [RACSubject subject];
+			_childrenRunningSignal = [RACSubject subject];
+			_childrenMessageSignal = [RACSubject subject];
 			
 			@weakify(self);
 			[[_childrenEstimatedTimeSignal flatten] subscribeNext:^(id x) {
 				@strongify(self);
-				int64_t total = 0;
+				NSTimeInterval total = 0;
 				
 				__block NSArray *children = nil;
 				dispatch_sync(serialQueue, ^{
 					
 					total += childTotal;
 					completed += child.fractionCompleted*childTotal;
-					
-//					total += child.totalUnitCount;
-//					completed += child.completedUnitCount;
 				}
 				
 				if (_totalUnitCount!=total)	self.totalUnitCount = total;
 				});
 				
 				for (BMFProgress *child in children) {
-//					DDLogInfo(@"child: %@ %d",child,child.running);
 					if (child.running) {
 						running = YES;
 						break;
 					}
 				}
 				
-//				DDLogInfo(@"running: %d",running);
 				if (_running!=running) self.running = running;
 			}];
 			
 		result = _totalUnitCount;
 	});
 	
-	return result;
+	// Don't allow a total unit count less than 1
+	return MAX(result,1);
 }
 
 - (void) setTotalUnitCount:(int64_t)totalUnitCount {
 - (CGFloat) fractionCompleted {
 	__block CGFloat result = 0;
 	dispatch_sync(serialQueue, ^{
-		if (_totalUnitCount==0) result = 1;
+		if (_totalUnitCount==0) result = 0;
 		else {
 			result = _completedUnitCount/(CGFloat)_totalUnitCount;	
 		}
 	});
 	
-	return MIN(result,1);
+	// Result should be between 0 and 1
+	return MAX(MIN(result,1),0);
 }
 
 - (void) clear {
 }
 
 - (void) reset {
-	[self clear];
 	[self.children removeAllObjects];
+	[self clear];
 }
 
 - (void) setKey:(NSString *)key {
 		_key = key;
 	});
 	
-	if (key.length>0) self.estimatedTime = [BMFAverageTime averageTime:key];
+	if (key.length>0) {
+		self.estimatedTime = [BMFAverageTime averageTime:key];	
+	}
 }
 
 - (BOOL) running {
 	});
 }
 
-- (int64_t) estimatedTime {
-	__block int64_t result = 0;
+- (NSTimeInterval) estimatedTime {
+	__block NSTimeInterval result = 0;
 	dispatch_sync(serialQueue, ^{
 		result = _estimatedTime;
 	});
-	return result;
+	
+	// Don't allow 0 as estimated time
+	return MAX(result,0.000001);
 }
 
-- (void) setEstimatedTime:(int64_t)estimatedTime {
+- (void) setEstimatedTime:(NSTimeInterval)estimatedTime {
 	dispatch_sync(serialQueue, ^{
 		_estimatedTime = estimatedTime;
 	});
 	});
 }
 
-- (void) update {
+/*- (void) update {
 	if (_children.count==0) return;
 	
 	__block NSArray *children = nil;
 	[self didChangeValueForKey:@"totalUnitCount"];
 	[self didChangeValueForKey:@"fractionCompleted"];
 	[self didChangeValueForKey:@"completedUnitCount"];
-}
+}*/
 
 - (void) start:(NSString *) key {
 	BMFAssertReturn(self.children.count==0);
 - (void) stop: (NSError *) error {
 	BMFAssertReturn(self.children.count==0);
 
-	if (error) [BMFAverageTime cancelTime:self.key];
-	else [BMFAverageTime stopTime:self.key];
-	
-	self.key = nil;
+	if (self.key) {
+		if (error) [BMFAverageTime cancelTime:self.key];
+		else [BMFAverageTime stopTime:self.key];
+		
+		self.key = nil;
+	}
+	else {
+		/// If we are running without a key something is wrong. We should always be started with a key
+		BMFAssertReturn(!self.running);
+	}
 	
 	self.running = NO;
 	self.completedUnitCount = self.totalUnitCount;

File bmf/shared/data/loaders/BMFAFURLSessionLoader.m

 	[self.progress stop:nil];
 }
 
+- (void) setUrl:(NSURL *)url {
+	_url = url;
+	if (_url) [self.progress setKey:_url.absoluteString];
+}
+
 - (void) load:(BMFCompletionBlock) completionBlock {
 	
 	BMFAssertReturn(self.url);
 	BMFAssertReturn(completionBlock);
 	
 	@autoreleasepool {
-//		if (!self.url) {
-//			[NSException raise:BMFLocalized(@"Error loading. Url can't be nil",nil) format:@"%@",self];
-//			return;
-//		}
-		
-//		if (!completionBlock) {
-//			[NSException raise:BMFLocalized(@"Error loading. Completion block can't be nil",nil) format:@"%@",self];
-//			return;
-//		}
 		
 		[_progress start:self.url.absoluteString];
 		

File bmf/shared/data/operations/BMFAsyncOperation.m

 	[self didChangeValueForKey:@"isFinished"];
 	[self didChangeValueForKey:@"isExecuting"];
 	
-	[self.progress start:@"com.bmf.AsyncOperation"];
+	//if (self.progress.children.count==0) [self.progress start:@"com.bmf.AsyncOperation"];
 	
 	[self performStart];
 }
 - (void) cancel {
 	[self performCancel];
 	
-	[self.progress stop:nil];
+	//if (self.progress.children.count==0) [self.progress stop:nil];
 	
 	[self finished];
 	
 	[self didChangeValueForKey:@"isFinished"];
 	[self didChangeValueForKey:@"isExecuting"];
 	
-	[self.progress stop:nil];
+	//[self.progress stop:nil];
 }
 
 - (BOOL) isCancelled {

File bmf/shared/data/parsers/BMFObjectParserProtocol.h

 
 #import <Foundation/Foundation.h>
 
+#import "BMFProgress.h"
+
 @protocol BMFObjectParserDelegateProtocol <NSObject>
 
 @optional

File bmf/shared/data/parsers/BMFParserStrategy.h

 
 #import <Foundation/Foundation.h>
 
-#import <BMF/BMFObjectParserProtocol.h>
+#import "BMFObjectParserProtocol.h"
 
 @interface BMFParserStrategy : NSObject
 

File bmf/shared/data/parsers/BMFParserStrategy.m

 
 #import "BMFParserStrategy.h"
 
-#import <BMF/BMF.h>
+#import "BMF.h"
 
 @implementation BMFParserStrategy
 

File bmf/shared/stats/BMFAverageStats.m

 
 #import "BMFUtils.h"
 
+#import <ReactiveCocoa/ReactiveCocoa.h>
+#import <ReactiveCocoa/RACExtScope.h>
+
 #define BMFAVERAGE_FILE @"com.bmf.stats.average.plist"
 
 static BMFAverageStats *globalInstance = nil;
 		dispatch_sync(_serialQueue, ^{
 			[self load];
 		});
+		
+		@weakify(self);
+		[[[self rac_signalForSelector:@selector(save)] throttle:1] subscribeNext:^(id x) {
+			@strongify(self);
+			dispatch_async(self.serialQueue, ^{
+				@try {
+					NSData *data = [NSKeyedArchiver archivedDataWithRootObject:self.valuesDic];
+					if (data) [data writeToURL:self.fileUrl atomically:YES];
+				}
+				@catch (NSException *exception) {
+					DDLogError(@"Exeption writing to file url: %@ %@",self.fileUrl,exception);
+				}
+			});
+		}];
 	}
 	return self;
 }
 }
 
 - (void) save {
-	@try {
-		NSData *data = [NSKeyedArchiver archivedDataWithRootObject:self.valuesDic];
-		if (data) [data writeToURL:self.fileUrl atomically:YES];
-	}
-	@catch (NSException *exception) {
-		DDLogError(@"Exeption writing to file url: %@ %@",self.fileUrl,exception);
-	}
+	DDLogDebug(@"Saving");
 }
 
 @end

File bmf/shared/stats/BMFAverageTime.m

 		_serialQueue = dispatch_queue_create("com.bmf.AverageTime", DISPATCH_QUEUE_SERIAL);
 		dispatch_sync(_serialQueue, ^{
 			_timesDic = [NSMutableDictionary dictionary];
-			_stats = [BMFAverageStats new];
+			NSURL *url = [NSURL fileURLWithPath:[[BMFUtils applicationCacheDirectory] stringByAppendingPathComponent:@"com.bmf.AverageTime"]];
+			_stats = [[BMFAverageStats alloc] initWithFileUrl:url];
 		});
     }
     return self;
 + (void) startTime:(NSString *) key {
 	BMFAssertReturn(key.length>0);
 	
-	dispatch_sync(averageTime.serialQueue, ^{
+	dispatch_async(averageTime.serialQueue, ^{
 		averageTime.timesDic[key] = [NSDate date];
+//		DDLogDebug(@"Added start date for key: %@ %@",key,averageTime.timesDic[key]);
 	});
 }
 
 + (void) stopTime:(NSString *) key {
 	BMFAssertReturn(key.length>0);
 
-	dispatch_sync(averageTime.serialQueue, ^{
+	dispatch_async(averageTime.serialQueue, ^{
 		NSDate *startDate = averageTime.timesDic[key];
 		BMFAssertReturn(startDate);
 		
+//		DDLogDebug(@"Added stop date for key: %@ %@",key,[NSDate date]);
+		
 		NSTimeInterval time = [[NSDate date] timeIntervalSinceDate:startDate];
+//		DDLogDebug(@"Adding time: %f for key: %@",time,key);
 		
 		[averageTime.timesDic removeObjectForKey:key];
 		
 + (void) cancelTime:(NSString *) key {
 	BMFAssertReturn(key.length>0);
 	
-	dispatch_sync(averageTime.serialQueue, ^{
+	dispatch_async(averageTime.serialQueue, ^{
 		[averageTime.timesDic removeObjectForKey:key];
 	});
 }
 
 + (double) averageTime:(NSString *) key {
-	return [averageTime.stats averageValueForKey:key];
+	double average = [averageTime.stats averageValueForKey:key];
+	DDLogDebug(@"Returning average time: %f for key: %@",average,key);
+	return average;
 }
 
 

File bmf/shared/subspecs/coredata/BMFCompareParserStrategy.m

 
 #import "BMFCompareParserStrategy.h"
 
-#import <BMF/BMF.h>
+#import "BMF.h"
 
 @implementation BMFCompareParserStrategy
 
+
 - (NSArray *) parseDictionaries:(NSArray *) dictionaries localObjects:(NSArray *) localObjects objectParser:(id<BMFObjectParserProtocol>) objectParser {
-	
+		
 	NSMutableArray *results = [NSMutableArray array];
 	
 	localObjects = [localObjects sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
 			
 			serverIndex++;
 			
-			
 			if (!entity) entity = [objectParser newObject];
 			
 			BOOL result = [objectParser updateObject:entity withDictionary:serverEntityDic error:&error];