summaryrefslogtreecommitdiff
path: root/Juick/MessagesViewController.m
diff options
context:
space:
mode:
Diffstat (limited to 'Juick/MessagesViewController.m')
-rw-r--r--Juick/MessagesViewController.m195
1 files changed, 195 insertions, 0 deletions
diff --git a/Juick/MessagesViewController.m b/Juick/MessagesViewController.m
new file mode 100644
index 0000000..19eebf4
--- /dev/null
+++ b/Juick/MessagesViewController.m
@@ -0,0 +1,195 @@
+//
+// MasterViewController.m
+// Juick
+//
+// Created by Vitaly Takmazov on 26.10.13.
+// Copyright (c) 2013 com.juick. All rights reserved.
+//
+
+#import "SWRevealViewController.h"
+
+#import "MessagesViewController.h"
+
+#import "MessageCell.h"
+
+#import "Message.h"
+
+#import "NSURL+PathParameters.h"
+
+static NSString *CellIdentifier = @"MessageCell";
+
+@interface MessagesViewController ();
+
+@property(nonatomic, strong) NSMutableArray *messages;
+@property(nonatomic, assign) Boolean dataLoading;
+@property(nonatomic, strong) NSURL *url;
+
+@end
+
+@implementation MessagesViewController
+
+- (id)initWithURL:(NSURL *)messagesURL
+{
+ self = [super init];
+ if (self) {
+ self.url = messagesURL;
+ }
+ return self;
+}
+
+- (void) refreshData:(UIRefreshControl *)refresh {
+ [self.messages removeAllObjects];
+ [self.tableView reloadData];
+
+ NSArray *next = [Message pullNextFromURL:self.url];
+ int64_t delayInSeconds = 1.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];
+ });
+}
+
+- (UIStatusBarStyle)preferredStatusBarStyle
+{
+ return UIStatusBarStyleLightContent;
+}
+
+- (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_DEFAULT, 0), ^{
+ self.dataLoading = YES;
+ Message * lastMessage = [self.messages lastObject];
+ NSArray *next = [Message pullNextFromURL:[self.url URLByAppendingParameters:[NSDictionary dictionaryWithObjectsAndKeys:lastMessage.MID, @"before_mid", nil]]];
+ int64_t delayInSeconds = 1.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 = @"Discover";
+ [self.view setBackgroundColor:[UIColor colorWithRed:238/255.0f green:238/255.0f blue:229/255.0f alpha:1.0f]];
+ SWRevealViewController *revealController = [self revealViewController];
+ [self.navigationController.navigationBar addGestureRecognizer:revealController.panGestureRecognizer];
+ UIBarButtonItem *revealButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"reveal-icon.png"]
+ style:UIBarButtonItemStyleBordered target:revealController action:@selector(revealToggle:)];
+
+ self.navigationItem.leftBarButtonItem = revealButtonItem;
+ self.messages = [NSMutableArray array];
+ UIRefreshControl *refresh = [[UIRefreshControl alloc] init];
+ [refresh addTarget:self action:@selector(refreshData:) forControlEvents:UIControlEventValueChanged];
+ self.refreshControl = refresh;
+
+ //UINib *cellNib = [UINib nibWithNibName:@"MessageCell" bundle:nil];
+ //[self.tableView registerNib:cellNib forCellReuseIdentifier:@"MessageCell"];
+ [self.tableView registerClass:[MessageCell class] forCellReuseIdentifier:CellIdentifier];
+
+}
+
+- (void)viewDidAppear:(BOOL)animated
+{
+ [super viewDidAppear:animated];
+
+ [[NSNotificationCenter defaultCenter] addObserver:self
+ selector:@selector(contentSizeCategoryChanged:)
+ name:UIContentSizeCategoryDidChangeNotification
+ object:nil];
+}
+
+- (void)viewDidDisappear:(BOOL)animated
+{
+ [super viewDidDisappear:animated];
+
+ [[NSNotificationCenter defaultCenter] removeObserver:self
+ name:UIContentSizeCategoryDidChangeNotification
+ object:nil];
+}
+
+- (void)didReceiveMemoryWarning
+{
+ [super didReceiveMemoryWarning];
+ // Dispose of any resources that can be recreated.
+}
+
+- (void)contentSizeCategoryChanged:(NSNotification *)notification
+{
+ [self.tableView reloadData];
+}
+
+- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
+ return 1;
+}
+
+- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
+ 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];
+ [cell.avatar setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://i.juick.com/as/%@.png", msg.userID]]]]];
+ cell.titleLabel.text = msg.user;
+ cell.titleLabel.textColor = [UIColor colorWithRed:0 green:102/255.0f blue:153/255.0f alpha:1.0f];
+ cell.bodyLabel.text = msg.text;
+ [cell setBackgroundColor:[UIColor whiteColor]];
+ [cell setNeedsUpdateConstraints];
+ return cell;
+}
+
+- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
+{
+
+
+ MessageCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
+
+ [cell updateFonts];
+
+ Message *msg = [_messages objectAtIndex:indexPath.row];
+ [cell.avatar setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://i.juick.com/as/%@.png", msg.userID]]]]];
+ 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;
+}
+
+- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
+{
+ return 100.0f;
+}
+
+
+@end