From 5f4a09ec689cb04ce71b4eb7425936f04a70f6f4 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Fri, 22 Mar 2019 00:33:30 +0300 Subject: Fix sign in --- Juick/APIClient.m | 5 +- Juick/Main.storyboard | 6 +-- Juick/ViewControllers/DialogsViewController.m | 17 +++++++ Juick/ViewControllers/FeedViewController.m | 64 +++++++++----------------- Juick/ViewControllers/MessagesViewController.h | 1 + Juick/ViewControllers/MessagesViewController.m | 6 ++- 6 files changed, 52 insertions(+), 47 deletions(-) (limited to 'Juick') diff --git a/Juick/APIClient.m b/Juick/APIClient.m index 869e944..e2ef67d 100644 --- a/Juick/APIClient.m +++ b/Juick/APIClient.m @@ -67,7 +67,10 @@ } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { NSLog(@"REST Error: %@", error); [[NSOperationQueue mainQueue] addOperationWithBlock:^{ - callback(nil, error); + NSInteger statusCode = ((NSHTTPURLResponse *)task.response).statusCode; + if (statusCode == 401) { + callback(nil, [NSError errorWithDomain:@"JuickErrorDomain" code:401 userInfo:nil]); + } }]; }]; }]; diff --git a/Juick/Main.storyboard b/Juick/Main.storyboard index c2c1717..b65af91 100644 --- a/Juick/Main.storyboard +++ b/Juick/Main.storyboard @@ -89,7 +89,7 @@ - + @@ -286,7 +286,7 @@ - + @@ -483,6 +483,6 @@ - + diff --git a/Juick/ViewControllers/DialogsViewController.m b/Juick/ViewControllers/DialogsViewController.m index cbf0024..c2296dd 100644 --- a/Juick/ViewControllers/DialogsViewController.m +++ b/Juick/ViewControllers/DialogsViewController.m @@ -36,6 +36,10 @@ [self.tableView beginUpdates]; [self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:YES]; [self.tableView endUpdates]; + self.tableView.backgroundView = [UIView new]; + } else { + [self setEmptyMessageView:@"Sign in to view chats"]; + [self.tableView reloadData]; } [self.refreshControl endRefreshing]; }]; @@ -77,5 +81,18 @@ } } +- (void) setEmptyMessageView:(NSString *)message { + CGRect rect = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height); + UILabel *messageLabel = [[UILabel alloc] initWithFrame:rect]; + messageLabel.text = message; + messageLabel.numberOfLines = 0; + messageLabel.textAlignment = NSTextAlignmentCenter; + messageLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleCallout]; + messageLabel.textColor = [UIColor colorNamed:@"Muted"]; + [messageLabel sizeToFit]; + self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; + [self.tableView setBackgroundView:messageLabel]; +} + @end diff --git a/Juick/ViewControllers/FeedViewController.m b/Juick/ViewControllers/FeedViewController.m index b6b578f..f6e36f4 100644 --- a/Juick/ViewControllers/FeedViewController.m +++ b/Juick/ViewControllers/FeedViewController.m @@ -23,7 +23,12 @@ -(void) viewDidLoad { self.messagesDelegate = self; self.title = @"Today"; - self.path = [APIClient feedUrl]; + if ([[APIClient sharedClient] isAuthenticated]) { + self.path = [APIClient feedUrl]; + } else { + self.path = [APIClient messagesUrl]; + self.params = @{@"popular": @1}; + } [self setShouldScrollToBottomOnRefresh:NO]; [super viewDidLoad]; } @@ -32,47 +37,6 @@ self.selectedUser = uname; [self performSegueWithIdentifier:@"profileSegue" sender:self]; } -/* -- (IBAction)filterAction:(id)sender { - if (![[APIClient sharedClient] isAuthenticated]) { - [self.navigationController performSegueWithIdentifier:@"loginSegue" sender:self.navigationController]; - return; - } - UIAlertController *filterAlert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet]; - [filterAlert addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil]]; - [filterAlert addAction:[UIAlertAction actionWithTitle:@"My feed" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { - self.path = [APIClient feedUrl]; - self.navigationController.visibleViewController.navigationItem.title = @"My feed"; - self.params = nil; - [self.messages removeAllObjects]; - [self.tableView reloadData]; - [self setShouldScrollToBottomOnRefresh:NO]; - }]]; - [filterAlert addAction:[UIAlertAction actionWithTitle:@"Discover" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { - self.path = [APIClient messagesUrl]; - self.navigationController.visibleViewController.navigationItem.title = @"Discover"; - self.params = nil; - [self.messages removeAllObjects]; - [self.tableView reloadData]; - [self setShouldScrollToBottomOnRefresh:NO]; - }]]; - [filterAlert addAction:[UIAlertAction actionWithTitle:@"Discussions" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { - self.path = [APIClient discussionsUrl]; - self.navigationController.visibleViewController.navigationItem.title = @"Discussions"; - self.params = nil; - [self.messages removeAllObjects]; - [self.tableView reloadData]; - [self setShouldScrollToBottomOnRefresh:NO]; - }]]; - [filterAlert.view setTintColor:[ColorScheme linkColor]]; - UIPopoverPresentationController *popover = [filterAlert popoverPresentationController]; - if (popover) { - popover.sourceView = self.view; - popover.sourceRect = CGRectMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds), 0, 0); - popover.permittedArrowDirections = UIPopoverArrowDirectionUp; - } - [self presentViewController:filterAlert animated:YES completion:nil]; -}*/ -(void) loadMore { Message *lastMsg = [self.messages lastObject]; @@ -88,4 +52,20 @@ } } +- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender { + if ([identifier isEqualToString:@"newPostSegue"]) { + if ([[APIClient sharedClient] isAuthenticated]) { + return YES; + } else { + [self.navigationController performSegueWithIdentifier:@"loginSegue" sender:self]; + return NO; + } + } + return YES; +} + +- (void)didReceiveChallenge { + +} + @end diff --git a/Juick/ViewControllers/MessagesViewController.h b/Juick/ViewControllers/MessagesViewController.h index c8eb4cf..8f41af7 100644 --- a/Juick/ViewControllers/MessagesViewController.h +++ b/Juick/ViewControllers/MessagesViewController.h @@ -12,6 +12,7 @@ extern NSString* const messageCellIdentifier; @protocol MessagesDelegate +-(void) didReceiveChallenge; -(void) loadMore; @end diff --git a/Juick/ViewControllers/MessagesViewController.m b/Juick/ViewControllers/MessagesViewController.m index 7029beb..88bee93 100644 --- a/Juick/ViewControllers/MessagesViewController.m +++ b/Juick/ViewControllers/MessagesViewController.m @@ -33,7 +33,11 @@ NSString* const messageCellIdentifier = @"messageCell"; } [[APIClient sharedClient] pullNextFromPath:self.path params:self.params callback:^(NSArray *next, NSError *err) { if (err) { - [User throwUnableToLogin:self error:err]; + if ([err.domain isEqual: @"JuickErrorDomain"] && err.code == 401) { + [self.messagesDelegate didReceiveChallenge]; + } else { + [User throwUnableToLogin:self error:err]; + } return; } NSArray *newMsgs = next; -- cgit v1.2.3