MWGridCell.m 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. //
  2. // MWGridCell.m
  3. // MWPhotoBrowser
  4. //
  5. // Created by Michael Waterfall on 08/10/2013.
  6. //
  7. //
  8. #import "MWGridCell.h"
  9. #import "MWCommon.h"
  10. #import "MWPhotoBrowserPrivate.h"
  11. #import "UIImage+MWPhotoBrowser.h"
  12. #define VIDEO_INDICATOR_PADDING 10
  13. @interface MWGridCell () {
  14. UIImageView *_imageView;
  15. UIImageView *_videoIndicator;
  16. UIImageView *_loadingError;
  17. UIButton *_selectedButton;
  18. }
  19. @end
  20. @implementation MWGridCell
  21. - (id)initWithFrame:(CGRect)frame {
  22. if ((self = [super initWithFrame:frame])) {
  23. // Grey background
  24. self.backgroundColor = [UIColor colorWithWhite:0.12 alpha:1];
  25. // Image
  26. _imageView = [UIImageView new];
  27. _imageView.frame = self.bounds;
  28. _imageView.contentMode = UIViewContentModeScaleAspectFill;
  29. _imageView.clipsToBounds = YES;
  30. _imageView.autoresizesSubviews = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
  31. [self addSubview:_imageView];
  32. // Video Image
  33. _videoIndicator = [UIImageView new];
  34. _videoIndicator.hidden = NO;
  35. UIImage *videoIndicatorImage = [UIImage imageForResourcePath:@"VideoOverlay" ofType:@"png" inBundle:[NSBundle bundleForClass:[self class]]];
  36. _videoIndicator.frame = CGRectMake(self.bounds.size.width - videoIndicatorImage.size.width - VIDEO_INDICATOR_PADDING, self.bounds.size.height - videoIndicatorImage.size.height - VIDEO_INDICATOR_PADDING, videoIndicatorImage.size.width, videoIndicatorImage.size.height);
  37. _videoIndicator.image = videoIndicatorImage;
  38. _videoIndicator.autoresizesSubviews = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin;
  39. [self addSubview:_videoIndicator];
  40. // Selection button
  41. _selectedButton = [UIButton buttonWithType:UIButtonTypeCustom];
  42. _selectedButton.contentMode = UIViewContentModeTopRight;
  43. _selectedButton.adjustsImageWhenHighlighted = NO;
  44. [_selectedButton setImage:[UIImage imageForResourcePath:@"ImageSelectedSmallOff" ofType:@"png" inBundle:[NSBundle bundleForClass:[self class]]] forState:UIControlStateNormal];
  45. [_selectedButton setImage:[UIImage imageForResourcePath:@"ImageSelectedSmallOn" ofType:@"png" inBundle:[NSBundle bundleForClass:[self class]]] forState:UIControlStateSelected];
  46. [_selectedButton addTarget:self action:@selector(selectionButtonPressed) forControlEvents:UIControlEventTouchDown];
  47. _selectedButton.hidden = YES;
  48. _selectedButton.frame = CGRectMake(0, 0, 44, 44);
  49. [self addSubview:_selectedButton];
  50. // Listen for photo loading notifications
  51. [[NSNotificationCenter defaultCenter] addObserver:self
  52. selector:@selector(setProgressFromNotification:)
  53. name:MWPHOTO_PROGRESS_NOTIFICATION
  54. object:nil];
  55. [[NSNotificationCenter defaultCenter] addObserver:self
  56. selector:@selector(handleMWPhotoLoadingDidEndNotification:)
  57. name:MWPHOTO_LOADING_DID_END_NOTIFICATION
  58. object:nil];
  59. }
  60. return self;
  61. }
  62. - (void)dealloc {
  63. [[NSNotificationCenter defaultCenter] removeObserver:self];
  64. }
  65. - (void)setGridController:(MWGridViewController *)gridController {
  66. _gridController = gridController;
  67. // Set custom selection image if required
  68. if (_gridController.browser.customImageSelectedSmallIconName) {
  69. [_selectedButton setImage:[UIImage imageNamed:_gridController.browser.customImageSelectedSmallIconName] forState:UIControlStateSelected];
  70. }
  71. }
  72. #pragma mark - View
  73. - (void)layoutSubviews {
  74. [super layoutSubviews];
  75. _imageView.frame = self.bounds;
  76. _selectedButton.frame = CGRectMake(self.bounds.size.width - _selectedButton.frame.size.width - 0,
  77. 0, _selectedButton.frame.size.width, _selectedButton.frame.size.height);
  78. }
  79. #pragma mark - Cell
  80. - (void)prepareForReuse {
  81. _photo = nil;
  82. _gridController = nil;
  83. _imageView.image = nil;
  84. _selectedButton.hidden = YES;
  85. [self hideImageFailure];
  86. [super prepareForReuse];
  87. }
  88. #pragma mark - Image Handling
  89. - (void)setPhoto:(id <MWPhoto>)photo {
  90. _photo = photo;
  91. if ([photo respondsToSelector:@selector(isVideo)]) {
  92. _videoIndicator.hidden = !photo.isVideo;
  93. } else {
  94. _videoIndicator.hidden = YES;
  95. }
  96. if (_photo) {
  97. } else {
  98. [self showImageFailure];
  99. }
  100. }
  101. - (void)displayImage {
  102. _imageView.image = [_photo underlyingImage];
  103. _selectedButton.hidden = !_selectionMode;
  104. [self hideImageFailure];
  105. }
  106. #pragma mark - Selection
  107. - (void)setSelectionMode:(BOOL)selectionMode {
  108. _selectionMode = selectionMode;
  109. }
  110. - (void)setIsSelected:(BOOL)isSelected {
  111. _isSelected = isSelected;
  112. _selectedButton.selected = isSelected;
  113. }
  114. - (void)selectionButtonPressed {
  115. _selectedButton.selected = !_selectedButton.selected;
  116. [_gridController.browser setPhotoSelected:_selectedButton.selected atIndex:_index];
  117. }
  118. #pragma mark - Touches
  119. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
  120. _imageView.alpha = 0.6;
  121. [super touchesBegan:touches withEvent:event];
  122. }
  123. - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
  124. _imageView.alpha = 1;
  125. [super touchesEnded:touches withEvent:event];
  126. }
  127. - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
  128. _imageView.alpha = 1;
  129. [super touchesCancelled:touches withEvent:event];
  130. }
  131. #pragma mark Indicators
  132. - (void)showImageFailure {
  133. // Only show if image is not empty
  134. if (![_photo respondsToSelector:@selector(emptyImage)] || !_photo.emptyImage) {
  135. if (!_loadingError) {
  136. _loadingError = [UIImageView new];
  137. _loadingError.image = [UIImage imageForResourcePath:@"ImageError" ofType:@"png" inBundle:[NSBundle bundleForClass:[self class]]];
  138. _loadingError.userInteractionEnabled = NO;
  139. [_loadingError sizeToFit];
  140. [self addSubview:_loadingError];
  141. }
  142. _loadingError.frame = CGRectMake(floorf((self.bounds.size.width - _loadingError.frame.size.width) / 2.),
  143. floorf((self.bounds.size.height - _loadingError.frame.size.height) / 2),
  144. _loadingError.frame.size.width,
  145. _loadingError.frame.size.height);
  146. }
  147. _imageView.image = nil;
  148. }
  149. - (void)hideImageFailure {
  150. if (_loadingError) {
  151. [_loadingError removeFromSuperview];
  152. _loadingError = nil;
  153. }
  154. }
  155. #pragma mark - Notifications
  156. - (void)setProgressFromNotification:(NSNotification *)notification {
  157. dispatch_async(dispatch_get_main_queue(), ^{
  158. NSDictionary *dict = [notification object];
  159. id <MWPhoto> photoWithProgress = [dict objectForKey:@"photo"];
  160. if (photoWithProgress == _photo) {
  161. // NSLog(@"%f", [[dict valueForKey:@"progress"] floatValue]);
  162. //float ioprogress = [[dict valueForKey:@"progress"] floatValue];
  163. //_loadingIndicator.progress = MAX(MIN(1, progress), 0);
  164. }
  165. });
  166. }
  167. - (void)handleMWPhotoLoadingDidEndNotification:(NSNotification *)notification {
  168. id <MWPhoto> photo = [notification object];
  169. if (photo == _photo) {
  170. if ([photo underlyingImage]) {
  171. // Successful load
  172. [self displayImage];
  173. } else {
  174. // Failed to load
  175. [self showImageFailure];
  176. }
  177. }
  178. }
  179. @end