// // JuickNavigationController.m // Juick // // Created by Vitaly Takmazov on 02/10/2019. // Copyright © 2019 com.juick. All rights reserved. // #import "JuickNavigationController.h" #import "MessagesViewController.h" #import "ThreadViewController.h" #import "NewPostViewController.h" #import "Message.h" #import "LoginViewController.h" @interface JuickNavigationController () @property(nonatomic, strong) UIButton *avatarButton; -(void) applicationActivated; @end @implementation JuickNavigationController -(void) refreshStatus { [[AppDelegate shared].api me:^(User *user, NSError *err) { NSString *avatarUrl; if (err || !user) { avatarUrl = [API defaultAvatarUrl]; } else { avatarUrl = user.avatar; } UIViewController *discussions = [self.viewControllers objectAtIndex:1]; if (user.unreadCount > 0) { [discussions tabBarItem].badgeColor = [UIColor colorNamed:@"Funny"]; [discussions tabBarItem].badgeValue = [NSString stringWithFormat:@"%ld", user.unreadCount]; } else { [discussions tabBarItem].badgeValue = nil; } [[AppDelegate shared].api fetchImageWithURL:[NSURL URLWithString:avatarUrl] callback:^(NSData *data) { self.avatarButton = [UIButton buttonWithType:UIButtonTypeCustom]; [self.avatarButton addTarget:self action:@selector(showLoginForm:) forControlEvents:UIControlEventTouchUpInside]; [self.avatarButton setImage:[UIImage imageWithImage:[UIImage imageWithData:data] fitOutsideWidth:44 fitOutsideHeight:44] forState:UIControlStateNormal]; self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.avatarButton]; }]; }]; } - (void)viewDidLoad { [super viewDidLoad]; [self refreshStatus]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userDidSignedIn:) name:UserChangedNotificationName object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationActivated) name:UIApplicationDidBecomeActiveNotification object:nil]; } - (void)userDidSignedIn:(NSNotification *) notification { [self refreshStatus]; } #pragma mark - Navigation - (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender { if ([identifier isEqualToString:@"editorSegue"]) { if ([[AppDelegate shared].api currentUser]) { return YES; } else { [[AppDelegate shared] presentLoginView:self]; return NO; } } return YES; } - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([segue.identifier isEqualToString:@"threadSegue"]) { ThreadViewController *threadVC = (ThreadViewController *)segue.destinationViewController; [threadVC setShouldScrollToUnreadOnRefresh:NO]; NSNumber *pushedThread = [AppDelegate shared].pushedThread; if (pushedThread) { [threadVC setParams:@{@"mid": pushedThread }]; if ([AppDelegate shared].pushedReplyId) { [threadVC setFirstUnread:[AppDelegate shared].pushedReplyId]; [threadVC setShouldScrollToUnreadOnRefresh:YES]; } } else { MessagesViewController *vc = (MessagesViewController *) sender; Message *msg = vc.messages[vc.tableView.indexPathForSelectedRow.row]; [threadVC setMessages:[@[msg] mutableCopy]]; [threadVC setParams:@{@"mid": msg.mid }]; [threadVC setShouldScrollToUnreadOnRefresh:NO]; } } if ([segue.identifier isEqualToString:@"editorSegue"]) { if ([sender isKindOfClass:[ThreadViewController class]]) { ThreadViewController *thread = (ThreadViewController *) sender; Message *msg = [thread.messages objectAtIndex:[thread.tableView indexPathForSelectedRow].row]; NewPostViewController *postVC = [[((UINavigationController *)segue.destinationViewController) viewControllers] firstObject]; [postVC setReplyTo:msg]; } } } - (IBAction)showLoginForm:(id)sender { [self performSegueWithIdentifier:@"loginSegue" sender:self]; } - (void)applicationActivated { [self refreshStatus]; } - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil]; } @end