diff options
author | Vitaly Takmazov | 2013-10-30 00:50:34 +0400 |
---|---|---|
committer | Vitaly Takmazov | 2013-10-30 00:50:34 +0400 |
commit | 566f122683240ea47f391033ec4e11ef424e7a5c (patch) | |
tree | fbe00959f88b3a16d2d4768c1cc223781a377160 /Juick/MasterViewController.m | |
parent | 7e7b734fc859234c9424b2ed98f5a2afef911de0 (diff) |
Working main feed
Diffstat (limited to 'Juick/MasterViewController.m')
-rw-r--r-- | Juick/MasterViewController.m | 152 |
1 files changed, 89 insertions, 63 deletions
diff --git a/Juick/MasterViewController.m b/Juick/MasterViewController.m index 41b2ef5..bd75f82 100644 --- a/Juick/MasterViewController.m +++ b/Juick/MasterViewController.m @@ -6,108 +6,134 @@ // Copyright (c) 2013 com.juick. All rights reserved. // +#import "SWRevealViewController.h" + #import "MasterViewController.h" -#import "DetailViewController.h" +#import "MessageCell.h" + +#import "Message.h" + +static NSString *CellIdentifier = @"MessageCell"; @interface MasterViewController () { - NSMutableArray *_objects; + NSMutableArray *messages; } @end @implementation MasterViewController -- (void)awakeFromNib -{ - [super awakeFromNib]; -} - (void)viewDidLoad { [super viewDidLoad]; - // Do any additional setup after loading the view, typically from a nib. - self.navigationItem.leftBarButtonItem = self.editButtonItem; - - UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)]; - self.navigationItem.rightBarButtonItem = addButton; + self.title = @"Messages"; + // Change button color + + // Set the gesture + messages = [[NSMutableArray alloc] init]; + NSError *error; + NSURL *url = [NSURL URLWithString:@"http://api.juick.com/messages"]; + NSString *data = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error]; + if (data == nil) { + NSLog(@"Download Error: %@", error); + return; + } + NSArray *resultObject = [NSJSONSerialization JSONObjectWithData:[data dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:&error]; + if (resultObject == nil) { + NSLog(@"JSON Error: %@", error); + return; + } + + for (NSDictionary *message in resultObject) { + Message *msg = [[Message alloc] initWithDictionary:message]; + [messages addObject:msg]; + } + //UINib *cellNib = [UINib nibWithNibName:@"MessageCell" bundle:nil]; + //[self.tableView registerNib:cellNib forCellReuseIdentifier:@"MessageCell"]; + [self.tableView registerClass:[MessageCell class] forCellReuseIdentifier:CellIdentifier]; + } -- (void)didReceiveMemoryWarning +- (void)viewDidAppear:(BOOL)animated { - [super didReceiveMemoryWarning]; - // Dispose of any resources that can be recreated. + [super viewDidAppear:animated]; + + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(contentSizeCategoryChanged:) + name:UIContentSizeCategoryDidChangeNotification + object:nil]; } -- (void)insertNewObject:(id)sender +- (void)viewDidDisappear:(BOOL)animated { - if (!_objects) { - _objects = [[NSMutableArray alloc] init]; - } - [_objects insertObject:[NSDate date] atIndex:0]; - NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; - [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; + [super viewDidDisappear:animated]; + + [[NSNotificationCenter defaultCenter] removeObserver:self + name:UIContentSizeCategoryDidChangeNotification + object:nil]; } -#pragma mark - Table View - -- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView +- (void)didReceiveMemoryWarning { - return 1; + [super didReceiveMemoryWarning]; + // Dispose of any resources that can be recreated. } -- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section +- (void)contentSizeCategoryChanged:(NSNotification *)notification { - return _objects.count; + [self.tableView reloadData]; } -- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath -{ - UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; - - NSDate *object = _objects[indexPath.row]; - cell.textLabel.text = [object description]; - return cell; +-(void) parserDidEndDocument:(NSXMLParser *)parser { + [self.tableView reloadData]; } -- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath -{ - // Return NO if you do not want the specified item to be editable. - return YES; +- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { + return 1; } -- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath -{ - if (editingStyle == UITableViewCellEditingStyleDelete) { - [_objects removeObjectAtIndex:indexPath.row]; - [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; - } else if (editingStyle == UITableViewCellEditingStyleInsert) { - // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view. - } +- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { + return messages.count; } -/* -// Override to support rearranging the table view. -- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath -{ +- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { + MessageCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; + [cell updateFonts]; + Message *msg = [messages objectAtIndex:indexPath.row]; + cell.titleLabel.text = msg.user; + cell.bodyLabel.text = msg.text; + [cell setNeedsUpdateConstraints]; + return cell; } -*/ -/* -// Override to support conditional rearranging of the table view. -- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath +- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { - // Return NO if you do not want the item to be re-orderable. - return YES; + + MessageCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; + + [cell updateFonts]; + + Message *msg = [messages objectAtIndex:indexPath.row]; + cell.titleLabel.text = msg.user; + cell.bodyLabel.text = msg.text; + + cell.bodyLabel.preferredMaxLayoutWidth = tableView.bounds.size.width - (kLabelHorizontalInsets * 2.0f); + + [cell setNeedsUpdateConstraints]; + [cell updateConstraintsIfNeeded]; + [cell.contentView setNeedsLayout]; + [cell.contentView layoutIfNeeded]; + + CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height; + + return height; } -*/ -- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender +- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath { - if ([[segue identifier] isEqualToString:@"showDetail"]) { - NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; - NSDate *object = _objects[indexPath.row]; - [[segue destinationViewController] setDetailItem:object]; - } + return 500.0f; } + @end |