-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMentionsViewController.m
More file actions
242 lines (197 loc) · 9.56 KB
/
MentionsViewController.m
File metadata and controls
242 lines (197 loc) · 9.56 KB
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
//
// MentionsViewController.m
// TwitterClient
//
// Created by Jessica Ko on 4/7/14.
// Copyright (c) 2014 Jessica Ko. All rights reserved.
//
#import "MentionsViewController.h"
#import "Client.h"
#import "NewTweetViewController.h"
#import "TweetCell.h"
#import "MBProgressHUD.h"
#import "DetailViewController.h"
#import "Tweet.h"
#import <QuartzCore/QuartzCore.h>
#import "User.h"
#import "MenuSliderViewController.h"
#import "MyProfileViewController.h"
@interface MentionsViewController ()
-(void) reload;
-(void) fetchMoreTweets:(NSString *)max_id;
-(void) onFavoriteButton:(id)sender;
-(void) onRetweetButton:(id)sender;
-(void) onReplyButton:(id)sender;
-(void)refresh:(UIRefreshControl *)refreshControl;
@end
@implementation MentionsViewController
@synthesize delegate = _delegate;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.tweets.dataSource = self;
self.tweets.delegate = self;
self.delegate = _delegate;
UINib *customNib = [UINib nibWithNibName:@"TweetCell" bundle:nil];
[self.tweets registerNib:customNib forCellWithReuseIdentifier:@"TweetCell"];
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
[refreshControl addTarget:self action:@selector(refresh:) forControlEvents:UIControlEventValueChanged];
[self.tweets addSubview:refreshControl];
NSLog(@"%@", @"View Load");
[self reload];
}
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
NSLog(@"%@", @"View appear");
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return [self.currentTweets count];
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
DetailViewController *detailview = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
detailview.savedTweets = self.currentTweets;
detailview.tweet = [self.currentTweets objectAtIndex:indexPath.row];
[self.navigationController pushViewController:detailview animated:YES];
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
NSString *name = [self.currentTweets objectAtIndex:indexPath.row][@"text"];
CGSize maximumLabelSize = CGSizeMake(230,9999);
UIFont *font=[UIFont systemFontOfSize:13];
CGRect textRect = [name boundingRectWithSize:maximumLabelSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil];
return CGSizeMake(320, textRect.size.height + 75);
}
- (void)sender:(TweetCell *)sender didTap:(NSString *)tweetId {
MyProfileViewController *modalViewController = [[MyProfileViewController alloc] init];
modalViewController.screenId = tweetId;
[self.navigationController presentViewController:modalViewController animated:YES completion:nil];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
TweetCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TweetCell" forIndexPath:indexPath];
//NSLog(@"%@", [[self.currentTweets objectAtIndex:indexPath.row] objectForKey:@"text"]);
NSDictionary *twt = [self.currentTweets objectAtIndex:indexPath.row];
[cell cellWithTweet:twt];
cell.tContent.numberOfLines = 0;
[cell.tContent sizeToFit];
BOOL favorited = [[[self.currentTweets objectAtIndex:indexPath.row] objectForKey:@"favorited"] boolValue];
if (favorited) {
[cell.favoriteButton setImage:[UIImage imageNamed:@"ic_star_yellow_8.png"] forState:UIControlStateNormal];
}
BOOL retweeted = [[[self.currentTweets objectAtIndex:indexPath.row] objectForKey:@"retweeted"] boolValue];
if (retweeted) {
[cell.reTweetButton setTitleColor:[UIColor colorWithRed:244.f/255.f green:180.f/255.f blue:0 alpha:1.f] forState:UIControlStateNormal];
}
cell.favoriteButton.tag = indexPath.row;
[cell.favoriteButton addTarget:self action:@selector(onFavoriteButton:) forControlEvents:UIControlEventTouchUpInside];
cell.reTweetButton.tag = indexPath.row;
[cell.reTweetButton addTarget:self action:@selector(onRetweetButton:) forControlEvents:UIControlEventTouchUpInside];
cell.replyButton.tag = indexPath.row;
[cell.replyButton addTarget:self action:@selector(onReplyButton:) forControlEvents:UIControlEventTouchUpInside];
if (indexPath.row == ([self.currentTweets count] - 1)) {
[self fetchMoreTweets:[[self.currentTweets objectAtIndex:([self.currentTweets count] - 1)] objectForKey:@"id"]];
}
cell.delegate = self;
return cell;
}
- (void) reload {
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
Client *client = [Client instance];
[client mentionsTimelineWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
self.currentTweets = [Tweet tweetsWithArray:responseObject];
[self.tweets reloadData];
[MBProgressHUD hideHUDForView:self.view animated:YES];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"%@", error);
[MBProgressHUD hideHUDForView:self.view animated:YES];
}];
}
- (void)refresh:(UIRefreshControl *)refreshControl {
[refreshControl endRefreshing];
[self reload];
}
- (void) fetchMoreTweets:(NSString *)max_id {
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
Client *client = [Client instance];
NSDictionary *param = [[NSDictionary alloc] initWithObjectsAndKeys:max_id, @"max_id", nil];
[client nexthomeTimelineWithSuccess:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSMutableArray *newTweets = [Tweet tweetsWithArray:responseObject];
[self.tweets performBatchUpdates:^{
int resultsSize = [self.currentTweets count];
[self.currentTweets addObjectsFromArray:newTweets];
for (int i = resultsSize; i < resultsSize + newTweets.count; i++) {
NSMutableArray *arrayWithIndexPaths = [NSMutableArray array];
[arrayWithIndexPaths addObject:[NSIndexPath indexPathForRow:i inSection:0]];
[self.tweets insertItemsAtIndexPaths:arrayWithIndexPaths];
}
[MBProgressHUD hideHUDForView:self.view animated:YES];
} completion:nil];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[MBProgressHUD hideHUDForView:self.view animated:YES];
}];
}
- (void) onFavoriteButton:(id)sender {
NSInteger tid = ((UIControl *) sender).tag;
//NSLog(@"%d", tid);
BOOL favorited = [[[self.currentTweets objectAtIndex:tid] objectForKey:@"favorited"] boolValue];
NSString *tweetId = [[self.currentTweets objectAtIndex:tid] objectForKey:@"id"];
NSDictionary *param = [[NSDictionary alloc] initWithObjectsAndKeys:tweetId, @"id", nil];
Client *client = [Client instance];
if (favorited) {
[sender setImage:[UIImage imageNamed:@"ic_star_outline_grey_8.png"] forState:UIControlStateNormal];
[client destoryFavoriteTweetWithSuccess:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
}];
} else {
[sender setImage:[UIImage imageNamed:@"ic_star_yellow_8.png"] forState:UIControlStateNormal];
[client favoriteTweetWithSuccess:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
}];
}
}
- (void) onRetweetButton:(id)sender {
NSInteger tid = ((UIControl *) sender).tag;
NSString *tweetId = [[self.currentTweets objectAtIndex:tid] objectForKey:@"id"];
Client *client = [Client instance];
NSString *retwtUrl = [NSString stringWithFormat:@"1.1/statuses/retweet/%@.json", tweetId];
BOOL retweeted = [[[self.currentTweets objectAtIndex:tid] objectForKey:@"retweeted"] boolValue];
if (!retweeted) {
[client retweetWithSuccess:retwtUrl success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"retweet id: %@", [responseObject objectForKey:@"id_str"]);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
}];
[sender setTitleColor:[UIColor colorWithRed:244.f/255.f green:180.f/255.f blue:0 alpha:1.f] forState:UIControlStateNormal];
[[self.currentTweets objectAtIndex:tid] setObject:[NSNumber numberWithBool:YES] forKey:@"retweeted"];
}
}
- (void) onReplyButton:(id)sender {
NSInteger tid = ((UIControl *) sender).tag;
NewTweetViewController *editor = [[NewTweetViewController alloc] initWithNibName:@"NewTweetViewController" bundle:nil];
editor.title = @"New tweet";
editor.backTo = @"";
editor.replyTo = [[[self.currentTweets objectAtIndex:tid] objectForKey:@"user"] objectForKey:@"screen_name"];
editor.savedTweets = self.currentTweets;
[self.navigationController pushViewController:editor animated:YES];
}
- (IBAction)onLeftNavButton:(id)sender {
if ([self.delegate respondsToSelector:@selector(toggleLeftMenu)]) {
[self.delegate toggleLeftMenu];
}
}
@end