MWGridCell.m 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 "DACircularProgressView.h"
  12. @interface MWGridCell () {
  13. UIImageView *_imageView;
  14. UIImageView *_loadingError;
  15. DACircularProgressView *_loadingIndicator;
  16. UIButton *_selectedButton;
  17. }
  18. @end
  19. @implementation MWGridCell
  20. - (id)initWithFrame:(CGRect)frame {
  21. if ((self = [super initWithFrame:frame])) {
  22. // Grey background
  23. self.backgroundColor = [UIColor colorWithWhite:0.12 alpha:1];
  24. // Image
  25. _imageView = [UIImageView new];
  26. _imageView.frame = self.bounds;
  27. _imageView.contentMode = UIViewContentModeScaleAspectFill;
  28. _imageView.clipsToBounds = YES;
  29. _imageView.autoresizesSubviews = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
  30. [self addSubview:_imageView];
  31. // Selection button
  32. _selectedButton = [UIButton buttonWithType:UIButtonTypeCustom];
  33. _selectedButton.contentMode = UIViewContentModeTopRight;
  34. _selectedButton.adjustsImageWhenHighlighted = NO;
  35. [_selectedButton setImage:nil forState:UIControlStateNormal];
  36. [_selectedButton setImage:[UIImage imageNamed:@"MWPhotoBrowser.bundle/images/ImageSelectedSmallOff.png"] forState:UIControlStateNormal];
  37. [_selectedButton setImage:[UIImage imageNamed:@"MWPhotoBrowser.bundle/images/ImageSelectedSmallOn.png"] forState:UIControlStateSelected];
  38. [_selectedButton addTarget:self action:@selector(selectionButtonPressed) forControlEvents:UIControlEventTouchDown];
  39. _selectedButton.hidden = YES;
  40. _selectedButton.frame = CGRectMake(0, 0, 44, 44);
  41. [self addSubview:_selectedButton];
  42. // Loading indicator
  43. _loadingIndicator = [[DACircularProgressView alloc] initWithFrame:CGRectMake(0, 0, 40.0f, 40.0f)];
  44. _loadingIndicator.userInteractionEnabled = NO;
  45. if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7")) {
  46. _loadingIndicator.thicknessRatio = 0.1;
  47. _loadingIndicator.roundedCorners = NO;
  48. } else {
  49. _loadingIndicator.thicknessRatio = 0.2;
  50. _loadingIndicator.roundedCorners = YES;
  51. }
  52. [self addSubview:_loadingIndicator];
  53. // Listen for photo loading notifications
  54. [[NSNotificationCenter defaultCenter] addObserver:self
  55. selector:@selector(setProgressFromNotification:)
  56. name:MWPHOTO_PROGRESS_NOTIFICATION
  57. object:nil];
  58. [[NSNotificationCenter defaultCenter] addObserver:self
  59. selector:@selector(handleMWPhotoLoadingDidEndNotification:)
  60. name:MWPHOTO_LOADING_DID_END_NOTIFICATION
  61. object:nil];
  62. }
  63. return self;
  64. }
  65. - (void)dealloc {
  66. [[NSNotificationCenter defaultCenter] removeObserver:self];
  67. }
  68. #pragma mark - View
  69. - (void)layoutSubviews {
  70. [super layoutSubviews];
  71. _imageView.frame = self.bounds;
  72. _loadingIndicator.frame = CGRectMake(floorf((self.bounds.size.width - _loadingIndicator.frame.size.width) / 2.),
  73. floorf((self.bounds.size.height - _loadingIndicator.frame.size.height) / 2),
  74. _loadingIndicator.frame.size.width,
  75. _loadingIndicator.frame.size.height);
  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. _loadingIndicator.progress = 0;
  85. _selectedButton.hidden = YES;
  86. [self hideImageFailure];
  87. [super prepareForReuse];
  88. }
  89. #pragma mark - Image Handling
  90. - (void)setPhoto:(id <MWPhoto>)photo {
  91. _photo = photo;
  92. if (_photo) {
  93. if (![_photo underlyingImage]) {
  94. [self showLoadingIndicator];
  95. } else {
  96. [self hideLoadingIndicator];
  97. }
  98. } else {
  99. [self showImageFailure];
  100. }
  101. }
  102. - (void)displayImage {
  103. _imageView.image = [_photo underlyingImage];
  104. _selectedButton.hidden = !_selectionMode;
  105. [self hideImageFailure];
  106. }
  107. #pragma mark - Selection
  108. - (void)setSelectionMode:(BOOL)selectionMode {
  109. _selectionMode = selectionMode;
  110. }
  111. - (void)setIsSelected:(BOOL)isSelected {
  112. _isSelected = isSelected;
  113. _selectedButton.selected = isSelected;
  114. }
  115. - (void)selectionButtonPressed {
  116. _selectedButton.selected = !_selectedButton.selected;
  117. [_gridController.browser setPhotoSelected:_selectedButton.selected atIndex:_index];
  118. }
  119. #pragma mark - Touches
  120. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
  121. _imageView.alpha = 0.6;
  122. [super touchesBegan:touches withEvent:event];
  123. }
  124. - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
  125. _imageView.alpha = 1;
  126. [super touchesEnded:touches withEvent:event];
  127. }
  128. - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
  129. _imageView.alpha = 1;
  130. [super touchesCancelled:touches withEvent:event];
  131. }
  132. #pragma mark Indicators
  133. - (void)hideLoadingIndicator {
  134. _loadingIndicator.hidden = YES;
  135. }
  136. - (void)showLoadingIndicator {
  137. _loadingIndicator.progress = 0;
  138. _loadingIndicator.hidden = NO;
  139. [self hideImageFailure];
  140. }
  141. - (void)showImageFailure {
  142. if (!_loadingError) {
  143. _loadingError = [UIImageView new];
  144. _loadingError.image = [UIImage imageNamed:@"MWPhotoBrowser.bundle/images/ImageError.png"];
  145. _loadingError.userInteractionEnabled = NO;
  146. [_loadingError sizeToFit];
  147. [self addSubview:_loadingError];
  148. }
  149. [self hideLoadingIndicator];
  150. _imageView.image = nil;
  151. _loadingError.frame = CGRectMake(floorf((self.bounds.size.width - _loadingError.frame.size.width) / 2.),
  152. floorf((self.bounds.size.height - _loadingError.frame.size.height) / 2),
  153. _loadingError.frame.size.width,
  154. _loadingError.frame.size.height);
  155. }
  156. - (void)hideImageFailure {
  157. if (_loadingError) {
  158. [_loadingError removeFromSuperview];
  159. _loadingError = nil;
  160. }
  161. }
  162. #pragma mark - Notifications
  163. - (void)setProgressFromNotification:(NSNotification *)notification {
  164. NSDictionary *dict = [notification object];
  165. id <MWPhoto> photoWithProgress = [dict objectForKey:@"photo"];
  166. if (photoWithProgress == _photo) {
  167. // NSLog(@"%f", [[dict valueForKey:@"progress"] floatValue]);
  168. float progress = [[dict valueForKey:@"progress"] floatValue];
  169. _loadingIndicator.progress = MAX(MIN(1, progress), 0);
  170. }
  171. }
  172. - (void)handleMWPhotoLoadingDidEndNotification:(NSNotification *)notification {
  173. id <MWPhoto> photo = [notification object];
  174. if (photo == _photo) {
  175. if ([photo underlyingImage]) {
  176. // Successful load
  177. [self displayImage];
  178. } else {
  179. // Failed to load
  180. [self showImageFailure];
  181. }
  182. [self hideLoadingIndicator];
  183. }
  184. }
  185. @end