123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- //
- // XSBRPhotoAlbumBottomChoseViewController.m
- // 般若
- //
- // Created by shanp on 2020/9/18.
- //
- #import "XSBRPhotoAlbumBottomChoseViewController.h"
- #import "XSBRUIGeneralHeader.h"
- #import "XSBRBusinessManager.h"
- #import "JXHPhotoManager.h"
- #import "XSBRNotificationDelegateManager.h"
- @interface XSBRPhotoAlbumBottomChoseViewCell : UICollectionViewCell {
- UIImageView *_imageView;
- UIView *_choseView;
- }
- @property (nonatomic, weak) JXHPhotoManager *xsbr_photoManager;
- @end
- @implementation XSBRPhotoAlbumBottomChoseViewCell
- - (instancetype)initWithFrame:(CGRect)frame {
- self = [super initWithFrame:frame];
- if (!self) return nil;
- self.backgroundColor = xsbr_1C1A19_color();
-
- _imageView = UIImageView.new;
- _imageView.contentMode = UIViewContentModeScaleAspectFill;
- _imageView.clipsToBounds = true;
- [self.contentView addSubview:_imageView];
- [_imageView makeConstraints:^(JXHConstraintMaker *make) {
- make.edges.equalTo(self.contentView);
- }];
-
- _choseView = UIView.new;
- _choseView.hidden = true;
- [_choseView addCornerWithRadius:0 size:(CGSize){64, 64} borderWidth:4 borderColor:xsbr_AF8C4A_color() backgroundColor:nil];
- [self.contentView addSubview:_choseView];
- [_choseView makeConstraints:^(JXHConstraintMaker *make) {
- make.edges.equalTo(self.contentView);
- }];
-
- return self;
- }
- - (void)prepareForReuse {
- [super prepareForReuse];
- _imageView.image = nil;
- }
- - (void)reloadImage:(UIImage *)image {
- _imageView.image = image;
- }
- - (void)setChose:(bool)isChose {
- _choseView.hidden = !isChose;
- }
- @end
- @interface XSBRPhotoAlbumBottomChoseViewController ()<XSBRNotificationDelegate> {
- NSMutableArray<JXHPhotoManager *> *_dataArray;
- }
- @end
- @implementation XSBRPhotoAlbumBottomChoseViewController
- - (instancetype)init {
- self = [super initWithCollectionViewLayout:[self createCollectionViewFlowLayout]];
- if (!self) return nil;
- _dataArray = [xsbr_currentChosePublishPhotos() mutableCopy];
- return self;
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
- self.view.backgroundColor = xsbr_1C1A19_color();
- [self setupCollectionView];
- xsbr_setNotificationKey(XSBRChoosePublishPhotosNotificationKey);
- }
- - (void)dealloc {
- xsbr_removeNotificationKey(XSBRChoosePublishPhotosNotificationKey);
- }
- #pragma mark - Action
- - (void)setXsbr_currentItem:(JXHPhotoManager *)xsbr_currentItem {
- _xsbr_currentItem = xsbr_currentItem;
- [self.collectionView reloadData];
- dispatch_async(dispatch_get_main_queue(), ^{
- [self scrollToItem:xsbr_currentItem];
- });
- }
- - (void)scrollToItem:(JXHPhotoManager *)item {
- NSInteger index = [_dataArray indexOfObject:item];
- if (index < 0 || index >= _dataArray.count) return;
- [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:index inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:true];
- }
- #pragma mark - XSBRNotificationDelegate
- - (void)xsbr_currenChosePublishPhotosChanged:(NSArray<JXHPhotoManager *> *)currentArray chose:(JXHPhotoManager *)chose {
- if ([_dataArray containsObject:chose]) {
- @weakify(self)
- [self.collectionView performBatchUpdates:^{
- @strongify(self)
- NSInteger index = [self->_dataArray indexOfObject:chose];
- self->_dataArray = [currentArray mutableCopy];
- [self.collectionView deleteItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:index inSection:0]]];
- } completion:nil];
- } else {
- @weakify(self)
- [self.collectionView performBatchUpdates:^{
- @strongify(self)
- self->_dataArray = [currentArray mutableCopy];
- [self.collectionView insertItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:self->_dataArray.count - 1 inSection:0]]];
- } completion:nil];
- }
- }
- #pragma mark - <UICollectionViewDataSource>
- - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
- return _dataArray.count;
- }
- static NSString * const reuseIdentifier = @"XSBRPhotoAlbumBottomChoseViewCell";
- - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
- XSBRPhotoAlbumBottomChoseViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
- if (indexPath.item < _dataArray.count) {
- JXHPhotoManager *photoManager = _dataArray[indexPath.item];
- cell.xsbr_photoManager = photoManager;
- [cell setChose:[photoManager isEqual:_xsbr_currentItem]];
- [photoManager requestThumbnailImageWithSize:(CGSize){60, 60} contentMode:JXHImageContentModeAspectFill completion:^(UIImage * _Nullable image, NSDictionary * _Nullable info) {
- if (cell.xsbr_photoManager == photoManager) {
- [cell reloadImage:image];
- }
- }];
- }
-
- return cell;
- }
- #pragma mark - <UICollectionViewDelegate>
- - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
- if (_xsbr_chooseAction) {
- _xsbr_chooseAction(indexPath.item);
- }
- }
- #pragma mark - UI
- - (void)setupCollectionView {
- self.collectionView.backgroundColor = xsbr_1C1A19_color();
- self.collectionView.showsHorizontalScrollIndicator = false;
- [self.collectionView registerClass:[XSBRPhotoAlbumBottomChoseViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
- }
- - (UICollectionViewFlowLayout *)createCollectionViewFlowLayout {
- UICollectionViewFlowLayout *layout = UICollectionViewFlowLayout.new;
- layout.scrollDirection = UICollectionViewScrollDirectionVertical;
- layout.itemSize = (CGSize){64, 64};
- layout.minimumLineSpacing = 12;
- layout.minimumInteritemSpacing = 0;
- layout.sectionInset = (UIEdgeInsets){16, 16, 16, 16};
- layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
- return layout;
- }
- @end
|