- ( void ) refreshWithDupes : ( float ) lat : ( float ) lon {

[ paginatedFeed fetchObjectsWithParameters : @ { @ "latitude" : @ ( lat ) , @ "longitude" : @ ( lon ) , @ "accuracy" : @ ( "40" ) , @ "timestamp" : @ ( [ self parsedDate ] )

} successBlock :^ ( NSArray * items, NSArray * refreshedItems ) {

NSLog ( @ "activity Indi after first refresh%@" , activityIndicator ) ;

activityIndicator.hidden = YES ;

[ activityIndicator removeFromSuperview ] ;

activityIndicator = nil ;

// NSLog(@"111111111111 %f %f", lat, lon);

itemsToUpload = [ NSArray arrayWithArray : items ] ;

[ _tileScrollView setTotalPages : [ paginatedFeed totalPages ] ] ;

[ _tileScrollView setCurrentPage : [ paginatedFeed currentPage ] ] ;

// allocate our initial arrays

NSMutableArray * mutableItemsEnqueued = [ [ NSMutableArray alloc ] initWithCapacity : 0 ] ;

NSMutableArray * mutableItemsToRecent = [ [ NSMutableArray alloc ] initWithCapacity : 0 ] ;

NSMutableArray * mutableItemsToComplete = [ [ NSMutableArray alloc ] initWithCapacity : 0 ] ;

NSMutableArray * mutableItemsToCompare = [ [ NSMutableArray alloc ] initWithCapacity : 0 ] ;

NSMutableArray * mutableItemsToUploading = [ [ NSMutableArray alloc ] initWithCapacity : 0 ] ;

// get our items being uploaded initally, add them to a mutable array

[ mutableItemsEnqueued addObjectsFromArray : [ [ [ MUser me ] uploadQueue ] itemsEnqueued ] ] ;

// signify these as being "uploading"

for ( id item in mutableItemsEnqueued ) {

[ mutableItemsToUploading addObject : [ NSMutableArray arrayWithObjects : item, [ NSString stringWithFormat : @ "upload" ] , nil ] ] ;

}

// we need this so we have any recently completed item added to the list (such as upon our first refresh after upload completion.

[ mutableItemsToRecent addObjectsFromArray : [ [ [ MUser me ] uploadQueue ] itemsRecentlyFinished ] ] ;

// NSLog(@"mutableItemsToRecent %@", mutableItemsToRecent);

for ( id item in mutableItemsToRecent ) {

[ mutableItemsToCompare addObject : [ NSMutableArray arrayWithObjects : item, [ NSString stringWithFormat : @ "complete" ] , nil ] ] ;

}

// reverse all these, so the order appears okay

mutableItemsToCompare = [ NSMutableArray arrayWithArray : [ [ mutableItemsToCompare reverseObjectEnumerator ] allObjects ] ] ;

// add our completed feed items from the server

[ mutableItemsToComplete addObjectsFromArray : items ] ;

// and assign them as "Complete"

for ( id item in mutableItemsToComplete ) {

[ mutableItemsToCompare addObject : [ NSMutableArray arrayWithObjects : item, [ NSString stringWithFormat : @ "complete" ] , nil ] ] ;

}

// remove duplicates while preserving ordering - taking care not to try and remove any dupes from our uploads

// NSMutableArray *uniqueItems = [[NSMutableArray alloc]initWithCapacity:0];

// NSMutableArray *uniqueItemsCompare = [[NSMutableArray alloc]initWithCapacity:0];

// for (NSArray *item in mutableItemsToCompare){

// if (![uniqueItems containsObject:[NSString stringWithFormat:@"%@",[item objectAtIndex:0]]]){

// [uniqueItems addObject:[NSString stringWithFormat:@"%@",[item objectAtIndex:0]]];

// [uniqueItemsCompare addObject:item];

// }

//

// }

// combine our uploading items, and the completed with dupes removed

NSMutableArray * combinedItems = [ [ NSMutableArray alloc ] initWithCapacity : 0 ] ;

for ( NSArray * item in mutableItemsToUploading ) {

[ combinedItems addObject : item ] ;

}

// for (NSArray *item in uniqueItemsCompare){

// [combinedItems addObject:item];

// }

for ( NSArray * item in mutableItemsToCompare ) {

[ combinedItems addObject : item ] ;

}

activityIndicator.hidden = YES ;

[ activityIndicator removeFromSuperview ] ;

activityIndicator = nil ;

[ _tileScrollView addItems : combinedItems ] ;

} failureBlock :^ ( NSError * error ) {

activityIndicator.hidden = YES ;

[ activityIndicator removeFromSuperview ] ;

activityIndicator = nil ;

} helperBlock :^ ( NSArray * items, NSDictionary * attributes ) {

for ( MItem * item in [ [ [ MUser me ] uploadQueue ] itemsRecentlyFinished ] ) {

NSString * compare1 = [ NSString stringWithFormat : @ "%@" , item ] ;

// Doing the hack version

// TODO - ask Bogo to take a look at his compareObjects predicate

// Currently failing the comparson to converting to strings.

int i = 0 ;

for ( MItem * itm in items ) {

NSString * compare2 = [ NSString stringWithFormat : @ "%@" , itm ] ;

if ( [ compare2 isEqualToString : compare1 ] ) {

[ [ [ MUser me ] uploadQueue ] purgeRecentlyFinishedItem : item ] ;

}

i ++ ;

}

}

} ] ;

}

