NewsBlur/media/ios/Classes/ActivityModule.m

150 lines
5.9 KiB
Mathematica
Raw Normal View History

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"
#import "ActivityCell.h"
2012-07-11 18:08:07 -07:00
#import "NewsBlurAppDelegate.h"
#import "UserProfileViewController.h"
2012-07-11 20:19:42 -07:00
#import <QuartzCore/QuartzCore.h>
2012-07-11 18:08:07 -07:00
@implementation ActivityModule
@synthesize appDelegate;
@synthesize activitiesTable;
@synthesize activitiesArray;
2012-07-11 20:19:42 -07:00
@synthesize activitiesUsername;
@synthesize popoverController;
2012-07-11 18:08:07 -07:00
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
}
2012-07-11 20:19:42 -07:00
- (void)refreshWithActivities:(NSDictionary *)activitiesDict {
2012-07-11 18:08:07 -07:00
self.appDelegate = (NewsBlurAppDelegate *)[[UIApplication sharedApplication] delegate];
2012-07-11 20:19:42 -07:00
self.activitiesArray = [activitiesDict objectForKey:@"activities"];
self.activitiesUsername = [activitiesDict objectForKey:@"username"];
2012-07-11 18:08:07 -07:00
if (!self.activitiesUsername) {
self.activitiesUsername = [[activitiesDict objectForKey:@"user_profile"] objectForKey:@"username"];
}
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;
self.activitiesTable.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
// self.activitiesTable.layer.cornerRadius = 10;
2012-07-12 10:59:17 -07:00
self.activitiesTable.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
2012-07-11 18:08:07 -07:00
[self addSubview:self.activitiesTable];
[self.activitiesTable reloadData];
}
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
{
int activitesCount = [self.activitiesArray count];
if (activitesCount) {
return activitesCount;
} else {
return 0;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
2012-07-15 15:06:06 -07:00
ActivityCell *activityCell = [[ActivityCell alloc] init];
int height = [activityCell refreshActivity:[self.activitiesArray objectAtIndex:(indexPath.row)]
withUsername:self.activitiesUsername
withWidth:self.frame.size.width] + 20;
return height;
}
2012-07-11 18:08:07 -07:00
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
2012-07-15 15:06:06 -07:00
cell = [[UITableViewCell alloc]
2012-07-11 18:08:07 -07:00
initWithStyle:UITableViewCellStyleDefault
2012-07-15 15:06:06 -07:00
reuseIdentifier:CellIdentifier];
} else {
[[[cell contentView] subviews] makeObjectsPerformSelector: @selector(removeFromSuperview)];
2012-07-11 18:08:07 -07:00
}
int activitesCount = [self.activitiesArray count];
if (activitesCount >= (indexPath.row + 1)) {
ActivityCell *activityCell = [[ActivityCell alloc] init];
[activityCell refreshActivity:[self.activitiesArray objectAtIndex:(indexPath.row)]
withUsername:self.activitiesUsername
withWidth:self.frame.size.width];
[cell.contentView addSubview:activityCell];
}
2012-07-11 18:08:07 -07:00
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
int activitiesCount = [self.activitiesArray count];
if (indexPath.row < activitiesCount) {
NSDictionary *activity = [self.activitiesArray objectAtIndex:indexPath.row];
NSString *category = [activity objectForKey:@"category"];
if ([category isEqualToString:@"follow"]) {
NSString *userId = [[activity objectForKey:@"with_user"] objectForKey:@"user_id"];
appDelegate.activeUserProfileId = userId;
[tableView deselectRowAtIndexPath:indexPath animated:YES];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
self.popoverController = [[UIPopoverController alloc] initWithContentViewController:appDelegate.userProfileViewController];
[self.popoverController setPopoverContentSize:CGSizeMake(320, 416)];
[self.popoverController presentPopoverFromRect:cell.bounds
inView:cell
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
} 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"]];
[appDelegate loadTryFeedDetailView:feedIdStr withStory:contentIdStr isSocial:YES];
} else if ([category isEqualToString:@"sharedstory"]) {
NSString *feedIdStr = [NSString stringWithFormat:@"%@", [[activity objectForKey:@"with_user"] objectForKey:@"id"]];
NSString *contentIdStr = [NSString stringWithFormat:@"%@", [activity objectForKey:@"content_id"]];
[appDelegate loadTryFeedDetailView:feedIdStr withStory:contentIdStr isSocial:YES];
} else if ([category isEqualToString:@"feedsub"]) {
NSString *feedIdStr = [NSString stringWithFormat:@"%@", [activity objectForKey:@"feed_id"]];
NSString *contentIdStr = nil;
[appDelegate loadTryFeedDetailView:feedIdStr withStory:contentIdStr isSocial:NO];
}
// have the selected cell deselect
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
}
2012-07-11 18:08:07 -07:00
@end