Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
299 lines (231 sloc) 8.62 KB
//
// StreamVC.m
// SecondScreen
//
// Created by Dave Durazzani on 1/31/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "StreamVC.h"
#import "TwitterService.h"
#import "TwitterServiceDelegate.h"
#import "AppDelegate_iPad.h"
#import "Tweet.h"
#import "TweetView.h"
#import "UIView+Additions.h"
#import "Show.h"
#define TWEET_WIDTH 340.f //<-- actual width is 320 (added 20px of padding)
#define SCREEN_WIDTH 1024.f
#define SCROLL_LEFT_PADDING 10.f
@interface StreamVC()
- (void)insertTweet:(Tweet*)twt;//<-- called by delegate when connection receieves a new tweet
- (void)tick;//<-- called by self.clock at constant intervals
- (void)refreshLayout;//<-- call this after a tweet delition;
@end
@implementation StreamVC
- (id) initWithShow:(Show*)shw {
if (self = [super initWithNibName:@"StreamVC" bundle:nil]) {
self.tweets = [NSMutableArray array];
self.showStreamed = shw;
self.tweetXPositions = [NSMutableArray array];
self.lastTweetRightCornerX = [NSNumber numberWithFloat:0.f];
//Instatiate the TwitterWrapper
TwitterService *tw = [[TwitterService alloc] initWithUser:[AppDelegate userName]
userPassword:[AppDelegate userPassword]
followers:shw.followers
andDelegate:self];
[tw connectToShow];
} return self;
}
#pragma mark -
#pragma mark VIEW
- (void)viewDidLoad {
[super viewDidLoad];
self.title = self.showStreamed.showTitle;
self.clock = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(tick) userInfo:nil repeats:YES];
self.loaderImage.animationImages = [AppDelegate loaderPngs];
self.loaderImage.animationDuration = 2.5f;
self.loaderImage.animationRepeatCount = 0;
}
- (void)insertTweet:(Tweet*)twt {
//Make sure positions are ordered!
NSArray* sortedArray = [self.tweetXPositions sortedArrayUsingSelector:@selector(compare:)];
self.tweetXPositions = [NSMutableArray arrayWithArray:sortedArray];
float lastX = 0;
if(![self.tweets count]) {
lastX = SCROLL_LEFT_PADDING;
} else {
lastX = [(NSNumber*)[self.tweetXPositions lastObject] floatValue];
}
TweetView *twtView = [[TweetView alloc] initWithNibName:@"TweetView"];
[twtView initializeWithTweet:twt withDelegate:self];
NSLog(@"TweetView Size: %@", NSStringFromCGRect(twtView.frame));
[self.streamScrollView addSubview:twtView];
//Store received tweet in self.tweets mutable array!
[self.tweets addObject:twt];
if([self.tweets count]>1) {
//twtView.positionOnView = [NSNumber numberWithFloat:([self.tweets count]-1)*TWEET_WIDTH + SCROLL_LEFT_PADDING];
twtView.positionOnView = [NSNumber numberWithFloat:(lastX + [self.lastTweetRightCornerX floatValue])];
} else {
twtView.positionOnView = [NSNumber numberWithFloat:lastX];
}
NSLog(@"Position calculate is: %@",twtView.positionOnView );
setFrameX(twtView, [twtView.positionOnView floatValue]);
NSLog(@"TWEET BOUNDS: %@", NSStringFromCGRect(twtView.bounds));
//NSLog(@"SCROLLVIEW BOUNDS: %@", NSStringFromCGRect(self.streamScrollView.bounds));
//NSLog(@"CENTER Y: %f", (self.streamScrollView.bounds.size.height - twtView.bounds.size.height)/2);
setFrameY(twtView, (self.streamScrollView.bounds.size.height - twtView.bounds.size.height)/2);
if([self.tweets count]>3) {
//resize scrollview content view accordingly
self.streamScrollView.contentSize = CGSizeMake([twtView.positionOnView floatValue] + twtView.bounds.size.width, self.streamScrollView.contentSize.height);
}
//Store tweetPosition
NSLog(@"ADDED POSITION: %@", [NSNumber numberWithFloat:twtView.frame.origin.x]);
[self.tweetXPositions addObject:[NSNumber numberWithFloat:twtView.frame.origin.x]];
self.lastTweetRightCornerX = [NSNumber numberWithFloat:[self.lastTweetRightCornerX floatValue] + twtView.bounds.size.width];
[self updatePositions];
[twtView release];
}
- (void)refreshLayout {
//TODO:need to implement refresh algorithm for deletion action!!
}
#pragma mark -
#pragma mark ACTIONS
-(IBAction)playPauseStream:(id)sender {
NSLog(@"PlayPause hit");
playing = !playing;
if(playing) {
[self.loaderImage startAnimating];
self.loaderImage.hidden = NO;
} else {
[self.loaderImage stopAnimating];
self.loaderImage.hidden = YES;
}
}
- (void)tick {
if(playing) {
[self moveToNextTweet];
//[self moveToLastTweet];
}
}
#pragma mark -
#pragma mark StramView Behaviors
- (void)moveToLastTweet {
dispatch_async(dispatch_get_main_queue(), ^{
[self updatePositions];
[self.streamScrollView setContentOffset:CGPointMake(xLastTweet - SCROLL_LEFT_PADDING, self.streamScrollView.contentOffset.y) animated:YES];
});
}
- (void)moveToNextTweet {
[self updatePositions];
dispatch_async(dispatch_get_main_queue(), ^{
//NSLog(@"About to move to Next Tweet @: %f", xNextTweet);
if(xRightLimit <= xNextTweet && [self.tweetXPositions count]>3) { //<-- go to bext tweet if xLimit is before it and there is at least 3 tweets
[self.streamScrollView setContentOffset:CGPointMake(xNextTweet-SCREEN_WIDTH + TWEET_WIDTH, self.streamScrollView.contentOffset.y) animated:YES];
NSLog(@"Moved to Next Tweet @: %f", xNextTweet);
} else {
NSLog(@"Reached end of stream");
}
});
}
- (void)updatePositions {
if([self.tweetXPositions count]) {
//Make sure positions are ordered!
NSArray* sortedArray = [self.tweetXPositions sortedArrayUsingSelector:@selector(compare:)];
self.tweetXPositions = [NSMutableArray arrayWithArray:sortedArray];
//NSLog(@"Current Positions");
for(NSNumber *n in self.tweetXPositions) {
NSLog(@"Position: %@", n);
}
xLeftLimit = self.streamScrollView.contentOffset.x;
xRightLimit = xLeftLimit + SCREEN_WIDTH; //assumes scrollview starts at X = 0;
NSNumber *lastTemp = [self.tweetXPositions lastObject];
xLastTweet = [lastTemp floatValue];
BOOL donechecking = NO;
for(NSNumber *n in self.tweetXPositions) {
NSLog(@"Checking on Position: %@", n);
if([n floatValue] >= xRightLimit && !donechecking) {
xNextTweet = [n floatValue];
donechecking = YES;
}
}
if([self.tweetXPositions count]<=3 && [self.tweetXPositions count]>=1) {//<-- if tweets are >1 and <=3 then
xNextTweet = xLastTweet;
donechecking = YES;
}
} else {
//ScrollView is empty
xLeftLimit = 0.f;
xRightLimit = xRightLimit = xLeftLimit + SCREEN_WIDTH; //assumes scrollview starts at X = 0;
xLastTweet = -1.f;//<-- nothing on view
xNextTweet = -1.f;
}
//NSLog(@"xUSER: %f", xLeftLimit);
//NSLog(@"xLIMIT: %f", xRightLimit);
//NSLog(@"xNextTweet: %f", xNextTweet);
//NSLog(@"*********** xLastTweet: %f ***************", xLastTweet); //<-- next to the limit
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
[super viewDidUnload];
self.streamScrollView = nil;
self.clock = nil;
self.playPauseButton = nil;
self.loaderImage = nil;
}
#pragma mark -
#pragma mark TwitterServiceDelegate
-(void)connectionReceivedTweet:(Tweet*)twt {
[self insertTweet:twt];
}
-(void)connectionRequestedTweetDeletionWithId:(NSString*)tId {
//NSLog(@"DELETE TWEET: %@", tId);
for(TweetView *tview in [self.streamScrollView subviews]) {
if([tview isKindOfClass:[TweetView class]]) {
//NSLog(@"tId: %@", tId);
//NSLog(@"TWEET ID: %@",[[tview tweet] tweetId] );
if([[[tview tweet] tweetId] isEqualToString:tId]) {
[tview removeFromSuperview];
[self.tweetXPositions removeObject:tview.positionOnView];//TODO: CHECK THIS!!!
[self.tweets removeObject:tview.tweet];
}
}
}
NSLog(@"Positions after deleting");
for(NSNumber *n in self.tweetXPositions) {
NSLog(@"Position: %@", n);
}
[self refreshLayout];
}
#pragma mark -
#pragma mark ScrollView Delegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
[self updatePositions];
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { }
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate { }
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView { }
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { }
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView { }
#pragma mark -
#pragma mark Memory management
@synthesize tweets, playPauseButton;
@synthesize streamScrollView, showStreamed;
@synthesize clock, tweetXPositions, loaderImage;
@synthesize lastTweetRightCornerX;
- (void)dealloc {
[lastTweetRightCornerX release];
[loaderImage release];
[clock release];
[tweetXPositions release];
[playPauseButton release];
[showStreamed release];
[tweets release];
[streamScrollView release];
[super dealloc];
}
@end