/
MyThumbsViewController.m
330 lines (261 loc) · 10.8 KB
/
MyThumbsViewController.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#import "AppDelegate.h"
#import "MyThumbsViewController.h"
#import "PhotoSource.h"
#import "MyItemDeleter.h"
#import "AddAlbumViewController.h"
#import "UpdateAlbumViewController.h"
#import "UIImage+cropping.h"
#import "MyUploadViewController.h"
#import "Three20UI/TTPhotoSource.h"
#import "MyViewController.h"
#import "Three20UICommon/UIViewControllerAdditions.h"
#import "MySettings.h"
#import "UIImage+scaleAndRotate.h"
#import "TTTableViewController+g3.h"
#import "Overlay.h"
@interface MyThumbsViewController ()
// Disable all other buttons on the toolbar
- (void)disableToolbarItemsExceptButton:(UIButton*)button;
// Enable all buttons on the toolbar
- (void)enableToolbarItems;
// Sets the default button with default behavior
- (void)setStandardRightBarButtonItem;
@end
@implementation MyThumbsViewController
@synthesize albumID = _albumID;
- (void)dealloc {
[[RKRequestQueue sharedQueue] cancelAllRequests];
self.albumID = nil;
TT_RELEASE_SAFELY(_photoSource);
[super dealloc];
}
- (id)initWithAlbumID:(NSString *)albumID {
if ((self = [super init])) {
self.albumID = albumID;
PhotoSource* photosource = [[PhotoSource alloc] initWithItemID:albumID];
photosource.photosOnly = NO;
self.photoSource = photosource;
TT_RELEASE_SAFELY(photosource);
}
return self;
}
- (void)updateTableLayout {
self.tableView.contentInset = UIEdgeInsetsMake(5, 0, 0, 0);
self.tableView.scrollIndicatorInsets = UIEdgeInsetsMake(TTBarsHeight(), 0, 0, 0);
}
- (void)loadView {
[super loadView];
self.hidesBottomBarWhenPushed = NO;
self.statusBarStyle = UIBarStyleDefault;
self.navigationBarStyle = UIBarStyleBlack;
self.navigationBarTintColor = nil;
[self setWantsFullScreenLayout:YES];
}
- (void)modelDidFinishLoad:(id <TTModel>)model {
self.title = self.photoSource.title;
[super modelDidFinishLoad:model];
}
// Shows the Login page with all the settings
- (void)setSettings {
TTNavigator* navigator = [TTNavigator navigator];
[navigator openURLAction:[[TTURLAction actionWithURLPath:@"tt://login"] applyAnimated:YES]];
}
- (NSString *)urlEncodeValue:(NSString *)str
{
NSString *result = (NSString *) CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)str, NULL, CFSTR("?=&+"), kCFStringEncodingUTF8);
return [result autorelease];
}
// Handles the creation of a new album
- (void)createAlbum:(id)sender {
AddAlbumViewController* addAlbum = [[AddAlbumViewController alloc] initWithParentAlbumID: self.albumID andDelegate:self];
[self.navigationController pushViewController:addAlbum animated:YES];
TT_RELEASE_SAFELY(addAlbum);
}
// Handles the modification of an album
- (void)editAlbum:(id)sender {
UpdateAlbumViewController* updateAlbum = [[UpdateAlbumViewController alloc] initWithAlbumID: self.albumID andDelegate:self];
[self.navigationController pushViewController:updateAlbum animated:YES];
TT_RELEASE_SAFELY(updateAlbum);
}
- (void)viewDidLoad {
[super viewDidLoad];
if ([self.albumID isEqual:@"1"]) {
self.navigationItem.leftBarButtonItem
= [[[UIBarButtonItem alloc] initWithTitle:@"Settings" style:
UIBarButtonItemStyleBordered
target:self action:@selector(setSettings)
] autorelease];
}
if (!GlobalSettings.viewOnly) {
[self setStandardRightBarButtonItem];
}
self.navigationController.toolbar.barStyle = self.navigationBarStyle;
[self.navigationController.toolbar sizeToFit];
NSArray* toolbarItems = [Overlay buildThumbViewToolbarWithDelegate:self];
[self setToolbarItems:toolbarItems animated:YES];
[self.navigationController setNavigationBarHidden:NO animated:YES];
[self.navigationController setToolbarHidden:YES animated:YES];
//self.tableView.backgroundColor = [UIColor blackColor];
}
////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
NSMutableDictionary* query = [[[NSMutableDictionary alloc] initWithObjectsAndKeys:
self, @"delegate",
self.albumID, @"albumID",
nil] autorelease];
if (buttonIndex == 0) {
[query setValue:@"UIImagePickerControllerSourceTypeCamera" forKey:@"sourceType"];
}
if (buttonIndex == 1) {
[query setValue:@"UIImagePickerControllerSourceTypeSavedPhotosAlbum" forKey:@"sourceType"];
}
if (buttonIndex == 2) {
return;
}
[[TTNavigator navigator] openURLAction:[[[TTURLAction actionWithURLPath:
@"tt://nib/MyUploadViewController"]
applyQuery:query] applyAnimated:YES]];
}
- (void)modalView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
if ([alertView isKindOfClass:[UIAlertView class]]) {
if (buttonIndex == 1) {
// start the indicator ...
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
[self performSelector:@selector(deleteCurrentItem) withObject:Nil afterDelay:0.05];
}
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark private
// Show/hide details of album above the first album
- (void)toggleEditing:(id)sender {
_isInEditingState = !_isInEditingState;
if (_isInEditingState) {
[self.navigationController setToolbarHidden:NO animated:YES];
} else {
[self.navigationController setToolbarHidden:YES animated:YES];
}
}
// Confirms via dialog that the current item should be deleted
- (void)deleteCurrentItem:(id)sender {
UIAlertView *dialog = [[[UIAlertView alloc] init] autorelease];
dialog.delegate = self;
dialog.title = @"Confirm Deletion";
dialog.message = @"Do you really want to delete this album?";
[dialog addButtonWithTitle:@"Cancel"];
[dialog addButtonWithTitle:@"OK"];
[dialog show];
}
- (void)deleteCurrentItem {
PhotoSource* ps = (PhotoSource* ) self.photoSource;
[MyItemDeleter initWithItemID:ps.albumID];
// stop the indicator ...
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[((id<MyViewController>)self) reloadViewController:YES];
}
- (void)disableToolbarItemsExceptButton:(UIButton*)button {
for (UIBarButtonItem* item in self.toolbarItems) {
if (item.customView != button) {
item.enabled = NO;
}
}
}
- (void)enableToolbarItems {
for (UIBarButtonItem* item in self.toolbarItems) {
item.enabled = YES;
}
}
- (void)setStandardRightBarButtonItem {
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(toggleEditing:)] autorelease];
}
- (void)uploadImage:(id)sender {
UIActionSheet *actionSheet = [[[UIActionSheet alloc] initWithTitle:nil
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil] autorelease];
actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
[actionSheet addButtonWithTitle:@"Camera"];
[actionSheet addButtonWithTitle:@"Library"];
[actionSheet addButtonWithTitle:@"Cancel"];
actionSheet.cancelButtonIndex = 2;
if (_isEmpty) {
[actionSheet showInView:self.view];
} else {
[actionSheet showFromToolbar:self.navigationController.toolbar];
}
}
// Reloads the data -> resets the detail-view
- (void)reload {
[super reload];
}
// Reloads after an action was taken
- (void)reloadViewController:(BOOL)goBack {
_isInEditingState = NO;
[[UIApplication sharedApplication] setStatusBarHidden:NO];
[self.navigationController setNavigationBarHidden:NO animated:YES];
[self.navigationController setToolbarHidden:YES animated:YES];
if (_isEmpty) {
goBack = YES;
}
RKObjectLoaderTTModel* model = (RKObjectLoaderTTModel *)self.photoSource;
RKMTree *tree = (RKMTree *)[model.objects objectAtIndex:0];
RKMEntity *entity = [tree root];
if (![entity.thumb_url_public isEqualToString:@""] && entity.thumb_url_public != nil) {
[[TTURLCache sharedCache] removeURL:entity.thumb_url_public fromDisk:YES];
}
if (![entity.thumb_url isEqualToString:@""] && entity.thumb_url != nil) {
[[TTURLCache sharedCache] removeURL:entity.thumb_url fromDisk:YES];
}
MyThumbsViewController* prev = ((MyThumbsViewController *)self.ttPreviousViewController);
[self invalidateView];
[prev invalidateView];
NSArray *viewControllers = [self.navigationController viewControllers];
TTViewController* viewController;
if ([viewControllers count] > 1 && goBack) {
viewController = [viewControllers objectAtIndex:[viewControllers count] - 2];
[self.navigationController popToViewController:viewController animated:YES];
}
[NSTimer scheduledTimerWithTimeInterval:1 target:self.photoSource
selector:@selector(load) userInfo:nil repeats:NO];
[NSTimer scheduledTimerWithTimeInterval:2 target:prev.photoSource
selector:@selector(load) userInfo:nil repeats:NO];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
- (void)showEmpty:(BOOL)show {
/*
* We expect a tree-resource
* Should the resource have only 1 object the load was complete and no children found
* --> empty album
*/
if (show) {
NSString* title = [_dataSource titleForEmpty];
NSString* subtitle = @""; //[_dataSource subtitleForEmpty];
UIImage* image = [_dataSource imageForEmpty];
if (title.length || subtitle.length || image) {
TTErrorView* errorView = [[[TTErrorView alloc] initWithTitle:title
subtitle:subtitle
image:nil] autorelease];
errorView.backgroundColor = _tableView.backgroundColor;
TTView* buttonMenu = [((TTTableViewController*)self) buildOverlayMenu];
[errorView addSubview:buttonMenu];
[errorView bringSubviewToFront:buttonMenu];
self.emptyView = errorView;
self.navigationItem.rightBarButtonItem = nil;
self->_isEmpty = YES;
} else {
self.emptyView = nil;
self->_isEmpty = NO;
}
_tableView.dataSource = nil;
[_tableView reloadData];
} else {
self.emptyView = nil;
}
}
@end