From bf573afb9d05ce34ecd22b3dca85aca2c3896ae6 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Wed, 2 Oct 2019 17:16:01 +0300 Subject: Shared navigation --- Juick.xcodeproj/project.pbxproj | 6 + Juick/APIClient.m | 8 +- Juick/AppDelegate.h | 6 +- Juick/AppDelegate.m | 31 +++--- Juick/Main.storyboard | 129 ++++++---------------- Juick/ViewControllers/ChatViewController.m | 2 +- Juick/ViewControllers/DialogsViewController.m | 2 +- Juick/ViewControllers/DiscoverViewController.m | 1 + Juick/ViewControllers/DiscussionsController.m | 1 + Juick/ViewControllers/FeedViewController.m | 14 +-- Juick/ViewControllers/JuickNavigationController.h | 17 +++ Juick/ViewControllers/JuickNavigationController.m | 65 +++++++++++ Juick/ViewControllers/MessagesViewController.m | 13 +-- Juick/ViewControllers/ThreadViewController.m | 12 +- 14 files changed, 163 insertions(+), 144 deletions(-) create mode 100644 Juick/ViewControllers/JuickNavigationController.h create mode 100644 Juick/ViewControllers/JuickNavigationController.m diff --git a/Juick.xcodeproj/project.pbxproj b/Juick.xcodeproj/project.pbxproj index 8350c1f..c8bbfbd 100644 --- a/Juick.xcodeproj/project.pbxproj +++ b/Juick.xcodeproj/project.pbxproj @@ -40,6 +40,7 @@ 776C41BE1FD3EF180063B82E /* MessageCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = 776C41BC1FD3EF180063B82E /* MessageCell.xib */; }; 776C41C11FD3FF6E0063B82E /* FeedViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 776C41C01FD3FF6E0063B82E /* FeedViewController.m */; }; 778560602343D24E00BB37A2 /* NSData+Hex.m in Sources */ = {isa = PBXBuildFile; fileRef = 7785605F2343D24E00BB37A2 /* NSData+Hex.m */; }; + 778560632344CF6F00BB37A2 /* JuickNavigationController.m in Sources */ = {isa = PBXBuildFile; fileRef = 778560622344CF6F00BB37A2 /* JuickNavigationController.m */; }; 77975A1D182B6E9A00410C2B /* NewPostViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 77975A1C182B6E9A00410C2B /* NewPostViewController.m */; }; 77975A1F182BDCE900410C2B /* SystemConfiguration.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 77975A1E182BDCE900410C2B /* SystemConfiguration.framework */; }; 77975A21182BDCF300410C2B /* MobileCoreServices.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 77975A20182BDCF300410C2B /* MobileCoreServices.framework */; }; @@ -144,6 +145,8 @@ 776C41C01FD3FF6E0063B82E /* FeedViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = FeedViewController.m; sourceTree = ""; }; 7785605E2343D24E00BB37A2 /* NSData+Hex.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "NSData+Hex.h"; sourceTree = ""; }; 7785605F2343D24E00BB37A2 /* NSData+Hex.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "NSData+Hex.m"; sourceTree = ""; }; + 778560612344CF6F00BB37A2 /* JuickNavigationController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = JuickNavigationController.h; sourceTree = ""; }; + 778560622344CF6F00BB37A2 /* JuickNavigationController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = JuickNavigationController.m; sourceTree = ""; }; 77975A1B182B6E9A00410C2B /* NewPostViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NewPostViewController.h; sourceTree = ""; }; 77975A1C182B6E9A00410C2B /* NewPostViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NewPostViewController.m; sourceTree = ""; }; 77975A1E182BDCE900410C2B /* SystemConfiguration.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SystemConfiguration.framework; path = System/Library/Frameworks/SystemConfiguration.framework; sourceTree = SDKROOT; }; @@ -331,6 +334,8 @@ 77C36494224417E90017522C /* DiscoverViewController.m */, 77C36496224417FC0017522C /* DiscussionsController.h */, 77C36497224417FC0017522C /* DiscussionsController.m */, + 778560612344CF6F00BB37A2 /* JuickNavigationController.h */, + 778560622344CF6F00BB37A2 /* JuickNavigationController.m */, ); path = ViewControllers; sourceTree = ""; @@ -587,6 +592,7 @@ 77B8B39C207A5629005CB20C /* MessageInputView.m in Sources */, 77C3648C2241B3060017522C /* DeviceRegistration.m in Sources */, 774528C21F930C06004D110B /* Attachment.m in Sources */, + 778560632344CF6F00BB37A2 /* JuickNavigationController.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/Juick/APIClient.m b/Juick/APIClient.m index 848ea77..c7e22cf 100644 --- a/Juick/APIClient.m +++ b/Juick/APIClient.m @@ -175,10 +175,12 @@ - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler { if (challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodHTTPBasic) { - completionHandler(NSURLSessionAuthChallengeUseCredential, self.credential); - } else { - completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil); + if (self.isAuthenticated) { + completionHandler(NSURLSessionAuthChallengeUseCredential, self.credential); + return; + } } + completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil); } -(void) fetchDataWithURL:(NSURL *) url data:(NSData *)postData boundary:(NSString *)boundary callback:(void(^)(NSData *, NSError *))callback { diff --git a/Juick/AppDelegate.h b/Juick/AppDelegate.h index 04a903a..8b89131 100644 --- a/Juick/AppDelegate.h +++ b/Juick/AppDelegate.h @@ -22,12 +22,14 @@ extern NSString * const UserUpdatedNotificationName; +(AppDelegate *) shared; -+(ThreadViewController *) threadViewController; +-(void) presentThread:(UIViewController *)vc; +-(void) presentEditor:(UIViewController *)vc; +-(void) presentLoginView:(UIViewController *)vc; @property (strong, nonatomic) NSNumber *pushedThread; @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 3bdb774..fe1b0eb 100644 --- a/Juick/AppDelegate.m +++ b/Juick/AppDelegate.m @@ -85,18 +85,16 @@ NSString * const UserUpdatedNotificationName = @"UserUpdated"; NSDictionary *userInfo = response.notification.request.content.userInfo; [self parseNotificationPayload:userInfo]; [[NSOperationQueue mainQueue] addOperationWithBlock:^{ - UITabBarController *main = (UITabBarController *)self.window.rootViewController; + UITabBarController *main = (UITabBarController *)[self navigator]; if ([self.pushedThread integerValue] > 0) { [main setSelectedIndex:0]; - UINavigationController *discover = (UINavigationController *)[main.viewControllers objectAtIndex:0]; - [discover popToRootViewControllerAnimated:NO]; - MessagesViewController *msgVC = (MessagesViewController *)[discover.viewControllers objectAtIndex:0]; - [msgVC viewThreadForMessage:nil mid:self.pushedThread scrollTo:self.pushedReplyId]; + MessagesViewController *discover = (MessagesViewController *)[main.viewControllers objectAtIndex:0]; + [discover viewThreadForMessage:nil mid:self.pushedThread scrollTo:self.pushedReplyId]; [self cleanupPushedData]; } else { [main setSelectedIndex:2]; - UINavigationController *dialogs = (UINavigationController *)[main.viewControllers objectAtIndex:2]; - [[dialogs.viewControllers objectAtIndex:0] performSegueWithIdentifier:@"chatSegue" sender:dialogs]; + MessagesViewController *dialogs = (MessagesViewController *)[main.viewControllers objectAtIndex:2]; + [dialogs performSegueWithIdentifier:@"chatSegue" sender:dialogs]; } }]; completionHandler(); @@ -106,8 +104,8 @@ NSString * const UserUpdatedNotificationName = @"UserUpdated"; if (userInfo[@"service"]) { User *user = [User fromJSON:userInfo[@"user"]]; application.applicationIconBadgeNumber = user.unreadCount; - UITabBarController *main = (UITabBarController *)self.window.rootViewController; - UINavigationController *discussions = [main.viewControllers objectAtIndex:1]; + UITabBarController *main = (UITabBarController *)[self navigator]; + UIViewController *discussions = [main.viewControllers objectAtIndex:1]; if (user.unreadCount > 0) { [discussions tabBarItem].badgeColor = [UIColor colorNamed:@"Funny"]; [discussions tabBarItem].badgeValue = [NSString stringWithFormat:@"%ld", user.unreadCount]; @@ -142,13 +140,20 @@ NSString * const UserUpdatedNotificationName = @"UserUpdated"; return (AppDelegate *)[UIApplication sharedApplication].delegate; } -+ (id)threadViewController { - UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; - return [mainStoryboard instantiateViewControllerWithIdentifier:@"threadViewControllerIdentifier"]; +- (UINavigationController *) navigator { + return [[(UINavigationController *)self.window.rootViewController viewControllers] firstObject]; +} + +- (void)presentThread:(UIViewController *)vc { + [[self navigator] performSegueWithIdentifier:@"threadSegue" sender:vc]; +} + +- (void) presentEditor:(UIViewController *)vc { + [[self navigator] performSegueWithIdentifier:@"editorSegue" sender:vc]; } - (void) presentLoginView:(UIViewController *)vc { - [self.window.rootViewController performSegueWithIdentifier:@"loginSegue" sender:vc]; + [[self navigator] performSegueWithIdentifier:@"loginSegue" sender:vc]; } @end diff --git a/Juick/Main.storyboard b/Juick/Main.storyboard index 419e5a5..4ab443e 100644 --- a/Juick/Main.storyboard +++ b/Juick/Main.storyboard @@ -1,5 +1,5 @@ - + @@ -24,9 +24,6 @@ - - - @@ -41,24 +38,6 @@ - - - - - - - - - - - - - - - - - - @@ -84,11 +63,7 @@ - - - - - + @@ -214,23 +189,6 @@ - - - - - - - - - - - - - - - - - @@ -345,7 +303,7 @@ - + @@ -368,35 +326,60 @@ + - + + + + + + + + + + + + + + + + + - + + + + + + + + - - - - + + + + + - + @@ -419,50 +402,13 @@ + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -491,7 +437,4 @@ - - - diff --git a/Juick/ViewControllers/ChatViewController.m b/Juick/ViewControllers/ChatViewController.m index 989b531..4f9481c 100644 --- a/Juick/ViewControllers/ChatViewController.m +++ b/Juick/ViewControllers/ChatViewController.m @@ -21,7 +21,7 @@ - (void)viewDidLoad { [super viewDidLoad]; - [self.navigationController.visibleViewController setTitle:self.uname]; + [self.navigationController.navigationItem setTitle:self.uname]; [self.tableView registerNib:[UINib nibWithNibName:@"BubbleMessageCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"bubbleMessageCell"]; self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; self.tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive; diff --git a/Juick/ViewControllers/DialogsViewController.m b/Juick/ViewControllers/DialogsViewController.m index 1e5d3c2..31a4e8b 100644 --- a/Juick/ViewControllers/DialogsViewController.m +++ b/Juick/ViewControllers/DialogsViewController.m @@ -15,7 +15,7 @@ @implementation DialogsViewController - (void)viewDidLoad { [super viewDidLoad]; - self.navigationController.visibleViewController.navigationItem.title = @"Chats"; + self.navigationController.visibleViewController.title = @"Chats"; [self.view setBackgroundColor:[UIColor colorNamed:@"Background"]]; [self.tableView registerNib:[UINib nibWithNibName:@"ConversationCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"notificationCell"]; [self refreshData]; diff --git a/Juick/ViewControllers/DiscoverViewController.m b/Juick/ViewControllers/DiscoverViewController.m index 7bf9f74..2b69c6f 100644 --- a/Juick/ViewControllers/DiscoverViewController.m +++ b/Juick/ViewControllers/DiscoverViewController.m @@ -19,6 +19,7 @@ self.title = @"Discover"; self.path = [APIClient messagesUrl]; [self setShouldScrollToUnreadOnRefresh:NO]; + self.navigationController.visibleViewController.title = @"Discover"; [super viewDidLoad]; } diff --git a/Juick/ViewControllers/DiscussionsController.m b/Juick/ViewControllers/DiscussionsController.m index 607ac26..03226ae 100644 --- a/Juick/ViewControllers/DiscussionsController.m +++ b/Juick/ViewControllers/DiscussionsController.m @@ -20,6 +20,7 @@ self.title = @"Discussions"; self.path = [APIClient discussionsUrl]; [self setShouldScrollToUnreadOnRefresh:NO]; + self.navigationController.visibleViewController.title = @"Discussions"; [super viewDidLoad]; } diff --git a/Juick/ViewControllers/FeedViewController.m b/Juick/ViewControllers/FeedViewController.m index a5f7a43..f1fdb0a 100644 --- a/Juick/ViewControllers/FeedViewController.m +++ b/Juick/ViewControllers/FeedViewController.m @@ -25,7 +25,7 @@ NSString * const UserNotAuthenticatedNotificationName = @"UserNotAuthenticated"; -(void) viewDidLoad { self.messagesDelegate = self; - self.title = @"Today"; + self.navigationController.visibleViewController.title = @"Today"; [self refreshPath]; [self setShouldScrollToUnreadOnRefresh:NO]; [super viewDidLoad]; @@ -47,17 +47,7 @@ NSString * const UserNotAuthenticatedNotificationName = @"UserNotAuthenticated"; } } -- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender { - if ([identifier isEqualToString:@"newPostSegue"]) { - if ([[APIClient sharedClient] isAuthenticated]) { - return YES; - } else { - [[AppDelegate shared] presentLoginView:self]; - return NO; - } - } - return YES; -} + - (void)didReceiveChallenge { diff --git a/Juick/ViewControllers/JuickNavigationController.h b/Juick/ViewControllers/JuickNavigationController.h new file mode 100644 index 0000000..b508466 --- /dev/null +++ b/Juick/ViewControllers/JuickNavigationController.h @@ -0,0 +1,17 @@ +// +// JuickNavigationController.h +// Juick +// +// Created by Vitaly Takmazov on 02/10/2019. +// Copyright © 2019 com.juick. All rights reserved. +// + +#import + +NS_ASSUME_NONNULL_BEGIN + +@interface JuickNavigationController : UITabBarController +- (IBAction)newMessage:(id)sender; +@end + +NS_ASSUME_NONNULL_END diff --git a/Juick/ViewControllers/JuickNavigationController.m b/Juick/ViewControllers/JuickNavigationController.m new file mode 100644 index 0000000..a066dbd --- /dev/null +++ b/Juick/ViewControllers/JuickNavigationController.m @@ -0,0 +1,65 @@ +// +// JuickNavigationController.m +// Juick +// +// Created by Vitaly Takmazov on 02/10/2019. +// Copyright © 2019 com.juick. All rights reserved. +// + +#import "JuickNavigationController.h" + +#import "AppDelegate.h" +#import "APIClient.h" +#import "MessagesViewController.h" +#import "ThreadViewController.h" + +#import "Message.h" + +@interface JuickNavigationController () + + +@end + +@implementation JuickNavigationController + +- (void)viewDidLoad { + [super viewDidLoad]; + // Do any additional setup after loading the view. +} + +#pragma mark - Navigation + +- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender { + if ([identifier isEqualToString:@"editorSegue"]) { + if ([[APIClient sharedClient] isAuthenticated]) { + return YES; + } else { + [[AppDelegate shared] presentLoginView:self]; + return NO; + } + } + return YES; +} + +- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { + if ([segue.identifier isEqualToString:@"threadSegue"]) { + if ([sender isKindOfClass:[MessagesViewController class]]) { + MessagesViewController *vc = (MessagesViewController *) sender; + Message *msg = vc.messages[vc.tableView.indexPathForSelectedRow.row]; + ThreadViewController *threadVC = (ThreadViewController *)segue.destinationViewController; + [threadVC setMessages:[@[msg] mutableCopy]]; + [threadVC setParams:@{@"mid": msg.mid }]; + [threadVC setShouldScrollToUnreadOnRefresh:NO]; + } + /* + if (rid) { + [threadVC setFirstUnread:rid]; + [threadVC setShouldScrollToUnreadOnRefresh:YES]; + }*/ + } +} + +- (IBAction)newMessage:(id)sender { + [[AppDelegate shared] presentLoginView:self]; +} +@end diff --git a/Juick/ViewControllers/MessagesViewController.m b/Juick/ViewControllers/MessagesViewController.m index ad28484..5dfbf8f 100644 --- a/Juick/ViewControllers/MessagesViewController.m +++ b/Juick/ViewControllers/MessagesViewController.m @@ -161,18 +161,7 @@ NSString* const messageCellIdentifier = @"messageCell"; } -(void) viewThreadForMessage:(Message *)msg mid:(NSNumber *)mid scrollTo:(NSNumber *)rid { - ThreadViewController *threadVC = [AppDelegate threadViewController]; - if (msg) { - [threadVC setMessages:[@[msg] mutableCopy]]; - } - [threadVC setParams:@{@"mid": mid }]; - if (rid) { - [threadVC setFirstUnread:rid]; - [threadVC setShouldScrollToUnreadOnRefresh:YES]; - } else { - [threadVC setShouldScrollToUnreadOnRefresh:NO]; - } - [self.navigationController pushViewController:threadVC animated:YES]; + [[AppDelegate shared] presentThread:self]; } -(void) newMessage:(NSNotification *)obj { diff --git a/Juick/ViewControllers/ThreadViewController.m b/Juick/ViewControllers/ThreadViewController.m index 41e9503..60a115f 100644 --- a/Juick/ViewControllers/ThreadViewController.m +++ b/Juick/ViewControllers/ThreadViewController.m @@ -24,23 +24,21 @@ } -(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { - MessageCell *cell = [tableView dequeueReusableCellWithIdentifier:messageCellIdentifier forIndexPath:indexPath]; - if ([self shouldPerformSegueWithIdentifier:@"replySegue" sender:self]){ - [self performSegueWithIdentifier:@"replySegue" sender:cell]; + if ([self shouldPerformSegueWithIdentifier:@"editorSegue" sender:self]){ + [[AppDelegate shared] presentEditor:self]; } } -(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { - if ([segue.identifier isEqual: @"replySegue"]) { + if ([segue.identifier isEqual: @"editorSegue"]) { Message *msg = [self.messages objectAtIndex:[self.tableView indexPathForSelectedRow].row]; - NewPostViewController *postVC = (NewPostViewController *)[((UINavigationController *)segue.destinationViewController) - .viewControllers firstObject]; + NewPostViewController *postVC = (NewPostViewController *)segue.destinationViewController; [postVC setReplyTo:msg]; } } - (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender { - if ([identifier isEqualToString:@"replySegue"]) { + if ([identifier isEqualToString:@"editorSegue"]) { if ([[APIClient sharedClient] isAuthenticated]) { return YES; } else { -- cgit v1.2.3