NewsBlur/clients/ios/Classes/InteractionsModule.m

312 lines
12 KiB
Mathematica
Raw Normal View History

2012-07-11 20:19:42 -07:00
//
// 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 "SmallInteractionCell.h"
2012-07-22 09:12:02 -07:00
#import <QuartzCore/QuartzCore.h>
2012-07-22 14:23:50 -07:00
#import "UserProfileViewController.h"
#import "DashboardViewController.h"
2012-07-11 20:19:42 -07:00
#define MINIMUM_INTERACTION_HEIGHT_IPAD 78
#define MINIMUM_INTERACTION_HEIGHT_IPHONE 54
2012-07-11 20:19:42 -07:00
@implementation InteractionsModule
@synthesize interactionsTable;
@synthesize interactionsArray;
@synthesize pageFetching;
@synthesize pageFinished;
@synthesize interactionsPage;
2012-07-11 20:19:42 -07:00
2017-05-04 19:46:03 -07:00
- (void)awakeFromNib {
[super awakeFromNib];
appDelegate = (NewsBlurAppDelegate *)[[UIApplication sharedApplication] delegate];
2012-07-11 20:19:42 -07:00
}
- (void)layoutSubviews {
[super layoutSubviews];
2012-07-15 15:06:06 -07:00
self.interactionsTable = [[UITableView alloc] init];
2012-07-11 20:19:42 -07:00
self.interactionsTable.dataSource = self;
self.interactionsTable.delegate = self;
self.interactionsTable.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
2012-07-12 10:59:17 -07:00
self.interactionsTable.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
2015-12-07 16:09:49 -08:00
self.interactionsTable.backgroundColor = UIColorFromRGB(NEWSBLUR_WHITE_COLOR);
2012-07-11 20:19:42 -07:00
[self addSubview:self.interactionsTable];
}
- (void)refreshWithInteractions:(NSArray *)interactions {
self.interactionsArray = interactions;
2012-07-11 20:19:42 -07:00
[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];
}
2012-07-11 20:19:42 -07:00
}
#pragma mark -
#pragma mark Get Interactions
- (void)fetchInteractionsDetail:(int)page {
2012-08-13 17:07:26 -07:00
// if there is no social profile, we are DONE
// if ([[appDelegate.dictSocialProfile allKeys] count] == 0) {
2012-08-13 17:07:26 -07:00
// self.pageFinished = YES;
// [self.interactionsTable reloadData];
// return;
// } else {
// if (page == 1) {
// self.pageFinished = NO;
// }
// }
if (page == 1) {
self.pageFetching = NO;
self.pageFinished = NO;
2012-07-26 23:07:47 -07:00
appDelegate.userInteractionsArray = nil;
}
2012-08-06 15:46:05 -07:00
if (!self.pageFetching && !self.pageFinished) {
self.interactionsPage = page;
self.pageFetching = YES;
2012-08-06 15:46:05 -07:00
2012-08-13 22:48:50 -07:00
NSString *urlString = [NSString stringWithFormat:@
"%@/social/interactions?user_id=%@&page=%i&limit=10"
2012-08-13 22:53:00 -07:00
"&category=follow&category=comment_reply&category=comment_like&category=reply_reply&category=story_reshare",
2017-05-04 19:46:03 -07:00
appDelegate.url,
[appDelegate.dictSocialProfile objectForKey:@"user_id"],
page];
[appDelegate GET:urlString parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
2017-03-19 18:15:36 -07:00
[self finishLoadInteractions:responseObject];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
2020-08-27 15:08:46 -07:00
[self->appDelegate informError:error];
}];
}
}
2017-03-19 18:15:36 -07:00
- (void)finishLoadInteractions:(NSDictionary *)results {
self.pageFetching = NO;
NSArray *newInteractions = [results objectForKey:@"interactions"];
// check for last page
if (![[results objectForKey:@"has_next_page"] intValue]) {
self.pageFinished = YES;
}
NSMutableArray *confirmedInteractions = [NSMutableArray array];
2020-08-27 15:08:46 -07:00
if ([self->appDelegate.userInteractionsArray count]) {
NSMutableSet *interactionsDates = [NSMutableSet set];
2012-07-26 23:07:47 -07:00
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) {
2012-07-26 23:07:47 -07:00
appDelegate.userInteractionsArray = confirmedInteractions;
} else {
2012-07-26 23:07:47 -07:00
appDelegate.userInteractionsArray = [appDelegate.userInteractionsArray arrayByAddingObjectsFromArray:newInteractions];
}
2012-07-26 23:07:47 -07:00
[self refreshWithInteractions:appDelegate.userInteractionsArray];
}
2012-07-11 20:19:42 -07:00
#pragma mark -
#pragma mark Table View - Interactions List
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
2012-07-11 20:19:42 -07:00
return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
2012-07-21 18:25:56 -07:00
return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSInteger userInteractions = [appDelegate.userInteractionsArray count];
int minimumHeight;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
minimumHeight = MINIMUM_INTERACTION_HEIGHT_IPAD;
} else {
minimumHeight = MINIMUM_INTERACTION_HEIGHT_IPHONE;
}
if (indexPath.row >= userInteractions) {
return minimumHeight;
}
InteractionCell *interactionCell;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
interactionCell = [[InteractionCell alloc] init];
} else {
interactionCell = [[SmallInteractionCell alloc] init];
}
int height = [interactionCell setInteraction:[appDelegate.userInteractionsArray objectAtIndex:(indexPath.row)] withWidth:self.frame.size.width - 20];
2012-07-18 17:23:57 -07:00
return height;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *blank = [[UIView alloc] init];
return blank;
}
2012-07-11 20:19:42 -07:00
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSInteger userInteractionsCount = [appDelegate.userInteractionsArray count];
return userInteractionsCount + 1;
2012-07-11 20:19:42 -07:00
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
InteractionCell *cell = [tableView
dequeueReusableCellWithIdentifier:@"InteractionCell"];
2012-07-11 20:19:42 -07:00
if (cell == nil) {
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
cell = [[InteractionCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"InteractionCell"];
} else {
cell = [[SmallInteractionCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"InteractionCell"];
}
2012-07-11 20:19:42 -07:00
}
2012-07-26 23:07:47 -07:00
if (indexPath.row >= [appDelegate.userInteractionsArray count]) {
// add in loading cell
return [self makeLoadingCell];
} else {
2012-08-09 10:18:15 -07:00
NSDictionary *interaction = [appDelegate.userInteractionsArray objectAtIndex:(indexPath.row)];
NSString *category = [interaction objectForKey:@"category"];
if (![category isEqualToString:@"follow"]) {
cell.accessoryType= UITableViewCellAccessoryDisclosureIndicator;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
2012-08-09 16:34:59 -07:00
2015-12-07 16:09:49 -08:00
cell.backgroundColor = UIColorFromRGB(NEWSBLUR_WHITE_COLOR);
// update the cell information
2012-08-09 16:34:59 -07:00
[cell setInteraction:interaction withWidth: self.frame.size.width - 20];
[cell layoutSubviews];
}
2012-07-11 20:19:42 -07:00
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSInteger userInteractions = [appDelegate.userInteractionsArray count];
if (indexPath.row < userInteractions) {
2012-07-26 23:07:47 -07:00
NSDictionary *interaction = [appDelegate.userInteractionsArray objectAtIndex:indexPath.row];
NSString *category = [interaction objectForKey:@"category"];
if ([category isEqualToString:@"follow"]) {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSString *userId = [NSString stringWithFormat:@"%@", [[interaction objectForKey:@"with_user"] objectForKey:@"user_id"]];
appDelegate.activeUserProfileId = userId;
NSString *username = [NSString stringWithFormat:@"%@", [[interaction objectForKey:@"with_user"] objectForKey:@"username"]];
appDelegate.activeUserProfileName = username;
// 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
withUser:[interaction objectForKey:@"with_user"]
showFindingStory:YES];
appDelegate.tryFeedCategory = category;
2012-07-26 23:07:47 -07:00
} else if ([category isEqualToString:@"story_reshare"]) {
NSString *feedIdStr = [NSString stringWithFormat:@"%@", [[interaction objectForKey:@"with_user"] objectForKey:@"id"]];
NSString *contentIdStr = [NSString stringWithFormat:@"%@", [interaction objectForKey:@"content_id"]];
2012-11-09 14:13:44 -08:00
[appDelegate loadTryFeedDetailView:feedIdStr
withStory:contentIdStr
isSocial:YES
withUser:[interaction objectForKey:@"with_user"]
showFindingStory:YES];
appDelegate.tryFeedCategory = category;
}
2012-07-26 23:07:47 -07:00
// have the selected cell deselect
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
}
- (UITableViewCell *)makeLoadingCell {
UITableViewCell *cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:@"NoReuse"];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
2015-12-07 16:09:49 -08:00
cell.backgroundColor = UIColorFromRGB(NEWSBLUR_WHITE_COLOR);
if (self.pageFinished) {
UIImage *img = [UIImage imageNamed:@"fleuron.png"];
UIImageView *fleuron = [[UIImageView alloc] initWithImage:img];
int height;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
height = MINIMUM_INTERACTION_HEIGHT_IPAD;
} else {
height = MINIMUM_INTERACTION_HEIGHT_IPHONE;
}
fleuron.frame = CGRectMake(0, 0, self.frame.size.width, height);
fleuron.contentMode = UIViewContentModeCenter;
[cell.contentView addSubview:fleuron];
2015-12-07 16:09:49 -08:00
fleuron.backgroundColor = UIColorFromRGB(NEWSBLUR_WHITE_COLOR);
} else {
cell.textLabel.text = @"Loading...";
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleMedium];
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