logicaroma
4/8/2011 - 12:01 PM

_InfiniteTableViewController.m

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
	if(_reachedEndOfFeed) return;
	if(!_dynamicFeed) return;
	
	DLog();
	if (!reloading)
	{
		checkForRefresh = YES;  //  only check offset when dragging
	}
} 


- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
	if (reloading) return;
	
	if(scrollView.contentSize.height - scrollView.contentOffset.y < (self.view.bounds.size.height)) {
		if(delegate_ != nil && [delegate_ respondsToSelector:@selector(infiniteTableViewController:requestsNextTitlesForPage:)]) {
			reloading=YES;
			DLog(@"Will request more at page %d", (currentPage_ + 1));
			[delegate_ infiniteTableViewController:self requestsNextTitlesForPage:(currentPage_ + 1)];
			[self startLoadingAnimation];
		}
	}
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView
				  willDecelerate:(BOOL)decelerate
{
	if (reloading) return;
	if(_reachedEndOfFeed) return;
	if(!_dynamicFeed) return;
	DLog(@"Scrollview offset y %f", scrollView.contentOffset.y);
	DLog(@"Scrollview height %f", scrollView.contentSize.height);
	DLog(@"Bounds height %f", self.view.bounds.size.height);
	
	if(scrollView.contentSize.height - scrollView.contentOffset.y < (self.view.bounds.size.height * 2)) {
		if(delegate_ != nil && [delegate_ respondsToSelector:@selector(infiniteTableViewController:requestsNextTitlesForPage:)]) {
			reloading=YES;
			DLog(@"Will request more at page %d", (currentPage_ + 1));
			[delegate_ infiniteTableViewController:self requestsNextTitlesForPage:(currentPage_ + 1)];
			[self startLoadingAnimation];
		}
	}
	
	checkForRefresh = NO;
}


- (void) startLoadingAnimation {
	if(loadingCell!=nil && activityView!=nil) {
		[loadingCell.textLabel setText:@"Loading..."];
		[activityView startAnimating];
		[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(stopLoadingAnimations) name:kConnectivityErrorNotification object:nil];
		id appDelegate = [[UIApplication sharedApplication] delegate];
		if (appDelegate != nil && [appDelegate respondsToSelector:@selector(checkReachability)]) {
			[appDelegate checkReachability];
		}
	}
}

- (void) stopLoadingAnimations {
	if(loadingCell!=nil && activityView!=nil) {
		[activityView stopAnimating];
		[loadingCell.textLabel setText:@"Load more..."];
		reloading = NO;
		[[NSNotificationCenter defaultCenter] removeObserver:self name:kConnectivityErrorNotification object:nil];
	}
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForLoadMoreAtIndexPath:(NSIndexPath *)indexPath {
	static NSString *LoadingCellIdentifier = @"LoadingCell";
	
	//UITableViewCell *cell =	[tableView dequeueReusableCellWithIdentifier:LoadingCellIdentifier];
    if (loadingCell == nil) {
        loadingCell = [[[ClearLabelsCellView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:LoadingCellIdentifier] autorelease];
		loadingCell.backgroundView = [[[GradientView alloc] init] autorelease];
		
		[loadingCell setSelectionStyle:UITableViewCellSelectionStyleBlue];
		[loadingCell.textLabel setTextColor:[UIColor colorWithRed:55.0/255.0 green:90.0/255.0 blue:163.0/255.0 alpha:1.0]]; 
		
		[loadingCell.textLabel setTextAlignment:UITextAlignmentCenter];
		loadingCell.backgroundColor = [UIColor clearColor];
		
		if([titles count] == 0) {
			reloading = YES;
			[loadingCell.textLabel setText:@"Loading..."];
			[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(stopLoadingAnimations) name:kConnectivityErrorNotification object:nil];
			id appDelegate = [[UIApplication sharedApplication] delegate];
			if (appDelegate != nil && [appDelegate respondsToSelector:@selector(checkReachability)]) {
				[appDelegate checkReachability];
			}
		}
		else {
			[loadingCell.textLabel setText:@"Load more..."];
			reloading = NO;
		}
		
		loadingCell.textLabel.shadowColor = [UIColor whiteColor];
		loadingCell.textLabel.shadowOffset = CGSizeMake(1, 1);
		
		
		activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
		activityView.frame = CGRectMake(25.0f, 88.0f - 38.0f, 20.0f, 20.0f);
		activityView.hidesWhenStopped = YES;
		if([titles count] == 0)
			[activityView startAnimating];
		[loadingCell setAccessoryView:activityView];
		[activityView release];
	}
	
	return loadingCell;
}