From 2d6630dda9f7c3df0da956c4644f94342d2bb747 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Wed, 31 Oct 2018 00:13:21 +0300 Subject: Refresh data on path changes --- Juick/Supporting Files/Juick-Info.plist | 2 +- Juick/ViewControllers/DiscoverViewController.m | 28 ++++---- Juick/ViewControllers/MessagesViewController.h | 3 +- Juick/ViewControllers/MessagesViewController.m | 93 ++++++++++++++------------ Juick/ViewControllers/NewPostViewController.m | 4 +- 5 files changed, 70 insertions(+), 60 deletions(-) (limited to 'Juick') diff --git a/Juick/Supporting Files/Juick-Info.plist b/Juick/Supporting Files/Juick-Info.plist index 96c1d40..91fc22c 100644 --- a/Juick/Supporting Files/Juick-Info.plist +++ b/Juick/Supporting Files/Juick-Info.plist @@ -21,7 +21,7 @@ CFBundleSignature ???? CFBundleVersion - 75 + 76 ITSAppUsesNonExemptEncryption LSApplicationCategoryType diff --git a/Juick/ViewControllers/DiscoverViewController.m b/Juick/ViewControllers/DiscoverViewController.m index b09e1df..092c19c 100644 --- a/Juick/ViewControllers/DiscoverViewController.m +++ b/Juick/ViewControllers/DiscoverViewController.m @@ -23,7 +23,6 @@ @implementation DiscoverViewController -(void) viewDidLoad { - [super viewDidLoad]; self.messagesDelegate = self; if ([User isAuthenticated]) { [User checkIsValid:^(BOOL success) { @@ -33,7 +32,8 @@ [self.tabBarItem setTitle:@"My feed"]; self.navigationController.visibleViewController.navigationItem.title = @"My feed"; } - [self refreshData:NO]; + [self setShouldScrollToBottomOnRefresh:NO]; + [self refreshData]; } else { [User throwUnableToLogin:self]; } @@ -45,8 +45,10 @@ [self.tabBarItem setTitle:@"Discover"]; self.navigationController.visibleViewController.navigationItem.title = @"Discover"; } - [self refreshData:NO]; + [self setShouldScrollToBottomOnRefresh:NO]; + [self refreshData]; } + [super viewDidLoad]; self.appDelegate = (AppDelegate *) [UIApplication sharedApplication].delegate; } @@ -83,17 +85,19 @@ Message *msg = [self.messages objectAtIndex:[self.tableView indexPathForSelectedRow].row]; mid = msg.mid; } - ThreadViewController *threadVC = (ThreadViewController *)segue.destinationViewController; - [threadVC setPath:[APIClient threadUrl]]; - [threadVC setParams:@{@"mid": mid }]; - [threadVC refreshData:scrollToEnd]; + if ([mid integerValue] > 0) { + ThreadViewController *threadVC = (ThreadViewController *)segue.destinationViewController; + [threadVC setPath:[APIClient threadUrl]]; + [threadVC setParams:@{@"mid": mid }]; + [threadVC setShouldScrollToBottomOnRefresh:scrollToEnd]; + } } if ([segue.identifier isEqual: @"profileSegue"]) { BlogViewController *blogVC = (BlogViewController *)segue.destinationViewController; [blogVC setUname:self.selectedUser]; [blogVC setTitle:self.selectedUser]; blogVC.path = [NSString stringWithFormat:@"/messages?uname=%@", self.selectedUser]; - [blogVC refreshData:NO]; + [blogVC setShouldScrollToBottomOnRefresh:NO]; } } - (IBAction)filterAction:(id)sender { @@ -110,7 +114,7 @@ self.params = nil; [self.messages removeAllObjects]; [self.tableView reloadData]; - [self refreshData:NO]; + [self setShouldScrollToBottomOnRefresh:NO]; }]]; [filterAlert addAction:[UIAlertAction actionWithTitle:@"Discover" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { self.path = [APIClient messagesUrl]; @@ -119,7 +123,7 @@ self.params = nil; [self.messages removeAllObjects]; [self.tableView reloadData]; - [self refreshData:NO]; + [self setShouldScrollToBottomOnRefresh:NO]; }]]; [filterAlert addAction:[UIAlertAction actionWithTitle:@"Discussions" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { self.path = [APIClient discussionsUrl]; @@ -128,7 +132,7 @@ self.params = nil; [self.messages removeAllObjects]; [self.tableView reloadData]; - [self refreshData:NO]; + [self setShouldScrollToBottomOnRefresh:NO]; }]]; [filterAlert.view setTintColor:[ColorScheme linkColor]]; UIPopoverPresentationController *popover = [filterAlert popoverPresentationController]; @@ -150,7 +154,7 @@ NSNumber *lastMid = lastMsg.mid; self.params = [@{@"before_mid":lastMid} mutableCopy]; } - [self refreshData:NO]; + [self setShouldScrollToBottomOnRefresh:NO]; } } diff --git a/Juick/ViewControllers/MessagesViewController.h b/Juick/ViewControllers/MessagesViewController.h index 688336b..9478a17 100644 --- a/Juick/ViewControllers/MessagesViewController.h +++ b/Juick/ViewControllers/MessagesViewController.h @@ -16,8 +16,9 @@ @interface MessagesViewController : UITableViewController @property(nonatomic, strong) NSString *path; @property(nonatomic, strong) NSDictionary *params; +@property(nonatomic, assign) BOOL shouldScrollToBottomOnRefresh; + -(void) refreshData; --(void) refreshData:(BOOL)scrollToBottom; @property(nonatomic, strong) NSMutableArray *messages; @property(nonatomic, assign) Boolean dataLoading; 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 *)change context:(void *)context { + [self refreshData]; +} + @end diff --git a/Juick/ViewControllers/NewPostViewController.m b/Juick/ViewControllers/NewPostViewController.m index 3e2d17e..95672e8 100644 --- a/Juick/ViewControllers/NewPostViewController.m +++ b/Juick/ViewControllers/NewPostViewController.m @@ -46,13 +46,13 @@ [[APIClient sharedClient] postMessage:self.textView.text result:^(Message *msg, NSError *err) { self.navigationController.navigationItem.rightBarButtonItem.enabled = YES; [self.navigationController popViewControllerAnimated:YES]; - [(MessagesViewController *)self.navigationController.visibleViewController refreshData:YES]; + [(MessagesViewController *)self.navigationController.visibleViewController setShouldScrollToBottomOnRefresh:YES]; }]; } else { [[APIClient sharedClient] postReplyToThread:_replyTo.mid inReplyTo:_replyTo.rid text:self.textView.text result:^(Message *msg, NSError *err) { self.navigationController.navigationItem.rightBarButtonItem.enabled = YES; [self.navigationController popViewControllerAnimated:YES]; - [(MessagesViewController *)self.navigationController.visibleViewController refreshData:YES]; + [(MessagesViewController *)self.navigationController.visibleViewController setShouldScrollToBottomOnRefresh:YES]; }]; } } -- cgit v1.2.3