/
WMFImageGalleryViewController.m
517 lines (406 loc) · 17.4 KB
/
WMFImageGalleryViewController.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
#import "WMFImageGalleryViewController.h"
@import WMF;
#import "Wikipedia-Swift.h"
#import "MWKImageInfoFetcher+PicOfTheDayInfo.h"
#import "WMFImageGalleryDetailOverlayView.h"
@import CoreServices;
// SINGLETONTODO - this whole file, find [MWKDataStore shared]
NS_ASSUME_NONNULL_BEGIN
@protocol WMFExposedDataSource <NYTPhotosViewControllerDataSource>
/**
* Exposing a private property of the data source
* In order to guarantee its existence, we assert photos
* on init in the VC
*/
@property (nonatomic, copy, readonly) NSArray *photos;
@end
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wincomplete-implementation"
@interface NYTPhotosViewController (WMFExposure)
- (NYTPhotoViewController *)newPhotoViewControllerForPhoto:(id<NYTPhoto>)photo;
@end
@interface WMFImageGalleryViewController ()
@property (nonatomic, strong, readonly) NSArray<id<NYTPhoto>> *photos;
@property (nonatomic, readonly) id<WMFExposedDataSource> dataSource;
- (NYTPhotoViewController *)currentPhotoViewController;
- (UIImageView *)currentImageView;
@property (nonatomic, strong) WMFTheme *theme;
@end
@interface WMFBasePhoto : NSObject
@property (nullable, nonatomic, strong) WMFTypedImageData *typedImageData;
@property (nullable, nonatomic, strong) NSString *imageDataUTType;
@property (nullable, nonatomic, strong) NSData *imageData;
//used for metadaata
@property (nonatomic, strong, nullable) MWKImageInfo *imageInfo;
@end
//@interface WMFArticlePhoto : WMFBasePhoto <WMFPhoto>
//
////set to display a thumbnail during download
//@property (nonatomic, strong, nullable) NSURL *thumbnailImageURL;
//
////used to fetch the full size image
//@property (nonatomic, strong, nullable) NSURL *imageURL;
//
//@end
@implementation WMFBasePhoto
- (nullable WMFTypedImageData *)typedImageData {
@synchronized(self) {
if (!_typedImageData) {
NSURL *URL = self.imageInfo.canonicalFileURL;
if (URL) {
_typedImageData = [[[MWKDataStore shared] cacheController] imageDataWithURL:URL];
}
}
return _typedImageData;
}
}
- (BOOL)isGIF {
return [self.imageInfo.canonicalFileURL.absoluteString.lowercaseString hasSuffix:@".gif"];
}
- (nullable NSData *)imageData {
return self.isGIF ? self.typedImageData.data : nil;
}
- (nullable NSString *)imageDataUTType {
return self.isGIF ? (NSString *)kUTTypeGIF : nil;
}
@end
@implementation WMFImageGalleryViewController
@dynamic dataSource;
- (instancetype)initWithPhotos:(nullable NSArray<id<NYTPhoto>> *)photos initialPhoto:(nullable id<NYTPhoto>)initialPhoto delegate:(nullable id<NYTPhotosViewControllerDelegate>)delegate theme:(WMFTheme *)theme overlayViewTopBarHidden:(BOOL)overlayViewTopBarHidden {
self = [super initWithPhotos:photos initialPhoto:initialPhoto delegate:self];
if (self) {
/**
* We are performing the following asserts to ensure that the
* implmentation of of NYTPhotosViewController does not change.
* We exposed these properties and methods via a category
* in lieu of subclassing. (and then maintaining a separate fork)
*/
NSParameterAssert(self.dataSource);
NSParameterAssert(self.photos);
NSAssert([self respondsToSelector:@selector(updateOverlayInformation)], @"NYTPhoto implementation changed!");
NSAssert([self respondsToSelector:@selector(currentPhotoViewController)], @"NYTPhoto implementation changed!");
NSAssert([self respondsToSelector:@selector(currentImageView)], @"NYTPhoto implementation changed!");
NSAssert([self respondsToSelector:@selector(newPhotoViewControllerForPhoto:)], @"NYTPhoto implementation changed!");
self.theme = theme;
[self setOverlayViewTopBarHidden:overlayViewTopBarHidden];
}
return self;
}
- (void)setOverlayViewTopBarHidden:(BOOL)hidden {
if (hidden) {
self.overlayView.rightBarButtonItem = nil;
self.overlayView.leftBarButtonItem = nil;
self.overlayView.topCoverBackgroundColor = [UIColor clearColor];
} else {
self.overlayView.topCoverBackgroundColor = [UIColor blackColor];
self.overlayView.navigationBar.backgroundColor = [UIColor clearColor];
UIBarButtonItem *share = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"share"] style:UIBarButtonItemStylePlain target:self action:@selector(didTapShareButton)];
share.tintColor = [UIColor whiteColor];
self.overlayView.rightBarButtonItem = share;
UIBarButtonItem *close = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"close"] style:UIBarButtonItemStylePlain target:self action:@selector(didTapCloseButton)];
close.tintColor = [UIColor whiteColor];
close.accessibilityLabel = [WMFCommonStrings closeButtonAccessibilityLabel];
self.overlayView.leftBarButtonItem = close;
}
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskAll;
}
- (BOOL)shouldAutorotate {
return YES;
}
- (UIImageView *)currentImageView {
return [self currentPhotoViewController].scalingImageView.imageView;
}
- (NSArray<id<NYTPhoto>> *)photos {
return [(id<WMFExposedDataSource>)self.dataSource photos];
}
- (NSUInteger)indexOfCurrentImage {
return [self indexOfPhoto:self.currentlyDisplayedPhoto];
}
- (NSUInteger)indexOfPhoto:(id<NYTPhoto>)photo {
return [self.photos indexOfObject:photo];
}
- (nullable id<WMFPhoto>)photoAtIndex:(NSUInteger)index {
if (index > self.photos.count) {
return nil;
}
return (id<WMFPhoto>)self.photos[index];
}
- (MWKImageInfo *)imageInfoForPhoto:(id<WMFPhoto>)photo {
return [photo bestImageInfo];
}
- (void)showImageAtIndex:(NSUInteger)index animated:(BOOL)animated {
id<NYTPhoto> photo = [self photoAtIndex:index];
[self displayPhoto:photo animated:animated];
}
- (NYTPhotoViewController *)newPhotoViewControllerForPhoto:(id<NYTPhoto>)photo {
NYTPhotoViewController *vc = [super newPhotoViewControllerForPhoto:photo];
vc.scalingImageView.imageView.backgroundColor = [UIColor whiteColor];
if (!self.theme) {
// don't do this elsewhere
// self.theme needs to be set before the [super init] call
// this is easiest way to do it for now
self.theme = [NSUserDefaults.standardUserDefaults themeCompatibleWith:self.traitCollection];
}
vc.scalingImageView.imageView.alpha = self.theme.imageOpacity;
return vc;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.accessibilityIgnoresInvertColors = YES;
// Very subtle gradient background so close and share buttons don't disappear when over white background parts of image.
UIImage *gradientImage = [[UIImage imageNamed:@"gallery-top-gradient"] stretchableImageWithLeftCapWidth:0 topCapHeight:0];
[self.overlayView.navigationBar setBackgroundImage:gradientImage forBarMetrics:UIBarMetricsDefault];
}
#pragma mark - Actions
- (void)didTapCloseButton {
[self dismissViewControllerAnimated:YES completion:NULL];
}
- (void)didTapShareButton {
id<WMFPhoto> photo = (id<WMFPhoto>)self.currentlyDisplayedPhoto;
MWKImageInfo *info = [photo bestImageInfo];
NSInteger targetWidth = [self.traitCollection wmf_galleryImageWidth];
NSURL *url = [info imageURLForTargetWidth:targetWidth];
@weakify(self);
[[[MWKDataStore shared] cacheController] fetchImageWithURL:url
failure:^(NSError *_Nonnull error) {
[[WMFAlertManager sharedInstance] showErrorAlert:error sticky:NO dismissPreviousAlerts:NO tapCallBack:NULL];
}
success:^(WMFImageDownload *_Nonnull download) {
@strongify(self);
UIActivityViewController *vc = [[WMFShareActivityController alloc] initWithImageInfo:info imageDownload:download];
vc.excludedActivityTypes = @[UIActivityTypeAddToReadingList];
UIPopoverPresentationController *presenter = [vc popoverPresentationController];
presenter.barButtonItem = self.rightBarButtonItem;
[self presentViewController:vc animated:YES completion:NULL];
}];
}
#pragma mark NYTPhotosViewControllerDelegate
- (UIView *_Nullable)photosViewController:(NYTPhotosViewController *)photosViewController referenceViewForPhoto:(id<NYTPhoto>)photo {
return nil; //TODO: remove this and re-enable animations when tickets for fixing anmimations are addressed
return [self.referenceViewDelegate referenceViewForImageController:self];
}
- (CGFloat)photosViewController:(NYTPhotosViewController *)photosViewController maximumZoomScaleForPhoto:(id<NYTPhoto>)photo {
return 2.0;
}
- (NSString *_Nullable)photosViewController:(NYTPhotosViewController *)photosViewController titleForPhoto:(id<NYTPhoto>)photo atIndex:(NSUInteger)photoIndex totalPhotoCount:(NSUInteger)totalPhotoCount {
return @"";
}
- (UIView *_Nullable)photosViewController:(NYTPhotosViewController *)photosViewController captionViewForPhoto:(id<NYTPhoto>)photo {
MWKImageInfo *imageInfo = [(id<WMFPhoto>)photo bestImageInfo];
if (!imageInfo) {
return nil;
}
WMFImageGalleryDetailOverlayView *caption = [WMFImageGalleryDetailOverlayView wmf_viewFromClassNib];
caption.imageDescriptionIsRTL = imageInfo.imageDescriptionIsRTL;
caption.imageDescription =
[imageInfo.imageDescription stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString *ownerOrFallback = imageInfo.owner ? [imageInfo.owner stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]
: WMFLocalizedStringWithDefaultValue(@"image-gallery-unknown-owner", nil, nil, @"Author unknown.", @"Fallback text for when an item in the image gallery doesn't have a specified owner.");
[caption setLicense:imageInfo.license owner:ownerOrFallback];
@weakify(self);
caption.ownerTapCallback = ^{
@strongify(self);
if (imageInfo.license.URL) {
[self wmf_navigateToURL:imageInfo.license.URL.wmf_urlByPrependingSchemeIfSchemeless];
} else if (imageInfo.filePageURL) {
[self wmf_navigateToURL:imageInfo.filePageURL.wmf_urlByPrependingSchemeIfSchemeless];
} else {
// There should always be a file page URL, but log an error anyway
DDLogError(@"No license URL or file page URL for %@", imageInfo);
}
};
caption.infoTapCallback = ^{
@strongify(self);
if (imageInfo.filePageURL) {
[self wmf_navigateToURL:imageInfo.filePageURL.wmf_urlByPrependingSchemeIfSchemeless];
}
};
@weakify(caption);
caption.descriptionTapCallback = ^{
[UIView animateWithDuration:0.3
animations:^{
@strongify(self);
@strongify(caption);
[caption toggleDescriptionOpenState];
[self.view layoutIfNeeded];
}
completion:NULL];
};
caption.maximumDescriptionHeight = self.view.frame.size.height;
return caption;
}
- (void)updateImageForPhotoAfterUserInteractionIsFinished:(id<NYTPhoto> _Nullable)photo {
//Exclude UITrackingRunLoopMode so the update doesn't happen while the user is pinching or scrolling
dispatch_async(dispatch_get_main_queue(), ^{
[self performSelector:@selector(updateImageForPhoto:) withObject:photo afterDelay:0 inModes:@[NSDefaultRunLoopMode]];
});
}
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
UIView *maybeDetailOverlayView = self.overlayView.captionView;
if (![maybeDetailOverlayView isKindOfClass:[WMFImageGalleryDetailOverlayView class]]) {
return;
}
WMFImageGalleryDetailOverlayView *detailOverlayView = (WMFImageGalleryDetailOverlayView *)maybeDetailOverlayView;
detailOverlayView.maximumDescriptionHeight = size.height;
}
#pragma mark - WMFThemeable
- (void)applyTheme:(WMFTheme *)theme {
self.theme = theme;
}
@end
#pragma clang diagnostic pop
@interface WMFPOTDPhoto : WMFBasePhoto <WMFPhoto>
//used to fetch imageInfo
@property (nonatomic, strong, nullable) NSDate *potdDate;
//set to display a thumbnail during download
@property (nonatomic, strong, nullable) MWKImageInfo *thumbnailImageInfo;
@end
@implementation WMFPOTDPhoto
+ (NSArray<WMFPOTDPhoto *> *)photosWithDates:(NSArray<NSDate *> *)dates {
return [dates wmf_map:^id(NSDate *obj) {
return [[WMFPOTDPhoto alloc] initWithPOTDDate:obj];
}];
}
- (instancetype)initWithPOTDDate:(NSDate *)date {
self = [super init];
if (self) {
self.potdDate = date;
}
return self;
}
- (nullable MWKImageInfo *)bestImageInfo {
return self.imageInfo;
}
- (nullable NSURL *)bestImageURL {
return self.imageURL;
}
- (nullable UIImage *)placeholderImage {
NSURL *url = [self thumbnailImageURL];
if (url) {
return [[[[MWKDataStore shared] cacheController] cachedImageWithURL:url] staticImage];
} else {
return nil;
}
}
- (nullable NSURL *)thumbnailImageURL {
return self.thumbnailImageInfo.imageThumbURL;
}
- (nullable UIImage *)image {
NSURL *url = [self imageURL];
if (url) {
return [[[[MWKDataStore shared] cacheController] cachedImageWithURL:url] staticImage];
} else {
return nil;
}
}
- (nullable UIImage *)memoryCachedImage {
NSURL *url = [self imageURL];
if (url) {
return [[[[MWKDataStore shared] cacheController] cachedImageWithURL:url] staticImage];
} else {
return nil;
}
}
- (nullable NSURL *)imageURL {
return [self.imageInfo imageURLForTargetWidth:[[UIScreen mainScreen] wmf_galleryImageWidthForScale]];
}
- (nullable NSAttributedString *)attributedCaptionTitle {
return nil;
}
- (nullable NSAttributedString *)attributedCaptionSummary {
return nil;
}
- (nullable NSAttributedString *)attributedCaptionCredit {
return nil;
}
@end
@interface WMFPOTDImageGalleryViewController ()
@property (nonatomic, strong) MWKImageInfoFetcher *infoFetcher;
@end
@implementation WMFPOTDImageGalleryViewController
- (instancetype)initWithDates:(NSArray<NSDate *> *)imageDates theme:(WMFTheme *)theme overlayViewTopBarHidden:(BOOL)overlayViewTopBarHidden {
NSParameterAssert(imageDates);
NSArray *items = imageDates;
NSArray<WMFPOTDPhoto *> *photos = [WMFPOTDPhoto photosWithDates:items];
self = [super initWithPhotos:photos initialPhoto:nil delegate:nil theme:theme overlayViewTopBarHidden:overlayViewTopBarHidden];
if (self) {
// SINGLETONTODO
self.infoFetcher = [[MWKImageInfoFetcher alloc] initWithDataStore:[MWKDataStore shared]];
}
return self;
}
- (MWKImageInfo *)imageInfoForPhoto:(id<NYTPhoto>)photo {
return [(WMFPOTDPhoto *)photo bestImageInfo];
}
- (MWKImageInfo *)currentImageInfo {
return [self imageInfoForPhoto:[self photoAtIndex:[self indexOfCurrentImage]]];
}
#pragma mark - UIViewController
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
if (self.currentlyDisplayedPhoto) {
[self fetchCurrentImageInfo];
}
}
#pragma mark - Fetch
- (void)fetchCurrentImageInfo {
[self fetchImageInfoForPhoto:(WMFPOTDPhoto *)self.currentlyDisplayedPhoto];
}
- (void)fetchImageInfoForIndex:(NSUInteger)index {
WMFPOTDPhoto *galleryImage = (WMFPOTDPhoto *)[self photoAtIndex:index];
[self fetchImageInfoForPhoto:galleryImage];
}
- (void)fetchImageInfoForPhoto:(WMFPOTDPhoto *)galleryImage {
NSDate *date = [galleryImage potdDate];
@weakify(self);
[self.infoFetcher fetchPicOfTheDayGalleryInfoForDate:date
metadataLanguage:[[NSLocale currentLocale] objectForKey:NSLocaleLanguageCode]
failure:^(NSError *_Nonnull error) {
UIViewController *vcToPresentError = [self presentingViewController];
[self dismissViewControllerAnimated:true completion:^{
[vcToPresentError wmf_showAlertWithError:error];
}];
}
success:^(id _Nonnull info) {
@strongify(self);
galleryImage.imageInfo = info;
dispatch_async(dispatch_get_main_queue(), ^{
[self updateOverlayInformation];
[self fetchImageForPhoto:galleryImage];
});
}];
}
- (void)fetchImageForPhoto:(WMFPOTDPhoto *)galleryImage {
@weakify(self);
UIImage *memoryCachedImage = [galleryImage memoryCachedImage];
if (memoryCachedImage == nil) {
[[[MWKDataStore shared] cacheController] fetchImageWithURL:[galleryImage bestImageURL]
failure:^(NSError *_Nonnull error) {
if (error) {
//show error
return;
}
}
success:^(WMFImageDownload *_Nonnull download) {
@strongify(self);
[self updateImageForPhotoAfterUserInteractionIsFinished:galleryImage];
}];
} else {
[self updateImageForPhotoAfterUserInteractionIsFinished:galleryImage];
}
}
#pragma mark NYTPhotosViewControllerDelegate
- (void)photosViewController:(NYTPhotosViewController *)photosViewController didNavigateToPhoto:(id<NYTPhoto>)photo atIndex:(NSUInteger)photoIndex {
WMFPOTDPhoto *galleryImage = (WMFPOTDPhoto *)photo;
if (![galleryImage imageURL]) {
[self fetchImageInfoForPhoto:galleryImage];
} else if (![galleryImage memoryCachedImage]) {
[self fetchImageForPhoto:galleryImage];
}
}
@end
NS_ASSUME_NONNULL_END