- ( void ) refreshWithoutDupes : ( float ) lat : ( float ) lon {

// [self placeActivityMon];

[ paginatedFeed fetchObjectsWithParameters : @ { @ "latitude" : @ ( lat ) , @ "longitude" : @ ( lon ) , @ "accuracy" : @ ( "40" ) , @ "timestamp" : @ ( [ self parsedDate ] )

} successBlock :^ ( NSArray * items, NSArray * refreshedItems ) {

NSLog ( @ "222222222222222 %f %f" , lat, lon ) ;

activityIndicator.hidden = YES ;

[ activityIndicator removeFromSuperview ] ;

activityIndicator = nil ;

[ _tileScrollView setTotalPages : [ paginatedFeed totalPages ] ] ;

[ _tileScrollView setCurrentPage : [ paginatedFeed currentPage ] ] ;

NSMutableArray * mutableItemsToDisplay = [ NSMutableArray array ] ;

[ mutableItemsToDisplay addObjectsFromArray : [ [ [ MUser me ] uploadQueue ] itemsEnqueued ] ] ;

[ mutableItemsToDisplay addObjectsFromArray : items ] ;

[ _tileScrollView addItems : [ NSArray arrayWithArray : mutableItemsToDisplay ] ] ;

} failureBlock :^ ( NSError * error ) {

activityIndicator.hidden = YES ;

[ activityIndicator removeFromSuperview ] ;

activityIndicator = nil ;

} helperBlock : nil ] ;

}

- ( void ) updateLocation {

CLLocation * currentLocation = [ [ MLocationController sharedLocationController ] currentLocation ] ;

CLGeocoder * geoCoder = [ [ CLGeocoder alloc ] init ] ;

[ geoCoder reverseGeocodeLocation : currentLocation completionHandler :^ ( NSArray * placemarks, NSError * error ) {

for ( CLPlacemark * placemark in placemarks ) {

float _lat = currentLocation.coordinate.latitude;

float _long = currentLocation.coordinate.longitude;

// Only pull the view controller in nearby people if the items are visible. Otherwise we could get the animated pull in people, which would call this, then animate again.

if ( self.isViewLoaded && self.view.window ) {

[ [ NSNotificationCenter defaultCenter ] postNotificationName : @ "callItemsPeoplePull" object : self userInfo : nil ] ;

}

// NSLog(@"[MNearbyItemsViewController] Updating Location");

if ( dupeRefresh == true ) {

[ self refreshWithDupes : _lat : _long ] ;

} else if ( dupeRefresh == false ) {

[ self refreshWithoutDupes : _lat : _long ] ;

}

}

return ;

} ] ;