summaryrefslogtreecommitdiff
path: root/Juick/ViewControllers/DiscoverViewController.m
blob: d57947ebf6cbfc2f345bd9826505cdb498ab07d0 (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
118
119
120
121
122
123
124
125
126
127
128
129
//
//  DiscoverViewController.m
//  Juick
//
//  Created by Vitaly Takmazov on 03/12/2017.
//  Copyright © 2017 com.juick. All rights reserved.
//

#import "DiscoverViewController.h"
#import "ThreadViewController.h"
#import "MessageCell.h"
#import "APIClient.h"
#import "AppDelegate.h"
#import "ColorScheme.h"

@interface DiscoverViewController ()

@property(nonatomic, strong) AppDelegate *appDelegate;

@end

@implementation DiscoverViewController

-(void) viewDidLoad {
    [super viewDidLoad];
    self.messagesDelegate = self;
    if ([User isAuthenticated]) {
        [User checkIsValid:^(BOOL success) {
            if (success) {
                if ([self.path length] == 0) {
                    self.path = [APIClient feedUrl];
                    [self.tabBarItem setTitle:@"My feed"];
                    self.navigationController.visibleViewController.navigationItem.title = @"My feed";
                }
                [self refreshData:NO];
            } else {
                [User throwUnableToLogin:self];
            }
        }];
        
    } else {
        if ([self.path length] == 0) {
            self.path = [APIClient messagesUrl];
            [self.tabBarItem setTitle:@"Discover"];
            self.navigationController.visibleViewController.navigationItem.title = @"Discover";
        }
        [self refreshData:NO];
    }
    self.appDelegate = (AppDelegate *) [UIApplication sharedApplication].delegate;
}

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString * cellIdentifier = @"messageCell";
    MessageCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    [self performSegueWithIdentifier:@"threadViewSegue" sender:cell];
}

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqual: @"threadViewSegue"]) {
        NSNumber *mid;
        BOOL scrollToEnd = NO;
        if (self.appDelegate.pushedThread != nil) {
            mid = [self.appDelegate.pushedThread copy];
            self.appDelegate.pushedThread = nil;
            self.appDelegate.pushedUname = nil;
            scrollToEnd = YES;
        } else {
            Message *msg = [self.messages objectAtIndex:[self.tableView indexPathForSelectedRow].row];
            mid = msg.mid;
        }
        ThreadViewController *threadVC = (ThreadViewController *)segue.destinationViewController;
        [threadVC setPath:[APIClient threadUrl]];
        [threadVC setParams:@{@"mid": mid }];
        [threadVC refreshData:scrollToEnd];
    }
}
- (IBAction)filterAction:(id)sender {
    if (![User isAuthenticated]) {
        [self.navigationController performSegueWithIdentifier:@"loginSegue" sender:self.navigationController];
        return;
    }
    UIAlertController *filterAlert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    [filterAlert addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil]];
    [filterAlert addAction:[UIAlertAction actionWithTitle:@"My feed" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        self.path = [APIClient feedUrl];
        [self.tabBarItem setTitle:@"My feed"];
        self.navigationController.visibleViewController.navigationItem.title = @"My feed";
        self.params = nil;
        [self.messages removeAllObjects];
        [self.tableView reloadData];
        [self refreshData:NO];
    }]];
    [filterAlert addAction:[UIAlertAction actionWithTitle:@"Discover" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        self.path = [APIClient messagesUrl];
        [self.tabBarItem setTitle:@"Discover"];
        self.navigationController.visibleViewController.navigationItem.title = @"Discover";
        self.params = nil;
        [self.messages removeAllObjects];
        [self.tableView reloadData];
        [self refreshData:NO];
    }]];
    [filterAlert addAction:[UIAlertAction actionWithTitle:@"Discussions" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        self.path = [APIClient discussionsUrl];
        [self.tabBarItem setTitle:@"Discussions"];
        self.navigationController.visibleViewController.navigationItem.title = @"Discussions";
        self.params = nil;
        [self.messages removeAllObjects];
        [self.tableView reloadData];
        [self refreshData:NO];
    }]];
    [filterAlert.view setTintColor:[ColorScheme linkColor]];
    [self presentViewController:filterAlert animated:YES completion:nil];
}

-(void) loadMore {
    Message *lastMsg = [self.messages lastObject];
    if (lastMsg != nil) {
        if ([self.path isEqualToString:[APIClient discussionsUrl]]) {
            NSDate *msgDate = [[APIClient sharedClient].dateFormatter dateFromString:lastMsg.timestamp];
            self.params = [@{@"to" : [NSString stringWithFormat:@"%.0f", [msgDate timeIntervalSince1970] * 1000]} mutableCopy];
        } else if (![self.path isEqualToString:[APIClient threadUrl]]) {
            NSNumber *lastMid = lastMsg.mid;
            self.params = [@{@"before_mid":lastMid} mutableCopy];
        }
        [self refreshData:NO];
    }
}

@end