summaryrefslogtreecommitdiff
path: root/Juick
diff options
context:
space:
mode:
Diffstat (limited to 'Juick')
-rw-r--r--Juick/MasterViewController.h1
-rw-r--r--Juick/MasterViewController.m92
-rw-r--r--Juick/Message.h3
-rw-r--r--Juick/Message.m21
4 files changed, 85 insertions, 32 deletions
diff --git a/Juick/MasterViewController.h b/Juick/MasterViewController.h
index a79b5c7..5e92717 100644
--- a/Juick/MasterViewController.h
+++ b/Juick/MasterViewController.h
@@ -9,4 +9,5 @@
#import <UIKit/UIKit.h>
@interface MasterViewController : UITableViewController
+- (void) refreshData:(UIRefreshControl *)refresh;
@end
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;
}
diff --git a/Juick/Message.h b/Juick/Message.h
index 4ec31e6..071d318 100644
--- a/Juick/Message.h
+++ b/Juick/Message.h
@@ -17,4 +17,7 @@
@property(nonatomic, copy) NSString *text;
-(id) initWithDictionary:(NSDictionary *)dictionary;
+
++(NSArray *) pullNextFromURL:(NSURL *)url;
+
@end
diff --git a/Juick/Message.m b/Juick/Message.m
index e87834b..08f9289 100644
--- a/Juick/Message.m
+++ b/Juick/Message.m
@@ -27,4 +27,25 @@
return result;
}
++(NSArray *) pullNextFromURL:(NSURL *)url {
+ // Set the gesture
+ NSError *error;
+ NSString *data = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
+ if (data == nil) {
+ NSLog(@"Download Error: %@", error);
+ return nil;
+ }
+ NSArray *resultObject = [NSJSONSerialization JSONObjectWithData:[data dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:&error];
+ if (resultObject == nil) {
+ NSLog(@"JSON Error: %@", error);
+ return nil;
+ }
+ NSMutableArray *result = [NSMutableArray array];
+ for (NSDictionary *message in resultObject) {
+ Message *msg = [[Message alloc] initWithDictionary:message];
+ [result addObject:msg];
+ }
+ return [result copy];
+}
+
@end