2012-07-11 18:08:07 -07:00
|
|
|
//
|
|
|
|
// ActivityModule.m
|
|
|
|
// NewsBlur
|
|
|
|
//
|
|
|
|
// Created by Roy Yang on 7/11/12.
|
|
|
|
// Copyright (c) 2012 NewsBlur. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "ActivityModule.h"
|
2012-07-13 21:31:48 -07:00
|
|
|
#import "ActivityCell.h"
|
2012-07-11 18:08:07 -07:00
|
|
|
#import "NewsBlurAppDelegate.h"
|
2012-07-22 14:23:50 -07:00
|
|
|
#import "UserProfileViewController.h"
|
2012-07-11 20:19:42 -07:00
|
|
|
#import <QuartzCore/QuartzCore.h>
|
2012-07-22 09:12:02 -07:00
|
|
|
#import "ASIHTTPRequest.h"
|
2012-07-28 23:31:12 -07:00
|
|
|
#import "ActivityCell.h"
|
2012-07-11 18:08:07 -07:00
|
|
|
|
|
|
|
@implementation ActivityModule
|
|
|
|
|
|
|
|
@synthesize appDelegate;
|
|
|
|
@synthesize activitiesTable;
|
2012-07-16 22:35:28 -07:00
|
|
|
@synthesize popoverController;
|
2012-07-22 09:12:02 -07:00
|
|
|
@synthesize pageFetching;
|
|
|
|
@synthesize pageFinished;
|
|
|
|
@synthesize activitiesPage;
|
2012-07-11 18:08:07 -07:00
|
|
|
|
2012-07-26 23:07:47 -07:00
|
|
|
#define MINIMUM_ACTIVITY_HEIGHT 48 + 30
|
2012-07-21 18:25:56 -07:00
|
|
|
|
2012-07-11 18:08:07 -07:00
|
|
|
- (id)initWithFrame:(CGRect)frame
|
|
|
|
{
|
|
|
|
self = [super initWithFrame:frame];
|
|
|
|
if (self) {
|
2012-07-21 18:25:56 -07:00
|
|
|
// initialize code here
|
2012-07-11 18:08:07 -07:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2012-07-22 09:12:02 -07:00
|
|
|
- (void)layoutSubviews {
|
|
|
|
[super layoutSubviews];
|
2012-07-15 15:06:06 -07:00
|
|
|
self.activitiesTable = [[UITableView alloc] init];
|
2012-07-11 18:08:07 -07:00
|
|
|
self.activitiesTable.dataSource = self;
|
|
|
|
self.activitiesTable.delegate = self;
|
2012-07-21 18:25:56 -07:00
|
|
|
self.activitiesTable.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);;
|
2012-07-12 10:59:17 -07:00
|
|
|
self.activitiesTable.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
|
2012-07-11 18:08:07 -07:00
|
|
|
|
2012-07-22 09:12:02 -07:00
|
|
|
[self addSubview:self.activitiesTable];
|
|
|
|
}
|
|
|
|
|
2012-07-28 23:31:12 -07:00
|
|
|
- (void)refreshWithActivities:(NSArray *)activities {
|
2012-07-22 09:12:02 -07:00
|
|
|
self.appDelegate = (NewsBlurAppDelegate *)[[UIApplication sharedApplication] delegate];
|
2012-08-14 17:20:45 -07:00
|
|
|
appDelegate.userActivitiesArray = activities;
|
2012-07-22 09:12:02 -07:00
|
|
|
|
2012-07-11 18:08:07 -07:00
|
|
|
[self.activitiesTable reloadData];
|
2012-07-22 09:12:02 -07:00
|
|
|
|
|
|
|
self.pageFetching = NO;
|
|
|
|
|
|
|
|
[self performSelector:@selector(checkScroll)
|
|
|
|
withObject:nil
|
|
|
|
afterDelay:0.1];
|
2012-07-11 18:08:07 -07:00
|
|
|
}
|
|
|
|
|
2012-07-22 09:12:02 -07:00
|
|
|
- (void)checkScroll {
|
|
|
|
NSInteger currentOffset = self.activitiesTable.contentOffset.y;
|
|
|
|
NSInteger maximumOffset = self.activitiesTable.contentSize.height - self.activitiesTable.frame.size.height;
|
|
|
|
|
|
|
|
if (maximumOffset - currentOffset <= 60.0) {
|
2012-07-26 23:07:47 -07:00
|
|
|
[self fetchActivitiesDetail:self.activitiesPage + 1];
|
2012-07-22 09:12:02 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
#pragma mark Get Interactions
|
|
|
|
|
2012-07-26 23:07:47 -07:00
|
|
|
- (void)fetchActivitiesDetail:(int)page {
|
2012-08-13 17:07:26 -07:00
|
|
|
|
|
|
|
// if there is no social profile, we are DONE
|
|
|
|
// if ([[appDelegate.dictUserProfile allKeys] count] == 0) {
|
|
|
|
// self.pageFinished = YES;
|
|
|
|
// [self.activitiesTable reloadData];
|
|
|
|
// return;
|
|
|
|
// } else {
|
|
|
|
// if (page == 1) {
|
|
|
|
// self.pageFinished = NO;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
2012-07-22 09:12:02 -07:00
|
|
|
if (page == 1) {
|
|
|
|
self.pageFetching = NO;
|
|
|
|
self.pageFinished = NO;
|
2012-07-26 23:07:47 -07:00
|
|
|
appDelegate.userActivitiesArray = nil;
|
2012-07-22 09:12:02 -07:00
|
|
|
}
|
|
|
|
if (!self.pageFetching && !self.pageFinished) {
|
|
|
|
self.activitiesPage = page;
|
|
|
|
self.pageFetching = YES;
|
|
|
|
self.appDelegate = (NewsBlurAppDelegate *)[[UIApplication sharedApplication] delegate];
|
2012-08-13 22:48:50 -07:00
|
|
|
NSString *urlString = [NSString stringWithFormat:@
|
|
|
|
"http://%@/social/activities?user_id=%@&page=%i&limit=10"
|
2012-08-15 19:31:34 -07:00
|
|
|
"&category=signup&category=star&category=feedsub&category=follow&category=comment_reply&category=comment_like&category=sharedstory",
|
2012-07-22 09:12:02 -07:00
|
|
|
NEWSBLUR_URL,
|
|
|
|
[appDelegate.dictUserProfile objectForKey:@"user_id"],
|
|
|
|
page];
|
|
|
|
|
|
|
|
NSURL *url = [NSURL URLWithString:urlString];
|
|
|
|
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
|
|
|
|
|
2012-07-26 23:07:47 -07:00
|
|
|
[request setDidFinishSelector:@selector(finishLoadActivities:)];
|
2012-07-22 09:12:02 -07:00
|
|
|
[request setDidFailSelector:@selector(requestFailed:)];
|
|
|
|
[request setDelegate:self];
|
|
|
|
[request startAsynchronous];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)finishLoadActivities:(ASIHTTPRequest *)request {
|
|
|
|
self.pageFetching = NO;
|
|
|
|
NSString *responseString = [request responseString];
|
2012-07-27 16:21:44 -07:00
|
|
|
NSData *responseData = [responseString dataUsingEncoding:NSUTF8StringEncoding];
|
|
|
|
NSError *error;
|
|
|
|
NSDictionary *results = [NSJSONSerialization
|
|
|
|
JSONObjectWithData:responseData
|
|
|
|
options:kNilOptions
|
|
|
|
error:&error];
|
2012-07-22 09:12:02 -07:00
|
|
|
|
2012-07-28 11:02:54 -07:00
|
|
|
// check for last page
|
|
|
|
if (![[results objectForKey:@"has_next_page"] intValue]) {
|
|
|
|
self.pageFinished = YES;
|
|
|
|
}
|
|
|
|
|
2012-07-26 23:07:47 -07:00
|
|
|
NSArray *newActivities = [results objectForKey:@"activities"];
|
2012-07-22 09:12:02 -07:00
|
|
|
NSMutableArray *confirmedActivities = [NSMutableArray array];
|
2012-07-26 23:07:47 -07:00
|
|
|
if ([appDelegate.userActivitiesArray count]) {
|
2012-07-22 09:12:02 -07:00
|
|
|
NSMutableSet *activitiesDate = [NSMutableSet set];
|
2012-07-26 23:07:47 -07:00
|
|
|
for (id activity in appDelegate.userActivitiesArray) {
|
2012-07-22 09:12:02 -07:00
|
|
|
[activitiesDate addObject:[activity objectForKey:@"date"]];
|
|
|
|
}
|
|
|
|
for (id activity in newActivities) {
|
|
|
|
if (![activitiesDate containsObject:[activity objectForKey:@"date"]]) {
|
|
|
|
[confirmedActivities addObject:activity];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
confirmedActivities = [newActivities copy];
|
|
|
|
}
|
|
|
|
|
2012-07-26 23:07:47 -07:00
|
|
|
if (self.activitiesPage == 1) {
|
|
|
|
appDelegate.userActivitiesArray = confirmedActivities;
|
|
|
|
} else {
|
|
|
|
appDelegate.userActivitiesArray = [appDelegate.userActivitiesArray arrayByAddingObjectsFromArray:newActivities];
|
|
|
|
}
|
|
|
|
|
2012-07-28 23:31:12 -07:00
|
|
|
[self refreshWithActivities:appDelegate.userActivitiesArray];
|
2012-07-22 09:12:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)requestFailed:(ASIHTTPRequest *)request {
|
|
|
|
NSError *error = [request error];
|
|
|
|
NSLog(@"Error: %@", error);
|
|
|
|
}
|
2012-07-12 00:10:42 -07:00
|
|
|
|
2012-07-11 18:08:07 -07:00
|
|
|
#pragma mark -
|
|
|
|
#pragma mark Table View - Interactions List
|
|
|
|
|
|
|
|
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
|
|
|
|
{
|
2012-08-14 17:20:45 -07:00
|
|
|
int activitesCount = [appDelegate.userActivitiesArray count];
|
2012-07-26 23:07:47 -07:00
|
|
|
return activitesCount + 1;
|
2012-07-11 18:08:07 -07:00
|
|
|
}
|
|
|
|
|
2012-07-21 18:25:56 -07:00
|
|
|
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
|
2012-08-14 17:20:45 -07:00
|
|
|
int activitiesCount = [appDelegate.userActivitiesArray count];
|
2012-07-21 18:25:56 -07:00
|
|
|
if (indexPath.row >= activitiesCount) {
|
2012-07-26 23:07:47 -07:00
|
|
|
return MINIMUM_ACTIVITY_HEIGHT;
|
2012-07-21 18:25:56 -07:00
|
|
|
}
|
|
|
|
|
2012-07-15 15:06:06 -07:00
|
|
|
ActivityCell *activityCell = [[ActivityCell alloc] init];
|
2012-07-28 23:31:12 -07:00
|
|
|
|
|
|
|
NSMutableDictionary *userProfile = [appDelegate.dictUserProfile mutableCopy];
|
|
|
|
[userProfile setValue:@"You" forKey:@"username"];
|
|
|
|
|
2012-08-14 17:20:45 -07:00
|
|
|
int height = [activityCell setActivity:[appDelegate.userActivitiesArray
|
2012-07-28 23:31:12 -07:00
|
|
|
objectAtIndex:(indexPath.row)]
|
|
|
|
withUserProfile:userProfile
|
2012-08-09 16:34:59 -07:00
|
|
|
withWidth:self.frame.size.width - 20] + 30;
|
2012-07-22 09:12:02 -07:00
|
|
|
|
|
|
|
return height;
|
2012-07-26 23:07:47 -07:00
|
|
|
|
2012-07-13 21:31:48 -07:00
|
|
|
}
|
|
|
|
|
2012-07-11 18:08:07 -07:00
|
|
|
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
|
2012-07-21 18:25:56 -07:00
|
|
|
ActivityCell *cell = [tableView
|
|
|
|
dequeueReusableCellWithIdentifier:@"ActivityCell"];
|
2012-07-11 18:08:07 -07:00
|
|
|
if (cell == nil) {
|
2012-07-21 18:25:56 -07:00
|
|
|
cell = [[ActivityCell alloc]
|
2012-07-11 18:08:07 -07:00
|
|
|
initWithStyle:UITableViewCellStyleDefault
|
2012-07-21 18:25:56 -07:00
|
|
|
reuseIdentifier:@"ActivityCell"];
|
|
|
|
}
|
2012-07-11 18:08:07 -07:00
|
|
|
|
2012-07-26 23:07:47 -07:00
|
|
|
if (indexPath.row >= [appDelegate.userActivitiesArray count]) {
|
|
|
|
// add in loading cell
|
|
|
|
return [self makeLoadingCell];
|
|
|
|
} else {
|
2012-07-21 18:25:56 -07:00
|
|
|
|
2012-07-28 23:31:12 -07:00
|
|
|
NSMutableDictionary *userProfile = [appDelegate.dictUserProfile mutableCopy];
|
|
|
|
[userProfile setValue:@"You" forKey:@"username"];
|
|
|
|
|
2012-08-14 17:20:45 -07:00
|
|
|
NSDictionary *activitiy = [appDelegate.userActivitiesArray
|
2012-08-09 10:18:15 -07:00
|
|
|
objectAtIndex:(indexPath.row)];
|
|
|
|
NSString *category = [activitiy objectForKey:@"category"];
|
2012-08-13 22:48:50 -07:00
|
|
|
if ([category isEqualToString:@"follow"]) {
|
2012-08-09 10:18:15 -07:00
|
|
|
cell.accessoryType = UITableViewCellAccessoryNone;
|
2012-08-15 19:31:34 -07:00
|
|
|
} else if ([category isEqualToString:@"star"] ||
|
|
|
|
[category isEqualToString:@"signup"]){
|
2012-08-13 22:48:50 -07:00
|
|
|
cell.accessoryType = UITableViewCellAccessoryNone;
|
|
|
|
cell.selectionStyle = UITableViewCellSelectionStyleNone;
|
|
|
|
} else {
|
|
|
|
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
|
2012-08-09 10:18:15 -07:00
|
|
|
}
|
2012-08-13 22:48:50 -07:00
|
|
|
|
2012-08-10 09:45:44 -07:00
|
|
|
UIView *myBackView = [[UIView alloc] initWithFrame:self.frame];
|
|
|
|
myBackView.backgroundColor = UIColorFromRGB(NEWSBLUR_HIGHLIGHT_COLOR);
|
|
|
|
cell.selectedBackgroundView = myBackView;
|
2012-08-09 10:18:15 -07:00
|
|
|
|
2012-07-26 23:07:47 -07:00
|
|
|
// update the cell information
|
2012-08-09 10:18:15 -07:00
|
|
|
[cell setActivity: activitiy
|
2012-07-28 23:31:12 -07:00
|
|
|
withUserProfile:userProfile
|
2012-08-09 16:34:59 -07:00
|
|
|
withWidth:self.frame.size.width - 20];
|
2012-07-26 23:07:47 -07:00
|
|
|
}
|
2012-07-11 18:08:07 -07:00
|
|
|
return cell;
|
|
|
|
}
|
|
|
|
|
2012-07-16 22:35:28 -07:00
|
|
|
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
|
2012-08-14 17:20:45 -07:00
|
|
|
int activitiesCount = [appDelegate.userActivitiesArray count];
|
2012-07-16 22:35:28 -07:00
|
|
|
if (indexPath.row < activitiesCount) {
|
2012-08-14 17:20:45 -07:00
|
|
|
NSDictionary *activity = [appDelegate.userActivitiesArray objectAtIndex:indexPath.row];
|
2012-07-16 22:35:28 -07:00
|
|
|
NSString *category = [activity objectForKey:@"category"];
|
|
|
|
if ([category isEqualToString:@"follow"]) {
|
2012-07-28 15:37:03 -07:00
|
|
|
[tableView deselectRowAtIndexPath:indexPath animated:YES];
|
|
|
|
|
|
|
|
NSString *userId = [NSString stringWithFormat:@"%@", [[activity objectForKey:@"with_user"] objectForKey:@"user_id"]];
|
2012-07-16 22:35:28 -07:00
|
|
|
appDelegate.activeUserProfileId = userId;
|
2012-07-28 14:15:20 -07:00
|
|
|
|
2012-07-28 15:37:03 -07:00
|
|
|
NSString *username = [NSString stringWithFormat:@"%@", [[activity objectForKey:@"with_user"] objectForKey:@"username"]];
|
2012-07-28 14:15:20 -07:00
|
|
|
appDelegate.activeUserProfileName = username;
|
|
|
|
|
2012-07-28 15:37:03 -07:00
|
|
|
// pass cell to the show UserProfile
|
2012-07-28 23:31:12 -07:00
|
|
|
ActivityCell *cell = (ActivityCell *)[tableView cellForRowAtIndexPath:indexPath];
|
2012-07-28 15:37:03 -07:00
|
|
|
[appDelegate showUserProfileModal:cell];
|
2012-07-16 22:35:28 -07:00
|
|
|
} else if ([category isEqualToString:@"comment_reply"] ||
|
|
|
|
[category isEqualToString:@"comment_like"]) {
|
|
|
|
NSString *feedIdStr = [NSString stringWithFormat:@"%@", [[activity objectForKey:@"with_user"] objectForKey:@"id"]];
|
|
|
|
NSString *contentIdStr = [NSString stringWithFormat:@"%@", [activity objectForKey:@"content_id"]];
|
2012-08-12 21:46:47 -07:00
|
|
|
[appDelegate loadTryFeedDetailView:feedIdStr withStory:contentIdStr isSocial:YES withUser:[activity objectForKey:@"with_user"] showFindingStory:YES];
|
2012-08-15 19:31:34 -07:00
|
|
|
appDelegate.tryFeedCategory = category;
|
2012-07-16 22:35:28 -07:00
|
|
|
} else if ([category isEqualToString:@"sharedstory"]) {
|
2012-07-29 20:55:11 -07:00
|
|
|
NSString *feedIdStr = [NSString stringWithFormat:@"%@", [appDelegate.dictUserProfile objectForKey:@"id"]];
|
2012-07-16 22:35:28 -07:00
|
|
|
NSString *contentIdStr = [NSString stringWithFormat:@"%@", [activity objectForKey:@"content_id"]];
|
2012-08-12 21:46:47 -07:00
|
|
|
[appDelegate loadTryFeedDetailView:feedIdStr withStory:contentIdStr isSocial:YES withUser:[activity objectForKey:@"with_user"] showFindingStory:YES];
|
2012-08-15 19:31:34 -07:00
|
|
|
appDelegate.tryFeedCategory = category;
|
|
|
|
|
2012-07-16 22:35:28 -07:00
|
|
|
} else if ([category isEqualToString:@"feedsub"]) {
|
|
|
|
NSString *feedIdStr = [NSString stringWithFormat:@"%@", [activity objectForKey:@"feed_id"]];
|
|
|
|
NSString *contentIdStr = nil;
|
2012-08-12 21:46:47 -07:00
|
|
|
[appDelegate loadTryFeedDetailView:feedIdStr withStory:contentIdStr isSocial:NO withUser:[activity objectForKey:@"with_user"] showFindingStory:NO];
|
2012-08-15 19:31:34 -07:00
|
|
|
appDelegate.tryFeedCategory = category;
|
|
|
|
|
2012-08-13 22:48:50 -07:00
|
|
|
}
|
|
|
|
|
2012-07-16 22:35:28 -07:00
|
|
|
// have the selected cell deselect
|
|
|
|
[tableView deselectRowAtIndexPath:indexPath animated:YES];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-26 23:07:47 -07:00
|
|
|
- (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_ACTIVITY_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];
|
|
|
|
}
|
|
|
|
|
2012-07-11 18:08:07 -07:00
|
|
|
@end
|