// // YMEditProfileAlbumView.m // MSYOUPAI // // Created by YoMi on 2024/2/18. // Copyright © 2024 MS. All rights reserved. // #import "YMEditProfileAlbumView.h" #import "YMEditProfileViewModel.h" #import "YMEditProfileAlbumCell.h" #import "CHTCollectionViewWaterfallLayout.h" @interface YMEditProfileAlbumView () /// 编辑资料VM @property (nonatomic, strong) YMEditProfileViewModel *viewModel; /// 相册标题标签 @property (nonatomic, strong) UILabel *albumTitleLb; /// 相册排版 @property (nonatomic, strong) CHTCollectionViewWaterfallLayout *albumLayout; /// 相册视图 @property (nonatomic, strong) UICollectionView *albumCollectionView; /// 提示标签 @property (nonatomic, strong) UILabel *albumTipsLb; @end @implementation YMEditProfileAlbumView - (void)ym_setupViews{ [self addSubview:self.albumTitleLb]; [self addSubview:self.albumCollectionView]; [self addSubview:self.albumTipsLb]; [self setNeedsUpdateConstraints]; [self updateConstraintsIfNeeded]; } - (void)updateConstraints{ [self.albumTitleLb mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self).offset(adapt(10)); make.left.equalTo(self).offset(adapt(15)); }]; [self.albumCollectionView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.albumTitleLb.mas_bottom).offset(adapt(10)); make.left.equalTo(self); make.right.equalTo(self); }]; [self.albumTipsLb mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.albumCollectionView.mas_bottom).offset(adapt(10)); make.left.equalTo(self).offset(adapt(15)); make.right.equalTo(self).offset(adapt(-15)); make.bottom.equalTo(self).offset(adapt(-10)); }]; [super updateConstraints]; } - (void)ym_bindViewModel:(YMEditProfileViewModel *)viewModel{ if (!viewModel) { return; } _viewModel = viewModel; @weakify(self) [[self.viewModel.refreshUISubject takeUntil:self.rac_willDeallocSignal] subscribeNext:^(id result) { @strongify(self) [self.albumCollectionView reloadData]; [self.albumCollectionView layoutIfNeeded]; //刷新高度 CGFloat albumCollectionViewHeight = self.albumCollectionView.collectionViewLayout.collectionViewContentSize.height; [self.albumCollectionView mas_updateConstraints:^(MASConstraintMaker *make) { make.height.mas_equalTo(albumCollectionViewHeight); }]; }]; } - (NSInteger)calculationAddItem{ NSInteger result = 0; for (int i = 1; i <= self.viewModel.albumDataArray.count; i++) { result = i; } return self.viewModel.albumDataArray.count + 6 - result; } #pragma mark - UICollectionViewDataSource - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 1; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return !self.viewModel.isHideAddCell ? [self calculationAddItem]: self.viewModel.albumDataArray.count; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { if (!self.viewModel.isHideAddCell) { if (self.viewModel.albumDataArray.count <= indexPath.item) { YMEditProfileAlbumCell *addCell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([YMEditProfileAlbumCell class]) forIndexPath:indexPath]; [addCell ym_bindViewModel:self.viewModel.addCellViewModel]; return addCell; } } YMEditProfileAlbumCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([YMEditProfileAlbumCell class]) forIndexPath:indexPath]; [cell ym_bindViewModel:self.viewModel.albumDataArray[indexPath.item]]; return cell; } - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{ UICollectionReusableView *reusableView = nil; reusableView.backgroundColor = [UIColor clearColor]; return reusableView; } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{ return CGSizeZero; } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{ return CGSizeZero; } ///允许移动的item - (BOOL)collectionView:(UICollectionView*)collectionView canMoveItemAtIndexPath:(nonnull NSIndexPath *)indexPath{ if (indexPath.item >= self.viewModel.albumDataArray.count){ return NO; } return YES; } ///对交换过的item数据进行操作 - (void)collectionView:(UICollectionView*)collectionView moveItemAtIndexPath:(nonnull NSIndexPath *)sourceIndexPath toIndexPath:(nonnull NSIndexPath *)destinationIndexPath{ NSMutableArray *tempArr = [NSMutableArray arrayWithArray:self.viewModel.albumDataArray]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.35 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ if (destinationIndexPath.row >= tempArr.count){ [self.albumCollectionView reloadData]; return; } if (sourceIndexPath.row >= tempArr.count){ [self.albumCollectionView reloadData]; return; } [tempArr exchangeObjectAtIndex:sourceIndexPath.item withObjectAtIndex:destinationIndexPath.item]; [self.viewModel.refreshAlbumSortSubject sendNext:tempArr]; }); } ///抑制item移动 指定位置禁止交换 - (NSIndexPath *)collectionView:(UICollectionView *)collectionView targetIndexPathForMoveOfItemFromOriginalIndexPath:(NSIndexPath *)originalIndexPath atCurrentIndexPath:(NSIndexPath *)currentIndexPath toProposedIndexPath:(NSIndexPath *)proposedIndexPath { return proposedIndexPath; } #pragma mark - UICollectionViewDelegate - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{ if (!self.viewModel.isHideAddCell && self.viewModel.albumDataArray.count <= indexPath.item) { [self.viewModel openAlbumPickerPopupView]; }else{ NSMutableArray *tempArr = [NSMutableArray array]; for (YMEditProfileAlbumCellViewModel *model in self.viewModel.albumDataArray) { YBIBImageData *data = [YBIBImageData new]; if (!OCStringIsEmpty(model.networkAlbum)) { data.imageURL = [LCTools getImageUrlWithAddress:model.networkAlbum]; } else { data.image = ^UIImage * _Nullable{ return model.localAlbum; }; } data.projectiveView = [collectionView cellForItemAtIndexPath:indexPath]; [tempArr addObject:data]; } YBImageBrowser *browser = [YBImageBrowser new]; browser.delegate = self; browser.dataSourceArray = tempArr; browser.currentPage = indexPath.item; [browser.defaultToolViewHandler.topView.operationButton setHidden:false]; [browser.defaultToolViewHandler.topView.operationButton setTitle:@"删除" forState:UIControlStateNormal]; [browser.defaultToolViewHandler.topView.operationButton setImage:NULL forState:UIControlStateNormal]; WS(weakSelf) [browser.defaultToolViewHandler.topView setClickOperation:^(YBIBTopViewOperationType type) { YMTipsPopupView *customView = [[YMTipsPopupView alloc]init]; customView.confirmTitle = @"删除"; [customView configutationWithTips:@"是否删除这张照片" TipsAlignment:NSTextAlignmentCenter IsHideTitle:NO IsHideSingleButton:YES]; YMPopupView *popupView = [YMPopupView initWithCustomView:customView parentView:nil popStyle:YMPopupStyleFade dismissStyle:YMDismissStyleFade]; popupView.priority = 999; popupView.cornerRadius = adapt(10); popupView.rectCorners = UIRectCornerAllCorners; popupView.positionStyle = YMPositionStyleCenter; popupView.isHideBg = NO; popupView.bgAlpha = 0.3; @weakify(popupView) customView.buttonBlock = ^(BOOL isConfirm) { @strongify(popupView) if (isConfirm) { [weakSelf.viewModel.deleteAlbumSubject sendNext:@(browser.currentPage)]; [browser hide]; } [popupView dismissWithStyle:YMDismissStyleFade duration:2.0]; }; [popupView pop]; }]; [browser show]; } } #pragma mark - CHTCollectionViewDelegateWaterfallLayout - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { return CGSizeMake(adapt(100), adapt(100)); } #pragma mark - 拖拽事件照片处理 - (void)longPressAction:(UIGestureRecognizer *)gesture{ UIGestureRecognizerState state = gesture.state; CGPoint location = [gesture locationInView:self.albumCollectionView]; switch(state) { //手势开始 case UIGestureRecognizerStateBegan: { //判断手势落点位置是否在路径上 NSIndexPath *indexPath = [self.albumCollectionView indexPathForItemAtPoint:location]; if(indexPath == nil) { break; } //在路径上则开始移动该路径上的cell [self.albumCollectionView beginInteractiveMovementForItemAtIndexPath:indexPath]; YMEditProfileAlbumCell *cell = (YMEditProfileAlbumCell*)[self.albumCollectionView cellForItemAtIndexPath:indexPath]; [UIView animateWithDuration:0.35 animations:^{ cell.alpha = 0.6; }]; break; } //手势变化 case UIGestureRecognizerStateChanged: { //移动过程当中随时更新cell位置 [self.albumCollectionView updateInteractiveMovementTargetPosition:location]; break; } case UIGestureRecognizerStateEnded: { //移动结束后关闭cell移动 [self.albumCollectionView endInteractiveMovement]; NSIndexPath *indexPath = [self.albumCollectionView indexPathForItemAtPoint:location]; if(indexPath == nil) { break; } YMEditProfileAlbumCell *cell = (YMEditProfileAlbumCell*)[self.albumCollectionView cellForItemAtIndexPath:indexPath]; [UIView animateWithDuration:0.35 animations:^{ cell.alpha = 1; }]; break; } //手势结束 default: { NSIndexPath *indexPath = [self.albumCollectionView indexPathForItemAtPoint:location]; if(indexPath == nil) { break; } YMEditProfileAlbumCell *cell = (YMEditProfileAlbumCell*)[self.albumCollectionView cellForItemAtIndexPath:indexPath]; [UIView animateWithDuration:0.35 animations:^{ cell.alpha = 1; }]; break; } } } - (UILabel *)albumTitleLb{ if (!_albumTitleLb) { _albumTitleLb = [[UILabel alloc]init]; _albumTitleLb.font = LCBoldFont(16); _albumTitleLb.textColor = HexColorFromRGB(0x333333); _albumTitleLb.textAlignment = NSTextAlignmentLeft; _albumTitleLb.text = @"展示照片"; } return _albumTitleLb; } - (CHTCollectionViewWaterfallLayout *)albumLayout{ if (!_albumLayout) { _albumLayout = [[CHTCollectionViewWaterfallLayout alloc] init]; _albumLayout.columnCount = 3; _albumLayout.sectionInset = UIEdgeInsetsMake(adapt(10), adapt(10), adapt(10), adapt(10)); } return _albumLayout; } - (UICollectionView *)albumCollectionView{ if (!_albumCollectionView) { _albumCollectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:self.albumLayout]; _albumCollectionView.delegate = self; _albumCollectionView.dataSource = self; _albumCollectionView.showsVerticalScrollIndicator = NO; _albumCollectionView.showsHorizontalScrollIndicator = NO; _albumCollectionView.backgroundColor = UIColor.whiteColor; [_albumCollectionView registerClass:[YMEditProfileAlbumCell class] forCellWithReuseIdentifier:NSStringFromClass([YMEditProfileAlbumCell class])]; UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAction:)]; [_albumCollectionView addGestureRecognizer:longPress]; } return _albumCollectionView; } - (UILabel *)albumTipsLb{ if (!_albumTipsLb) { _albumTipsLb = [[UILabel alloc]init]; _albumTipsLb.font = LCFont(12); _albumTipsLb.textColor = HexColorFromRGB(0x9C9C9C); _albumTipsLb.textAlignment = NSTextAlignmentLeft; _albumTipsLb.text = @"1、拖拽相片可以更改排序,单击查看大图可删除相册;"; //1、拖拽相片可以更改排序,单击查看大图可删除相册\n2、照片涉黄或者审核不通过将被删除,严重者冻结账号 } return _albumTipsLb; } @end