XSBRPhotoAlbumBottomChoseViewController.m 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. //
  2. // XSBRPhotoAlbumBottomChoseViewController.m
  3. // 般若
  4. //
  5. // Created by shanp on 2020/9/18.
  6. //
  7. #import "XSBRPhotoAlbumBottomChoseViewController.h"
  8. #import "XSBRUIGeneralHeader.h"
  9. #import "XSBRBusinessManager.h"
  10. #import "JXHPhotoManager.h"
  11. #import "XSBRNotificationDelegateManager.h"
  12. @interface XSBRPhotoAlbumBottomChoseViewCell : UICollectionViewCell {
  13. UIImageView *_imageView;
  14. UIView *_choseView;
  15. }
  16. @property (nonatomic, weak) JXHPhotoManager *xsbr_photoManager;
  17. @end
  18. @implementation XSBRPhotoAlbumBottomChoseViewCell
  19. - (instancetype)initWithFrame:(CGRect)frame {
  20. self = [super initWithFrame:frame];
  21. if (!self) return nil;
  22. self.backgroundColor = xsbr_1C1A19_color();
  23. _imageView = UIImageView.new;
  24. _imageView.contentMode = UIViewContentModeScaleAspectFill;
  25. _imageView.clipsToBounds = true;
  26. [self.contentView addSubview:_imageView];
  27. [_imageView makeConstraints:^(JXHConstraintMaker *make) {
  28. make.edges.equalTo(self.contentView);
  29. }];
  30. _choseView = UIView.new;
  31. _choseView.hidden = true;
  32. [_choseView addCornerWithRadius:0 size:(CGSize){64, 64} borderWidth:4 borderColor:xsbr_AF8C4A_color() backgroundColor:nil];
  33. [self.contentView addSubview:_choseView];
  34. [_choseView makeConstraints:^(JXHConstraintMaker *make) {
  35. make.edges.equalTo(self.contentView);
  36. }];
  37. return self;
  38. }
  39. - (void)prepareForReuse {
  40. [super prepareForReuse];
  41. _imageView.image = nil;
  42. }
  43. - (void)reloadImage:(UIImage *)image {
  44. _imageView.image = image;
  45. }
  46. - (void)setChose:(bool)isChose {
  47. _choseView.hidden = !isChose;
  48. }
  49. @end
  50. @interface XSBRPhotoAlbumBottomChoseViewController ()<XSBRNotificationDelegate> {
  51. NSMutableArray<JXHPhotoManager *> *_dataArray;
  52. }
  53. @end
  54. @implementation XSBRPhotoAlbumBottomChoseViewController
  55. - (instancetype)init {
  56. self = [super initWithCollectionViewLayout:[self createCollectionViewFlowLayout]];
  57. if (!self) return nil;
  58. _dataArray = [xsbr_currentChosePublishPhotos() mutableCopy];
  59. return self;
  60. }
  61. - (void)viewDidLoad {
  62. [super viewDidLoad];
  63. self.view.backgroundColor = xsbr_1C1A19_color();
  64. [self setupCollectionView];
  65. xsbr_setNotificationKey(XSBRChoosePublishPhotosNotificationKey);
  66. }
  67. - (void)dealloc {
  68. xsbr_removeNotificationKey(XSBRChoosePublishPhotosNotificationKey);
  69. }
  70. #pragma mark - Action
  71. - (void)setXsbr_currentItem:(JXHPhotoManager *)xsbr_currentItem {
  72. _xsbr_currentItem = xsbr_currentItem;
  73. [self.collectionView reloadData];
  74. dispatch_async(dispatch_get_main_queue(), ^{
  75. [self scrollToItem:xsbr_currentItem];
  76. });
  77. }
  78. - (void)scrollToItem:(JXHPhotoManager *)item {
  79. NSInteger index = [_dataArray indexOfObject:item];
  80. if (index < 0 || index >= _dataArray.count) return;
  81. [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:index inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:true];
  82. }
  83. #pragma mark - XSBRNotificationDelegate
  84. - (void)xsbr_currenChosePublishPhotosChanged:(NSArray<JXHPhotoManager *> *)currentArray chose:(JXHPhotoManager *)chose {
  85. if ([_dataArray containsObject:chose]) {
  86. @weakify(self)
  87. [self.collectionView performBatchUpdates:^{
  88. @strongify(self)
  89. NSInteger index = [self->_dataArray indexOfObject:chose];
  90. self->_dataArray = [currentArray mutableCopy];
  91. [self.collectionView deleteItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:index inSection:0]]];
  92. } completion:nil];
  93. } else {
  94. @weakify(self)
  95. [self.collectionView performBatchUpdates:^{
  96. @strongify(self)
  97. self->_dataArray = [currentArray mutableCopy];
  98. [self.collectionView insertItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:self->_dataArray.count - 1 inSection:0]]];
  99. } completion:nil];
  100. }
  101. }
  102. #pragma mark - <UICollectionViewDataSource>
  103. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  104. return _dataArray.count;
  105. }
  106. static NSString * const reuseIdentifier = @"XSBRPhotoAlbumBottomChoseViewCell";
  107. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  108. XSBRPhotoAlbumBottomChoseViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
  109. if (indexPath.item < _dataArray.count) {
  110. JXHPhotoManager *photoManager = _dataArray[indexPath.item];
  111. cell.xsbr_photoManager = photoManager;
  112. [cell setChose:[photoManager isEqual:_xsbr_currentItem]];
  113. [photoManager requestThumbnailImageWithSize:(CGSize){60, 60} contentMode:JXHImageContentModeAspectFill completion:^(UIImage * _Nullable image, NSDictionary * _Nullable info) {
  114. if (cell.xsbr_photoManager == photoManager) {
  115. [cell reloadImage:image];
  116. }
  117. }];
  118. }
  119. return cell;
  120. }
  121. #pragma mark - <UICollectionViewDelegate>
  122. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  123. if (_xsbr_chooseAction) {
  124. _xsbr_chooseAction(indexPath.item);
  125. }
  126. }
  127. #pragma mark - UI
  128. - (void)setupCollectionView {
  129. self.collectionView.backgroundColor = xsbr_1C1A19_color();
  130. self.collectionView.showsHorizontalScrollIndicator = false;
  131. [self.collectionView registerClass:[XSBRPhotoAlbumBottomChoseViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
  132. }
  133. - (UICollectionViewFlowLayout *)createCollectionViewFlowLayout {
  134. UICollectionViewFlowLayout *layout = UICollectionViewFlowLayout.new;
  135. layout.scrollDirection = UICollectionViewScrollDirectionVertical;
  136. layout.itemSize = (CGSize){64, 64};
  137. layout.minimumLineSpacing = 12;
  138. layout.minimumInteritemSpacing = 0;
  139. layout.sectionInset = (UIEdgeInsets){16, 16, 16, 16};
  140. layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  141. return layout;
  142. }
  143. @end