summaryrefslogtreecommitdiff
path: root/Juick/ViewControllers/DiscoverViewController.m
blob: b0d965c8420c10b7c77aca11647eeb4156c156f2 (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
//
//  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"

@interface DiscoverViewController ()

@end

@implementation DiscoverViewController

AppDelegate *appDelegate;

-(void) viewDidLoad {
    [super viewDidLoad];
    [self.navigationController.navigationBar setPrefersLargeTitles:YES];
    if ([self.path isEqualToString:[APIClient feedUrl]]) {
        [self setTitle:@"My feed"];
    } else {
        [self setTitle:@"Discover"];
    }
    appDelegate = (AppDelegate *) [UIApplication sharedApplication].delegate;
    if (appDelegate.pushedThread != nil) {
        [self performSegueWithIdentifier:@"threadViewSegue" sender:self];
    }
}

-(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;
        if (appDelegate.pushedThread != nil) {
            mid = appDelegate.pushedThread;
            appDelegate.pushedThread = nil;
        } 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 } mutableCopy]];
    }
}
- (IBAction)filterAction:(id)sender {
    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.messages removeAllObjects];
        [self.tableView reloadData];
        [self refreshData:NO];
    }]];
    [filterAlert addAction:[UIAlertAction actionWithTitle:@"Discover" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        self.path = [APIClient messagesUrl];
        [self.messages removeAllObjects];
        [self.tableView reloadData];
        [self refreshData:NO];
    }]];
    [self presentViewController:filterAlert animated:YES completion:nil];
}

@end