-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyProfileViewController.m
More file actions
300 lines (242 loc) · 12.5 KB
/
MyProfileViewController.m
File metadata and controls
300 lines (242 loc) · 12.5 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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
//
// MyProfileViewController.m
// TwitterClient
//
// Created by Jessica Ko on 4/4/14.
// Copyright (c) 2014 Jessica Ko. All rights reserved.
//
#import "MyProfileViewController.h"
#import "User.h"
#import "ProfileHeader.h"
#import "TweetCell.h"
#import "MBProgressHUD.h"
#import "Client.h"
#import "NewTweetViewController.h"
@interface MyProfileViewController ()
- (void) reload;
-(void) fetchMoreTweets:(NSString *)max_id;
-(void) onFavoriteButton:(id)sender;
-(void) onRetweetButton:(id)sender;
-(void) onReplyButton:(id)sender;
-(void) loadOtherProfile;
@property (strong, nonatomic) NSDictionary *currentUser;
@property (strong, nonatomic) NSMutableArray *currentTweets;
@end
@implementation MyProfileViewController
@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.tweetTable.dataSource = self;
self.tweetTable.delegate = self;
UINib *customNib = [UINib nibWithNibName:@"TweetCell" bundle:nil];
[self.tweetTable registerNib:customNib forCellWithReuseIdentifier:@"TweetCell"];
UINib *customHeaderNib = [UINib nibWithNibName:@"ProfileHeader" bundle:nil];
[self.tweetTable registerNib:customHeaderNib forCellWithReuseIdentifier:@"ProfileHeader"];
NSLog(@"%@", self.screenId);
if (!self.screenId) {
self.currentUser = [User currentUserDictionary];
[self reload];
} else {
[self loadOtherProfile];
}
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return [self.currentTweets count] + 1;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0) {
return CGSizeMake(320, 295);
} else {
NSString *name = [self.currentTweets objectAtIndex:indexPath.row-1][@"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 {
if (indexPath.row == 0) {
ProfileHeader *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ProfileHeader" forIndexPath:indexPath];
[cell.profileBanner setBackgroundColor:[UIColor blackColor]];
NSURL *urlbg = [NSURL URLWithString:[NSString stringWithFormat:@"%@/web", [self.currentUser objectForKey:@"profile_banner_url"]]];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
NSData *imageDataBG = [NSData dataWithContentsOfURL:urlbg];
dispatch_async(dispatch_get_main_queue(), ^{
cell.profileBanner.image = [UIImage imageWithData:imageDataBG];
});
});
NSString *biggerImgUrl = [[self.currentUser objectForKey:@"profile_image_url"] stringByReplacingOccurrencesOfString:@"_normal" withString:@"_bigger"];
NSURL *url = [NSURL URLWithString:biggerImgUrl];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
NSData *imageData = [NSData dataWithContentsOfURL:url];
dispatch_async(dispatch_get_main_queue(), ^{
cell.profileImage.image = [UIImage imageWithData:imageData];
});
});
cell.profileName.text = [self.currentUser objectForKey:@"name"];
cell.screenName.text = [NSString stringWithFormat:@"@%@", [self.currentUser objectForKey:@"screen_name"]];
cell.profileDescription.text = [self.currentUser objectForKey:@"description"];
cell.tweetsTotal.text = [NSString stringWithFormat:@"%@", [self.currentUser objectForKey:@"statuses_count"]];
cell.followerTotal.text = [NSString stringWithFormat:@"%@", [self.currentUser objectForKey:@"followers_count"]];
cell.followingTotal.text = [NSString stringWithFormat:@"%@", [self.currentUser objectForKey:@"following"]];
CALayer * l = [cell.profileImage layer];
[l setMasksToBounds:YES];
[l setCornerRadius:8.0];
[cell.profileImage.layer setBorderColor: [[UIColor whiteColor] CGColor]];
[cell.profileImage.layer setBorderWidth: 2.0];
return cell;
} else {
TweetCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TweetCell" forIndexPath:indexPath];
//NSLog(@"%@", [[self.currentTweets objectAtIndex:indexPath.row] objectForKey:@"text"]);
NSDictionary *twt = [self.currentTweets objectAtIndex:indexPath.row-1];
[cell cellWithTweet:twt];
cell.tContent.numberOfLines = 0;
[cell.tContent sizeToFit];
BOOL favorited = [[[self.currentTweets objectAtIndex:indexPath.row-1] objectForKey:@"favorited"] boolValue];
if (favorited) {
[cell.favoriteButton setImage:[UIImage imageNamed:@"ic_star_yellow_8.png"] forState:UIControlStateNormal];
}
BOOL retweeted = [[[self.currentTweets objectAtIndex:indexPath.row-1] 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])) {
[self fetchMoreTweets:[[self.currentTweets objectAtIndex:([self.currentTweets count] - 1)] objectForKey:@"id"]];
}
cell.delegate = self;
return cell;
}
}
- (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.tweetTable 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.tweetTable 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];
}
- (void) reload {
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
Client *client = [Client instance];
[client homeTimelineWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
self.currentTweets = [Tweet tweetsWithArray:responseObject];
[self.tweetTable reloadData];
[MBProgressHUD hideHUDForView:self.view animated:YES];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"%@", error);
[MBProgressHUD hideHUDForView:self.view animated:YES];
}];
}
-(void) loadOtherProfile {
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
Client *client = [Client instance];
NSMutableDictionary *param = [[NSMutableDictionary alloc] initWithObjects:@[self.screenId] forKeys:@[@"screen_name"]];
[client getUserProfile:param success:^(AFHTTPRequestOperation *operation, id responseUserObject) {
self.currentUser = responseUserObject;
[client otherUserTimelineWithSuccess:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
self.currentTweets = [Tweet tweetsWithArray:responseObject];
[self.tweetTable reloadData];
[MBProgressHUD hideHUDForView:self.view animated:YES];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"%@", error);
[MBProgressHUD hideHUDForView:self.view animated:YES];
}];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"%@", error);
}];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)onLeftNavButton:(id)sender {
if (!self.screenId) {
if ([self.delegate respondsToSelector:@selector(toggleLeftMenu)]) {
[self.delegate toggleLeftMenu];
}
} else {
[self dismissViewControllerAnimated:YES completion:nil];
}
}
@end