forked from einsteinx2/iSub-old-swift-port
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IDTwitterAccountChooserViewController.m
313 lines (232 loc) · 10.5 KB
/
IDTwitterAccountChooserViewController.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
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
301
302
303
304
305
306
307
308
309
310
311
312
313
//
// IDTwitterAccountChooserViewController.m
// AngelFon
//
// Created by Carlos Oliva on 05-12-12.
// Copyright (c) 2012 iDev Software. All rights reserved.
//
#import <Accounts/Accounts.h>
#import <Twitter/Twitter.h>
#import "IDTwitterAccountChooserViewController.h"
#define kTwitterAPIRootURL @"https://api.twitter.com/1.1/"
@interface IDTwitterAccountChooserContentViewController : UIViewController <UITableViewDataSource, UITableViewDelegate> {
UITableView *tableView;
NSArray *twitterAccounts;
__weak id <IDTwitterAccountChooserViewControllerDelegate> accountChooserDelegate;
NSMutableDictionary *imagesDictionary;
NSMutableDictionary *realNamesDictionary;
IDTwitterAccountChooserViewControllerCompletionHandler completionHandler;
}
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *twitterAccounts;
@property (nonatomic, weak) id <IDTwitterAccountChooserViewControllerDelegate> accountChooserDelegate;
@property (nonatomic, copy) IDTwitterAccountChooserViewControllerCompletionHandler completionHandler;
@end
@implementation IDTwitterAccountChooserContentViewController
@synthesize tableView;
@synthesize twitterAccounts;
@synthesize accountChooserDelegate;
@synthesize completionHandler;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if(self) {
imagesDictionary = [[NSMutableDictionary alloc] initWithCapacity:0];
realNamesDictionary = [[NSMutableDictionary alloc] initWithCapacity:0];
}
return self;
}
- (void)loadView {
self.view = self.tableView;
}
- (void)viewDidLoad {
self.navigationItem.title = NSLocalizedString(@"Choose an Account", @"Choose an Account");
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self
action:@selector(cancel:)];
}
- (void)viewDidAppear:(BOOL)animated {
[self loadMetadataForCellsWithIndexPaths:[self.tableView indexPathsForVisibleRows]];
}
- (void)viewDidUnload {
[super viewDidUnload];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
[imagesDictionary removeAllObjects];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return toInterfaceOrientation == UIInterfaceOrientationPortrait;
}
- (void)loadMetadataForCellsWithIndexPaths:(NSArray *)indexPaths {
return;// This is broken on iOS 5 apparantly so fuck it
for(NSIndexPath *indexPath in indexPaths) {
ACAccount *account = [self.twitterAccounts objectAtIndex:indexPath.row];
NSString *username = [account username];
if(username == nil || ([imagesDictionary objectForKey:username] != nil && [realNamesDictionary objectForKey:username] != nil))
continue;
NSURL *url = [NSURL URLWithString:[kTwitterAPIRootURL stringByAppendingString:@"users/show.json"]];
NSDictionary *parameters = @{
@"screen_name": username
};
TWRequest *request = [[TWRequest alloc] initWithURL:url
parameters:parameters
requestMethod:TWRequestMethodGET];
[request setAccount:account];
[request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
if(error != nil && responseData == nil) {
NSLog(@"TWRequest error: %@", [error localizedDescription]);
return;
}
error = nil;
NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
if(error != nil) {
NSLog(@"JSON deserialization error: %@", [error localizedDescription]);
return;
}
NSString *name = [responseDictionary objectForKey:@"name"];
if([realNamesDictionary objectForKey:username] == nil && name != nil) {
dispatch_async(dispatch_get_main_queue(), ^{
[realNamesDictionary setObject:name forKey:username];
[self.tableView reloadData];
});
}
NSString *profileImageURLString = [responseDictionary objectForKey:@"profile_image_url"];
if([imagesDictionary objectForKey:username] == nil && profileImageURLString != nil) {
NSString *filename = [[profileImageURLString componentsSeparatedByString:@"/"] lastObject];
NSString *extension = [filename pathExtension];
NSString *basename = [filename stringByDeletingPathExtension];
NSString *biggerFilename = [[basename stringByReplacingOccurrencesOfString:@"normal" withString:@"bigger" options:(NSAnchoredSearch | NSBackwardsSearch) range:NSMakeRange(0, [basename length])] stringByAppendingPathExtension:extension];
NSString *biggerFilenameURLString = [profileImageURLString stringByReplacingOccurrencesOfString:filename
withString:biggerFilename
options:(NSAnchoredSearch | NSBackwardsSearch)
range:NSMakeRange(0, [profileImageURLString length])];
NSURL *imageURL = [NSURL URLWithString:biggerFilenameURLString];
TWRequest *imageRequest = [[TWRequest alloc] initWithURL:imageURL
parameters:nil
requestMethod:TWRequestMethodGET];
[imageRequest setAccount:account];
[imageRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
if(error != nil && responseData == nil) {
NSLog(@"TWRequest error: %@", [error localizedDescription]);
return;
}
UIImage *biggerImage = [UIImage imageWithData:responseData];
UIImage *image = [UIImage imageWithCGImage:[biggerImage CGImage]
scale:2.0
orientation:UIImageOrientationUp];
if([imagesDictionary objectForKey:username] == nil) {
dispatch_async(dispatch_get_main_queue(), ^{
[imagesDictionary setObject:image forKey:username];
[self.tableView reloadData];
});
}
}];
}
}];
}
}
#pragma mark - Button Actions
- (IBAction)cancel:(id)sender {
[self dismissViewControllerAnimated:YES completion:^{
if(completionHandler != nil) {
completionHandler(nil);
return;
}
if(self.accountChooserDelegate != nil && [self.accountChooserDelegate conformsToProtocol:@protocol(IDTwitterAccountChooserViewControllerDelegate)] &&
[self.accountChooserDelegate respondsToSelector:@selector(twitterAccountChooserViewController:didChooseTwitterAccount:)]) {
[self.accountChooserDelegate twitterAccountChooserViewController:(IDTwitterAccountChooserViewController *)self.navigationController didChooseTwitterAccount:nil];
}
}];
}
#pragma mark - Getter Methods
- (UITableView *)tableView {
if(tableView == nil) {
tableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStyleGrouped];
tableView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
tableView.autoresizesSubviews = YES;
tableView.delegate = self;
tableView.dataSource = self;
}
return tableView;
}
#pragma mark - <UITableViewDataSource> Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.twitterAccounts count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier = @"cell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:identifier];
if(cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
cell.accessoryType = UITableViewCellAccessoryNone;
}
ACAccount *account = [self.twitterAccounts objectAtIndex:indexPath.row];
// NSDictionary *properties = [account valueForKey:@"properties"];
// if(properties != nil) {
// NSLog(@"properties: %@", properties);
// }
NSString *username = [account username];
cell.textLabel.text = [account accountDescription];
NSString *realName = [realNamesDictionary objectForKey:username];
cell.detailTextLabel.text = realName;
UIImage *image = [imagesDictionary objectForKey:username];
[cell.imageView setImage:image];
return cell;
}
#pragma mark - <UITableViewDelegate> Methods
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self dismissViewControllerAnimated:YES completion:^{
ACAccount *account = [self.twitterAccounts objectAtIndex:indexPath.row];
if(completionHandler != nil) {
completionHandler(account);
return;
}
if(self.accountChooserDelegate != nil && [self.accountChooserDelegate conformsToProtocol:@protocol(IDTwitterAccountChooserViewControllerDelegate)] &&
[self.accountChooserDelegate respondsToSelector:@selector(twitterAccountChooserViewController:didChooseTwitterAccount:)]) {
[self.accountChooserDelegate twitterAccountChooserViewController:(IDTwitterAccountChooserViewController *)self.navigationController didChooseTwitterAccount:account];
}
}];
}
- (void)scrollViewDidEndDecelerating:(UITableView *)tableView {
[self loadMetadataForCellsWithIndexPaths:[self.tableView indexPathsForVisibleRows]];
}
- (void)scrollViewDidEndDragging:(UITableView *)tableView willDecelerate:(BOOL)decelerate {
if(!decelerate)
[self loadMetadataForCellsWithIndexPaths:[self.tableView indexPathsForVisibleRows]];
}
- (void)scrollViewDidScrollToTop:(UITableView *)tableView {
[self loadMetadataForCellsWithIndexPaths:[self.tableView indexPathsForVisibleRows]];
}
@end
#pragma mark -
@interface IDTwitterAccountChooserViewController () {
IDTwitterAccountChooserContentViewController *contentViewController;
}
@property (nonatomic, strong, readonly) IDTwitterAccountChooserContentViewController *contentViewController;
@end
@implementation IDTwitterAccountChooserViewController
@synthesize contentViewController;
- (id)initWithRootViewController:(UIViewController *)rootViewController {
self = [super initWithRootViewController:[[IDTwitterAccountChooserContentViewController alloc] init]];
if (self) {
// Custom initialization
}
return self;
}
#pragma mark - Forwarded Methods
- (void)setTwitterAccounts:(NSArray *)twitterAccounts {
[self.contentViewController setTwitterAccounts:twitterAccounts];
}
- (void)setAccountChooserDelegate:(id <IDTwitterAccountChooserViewControllerDelegate>)delegate {
[self.contentViewController setAccountChooserDelegate:delegate];
}
- (void)setCompletionHandler:(IDTwitterAccountChooserViewControllerCompletionHandler)completionHandler {
[self.contentViewController setCompletionHandler:completionHandler];
}
#pragma mark - Getter Methods
- (IDTwitterAccountChooserContentViewController *)contentViewController {
if([self.viewControllers count] == 0)
return nil;
return [self.viewControllers objectAtIndex:0];
}
@end