From 33539a3c9c8534776722aa1148499f984e0019e7 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Wed, 2 Oct 2019 13:59:51 +0300 Subject: Move loginSegue to Tab bar controller --- Juick/APIClient.m | 9 ++++++++- Juick/AppDelegate.h | 2 ++ Juick/AppDelegate.m | 4 ++++ Juick/Main.storyboard | 2 +- Juick/ViewControllers/FeedViewController.m | 4 +++- Juick/ViewControllers/LoginViewController.m | 1 + Juick/ViewControllers/ThreadViewController.m | 17 ++++++++++++++++- 7 files changed, 35 insertions(+), 4 deletions(-) diff --git a/Juick/APIClient.m b/Juick/APIClient.m index 4652c0c..848ea77 100644 --- a/Juick/APIClient.m +++ b/Juick/APIClient.m @@ -19,6 +19,8 @@ @implementation APIClient +@synthesize credential = _credential; + +(APIClient *) sharedClient { static APIClient *sharedAPIClient = nil; static dispatch_once_t onceToken; @@ -157,7 +159,12 @@ } - (void)setCredential:(NSURLCredential *)credential { - [[NSURLCredentialStorage sharedCredentialStorage] setCredential:credential forProtectionSpace:self.apiProtectionSpace]; + if (credential) { + [[NSURLCredentialStorage sharedCredentialStorage] removeCredential:_credential forProtectionSpace:self.apiProtectionSpace]; + [[NSURLCredentialStorage sharedCredentialStorage] setCredential:credential forProtectionSpace:self.apiProtectionSpace]; + } else { + [[NSURLCredentialStorage sharedCredentialStorage] removeCredential:_credential forProtectionSpace:self.apiProtectionSpace]; + } } diff --git a/Juick/AppDelegate.h b/Juick/AppDelegate.h index 5357af2..04a903a 100644 --- a/Juick/AppDelegate.h +++ b/Juick/AppDelegate.h @@ -28,4 +28,6 @@ extern NSString * const UserUpdatedNotificationName; @property (strong, nonatomic) NSString *pushedUname; @property (strong, nonatomic) NSNumber *pushedReplyId; +- (void) presentLoginView:(UIViewController *)vc; + @end diff --git a/Juick/AppDelegate.m b/Juick/AppDelegate.m index 61c8da5..3bdb774 100644 --- a/Juick/AppDelegate.m +++ b/Juick/AppDelegate.m @@ -147,4 +147,8 @@ NSString * const UserUpdatedNotificationName = @"UserUpdated"; return [mainStoryboard instantiateViewControllerWithIdentifier:@"threadViewControllerIdentifier"]; } +- (void) presentLoginView:(UIViewController *)vc { + [self.window.rootViewController performSegueWithIdentifier:@"loginSegue" sender:vc]; +} + @end diff --git a/Juick/Main.storyboard b/Juick/Main.storyboard index 00ee720..419e5a5 100644 --- a/Juick/Main.storyboard +++ b/Juick/Main.storyboard @@ -52,7 +52,6 @@ - @@ -390,6 +389,7 @@ + diff --git a/Juick/ViewControllers/FeedViewController.m b/Juick/ViewControllers/FeedViewController.m index 31bf0ca..a5f7a43 100644 --- a/Juick/ViewControllers/FeedViewController.m +++ b/Juick/ViewControllers/FeedViewController.m @@ -14,6 +14,8 @@ #import "AppDelegate.h" #import "LoginViewController.h" +NSString * const UserNotAuthenticatedNotificationName = @"UserNotAuthenticated"; + @interface FeedViewController () @property NSString *selectedUser; @@ -50,7 +52,7 @@ if ([[APIClient sharedClient] isAuthenticated]) { return YES; } else { - [self.navigationController performSegueWithIdentifier:@"loginSegue" sender:self]; + [[AppDelegate shared] presentLoginView:self]; return NO; } } diff --git a/Juick/ViewControllers/LoginViewController.m b/Juick/ViewControllers/LoginViewController.m index f9b17a3..55099eb 100644 --- a/Juick/ViewControllers/LoginViewController.m +++ b/Juick/ViewControllers/LoginViewController.m @@ -71,6 +71,7 @@ NSString * const UserSignedInNotificationName = @"UserSignedIn"; [self.navigationController dismissViewControllerAnimated:NO completion:nil]; } else { [User throwUnableToLogin:self error:error]; + [[APIClient sharedClient] setCredential:nil]; } }]; } diff --git a/Juick/ViewControllers/ThreadViewController.m b/Juick/ViewControllers/ThreadViewController.m index af56bc3..41e9503 100644 --- a/Juick/ViewControllers/ThreadViewController.m +++ b/Juick/ViewControllers/ThreadViewController.m @@ -10,6 +10,7 @@ #import "NewPostViewController.h" #import "MessageCell.h" #import "APIClient.h" +#import "AppDelegate.h" @implementation ThreadViewController @@ -24,7 +25,9 @@ -(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { MessageCell *cell = [tableView dequeueReusableCellWithIdentifier:messageCellIdentifier forIndexPath:indexPath]; - [self performSegueWithIdentifier:@"replySegue" sender:cell]; + if ([self shouldPerformSegueWithIdentifier:@"replySegue" sender:self]){ + [self performSegueWithIdentifier:@"replySegue" sender:cell]; + } } -(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { @@ -36,6 +39,18 @@ } } +- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender { + if ([identifier isEqualToString:@"replySegue"]) { + if ([[APIClient sharedClient] isAuthenticated]) { + return YES; + } else { + [[AppDelegate shared] presentLoginView:self]; + return NO; + } + } + return YES; +} + -(void) loadMore { Message *lastMsg = [self.messages lastObject]; [self setShouldScrollToUnreadOnRefresh:lastMsg.rid > 0]; -- cgit v1.2.3