summaryrefslogtreecommitdiff
path: root/Juick/ViewControllers/JuickNavigationController.m
blob: aaf37655a98633c018820092df5377a31d7c2dae (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
//
//  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;

@end

@implementation JuickNavigationController


-(void) refreshStatus {
    [[AppDelegate shared].api me:^(User *user, NSError *err) {
        NSString *avatarUrl;
        if (err || !user) {
            avatarUrl = @"https://i.juick.com/av-96.png";
        } else {
            avatarUrl = user.avatar;
        }
        [[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] fitInsideWidth:44 fitInsideHeight: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];
}

- (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];
}
@end