summaryrefslogtreecommitdiff
path: root/Juick/RevealPanelViewController.m
blob: d890f973f16d46d558b57c70115ae57f2cfeada4 (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//
//  RevealPanelViewController.m
//  Juick
//
//  Created by Vitaly Takmazov on 04.11.13.
//  Copyright (c) 2013 com.juick. All rights reserved.
//

#import "SWRevealViewController.h"

#import "RevealPanelViewController.h"
#import "MessagesViewController.h"

#import "ColorScheme.h"
#import "NSURL+PathParameters.h"
#import "NavCell.h"
#import "Message.h"
#import "User.h"


static NSString *CellIdentifier = @"NavCell";

@interface RevealPanelViewController ()

@end

@implementation RevealPanelViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.view setBackgroundColor:[ColorScheme mainBackground]];
    self.tableView = [[UITableView alloc] init];
    [self.tableView setBackgroundColor:[ColorScheme mainBackground]];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView setSeparatorInset:UIEdgeInsetsZero];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    self.tableView.separatorColor =[UIColor darkGrayColor];
    [self.tableView registerNib:[UINib nibWithNibName:@"NavCell" bundle:nil] forCellReuseIdentifier:CellIdentifier];
    [self.view addSubview:self.tableView];
    self.signButton = [[UIButton alloc] init];
    [self.signButton addTarget:self action:@selector(signInOut) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.signButton];
    [self refreshInfo];
}

-(void) refreshInfo {
    NSString *text;
    UIColor *color;
    
    if ([User isAuthenticated]) {
        text = @"Sign Out";
        color = [UIColor redColor];
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
            [User get:[[PDKeychainBindings sharedKeychainBindings] stringForKey:@"com.juick.username"] callback:^(User *user) {
                if (user != nil) {
                    /*[self.titleView.image setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://i.juick.com/as/%@.png", user.uid]]];
                    [self.titleView.title setText:user.uname];*/
                }
            }];
        });
    } else {
        text = @"Sign In";
        color = [UIColor greenColor];
    }
    [self.signButton setTitle:text forState:UIControlStateNormal];
    [self.signButton setBackgroundColor:color];
}

-(void) signInOut {
    [self refreshInfo];
    [self pushLoginForm];    
}

-(void) viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    self.tableView.frame = CGRectMake(0, 0, self.view.bounds.size.width, 400);
    self.signButton.frame = CGRectMake(0, self.view.bounds.size.height - 40, self.view.bounds.size.width, 40);
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 4;
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 32 + 10*2; // icon size + insets
}

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NavCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    NSInteger row = indexPath.row;
    [cell.contentView setBackgroundColor:[UIColor whiteColor]];
    cell.icon.font = [UIFont fontWithName:kFontAwesomeFamilyName size:32.f];
    cell.icon.textColor = [ColorScheme linkColor];
    cell.descriptionText.textColor = [ColorScheme linkColor];
    if (row == 0) {
        cell.descriptionText.text = @"My feed";
        cell.icon.text = [NSString fontAwesomeIconStringForEnum:FAIconHome];
        
    } else if (row == 1) {
        cell.descriptionText.text = @"Popular";
        cell.icon.text = [NSString fontAwesomeIconStringForEnum:FAIconComments];
    } else if (row == 2) {
        cell.descriptionText.text = @"Discover";
        cell.icon.text = [NSString fontAwesomeIconStringForEnum:FAIconSearch];
    } else if (row == 3) {
        cell.descriptionText.text = @"Images";
        cell.icon.text = [NSString fontAwesomeIconStringForEnum:FAIconPicture];
    }
    UIView *bgColorView = [[UIView alloc] init];
    bgColorView.backgroundColor = [UIColor blackColor];
    bgColorView.layer.masksToBounds = YES;
    [cell setSelectedBackgroundView:bgColorView];
    [cell.contentView setNeedsLayout];
    [cell.contentView layoutIfNeeded];
    return cell;
}

- (void) pushLoginForm {
    [self.revealViewController revealToggle:self];
    UIViewController *loginView = [[LoginViewController alloc] init];
    UINavigationController *messages = (UINavigationController *)self.revealViewController.frontViewController;
    [messages pushViewController:loginView animated:YES];
}

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger row = indexPath.row;
    NSString *targetPath;
    NSDictionary *targetParams;
    NSString *targetTitle;
    if (row == 0) {
        if ([[PDKeychainBindings sharedKeychainBindings] stringForKey:@"com.juick.username"] == nil) {
            [self pushLoginForm];
            return;
        } else {
            targetTitle = @"My feed";
            targetPath = [Message feedUrl];
        }
        
    }
    if (row == 1) {
        targetTitle = @"Popular";
        targetPath = [Message messagesUrl];
        targetParams = [NSDictionary dictionaryWithObjectsAndKeys:@"1", @"popular", nil];
    } else if (row == 2) {
        targetTitle = @"Discover";
        targetPath = [Message messagesUrl];
    }
    if (row == 3) {
        targetTitle = @"Images";
        targetPath = [Message messagesUrl];
        targetParams = [NSDictionary dictionaryWithObjectsAndKeys:@"photo", @"media", nil];
    }
    SWRevealViewController *reveal = self.revealViewController;
    UINavigationController *front = (UINavigationController *)reveal.frontViewController;
    MessagesViewController *messages = (MessagesViewController *)[front.viewControllers objectAtIndex:0];
    [messages loadFromPath:targetPath withParams:targetParams withTitle:targetTitle];
    [reveal revealToggle:self];
}

@end