From 91b2135a82e8f41fafedf10ca5a195bfc47aad49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Se=CC=81rgio=20Este=CC=82va=CC=83o?= Date: Tue, 24 Nov 2015 23:39:09 +0000 Subject: [PATCH 1/2] Move code to configure cell to the cell class. Cleans up accessibility labels and reused of the cell. --- Pod/Classes/WPMediaCollectionViewCell.h | 7 +- Pod/Classes/WPMediaCollectionViewCell.m | 87 +++++++++++++++++-- Pod/Classes/WPMediaCollectionViewController.m | 74 +--------------- WPMediaPicker.podspec | 2 +- 4 files changed, 85 insertions(+), 85 deletions(-) diff --git a/Pod/Classes/WPMediaCollectionViewCell.h b/Pod/Classes/WPMediaCollectionViewCell.h index 6ebc2a48..586f21d1 100644 --- a/Pod/Classes/WPMediaCollectionViewCell.h +++ b/Pod/Classes/WPMediaCollectionViewCell.h @@ -1,12 +1,9 @@ @import UIKit; +#import "WPMediaCollectionDataSource.h" @interface WPMediaCollectionViewCell : UICollectionViewCell -@property (nonatomic, strong) UIImage *image; +@property (nonatomic, strong) id asset; @property (nonatomic, assign) NSInteger position; -- (void)setCaption:(NSString *)caption; - -- (void)setImage:(UIImage *)image animated:(BOOL)animated withAccessibilityLabel:(NSString*)accessibilityLabel; - @end diff --git a/Pod/Classes/WPMediaCollectionViewCell.m b/Pod/Classes/WPMediaCollectionViewCell.m index 08e2a0a8..1aaf7edb 100644 --- a/Pod/Classes/WPMediaCollectionViewCell.m +++ b/Pod/Classes/WPMediaCollectionViewCell.m @@ -32,7 +32,11 @@ - (id)initWithCoder:(NSCoder *)aDecoder - (void)prepareForReuse { [super prepareForReuse]; - [self setImage:nil]; + if (self.tag != 0) { + [self.asset cancelImageRequest:(WPMediaRequestID)self.tag]; + } + self.tag = 0; + [self setImage:nil animated:NO]; [self setCaption:@""]; [self setPosition:NSNotFound]; [self setSelected:NO]; @@ -72,28 +76,97 @@ - (void)commonInit [self.contentView addSubview:_captionLabel]; } -- (void)setImage:(UIImage *)image withAccessibilityLabel:(NSString*)accessibilityLabel +- (void)setAsset:(id)asset { + _asset = asset; + __block WPMediaRequestID requestKey = 0; + NSTimeInterval timestamp = [NSDate timeIntervalSinceReferenceDate]; + requestKey = [_asset imageWithSize:self.frame.size completionHandler:^(UIImage *result, NSError *error) { + BOOL animated = ([NSDate timeIntervalSinceReferenceDate] - timestamp) > 0.03; + if (error) { + self.image = nil; + NSLog(@"%@", [error localizedDescription]); + return; + } + // Did this request changed meanwhile + if (requestKey != self.tag) { + return; + } + if ([NSThread isMainThread]){ + [self setImage:result + animated:animated]; + } else { + dispatch_async(dispatch_get_main_queue(), ^{ + [self setImage:result + animated:animated]; + }); + } + }]; + self.tag = requestKey; + NSString *label = @""; + NSString *caption = @""; + WPMediaType assetType = _asset.assetType; + switch (assetType) { + case WPMediaTypeImage: + label = [NSString stringWithFormat:NSLocalizedString(@"Image, %@", @"Accessibility label for image thumbnails in the media collection view. The parameter is the creation date of the image."), + [[[self class] dateFormatter] stringFromDate:_asset.date]]; + break; + case WPMediaTypeVideo: + label = [NSString stringWithFormat:NSLocalizedString(@"Video, %@", @"Accessibility label for video thumbnails in the media collection view. The parameter is the creation date of the video."), + [[[self class] dateFormatter] stringFromDate:_asset.date]]; + NSTimeInterval duration = [asset duration]; + caption = [self stringFromTimeInterval:duration]; + break; + default: + break; + } + self.imageView.accessibilityLabel = label; + [self setCaption:caption]; +} + ++ (NSDateFormatter *) dateFormatter { + static NSDateFormatter *_dateFormatter = nil; + static dispatch_once_t _onceToken; + dispatch_once(&_onceToken, ^{ + _dateFormatter = [[NSDateFormatter alloc] init]; + _dateFormatter.dateStyle = NSDateFormatterMediumStyle; + _dateFormatter.timeStyle = NSDateFormatterMediumStyle; + }); + + return _dateFormatter; +} + +- (NSString *)stringFromTimeInterval:(NSTimeInterval)timeInterval +{ + NSInteger roundedHours = floor(timeInterval / 3600); + NSInteger roundedMinutes = floor((timeInterval - (3600 * roundedHours)) / 60); + NSInteger roundedSeconds = round(timeInterval - (roundedHours * 60 * 60) - (roundedMinutes * 60)); + + if (roundedHours > 0) + return [NSString stringWithFormat:@"%ld:%02ld:%02ld", (long)roundedHours, (long)roundedMinutes, (long)roundedSeconds]; + + else + return [NSString stringWithFormat:@"%ld:%02ld", (long)roundedMinutes, (long)roundedSeconds]; +} + +- (void)setImage:(UIImage *)image { - [self setImage:image animated:YES withAccessibilityLabel:accessibilityLabel]; + [self setImage:image animated:YES]; } -- (void)setImage:(UIImage *)image animated:(BOOL)animated withAccessibilityLabel:(NSString*)accessibilityLabel +- (void)setImage:(UIImage *)image animated:(BOOL)animated { if (!image){ self.imageView.alpha = 0; self.imageView.image = nil; - self.imageView.accessibilityLabel = nil; } else { if (animated) { [UIView animateWithDuration:0.3 animations:^{ self.imageView.alpha = 1.0; self.imageView.image = image; - self.imageView.accessibilityLabel = accessibilityLabel; }]; } else { self.imageView.alpha = 1.0; self.imageView.image = image; - self.imageView.accessibilityLabel = accessibilityLabel; } } } diff --git a/Pod/Classes/WPMediaCollectionViewController.m b/Pod/Classes/WPMediaCollectionViewController.m index c5e5eefc..90100ce0 100644 --- a/Pod/Classes/WPMediaCollectionViewController.m +++ b/Pod/Classes/WPMediaCollectionViewController.m @@ -380,24 +380,6 @@ - (BOOL)isCaptureCellIndexPath:(NSIndexPath *)indexPath return asset; } -- (NSString *)accessibilityLabelForType:(WPMediaType)assetType atIndexPath:(NSInteger)index -{ - NSString *accessibilityLabelFormat; - switch (assetType) { - case WPMediaTypeImage: - accessibilityLabelFormat = NSLocalizedString(@"Asset %d, image.", @"Accessibility label for image thumbnails in the media collection view. The parameter is the index of the image in the collection view."); - break; - case WPMediaTypeVideo: - accessibilityLabelFormat = NSLocalizedString(@"Asset %d, video", @"Accessibility label for video thumbnails in the media collection view. The parameter is the index of the video in the collection view."); - break; - default: - accessibilityLabelFormat = NSLocalizedString(@"Asset %d", @"Accessibility label for asset (of unknown type) thumbnails in the media collection view. The parameter is the index of the asset in the collection view."); - break; - } - - return [NSString stringWithFormat:accessibilityLabelFormat, index]; -} - - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { if ([self isCaptureCellIndexPath:indexPath]) { @@ -408,40 +390,9 @@ - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cell id asset = [self assetForPosition:indexPath]; WPMediaCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([WPMediaCollectionViewCell class]) forIndexPath:indexPath]; - if (cell.tag != 0) { - [asset cancelImageRequest:(WPMediaRequestID)cell.tag]; - cell.tag = 0; - } + // Configure the cell - __block WPMediaRequestID requestKey = 0; - NSTimeInterval timestamp = [NSDate timeIntervalSinceReferenceDate]; - NSInteger itemIndex = indexPath.item + 1; // Asset position + 1 to avoid starting at "Asset 0" - WPMediaType assetType = asset.assetType; - requestKey = [asset imageWithSize:cell.frame.size completionHandler:^(UIImage *result, NSError *error) { - BOOL animated = ([NSDate timeIntervalSinceReferenceDate] - timestamp) > 0.03; - if (error) { - cell.image = nil; - NSLog(@"%@", [error localizedDescription]); - return; - } - void (^setImage)() = ^{ - if (requestKey == cell.tag){ - NSString *accessibilityLabel = [self accessibilityLabelForType:assetType - atIndexPath:itemIndex]; - [cell setImage:result - animated:animated - withAccessibilityLabel:accessibilityLabel]; - } - }; - if ([NSThread isMainThread]){ - setImage(); - } else { - dispatch_async(dispatch_get_main_queue(), ^{ - setImage(); - }); - } - }]; - cell.tag = requestKey; + cell.asset = asset; NSUInteger position = [self positionOfAssetInSelection:asset]; if (position != NSNotFound) { [self.collectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone]; @@ -456,30 +407,9 @@ - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cell cell.selected = NO; } - if ([asset assetType] == WPMediaTypeVideo) { - NSTimeInterval duration = [asset duration]; - NSString *caption = [self stringFromTimeInterval:duration]; - [cell setCaption:caption]; - } else { - [cell setCaption:@""]; - } - return cell; } -- (NSString *)stringFromTimeInterval:(NSTimeInterval)timeInterval -{ - NSInteger roundedHours = floor(timeInterval / 3600); - NSInteger roundedMinutes = floor((timeInterval - (3600 * roundedHours)) / 60); - NSInteger roundedSeconds = round(timeInterval - (roundedHours * 60 * 60) - (roundedMinutes * 60)); - - if (roundedHours > 0) - return [NSString stringWithFormat:@"%ld:%02ld:%02ld", (long)roundedHours, (long)roundedMinutes, (long)roundedSeconds]; - - else - return [NSString stringWithFormat:@"%ld:%02ld", (long)roundedMinutes, (long)roundedSeconds]; -} - - (NSUInteger)positionOfAssetInSelection:(id)asset { NSUInteger position = [self.selectedAssets indexOfObjectPassingTest:^BOOL(id loopAsset, NSUInteger idx, BOOL *stop) { diff --git a/WPMediaPicker.podspec b/WPMediaPicker.podspec index bfbedd4c..e10e5af6 100644 --- a/WPMediaPicker.podspec +++ b/WPMediaPicker.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = "WPMediaPicker" - s.version = "0.7.1" + s.version = "0.7.2" s.summary = "WPMediaPicker is an iOS controller that allows capture and picking of media assets." s.description = <<-DESC WPMediaPicker is an iOS controller that allows capture and picking of media assets. From 6b0ab0872ee945bf8d135ced2551522c51e8c9dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Se=CC=81rgio=20Este=CC=82va=CC=83o?= Date: Tue, 24 Nov 2015 23:46:34 +0000 Subject: [PATCH 2/2] Replace magic numbers with constants. --- Pod/Classes/WPMediaCollectionViewCell.m | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Pod/Classes/WPMediaCollectionViewCell.m b/Pod/Classes/WPMediaCollectionViewCell.m index 1aaf7edb..255081b6 100644 --- a/Pod/Classes/WPMediaCollectionViewCell.m +++ b/Pod/Classes/WPMediaCollectionViewCell.m @@ -1,5 +1,8 @@ #import "WPMediaCollectionViewCell.h" +static const NSTimeInterval ThredsholdForAnimation = 0.03; +static const CGFloat TimeForFadeAnimation = 0.3; + @interface WPMediaCollectionViewCell () @property (nonatomic, strong) UILabel *positionLabel; @@ -81,7 +84,7 @@ - (void)setAsset:(id)asset { __block WPMediaRequestID requestKey = 0; NSTimeInterval timestamp = [NSDate timeIntervalSinceReferenceDate]; requestKey = [_asset imageWithSize:self.frame.size completionHandler:^(UIImage *result, NSError *error) { - BOOL animated = ([NSDate timeIntervalSinceReferenceDate] - timestamp) > 0.03; + BOOL animated = ([NSDate timeIntervalSinceReferenceDate] - timestamp) > ThredsholdForAnimation; if (error) { self.image = nil; NSLog(@"%@", [error localizedDescription]); @@ -160,7 +163,7 @@ - (void)setImage:(UIImage *)image animated:(BOOL)animated self.imageView.image = nil; } else { if (animated) { - [UIView animateWithDuration:0.3 animations:^{ + [UIView animateWithDuration:TimeForFadeAnimation animations:^{ self.imageView.alpha = 1.0; self.imageView.image = image; }];