summaryrefslogtreecommitdiff
path: root/Juick/ViewControllers/JuickNavigationController.m
blob: 86738665b58533a80c2a99a3c67142508b31434b (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
//
//  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 "NewPostViewController.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"]) {
        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)newMessage:(id)sender {
    [[AppDelegate shared] presentLoginView:self];
}
@end