-
Notifications
You must be signed in to change notification settings - Fork 117
/
PhotoViewController.m
204 lines (178 loc) · 8.5 KB
/
PhotoViewController.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
//
// PhotoViewController.m
// DBRoulette
//
// Copyright © 2016 Dropbox. All rights reserved.
//
#import <ObjectiveDropboxOfficial/ObjectiveDropboxOfficial.h>
#import "PhotoViewController.h"
@interface PhotoViewController ()
@property(weak, nonatomic) IBOutlet UIButton *randomPhotoButton;
@property(weak, nonatomic) IBOutlet UIActivityIndicatorView *indicatorView;
@property(nonatomic) UIImageView *currentImageView;
@end
@implementation PhotoViewController
- (IBAction)randomPhotoButtonPressed:(id)sender {
[self setStarted];
if (_currentImageView) {
[_currentImageView removeFromSuperview];
}
DBUserClient *client = [DBClientsManager authorizedClient];
NSString *searchPath = @"";
// list folder metadata contents (folder will be root "/" Dropbox folder if app has permission
// "Full Dropbox" or "/Apps/<APP_NAME>/" if app has permission "App Folder").
[[client.filesRoutes listFolder:searchPath]
setResponseBlock:^(DBFILESListFolderResult *result, DBFILESListFolderError *routeError, DBRequestError *error) {
if (result) {
[self displayPhotos:result.entries];
} else {
NSString *title = @"";
NSString *message = @"";
if (routeError) {
// Route-specific request error
title = @"Route-specific error";
if ([routeError isPath]) {
message = [NSString stringWithFormat:@"Invalid path: %@", routeError.path];
}
} else {
// Generic request error
title = @"Generic request error";
if ([error isInternalServerError]) {
DBRequestInternalServerError *internalServerError = [error asInternalServerError];
message = [NSString stringWithFormat:@"%@", internalServerError];
} else if ([error isBadInputError]) {
DBRequestBadInputError *badInputError = [error asBadInputError];
message = [NSString stringWithFormat:@"%@", badInputError];
} else if ([error isAuthError]) {
DBRequestAuthError *authError = [error asAuthError];
message = [NSString stringWithFormat:@"%@", authError];
} else if ([error isRateLimitError]) {
DBRequestRateLimitError *rateLimitError = [error asRateLimitError];
message = [NSString stringWithFormat:@"%@", rateLimitError];
} else if ([error isHttpError]) {
DBRequestHttpError *genericHttpError = [error asHttpError];
message = [NSString stringWithFormat:@"%@", genericHttpError];
} else if ([error isClientError]) {
DBRequestClientError *genericLocalError = [error asClientError];
message = [NSString stringWithFormat:@"%@", genericLocalError];
}
}
UIAlertController *alertController =
[UIAlertController alertControllerWithTitle:title
message:message
preferredStyle:(UIAlertControllerStyle)UIAlertControllerStyleAlert];
[alertController addAction:[UIAlertAction actionWithTitle:@"OK"
style:(UIAlertActionStyle)UIAlertActionStyleCancel
handler:nil]];
[self presentViewController:alertController animated:YES completion:nil];
[self setFinished];
}
}];
}
- (void)displayPhotos:(NSArray<DBFILESMetadata *> *)folderEntries {
NSMutableArray<NSString *> *imagePaths = [NSMutableArray new];
for (DBFILESMetadata *entry in folderEntries) {
NSString *itemName = entry.name;
if ([self isImageType:itemName]) {
[imagePaths addObject:entry.pathDisplay];
}
}
if ([imagePaths count] > 0) {
NSString *imagePathToDownload = imagePaths[arc4random_uniform((int)[imagePaths count] - 1)];
[self downloadImage:imagePathToDownload];
} else {
NSString *title = @"No images found";
NSString *message = @"There are currently no valid image files in the specified search path in your Dropbox. "
@"Please add some images and try again.";
UIAlertController *alertController =
[UIAlertController alertControllerWithTitle:title
message:message
preferredStyle:(UIAlertControllerStyle)UIAlertControllerStyleAlert];
[alertController
addAction:[UIAlertAction actionWithTitle:@"OK" style:(UIAlertActionStyle)UIAlertActionStyleCancel handler:nil]];
[self presentViewController:alertController animated:YES completion:nil];
[self setFinished];
}
}
- (BOOL)isImageType:(NSString *)itemName {
NSRange range = [itemName rangeOfString:@"\\.jpeg|\\.jpg|\\.JPEG|\\.JPG|\\.png" options:NSRegularExpressionSearch];
return range.location != NSNotFound;
}
- (void)downloadImage:(NSString *)imagePath {
DBUserClient *client = [DBClientsManager authorizedClient];
[[client.filesRoutes downloadData:imagePath]
setResponseBlock:^(DBFILESFileMetadata *result, DBFILESDownloadError *routeError, DBRequestError *error, NSData *fileData) {
if (result) {
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageWithData:fileData]];
imageView.frame = CGRectMake(100, 100, 300, 300);
[imageView setCenter:CGPointMake(self.view.bounds.size.width/2, self.view.bounds.size.height/2)];
[self.view addSubview:imageView];
_currentImageView = imageView;
[self setFinished];
} else {
NSString *title = @"";
NSString *message = @"";
if (routeError) {
// Route-specific request error
title = @"Route-specific error";
if ([routeError isPath]) {
message = [NSString stringWithFormat:@"Invalid path: %@", routeError.path];
} else if ([routeError isOther]) {
message = [NSString stringWithFormat:@"Unknown error: %@", routeError];
}
} else {
// Generic request error
title = @"Generic request error";
if ([error isInternalServerError]) {
DBRequestInternalServerError *internalServerError = [error asInternalServerError];
message = [NSString stringWithFormat:@"%@", internalServerError];
} else if ([error isBadInputError]) {
DBRequestBadInputError *badInputError = [error asBadInputError];
message = [NSString stringWithFormat:@"%@", badInputError];
} else if ([error isAuthError]) {
DBRequestAuthError *authError = [error asAuthError];
message = [NSString stringWithFormat:@"%@", authError];
} else if ([error isRateLimitError]) {
DBRequestRateLimitError *rateLimitError = [error asRateLimitError];
message = [NSString stringWithFormat:@"%@", rateLimitError];
} else if ([error isHttpError]) {
DBRequestHttpError *genericHttpError = [error asHttpError];
message = [NSString stringWithFormat:@"%@", genericHttpError];
} else if ([error isClientError]) {
DBRequestClientError *genericLocalError = [error asClientError];
message = [NSString stringWithFormat:@"%@", genericLocalError];
}
}
UIAlertController *alertController =
[UIAlertController alertControllerWithTitle:title
message:message
preferredStyle:(UIAlertControllerStyle)UIAlertControllerStyleAlert];
[alertController addAction:[UIAlertAction actionWithTitle:@"OK"
style:(UIAlertActionStyle)UIAlertActionStyleCancel
handler:nil]];
[self presentViewController:alertController animated:YES completion:nil];
[self setFinished];
}
}];
}
- (void)setStarted {
[_indicatorView startAnimating];
_indicatorView.hidden = NO;
}
- (void)setFinished {
[_indicatorView stopAnimating];
_indicatorView.hidden = YES;
}
- (void)viewDidLoad {
[super viewDidLoad];
_indicatorView.hidden = YES;
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end