// // InteractionsModule.m // NewsBlur // // Created by Roy Yang on 7/11/12. // Copyright (c) 2012 NewsBlur. All rights reserved. // #import "InteractionsModule.h" #import "NewsBlurAppDelegate.h" #import "InteractionCell.h" #import #import "ASIHTTPRequest.h" #import "UserProfileViewController.h" #define MINIMUM_INTERACTION_HEIGHT 78 @implementation InteractionsModule @synthesize appDelegate; @synthesize interactionsTable; @synthesize interactionsArray; @synthesize popoverController; @synthesize pageFetching; @synthesize pageFinished; @synthesize interactionsPage; - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { } return self; } - (void)layoutSubviews { [super layoutSubviews]; self.interactionsTable = [[UITableView alloc] init]; self.interactionsTable.dataSource = self; self.interactionsTable.delegate = self; self.interactionsTable.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height); self.interactionsTable.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; [self addSubview:self.interactionsTable]; } - (void)refreshWithInteractions:(NSArray *)interactions { self.interactionsArray = interactions; [self.interactionsTable reloadData]; self.pageFetching = NO; [self performSelector:@selector(checkScroll) withObject:nil afterDelay:0.1]; } - (void)checkScroll { NSInteger currentOffset = self.interactionsTable.contentOffset.y; NSInteger maximumOffset = self.interactionsTable.contentSize.height - self.interactionsTable.frame.size.height; if (maximumOffset - currentOffset <= 60.0) { [self fetchInteractionsDetail:self.interactionsPage + 1]; } } #pragma mark - #pragma mark Get Interactions - (void)fetchInteractionsDetail:(int)page { if (page == 1) { self.pageFetching = NO; self.pageFinished = NO; appDelegate.userInteractionsArray = nil; } if (!self.pageFetching && !self.pageFinished) { self.interactionsPage = page; self.pageFetching = YES; self.appDelegate = (NewsBlurAppDelegate *)[[UIApplication sharedApplication] delegate]; NSString *urlString = [NSString stringWithFormat:@"http://%@/social/interactions?user_id=%@&page=%i&limit=10", NEWSBLUR_URL, [appDelegate.dictUserProfile objectForKey:@"user_id"], page]; NSURL *url = [NSURL URLWithString:urlString]; ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; [request setDidFinishSelector:@selector(finishLoadInteractions:)]; [request setDidFailSelector:@selector(requestFailed:)]; [request setDelegate:self]; [request startAsynchronous]; } } - (void)finishLoadInteractions:(ASIHTTPRequest *)request { self.pageFetching = NO; NSString *responseString = [request responseString]; NSData *responseData=[responseString dataUsingEncoding:NSUTF8StringEncoding]; NSError *error; NSDictionary *results = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error]; NSArray *newInteractions = [results objectForKey:@"interactions"]; // check for last page if (![[results objectForKey:@"has_next_page"] intValue]) { self.pageFinished = YES; } NSMutableArray *confirmedInteractions = [NSMutableArray array]; if ([appDelegate.userInteractionsArray count]) { NSMutableSet *interactionsDates = [NSMutableSet set]; for (id interaction in appDelegate.userInteractionsArray) { [interactionsDates addObject:[interaction objectForKey:@"date"]]; } for (id interaction in newInteractions) { if (![interactionsDates containsObject:[interaction objectForKey:@"date"]]) { [confirmedInteractions addObject:interaction]; } } } else { confirmedInteractions = [newInteractions copy]; } if (self.interactionsPage == 1) { appDelegate.userInteractionsArray = confirmedInteractions; } else { appDelegate.userInteractionsArray = [appDelegate.userInteractionsArray arrayByAddingObjectsFromArray:newInteractions]; } [self refreshWithInteractions:appDelegate.userInteractionsArray]; } - (void)requestFailed:(ASIHTTPRequest *)request { NSError *error = [request error]; NSLog(@"Error: %@", error); } #pragma mark - #pragma mark Table View - Interactions List - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 0; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { int userInteractions = [appDelegate.userInteractionsArray count]; if (indexPath.row >= userInteractions) { return MINIMUM_INTERACTION_HEIGHT; } InteractionCell *interactionCell = [[InteractionCell alloc] init]; int height = [interactionCell setInteraction:[appDelegate.userInteractionsArray objectAtIndex:(indexPath.row)] withWidth:self.frame.size.width] + 30; if (height < MINIMUM_INTERACTION_HEIGHT) { return MINIMUM_INTERACTION_HEIGHT; } else { return height; } } - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { UIView *blank = [[UIView alloc] init]; return blank; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { int userInteractionsCount = [appDelegate.userInteractionsArray count]; return userInteractionsCount + 1; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { InteractionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"InteractionCell"]; if (cell == nil) { cell = [[InteractionCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"InteractionCell"]; } if (indexPath.row >= [appDelegate.userInteractionsArray count]) { // add in loading cell return [self makeLoadingCell]; } else { // update the cell information [cell setInteraction:[appDelegate.userInteractionsArray objectAtIndex:(indexPath.row)] withWidth: self.frame.size.width]; } return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { int userInteractions = [appDelegate.userInteractionsArray count]; if (indexPath.row < userInteractions) { NSDictionary *interaction = [appDelegate.userInteractionsArray objectAtIndex:indexPath.row]; NSString *category = [interaction objectForKey:@"category"]; if ([category isEqualToString:@"follow"]) { NSString *userId = [[interaction objectForKey:@"with_user"] objectForKey:@"user_id"]; appDelegate.activeUserProfileId = userId; [tableView deselectRowAtIndexPath:indexPath animated:YES]; // pass cell to the show UserProfile UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; [appDelegate showUserProfileModal:cell]; } else if ([category isEqualToString:@"comment_reply"] || [category isEqualToString:@"reply_reply"] || [category isEqualToString:@"comment_like"]) { NSString *feedIdStr = [NSString stringWithFormat:@"%@", [interaction objectForKey:@"feed_id"]]; NSString *contentIdStr = [NSString stringWithFormat:@"%@", [interaction objectForKey:@"content_id"]]; [appDelegate loadTryFeedDetailView:feedIdStr withStory:contentIdStr isSocial:YES]; } else if ([category isEqualToString:@"story_reshare"]) { NSString *feedIdStr = [NSString stringWithFormat:@"%@", [[interaction objectForKey:@"with_user"] objectForKey:@"id"]]; NSString *contentIdStr = [NSString stringWithFormat:@"%@", [interaction objectForKey:@"content_id"]]; [appDelegate loadTryFeedDetailView:feedIdStr withStory:contentIdStr isSocial:YES]; } // have the selected cell deselect [tableView deselectRowAtIndexPath:indexPath animated:YES]; } } - (UITableViewCell *)makeLoadingCell { UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"NoReuse"]; cell.selectionStyle = UITableViewCellSelectionStyleNone; if (self.pageFinished) { UIImage *img = [UIImage imageNamed:@"fleuron.png"]; UIImageView *fleuron = [[UIImageView alloc] initWithImage:img]; int height = MINIMUM_INTERACTION_HEIGHT; fleuron.frame = CGRectMake(0, 0, self.frame.size.width, height); fleuron.contentMode = UIViewContentModeCenter; [cell.contentView addSubview:fleuron]; fleuron.backgroundColor = [UIColor whiteColor]; } else { cell.textLabel.text = @"Loading..."; UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; UIImage *spacer = [UIImage imageNamed:@"spacer"]; UIGraphicsBeginImageContext(spinner.frame.size); [spacer drawInRect:CGRectMake(0, 0, spinner.frame.size.width,spinner.frame.size.height)]; UIImage* resizedSpacer = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); cell.imageView.image = resizedSpacer; [cell.imageView addSubview:spinner]; [spinner startAnimating]; } return cell; } - (void)scrollViewDidScroll: (UIScrollView *)scroll { [self checkScroll]; } @end