-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMentionsViewController.m
247 lines (170 loc) · 7.69 KB
/
MentionsViewController.m
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
//
// MentionsViewController.m
// Twitter
//
// Created by Syed, Afzal on 2/28/15.
// Copyright (c) 2015 afzalsyed. All rights reserved.
//
#import "MentionsViewController.h"
#import "User.h"
#import "TwitterClient.h"
#import "Tweet.h"
#import "TweetCell.h"
#import "SVProgressHUD.h"
#import "ComposeTweetViewController.h"
#import "TweetDetailViewController.h"
#import "ProfileViewController.h"
@interface MentionsViewController ()<UITableViewDataSource, UITableViewDelegate, ComposeTweetViewControllerDelegate, TweetDetailViewControllerDelegate, TweetCellDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property(nonatomic, strong) NSArray *tweets;
@property (nonatomic, strong) UIRefreshControl *refreshControl;
@end
@implementation MentionsViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.tableView registerNib:[UINib nibWithNibName:@"TweetCell" bundle:nil] forCellReuseIdentifier:@"TweetCell"];
UIColor *myBlueColor = [UIColor colorWithRed:0.251 green:0.6 blue:1 alpha:1];
NSDictionary *textAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor],NSForegroundColorAttributeName,
[UIColor whiteColor],NSBackgroundColorAttributeName,nil];
self.navigationController.navigationBar.titleTextAttributes = textAttributes;
//self.tableView.estimatedRowHeight = 105;
self.tableView.rowHeight = UITableViewAutomaticDimension;
// Do any additional setup after loading the view from its nib.
self.title = @"Mentions";
self.navigationController.navigationBar.backgroundColor = myBlueColor;
self.navigationController.navigationBar.barTintColor = myBlueColor;
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"Sign Out" style:UIBarButtonItemStylePlain target:self action:@selector(onSignOutButton)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"New" style:UIBarButtonItemStylePlain target:self action:@selector(onNewButton)];
[self.navigationItem.rightBarButtonItem setTintColor:[UIColor whiteColor]];
[self.navigationItem.leftBarButtonItem setTintColor:[UIColor whiteColor]];
self.tableView.dataSource = self;
self.tableView.delegate = self;
self.refreshControl = [[UIRefreshControl alloc] init];
[self.refreshControl addTarget:self action:@selector(refreshTweets) forControlEvents:UIControlEventValueChanged];
[self.tableView insertSubview:self.refreshControl atIndex:0];
[self showLoadingHUD];
[self refreshTweets];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table Methods
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
TweetDetailViewController *tdvc = [[TweetDetailViewController alloc] init];
tdvc.delegate = self;
tdvc.tweet = self.tweets[indexPath.row];
UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:tdvc];
nvc.navigationBar.translucent = NO;
CATransition *tdvctransition = [CATransition animation];
tdvctransition.type = kCATransitionPush;
tdvctransition.subtype = kCATransitionFromRight;
[self.view.window.layer addAnimation:tdvctransition forKey:kCATransition];
[self.navigationController presentViewController:nvc animated:NO completion:nil];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.tweets.count;
}
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
TweetCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TweetCell"];
cell.delegate = self;
cell.tweet = self.tweets[indexPath.row];
if (indexPath.row == self.tweets.count - 1) {
[self grabMoreTweets];
}
return cell;
}
#pragma Mark Twitter Private Methods
- (void) onSignOutButton {
[User logout];
}
- (void)refreshTweets {
[[TwitterClient sharedInstance] mentionsTimelineWithParams:nil completion:^(NSArray *tweets, NSError *error) {
if (error) {
NSLog(@"There is an error: %@", [error userInfo]);
} else {
self.tweets = tweets;
[SVProgressHUD dismiss];
[self.refreshControl endRefreshing];
[self.tableView reloadData];
}
}];
}
- (void) onNewButton {
[self composeTweet:nil];
}
-(void) composeTweet:(TweetCell *)tweetCell{
ComposeTweetViewController *tvc = [[ComposeTweetViewController alloc] init];
tvc.delegate = self;
UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:tvc];
nvc.navigationBar.translucent = NO;
if(tweetCell != nil){
tvc.tweet = tweetCell.tweet;
}
[self.navigationController presentViewController:nvc animated:YES completion:nil];
}
- (void) grabMoreTweets {
// using this page https://dev.twitter.com/rest/public/timelines
NSString *lastTweetIdString = [self.tweets[self.tweets.count - 1] idString];
if (lastTweetIdString == nil) {
return;
}
NSMutableDictionary *params = [NSMutableDictionary dictionary];
params[@"max_id"] = lastTweetIdString;
[[TwitterClient sharedInstance] mentionsTimelineWithParams:params completion:^(NSArray *tweets, NSError *error) {
if (tweets != nil && tweets.count > 0) {
//NSLog(@"Got %ld more tweets", tweets.count);
NSMutableArray *allTweets = [NSMutableArray arrayWithArray:self.tweets];
[allTweets addObjectsFromArray:tweets];
self.tweets = [allTweets copy];
[self.tableView reloadData];
} else {
NSLog(@"Couldn't get more tweets");
}
if(error){
NSLog(@"Error: %@", [error userInfo]);
}
}];
}
#pragma mark ComposeTweetViewController delegate methods
- (void)tweetedThisTweet:(Tweet *)tweet {
NSMutableArray *allTweets = [NSMutableArray arrayWithArray:self.tweets];
[allTweets insertObject:tweet atIndex:0];
self.tweets = [allTweets copy];
[self.tableView reloadData];
}
#pragma mark TweetDetailViewControllerDelegate methods
// Reload the table view data, since we are doing
// everything locally, don't need to fetch from network
// on retweeting or favoriting
- (void)retweeted:(BOOL)retweeted {
[self.tableView reloadData];
}
- (void)favorited:(BOOL)favorited {
[self.tableView reloadData];
}
#pragma mark TweetCell Delegate methods
- (void)onReplyButton:(TweetCell *)tweetCell {
[self composeTweet:tweetCell];
}
- (void)onImageTapGesture:(TweetCell *)tweetCell {
ProfileViewController *pvc = [[ProfileViewController alloc] init];
//tdvc.delegate = self;
pvc.user = tweetCell.tweet.user;
UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:pvc];
nvc.navigationBar.translucent = NO;
CATransition *tdvctransition = [CATransition animation];
tdvctransition.type = kCATransitionPush;
tdvctransition.subtype = kCATransitionFromRight;
[self.view.window.layer addAnimation:tdvctransition forKey:kCATransition];
[self.navigationController presentViewController:nvc animated:NO completion:nil];
}
#pragma mark Private methods
- (void) showLoadingHUD {
[SVProgressHUD show];
[SVProgressHUD setBackgroundColor:[UIColor colorWithRed:0.251 green:0.6 blue:1 alpha:1]];
[SVProgressHUD showWithStatus:@"Loading"];
}
@end