summaryrefslogtreecommitdiff
path: root/Juick/ViewControllers/JuickNavigationController.m
blob: 13838d1a956c43a44ce212fb562399eec4f04e94 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
//
//  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