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"
|
2012-07-16 23:48:07 -07:00
|
|
|
#import "InteractionCell.h"
|
2013-02-21 17:57:32 -08:00
|
|
|
#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"
|
2013-09-12 16:40:23 -07:00
|
|
|
#import "DashboardViewController.h"
|
2012-07-11 20:19:42 -07:00
|
|
|
|
2013-09-11 16:16:56 -07:00
|
|
|
#define MINIMUM_INTERACTION_HEIGHT_IPAD 78
|
2013-09-11 17:05:47 -07:00
|
|
|
#define MINIMUM_INTERACTION_HEIGHT_IPHONE 54
|
2012-07-18 13:56:09 -07:00
|
|
|
|
2012-07-11 20:19:42 -07:00
|
|
|
@implementation InteractionsModule
|
|
|
|
|
|
|
|
@synthesize interactionsTable;
|
|
|
|
@synthesize interactionsArray;
|
2012-07-18 13:56:09 -07:00
|
|
|
@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
|
|
|
|
2012-07-18 13:56:09 -07:00
|
|
|
[self addSubview:self.interactionsTable];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)refreshWithInteractions:(NSArray *)interactions {
|
|
|
|
self.interactionsArray = interactions;
|
|
|
|
|
2012-07-11 20:19:42 -07:00
|
|
|
[self.interactionsTable reloadData];
|
|
|
|
|
2012-07-18 13:56:09 -07:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-07-18 12:17:55 -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
|
2013-03-04 20:21:29 -08:00
|
|
|
// 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;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
2012-07-18 15:26:55 -07:00
|
|
|
if (page == 1) {
|
|
|
|
self.pageFetching = NO;
|
|
|
|
self.pageFinished = NO;
|
2012-07-26 23:07:47 -07:00
|
|
|
appDelegate.userInteractionsArray = nil;
|
2012-07-18 15:26:55 -07:00
|
|
|
}
|
2012-08-06 15:46:05 -07:00
|
|
|
|
2012-07-18 13:56:09 -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:@
|
2013-06-18 21:23:20 -04:00
|
|
|
"%@/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,
|
2013-03-04 20:21:29 -08:00
|
|
|
[appDelegate.dictSocialProfile objectForKey:@"user_id"],
|
2012-07-18 13:56:09 -07:00
|
|
|
page];
|
|
|
|
|
2019-08-22 12:03:39 -07:00
|
|
|
[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-04-03 16:07:01 -07:00
|
|
|
}];
|
2012-07-18 13:56:09 -07:00
|
|
|
}
|
2012-07-18 12:17:55 -07:00
|
|
|
}
|
|
|
|
|
2017-03-19 18:15:36 -07:00
|
|
|
- (void)finishLoadInteractions:(NSDictionary *)results {
|
2012-07-18 13:56:09 -07:00
|
|
|
self.pageFetching = NO;
|
2012-07-18 12:17:55 -07:00
|
|
|
|
2012-07-18 13:56:09 -07:00
|
|
|
NSArray *newInteractions = [results objectForKey:@"interactions"];
|
2012-07-28 11:02:54 -07:00
|
|
|
|
|
|
|
// check for last page
|
|
|
|
if (![[results objectForKey:@"has_next_page"] intValue]) {
|
|
|
|
self.pageFinished = YES;
|
|
|
|
}
|
|
|
|
|
2012-07-18 13:56:09 -07:00
|
|
|
NSMutableArray *confirmedInteractions = [NSMutableArray array];
|
2020-08-27 15:08:46 -07:00
|
|
|
if ([self->appDelegate.userInteractionsArray count]) {
|
2012-07-18 13:56:09 -07:00
|
|
|
NSMutableSet *interactionsDates = [NSMutableSet set];
|
2012-07-26 23:07:47 -07:00
|
|
|
for (id interaction in appDelegate.userInteractionsArray) {
|
2012-07-18 13:56:09 -07:00
|
|
|
[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;
|
2012-07-18 13:56:09 -07:00
|
|
|
} else {
|
2012-07-26 23:07:47 -07:00
|
|
|
appDelegate.userInteractionsArray = [appDelegate.userInteractionsArray arrayByAddingObjectsFromArray:newInteractions];
|
2012-07-18 13:56:09 -07:00
|
|
|
}
|
2012-07-18 12:17:55 -07:00
|
|
|
|
2013-02-21 16:53:37 -08:00
|
|
|
|
2012-07-26 23:07:47 -07:00
|
|
|
[self refreshWithInteractions:appDelegate.userInteractionsArray];
|
2012-07-18 12:17:55 -07:00
|
|
|
}
|
|
|
|
|
2012-07-11 20:19:42 -07:00
|
|
|
#pragma mark -
|
|
|
|
#pragma mark Table View - Interactions List
|
|
|
|
|
2012-07-16 23:48:07 -07:00
|
|
|
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
|
2012-07-11 20:19:42 -07:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-07-18 12:17:55 -07:00
|
|
|
|
2012-07-18 13:56:09 -07:00
|
|
|
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
|
2012-07-21 18:25:56 -07:00
|
|
|
return 0;
|
2012-07-18 13:56:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
|
2013-09-25 17:43:00 -07:00
|
|
|
NSInteger userInteractions = [appDelegate.userInteractionsArray count];
|
2013-09-11 16:16:56 -07:00
|
|
|
int minimumHeight;
|
2020-03-29 16:21:00 -07:00
|
|
|
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
|
2013-09-11 16:16:56 -07:00
|
|
|
minimumHeight = MINIMUM_INTERACTION_HEIGHT_IPAD;
|
|
|
|
} else {
|
|
|
|
minimumHeight = MINIMUM_INTERACTION_HEIGHT_IPHONE;
|
|
|
|
}
|
|
|
|
|
2012-07-18 13:56:09 -07:00
|
|
|
if (indexPath.row >= userInteractions) {
|
2013-09-11 16:16:56 -07:00
|
|
|
return minimumHeight;
|
2012-07-18 13:56:09 -07:00
|
|
|
}
|
2012-07-18 12:17:55 -07:00
|
|
|
|
2013-02-21 17:57:32 -08:00
|
|
|
InteractionCell *interactionCell;
|
2020-03-29 16:21:00 -07:00
|
|
|
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
|
2013-02-21 17:57:32 -08:00
|
|
|
interactionCell = [[InteractionCell alloc] init];
|
|
|
|
} else {
|
|
|
|
interactionCell = [[SmallInteractionCell alloc] init];
|
|
|
|
}
|
2013-09-11 17:05:47 -07:00
|
|
|
int height = [interactionCell setInteraction:[appDelegate.userInteractionsArray objectAtIndex:(indexPath.row)] withWidth:self.frame.size.width - 20];
|
2012-07-18 17:23:57 -07:00
|
|
|
|
2013-09-12 17:03:18 -07:00
|
|
|
return height;
|
2012-07-18 13:56:09 -07:00
|
|
|
}
|
2012-07-18 12:17:55 -07:00
|
|
|
|
2012-07-18 13:56:09 -07:00
|
|
|
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
|
|
|
|
UIView *blank = [[UIView alloc] init];
|
|
|
|
return blank;
|
2012-07-16 23:48:07 -07:00
|
|
|
}
|
|
|
|
|
2012-07-11 20:19:42 -07:00
|
|
|
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
|
|
|
|
{
|
2013-09-25 17:43:00 -07:00
|
|
|
NSInteger userInteractionsCount = [appDelegate.userInteractionsArray count];
|
2012-07-18 13:56:09 -07:00
|
|
|
return userInteractionsCount + 1;
|
2012-07-11 20:19:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
|
2013-02-21 17:57:32 -08:00
|
|
|
InteractionCell *cell = [tableView
|
2012-07-21 15:59:10 -07:00
|
|
|
dequeueReusableCellWithIdentifier:@"InteractionCell"];
|
2012-07-11 20:19:42 -07:00
|
|
|
if (cell == nil) {
|
2020-03-29 16:21:00 -07:00
|
|
|
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
|
2013-02-21 17:57:32 -08:00
|
|
|
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]) {
|
2012-07-21 15:59:10 -07:00
|
|
|
// add in loading cell
|
2012-07-18 13:56:09 -07:00
|
|
|
return [self makeLoadingCell];
|
2012-07-21 15:59:10 -07:00
|
|
|
} 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);
|
|
|
|
|
2012-07-21 15:59:10 -07:00
|
|
|
// update the cell information
|
2012-08-09 16:34:59 -07:00
|
|
|
[cell setInteraction:interaction withWidth: self.frame.size.width - 20];
|
2013-09-11 16:16:56 -07:00
|
|
|
[cell layoutSubviews];
|
2012-07-18 13:56:09 -07:00
|
|
|
}
|
|
|
|
|
2012-07-11 20:19:42 -07:00
|
|
|
return cell;
|
|
|
|
}
|
|
|
|
|
2012-07-12 00:10:42 -07:00
|
|
|
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
|
2013-09-25 17:43:00 -07:00
|
|
|
NSInteger userInteractions = [appDelegate.userInteractionsArray count];
|
2012-07-12 00:10:42 -07:00
|
|
|
if (indexPath.row < userInteractions) {
|
2012-07-26 23:07:47 -07:00
|
|
|
NSDictionary *interaction = [appDelegate.userInteractionsArray objectAtIndex:indexPath.row];
|
2012-07-12 00:10:42 -07:00
|
|
|
NSString *category = [interaction objectForKey:@"category"];
|
|
|
|
if ([category isEqualToString:@"follow"]) {
|
2012-07-28 14:15:20 -07:00
|
|
|
[tableView deselectRowAtIndexPath:indexPath animated:YES];
|
|
|
|
|
2012-07-29 20:55:11 -07:00
|
|
|
NSString *userId = [NSString stringWithFormat:@"%@", [[interaction objectForKey:@"with_user"] objectForKey:@"user_id"]];
|
2012-07-12 00:10:42 -07:00
|
|
|
appDelegate.activeUserProfileId = userId;
|
2012-07-28 12:56:51 -07:00
|
|
|
|
2012-07-29 20:55:11 -07:00
|
|
|
NSString *username = [NSString stringWithFormat:@"%@", [[interaction objectForKey:@"with_user"] objectForKey:@"username"]];
|
2012-07-28 14:15:20 -07:00
|
|
|
appDelegate.activeUserProfileName = username;
|
|
|
|
|
2012-07-28 12:56:51 -07:00
|
|
|
// pass cell to the show UserProfile
|
2012-07-12 00:10:42 -07:00
|
|
|
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
|
2012-07-28 12:56:51 -07:00
|
|
|
[appDelegate showUserProfileModal:cell];
|
2012-07-28 11:49:31 -07:00
|
|
|
} else if ([category isEqualToString:@"comment_reply"] ||
|
|
|
|
[category isEqualToString:@"reply_reply"] ||
|
|
|
|
[category isEqualToString:@"comment_like"]) {
|
2012-07-16 22:35:28 -07:00
|
|
|
NSString *feedIdStr = [NSString stringWithFormat:@"%@", [interaction objectForKey:@"feed_id"]];
|
|
|
|
NSString *contentIdStr = [NSString stringWithFormat:@"%@", [interaction objectForKey:@"content_id"]];
|
2012-07-28 23:31:12 -07:00
|
|
|
[appDelegate loadTryFeedDetailView:feedIdStr
|
|
|
|
withStory:contentIdStr
|
|
|
|
isSocial:YES
|
2012-08-12 21:46:47 -07:00
|
|
|
withUser:[interaction objectForKey:@"with_user"]
|
|
|
|
showFindingStory:YES];
|
2012-07-31 23:49:51 -07:00
|
|
|
appDelegate.tryFeedCategory = category;
|
2012-07-26 23:07:47 -07:00
|
|
|
} else if ([category isEqualToString:@"story_reshare"]) {
|
2012-07-16 22:35:28 -07:00
|
|
|
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];
|
2012-07-31 23:49:51 -07:00
|
|
|
appDelegate.tryFeedCategory = category;
|
2012-07-28 11:49:31 -07:00
|
|
|
}
|
2012-07-26 23:07:47 -07:00
|
|
|
|
2012-07-16 22:35:28 -07:00
|
|
|
// have the selected cell deselect
|
|
|
|
[tableView deselectRowAtIndexPath:indexPath animated:YES];
|
2012-07-12 00:10:42 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-18 13:56:09 -07:00
|
|
|
- (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);
|
2012-07-18 13:56:09 -07:00
|
|
|
|
|
|
|
if (self.pageFinished) {
|
|
|
|
UIImage *img = [UIImage imageNamed:@"fleuron.png"];
|
|
|
|
UIImageView *fleuron = [[UIImageView alloc] initWithImage:img];
|
2013-09-11 16:16:56 -07:00
|
|
|
|
|
|
|
int height;
|
2020-03-29 16:21:00 -07:00
|
|
|
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
|
2013-09-11 16:16:56 -07:00
|
|
|
height = MINIMUM_INTERACTION_HEIGHT_IPAD;
|
|
|
|
} else {
|
|
|
|
height = MINIMUM_INTERACTION_HEIGHT_IPHONE;
|
|
|
|
}
|
2012-07-18 13:56:09 -07:00
|
|
|
|
|
|
|
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);
|
2012-07-18 13:56:09 -07:00
|
|
|
} else {
|
|
|
|
cell.textLabel.text = @"Loading...";
|
|
|
|
|
|
|
|
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc]
|
2020-09-22 17:28:14 -07:00
|
|
|
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleMedium];
|
2012-07-18 13:56:09 -07:00
|
|
|
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];
|
|
|
|
}
|
|
|
|
|
2017-01-05 11:44:18 -08:00
|
|
|
@end
|