summaryrefslogtreecommitdiff
path: root/Juick/ViewControllers/MessagesViewController.m
diff options
context:
space:
mode:
Diffstat (limited to 'Juick/ViewControllers/MessagesViewController.m')
-rw-r--r--Juick/ViewControllers/MessagesViewController.m93
1 files changed, 49 insertions, 44 deletions
diff --git a/Juick/ViewControllers/MessagesViewController.m b/Juick/ViewControllers/MessagesViewController.m
index 6b8fdb7..77406a6 100644
--- a/Juick/ViewControllers/MessagesViewController.m
+++ b/Juick/ViewControllers/MessagesViewController.m
@@ -24,54 +24,52 @@
@implementation MessagesViewController
-(void) refreshData {
- [self refreshData:NO];
-}
-
--(void) refreshData:(BOOL)scrollToBottom {
- self.dataLoading = YES;
- if (self.messages.count == 0) {
- [self.tableView reloadData];
- }
- [[APIClient sharedClient] pullNextFromPath:self.path params:self.params callback:^(NSArray *next, NSError *err) {
- if (err) {
- [User throwUnableToLogin:self];
- return;
- }
- NSArray *newMsgs = next;
- if ([self isAtTop:self.params]) {
- [self.messages removeAllObjects];
+ if ([self.path length] > 0) {
+ self.dataLoading = YES;
+ if (self.messages.count == 0) {
[self.tableView reloadData];
}
- NSUInteger oldCount = [self.messages count];
- if ([self.path isEqualToString:[APIClient threadUrl]]) {
- NSUInteger lastRid = [((Message *)[self.messages lastObject]).rid unsignedIntegerValue] + 1;
- NSUInteger count = [next count];
- if (oldCount > 0) {
- if (lastRid && lastRid < count) {
- newMsgs = [next subarrayWithRange:NSMakeRange(lastRid, count - lastRid)];
- } else {
- return;
+ [[APIClient sharedClient] pullNextFromPath:self.path params:self.params callback:^(NSArray *next, NSError *err) {
+ if (err) {
+ [User throwUnableToLogin:self];
+ return;
+ }
+ NSArray *newMsgs = next;
+ if ([self isAtTop:self.params]) {
+ [self.messages removeAllObjects];
+ [self.tableView reloadData];
+ }
+ NSUInteger oldCount = [self.messages count];
+ if ([self.path isEqualToString:[APIClient threadUrl]]) {
+ NSUInteger lastRid = [((Message *)[self.messages lastObject]).rid unsignedIntegerValue] + 1;
+ NSUInteger count = [next count];
+ if (oldCount > 0) {
+ if (lastRid && lastRid < count) {
+ newMsgs = [next subarrayWithRange:NSMakeRange(lastRid, count - lastRid)];
+ } else {
+ return;
+ }
}
}
- }
- self.dataLoading = NO;
- if (self.messages.count == 0) {
- [self.tableView reloadData];
- }
- [self.messages addObjectsFromArray:newMsgs];
- NSMutableArray *indexPaths = [NSMutableArray new];
- for (NSUInteger index = oldCount; index <= oldCount + [newMsgs count] - 1; index++) {
- [indexPaths addObject:[NSIndexPath indexPathForRow:index inSection: 0]];
- }
- [self.tableView beginUpdates];
- [self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:NO];
- [self.tableView endUpdates];
- [self.refreshControl endRefreshing];
- if (scrollToBottom) {
- NSIndexPath *lastRow = [NSIndexPath indexPathForRow:self.messages.count - 1 inSection:0];
- [self.tableView scrollToRowAtIndexPath:lastRow atScrollPosition:UITableViewScrollPositionBottom animated:NO];
- }
- }];
+ self.dataLoading = NO;
+ if (self.messages.count == 0) {
+ [self.tableView reloadData];
+ }
+ [self.messages addObjectsFromArray:newMsgs];
+ NSMutableArray *indexPaths = [NSMutableArray new];
+ for (NSUInteger index = oldCount; index <= oldCount + [newMsgs count] - 1; index++) {
+ [indexPaths addObject:[NSIndexPath indexPathForRow:index inSection: 0]];
+ }
+ [self.tableView beginUpdates];
+ [self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:NO];
+ [self.tableView endUpdates];
+ [self.refreshControl endRefreshing];
+ if (self.shouldScrollToBottomOnRefresh) {
+ NSIndexPath *lastRow = [NSIndexPath indexPathForRow:self.messages.count - 1 inSection:0];
+ [self.tableView scrollToRowAtIndexPath:lastRow atScrollPosition:UITableViewScrollPositionBottom animated:NO];
+ }
+ }];
+ }
}
-(BOOL) isAtTop:(NSDictionary *)params {
@@ -94,6 +92,9 @@
self.messages = [NSMutableArray array];
self.refreshControl = [UIRefreshControl new];
[self.refreshControl addTarget:self action:@selector(refreshData) forControlEvents:UIControlEventValueChanged];
+ [self refreshData];
+ [self addObserver:self forKeyPath:@"path" options:0 context:nil];
+ [self addObserver:self forKeyPath:@"params" options:0 context:nil];
}
- (void) composePressed {
@@ -132,4 +133,8 @@
}
}
+- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
+ [self refreshData];
+}
+
@end