Created
June 18, 2013 16:44
-
-
Save ajamaica/5807074 to your computer and use it in GitHub Desktop.
FeedController3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// FeedController3.m | |
// ADVFlatUI | |
// | |
// Created by Tope on 03/06/2013. | |
// Copyright (c) 2013 App Design Vault. All rights reserved. | |
// | |
#import "FeedController3.h" | |
#import "FeedCell3.h" | |
#import "UIImageView+WebCache.h" | |
#import "REComposeViewController.h" | |
#import <CoreMotion/CoreMotion.h> | |
#import <QuartzCore/QuartzCore.h> | |
enum { | |
COPAMIGOS = 0, | |
COPTODOS, | |
}; | |
@interface FeedController3 (){ | |
NSMutableArray *objects; | |
int actual; | |
} | |
@property (nonatomic, strong) IBOutlet UIView *square1; | |
@property (nonatomic) UIDynamicAnimator* animator; | |
@property (nonatomic, strong) NSArray* profileImages; | |
@property (readonly) CMMotionManager *motionManager; | |
@property (nonatomic) UIPushBehavior* pushBehavior; | |
@end | |
@implementation FeedController3 | |
- (IBAction)componer:(id)sender { | |
REComposeViewController *composeViewController = [[REComposeViewController alloc] init]; | |
composeViewController.title = @"Copalive"; | |
composeViewController.text = @"Hi there!"; | |
[self presentViewController:composeViewController animated:YES completion:nil]; | |
composeViewController.completionHandler = ^(REComposeViewController *composeViewController, REComposeResult result) { | |
[composeViewController dismissViewControllerAnimated:YES completion:nil]; | |
if (result == REComposeResultCancelled) { | |
NSLog(@"Cancelled"); | |
} | |
if (result == REComposeResultPosted) { | |
NSLog(@"Text = %@", composeViewController.text); | |
} | |
}; | |
} | |
- (CMMotionManager *)motionManager | |
{ | |
CMMotionManager *motionManager = nil; | |
id appDelegate = [UIApplication sharedApplication].delegate; | |
if ([appDelegate respondsToSelector:@selector(motionManager)]) { | |
motionManager = [appDelegate motionManager]; | |
} | |
return motionManager; | |
} | |
- (void)startMyMotionDetect | |
{ | |
[self.motionManager | |
startAccelerometerUpdatesToQueue:[[NSOperationQueue alloc] init] | |
withHandler:^(CMAccelerometerData *data, NSError *error) | |
{ | |
dispatch_async(dispatch_get_main_queue(), | |
^{ | |
CGFloat angle = atan2( data.acceleration.y, | |
-data.acceleration.x) | |
+ M_PI ; | |
[self.pushBehavior setMagnitude:30 / 100.0]; | |
[self.pushBehavior setAngle:angle]; | |
[self.pushBehavior setActive:TRUE]; | |
} | |
); | |
} | |
]; | |
} | |
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil | |
{ | |
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; | |
if (self) { | |
// Custom initialization | |
} | |
return self; | |
} | |
- (void)viewDidLoad | |
{ | |
[super viewDidLoad]; | |
actual = COPAMIGOS; | |
[self loadData]; | |
self.title = @"Comunidad"; | |
self.feedTableView.dataSource = self; | |
self.feedTableView.delegate = self; | |
self.feedTableView.backgroundColor = [UIColor clearColor]; | |
self.feedTableView.separatorColor = [UIColor colorWithWhite:0.9 alpha:0.6]; | |
self.square1 = [[UIView alloc] initWithFrame:CGRectMake(0,0, 100, 100)]; | |
self.square1.clipsToBounds = YES; | |
self.square1.layer.cornerRadius = 50.0f; | |
[self.square1 setBackgroundColor:[UIColor grayColor]]; | |
[self.view addSubview:self.square1]; | |
UIDynamicAnimator* animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view]; | |
UICollisionBehavior* collisionBehavior = [[UICollisionBehavior alloc] initWithItems:@[self.square1]]; | |
collisionBehavior.translatesReferenceBoundsIntoBoundary = YES; | |
[animator addBehavior:collisionBehavior]; | |
UIPushBehavior *pushBehavior = [[UIPushBehavior alloc] initWithItems:@[self.square1] mode:UIPushBehaviorModeInstantaneous]; | |
pushBehavior.angle = 0.0; | |
pushBehavior.magnitude = 0.0; | |
self.pushBehavior = pushBehavior; | |
[animator addBehavior:self.pushBehavior]; | |
self.animator = animator; | |
} | |
-(void)collisionBehavior:(UICollisionBehavior *)behavior beganContactForItem:(id<UIDynamicItem>)item withBoundaryIdentifier:(id<NSCopying>)identifier atPoint:(CGPoint)p | |
{ | |
// Lighten the background color when the view is in contact with a boundary. | |
[(UIView*)item setBackgroundColor:[UIColor lightGrayColor]]; | |
} | |
-(void)collisionBehavior:(UICollisionBehavior *)behavior endedContactForItem:(id<UIDynamicItem>)item withBoundaryIdentifier:(id<NSCopying>)identifier | |
{ | |
// Restore the default color when ending a contcact. | |
[(UIView*)item setBackgroundColor:[UIColor grayColor]]; | |
} | |
- (void)loadData{ | |
if (actual == COPAMIGOS) { | |
PFQuery *query = [PFQuery queryWithClassName:@"Mensaje"]; | |
[query includeKey:@"user"]; | |
NSArray *friends = [[PFUser currentUser] objectForKey:@"friends"]; | |
NSMutableArray *friendsobj = [NSMutableArray array]; | |
for (NSString *str in friends) { | |
PFUser *user =[PFUser objectWithoutDataWithObjectId:str]; | |
[friendsobj addObject:user]; | |
} | |
[query whereKey:@"user" containedIn:friendsobj]; | |
[query orderByDescending:@"createdAt"]; | |
[query findObjectsInBackgroundWithBlock:^(NSArray *objectss, NSError *error) { | |
if(!error){ | |
objects = [NSMutableArray arrayWithArray:objectss]; | |
[self.feedTableView reloadData]; | |
} | |
}]; | |
}else{ | |
PFQuery *query = [PFQuery queryWithClassName:@"Mensaje"]; | |
[query includeKey:@"user"]; | |
[query orderByDescending:@"createdAt"]; | |
[query findObjectsInBackgroundWithBlock:^(NSArray *objectss, NSError *error) { | |
if(!error){ | |
objects = [NSMutableArray arrayWithArray:objectss]; | |
[self.feedTableView reloadData]; | |
} | |
}]; | |
} | |
} | |
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ | |
return [objects count]; | |
} | |
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ | |
PFObject *object = [objects objectAtIndex:indexPath.row]; | |
FeedCell3* cell = [tableView dequeueReusableCellWithIdentifier:@"FeedCell3"]; | |
if (cell == nil) { | |
cell = [[FeedCell3 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"FeedCell3"] ; | |
} | |
cell.nameLabel.text = [[object objectForKey:@"user"] objectForKey:@"username"]; | |
cell.nameLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline1]; | |
cell.updateLabel.text =[object objectForKey:@"mensaje"]; | |
cell.updateLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody]; | |
[cell.updateLabel sizeToFit]; | |
CGRect myFrame = cell.updateLabel.frame; | |
// Resize the frame's width to 280 (320 - margins) | |
// width could also be myOriginalLabelFrame.size.width | |
myFrame = CGRectMake(myFrame.origin.x, myFrame.origin.y, 261, myFrame.size.height); | |
cell.updateLabel.frame = myFrame; | |
cell.dateLabel.text = @"1 hr ago"; | |
cell.likeCountLabel.text = @"293 likes"; | |
cell.commentCountLabel.text = @"55 comments"; | |
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",[[object objectForKey:@"user"] objectForKey:@"avatar"]]]; | |
[cell.profileImageView setImageWithURL:url placeholderImage:nil]; | |
return cell; | |
} | |
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ | |
UIFont *font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody]; | |
PFObject *object = [objects objectAtIndex:indexPath.row]; | |
NSString *text =[object objectForKey:@"mensaje"]; | |
NSMutableDictionary *atts = [[NSMutableDictionary alloc] init]; | |
[atts setObject:font forKey:NSFontAttributeName]; | |
CGRect rect = [text boundingRectWithSize:CGSizeMake(238, 100) | |
options:NSStringDrawingUsesLineFragmentOrigin | |
attributes:atts | |
context:nil]; | |
return rect.size.height + 80; | |
} | |
- (void)didReceiveMemoryWarning | |
{ | |
[super didReceiveMemoryWarning]; | |
// Dispose of any resources that can be recreated. | |
} | |
- (void)viewDidAppear:(BOOL)animated | |
{ | |
[super viewDidAppear:animated]; | |
[self startMyMotionDetect]; | |
} | |
- (void)viewDidDisappear:(BOOL)animated | |
{ | |
[super viewDidDisappear:animated]; | |
[self.motionManager stopAccelerometerUpdates]; | |
} | |
- (IBAction)change:(id)sender { | |
if (actual == COPAMIGOS) { | |
actual = COPTODOS; | |
}else{ | |
actual = COPAMIGOS; | |
} | |
[self loadData]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment