This repository has been archived by the owner on Jan 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
TTThumbsViewController.m
316 lines (247 loc) · 9.97 KB
/
TTThumbsViewController.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
314
315
316
#import "Three20/TTThumbsViewController.h"
#import "Three20/TTPhotoViewController.h"
#import "Three20/TTURLRequest.h"
#import "Three20/TTUnclippedView.h"
#import "Three20/TTTableField.h"
#import "Three20/TTURLCache.h"
#import "Three20/TTStyleSheet.h"
///////////////////////////////////////////////////////////////////////////////////////////////////
// global
static NSInteger kColumnCount = 4;
static CGFloat kThumbnailRowHeight = 79;
///////////////////////////////////////////////////////////////////////////////////////////////////
@implementation TTThumbsDataSource
///////////////////////////////////////////////////////////////////////////////////////////////////
// private
- (BOOL)hasMoreToLoad {
return _controller.photoSource.maxPhotoIndex+1 < _controller.photoSource.numberOfPhotos;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// NSObject
- (id)initWithController:(TTThumbsViewController*)controller {
if (self = [super init]) {
_controller = controller;
[_controller.photoSource.delegates addObject:self];
}
return self;
}
- (void)dealloc {
[_controller.photoSource.delegates removeObject:self];
[super dealloc];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// TTURLRequestDelegate
- (void)photoSourceDidStartLoad:(id<TTPhotoSource>)photoSource {
[self dataSourceDidStartLoad];
}
- (void)photoSourceDidFinishLoad:(id<TTPhotoSource>)photoSource {
[self dataSourceDidFinishLoad];
}
- (void)photoSource:(id<TTPhotoSource>)photoSource didFailLoadWithError:(NSError*)error {
[self dataSourceDidFailLoadWithError:error];
}
- (void)photoSourceDidCancelLoad:(id<TTPhotoSource>)photoSource {
[self dataSourceDidCancelLoad];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// TTThumbsTableViewCellDelegate
- (void)thumbsTableViewCell:(TTThumbsTableViewCell*)cell didSelectPhoto:(id<TTPhoto>)photo {
[_controller.delegate thumbsViewController:_controller didSelectPhoto:photo];
BOOL shouldNavigate = YES;
if ([_controller.delegate
respondsToSelector:@selector(thumbsViewController:shouldNavigateToPhoto:)]) {
shouldNavigate = [_controller.delegate thumbsViewController:_controller
shouldNavigateToPhoto:photo];
}
if (shouldNavigate) {
TTPhotoViewController* controller = [_controller createPhotoViewController];
controller.centerPhoto = photo;
[_controller.navigationController pushViewController:controller animated:YES];
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////
// UITableViewDataSource
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section {
NSInteger maxIndex = _controller.photoSource.maxPhotoIndex+1;
if (!_controller.photoSource.isLoading && maxIndex > 0) {
NSInteger count = ceil((maxIndex / kColumnCount) + (maxIndex % kColumnCount ? 1 : 0));
if (self.hasMoreToLoad) {
return count + 1;
} else {
return count;
}
} else {
return 0;
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////
// TTTableViewDataSource
- (NSDate*)loadedTime {
return _controller.photoSource.loadedTime;
}
- (BOOL)isLoading {
return _controller.photoSource.isLoading;
}
- (BOOL)isLoadingMore {
return _controller.photoSource.isLoadingMore;
}
- (BOOL)isLoaded {
return _controller.photoSource.isLoaded;
}
- (BOOL)isOutdated {
return _controller.photoSource.isOutdated;
}
- (BOOL)isEmpty {
return _controller.photoSource.isEmpty;
}
- (id)tableView:(UITableView*)tableView objectForRowAtIndexPath:(NSIndexPath*)indexPath {
if (indexPath.row == [tableView numberOfRowsInSection:0]-1 && self.hasMoreToLoad) {
NSString* title = TTLocalizedString(@"Load More Photos...", @"");
NSString* subtitle = [NSString stringWithFormat:
TTLocalizedString(@"Showing %d of %d Photos", @""), _controller.photoSource.maxPhotoIndex+1,
_controller.photoSource.numberOfPhotos];
return [[[TTMoreButtonTableField alloc] initWithText:title subtitle:subtitle] autorelease];
} else {
return [_controller.photoSource photoAtIndex:indexPath.row * kColumnCount];
}
}
- (Class)tableView:(UITableView*)tableView cellClassForObject:(id)object {
if ([object conformsToProtocol:@protocol(TTPhoto)]) {
return [TTThumbsTableViewCell class];
} else {
return [super tableView:tableView cellClassForObject:object];
}
}
- (void)tableView:(UITableView*)tableView prepareCell:(UITableViewCell*)cell
forRowAtIndexPath:(NSIndexPath*)indexPath {
if ([cell isKindOfClass:[TTThumbsTableViewCell class]]) {
TTThumbsTableViewCell* thumbsCell = (TTThumbsTableViewCell*)cell;
thumbsCell.delegate = self;
}
}
- (void)load:(TTURLRequestCachePolicy)cachePolicy nextPage:(BOOL)nextPage {
NSInteger index = nextPage ? _controller.photoSource.maxPhotoIndex : 0;
[_controller.photoSource loadPhotosFromIndex:index toIndex:TT_INFINITE_PHOTO_INDEX
cachePolicy:cachePolicy];
}
@end
///////////////////////////////////////////////////////////////////////////////////////////////////
@implementation TTThumbsViewController
@synthesize delegate = _delegate, photoSource = _photoSource;
///////////////////////////////////////////////////////////////////////////////////////////////////
// private
- (void)suspendLoadingThumbnails:(BOOL)suspended {
if (_photoSource.maxPhotoIndex >= 0) {
NSArray* cells = _tableView.visibleCells;
for (int i = 0; i < cells.count; ++i) {
TTThumbsTableViewCell* cell = [cells objectAtIndex:i];
if ([cell isKindOfClass:[TTThumbsTableViewCell class]]) {
[cell suspendLoading:suspended];
}
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// NSObject
- (id)init {
if (self = [super init]) {
_delegate = nil;
_photoSource = nil;
self.hidesBottomBarWhenPushed = YES;
self.navigationBarStyle = UIBarStyleBlackTranslucent;
self.navigationBarTintColor = nil;
self.statusBarStyle = UIStatusBarStyleBlackTranslucent;
}
return self;
}
- (void)dealloc {
[_photoSource.delegates removeObject:self];
[_photoSource release];
[super dealloc];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// UIViewController
- (void)loadView {
CGRect screenFrame = [UIScreen mainScreen].bounds;
self.view = [[[TTUnclippedView alloc] initWithFrame:screenFrame] autorelease];
self.view.autoresizesSubviews = YES;
self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
CGRect innerFrame = CGRectMake(0, -CHROME_HEIGHT,
screenFrame.size.width, screenFrame.size.height + CHROME_HEIGHT);
UIView* innerView = [[UIView alloc] initWithFrame:innerFrame];
innerView.backgroundColor = TTSTYLEVAR(backgroundColor);
[self.view addSubview:innerView];
CGRect tableFrame = CGRectMake(0, CHROME_HEIGHT,
screenFrame.size.width, screenFrame.size.height - CHROME_HEIGHT);
self.tableView = [[[UITableView alloc] initWithFrame:tableFrame
style:UITableViewStylePlain] autorelease];
self.tableView.rowHeight = kThumbnailRowHeight;
self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth
| UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
self.tableView.backgroundColor = TTSTYLEVAR(backgroundColor);
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
self.tableView.contentInset = UIEdgeInsetsMake(4, 0, 0, 0);
self.tableView.clipsToBounds = NO;
[innerView addSubview:self.tableView];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self suspendLoadingThumbnails:NO];
if (!self.nextViewController) {
self.view.superview.frame = CGRectOffset(self.view.superview.frame, 0, TOOLBAR_HEIGHT);
}
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
self.view.superview.frame = CGRectOffset(self.view.superview.frame, 0, TOOLBAR_HEIGHT);
}
- (void)viewDidDisappear:(BOOL)animated {
[self suspendLoadingThumbnails:YES];
[super viewDidDisappear:animated];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// TTViewController
- (id<TTPersistable>)viewObject {
return _photoSource;
}
- (void)showObject:(id)object inView:(NSString*)viewType withState:(NSDictionary*)state {
[super showObject:object inView:viewType withState:state];
self.photoSource = (id<TTPhotoSource>)object;
}
- (UIImage*)imageForNoData {
return TTIMAGE(@"bundle://Three20.bundle/images/photoDefault.png");
}
- (NSString*)titleForNoData {
return TTLocalizedString(@"No Photos", @"");
}
- (NSString*)subtitleForNoData {
return TTLocalizedString(@"This photo set contains no photos.", @"");
}
- (UIImage*)imageForError:(NSError*)error {
return TTIMAGE(@"bundle://Three20.bundle/images/photoDefault.png");
}
- (NSString*)titleForError:(NSError*)error {
return TTLocalizedString(@"Error", @"");
}
- (NSString*)subtitleForError:(NSError*)error {
return TTLocalizedString(@"This photo set could not be loaded.", @"");
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// TTTableViewController
- (id<TTTableViewDataSource>)createDataSource {
return [[[TTThumbsDataSource alloc] initWithController:self] autorelease];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// public
- (void)setPhotoSource:(id<TTPhotoSource>)photoSource {
if (photoSource != _photoSource) {
[_photoSource release];
_photoSource = [photoSource retain];
self.title = _photoSource.title;
[self invalidateView];
}
}
- (TTPhotoViewController*)createPhotoViewController {
return [[[TTPhotoViewController alloc] init] autorelease];
}
@end