diff options
Diffstat (limited to 'Juick/MasterViewController.m')
-rw-r--r-- | Juick/MasterViewController.m | 92 |
1 files changed, 60 insertions, 32 deletions
diff --git a/Juick/MasterViewController.m b/Juick/MasterViewController.m index 74b4fdc..26035cd 100644 --- a/Juick/MasterViewController.m +++ b/Juick/MasterViewController.m @@ -14,42 +14,74 @@ static NSString *CellIdentifier = @"MessageCell"; -@interface MasterViewController () { - NSMutableArray *messages; -} +@interface MasterViewController (); + +@property(nonatomic, strong) NSMutableArray *messages; +@property(nonatomic, assign) Boolean dataLoading; + @end @implementation MasterViewController +- (void) refreshData:(UIRefreshControl *)refresh { + [self.messages removeAllObjects]; + [self.tableView reloadData]; + NSURL *url = [NSURL URLWithString:@"https://api.juick.com/messages"]; + + NSArray *next = [Message pullNextFromURL:url]; + int64_t delayInSeconds = 2.0; + dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (delayInSeconds * NSEC_PER_SEC)); + dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ + [self.tableView beginUpdates]; + CGPoint offset = self.tableView.contentOffset; + for (int i = 0; i < [next count]; i++) { + [self.messages addObject:[next objectAtIndex:i]]; + [self.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:self.messages.count-1 inSection:0]] withRowAnimation:UITableViewRowAnimationNone]; + } + [self.tableView setContentOffset:offset animated:NO]; + [self.tableView endUpdates]; + [refresh endRefreshing]; + }); +} + +- (void)scrollViewDidScroll:(UIScrollView *)scrollView { + CGPoint pos = scrollView.contentOffset; + CGFloat contentHeight = scrollView.contentSize.height - scrollView.contentSize.height / 3; + if (pos.y >= contentHeight && !self.dataLoading) { + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{ + self.dataLoading = YES; + Message * lastMessage = [self.messages lastObject]; + NSArray *next = [Message pullNextFromURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://api.juick.com/messages?before_mid=%@", lastMessage.MID]]]; + int64_t delayInSeconds = 2.0; + dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (delayInSeconds * NSEC_PER_SEC)); + dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ + [self.tableView beginUpdates]; + for (int i = 0; i < [next count]; i++) { + [self.messages addObject:[next objectAtIndex:i]]; + [self.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:self.messages.count-1 inSection:0]] withRowAnimation:UITableViewRowAnimationNone]; + } + [self.tableView setContentOffset:pos animated:NO]; + [self.tableView endUpdates]; + self.dataLoading = NO; + }); + }); + + } +} - (void)viewDidLoad { [super viewDidLoad]; + self.dataLoading = NO; 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; - } + self.messages = [NSMutableArray array]; + UIRefreshControl *refresh = [[UIRefreshControl alloc] init]; + [refresh addTarget:self action:@selector(refreshData:) forControlEvents:UIControlEventValueChanged]; + self.refreshControl = refresh; - 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]; + [self.tableView registerClass:[MessageCell class] forCellReuseIdentifier:CellIdentifier]; } @@ -83,22 +115,18 @@ static NSString *CellIdentifier = @"MessageCell"; [self.tableView reloadData]; } --(void) parserDidEndDocument:(NSXMLParser *)parser { - [self.tableView reloadData]; -} - - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { - return messages.count; + return _messages.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { MessageCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; [cell updateFonts]; - Message *msg = [messages objectAtIndex:indexPath.row]; + Message *msg = [_messages objectAtIndex:indexPath.row]; cell.titleLabel.text = msg.user; cell.bodyLabel.text = msg.text; [cell setNeedsUpdateConstraints]; @@ -112,7 +140,7 @@ static NSString *CellIdentifier = @"MessageCell"; [cell updateFonts]; - Message *msg = [messages objectAtIndex:indexPath.row]; + Message *msg = [_messages objectAtIndex:indexPath.row]; cell.titleLabel.text = msg.user; cell.bodyLabel.text = msg.text; @@ -130,7 +158,7 @@ static NSString *CellIdentifier = @"MessageCell"; - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath { - return 500.0f; + return 100.0f; } |