YMEditProfileAlbumView.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. //
  2. // YMEditProfileAlbumView.m
  3. // MSYOUPAI
  4. //
  5. // Created by YoMi on 2024/2/18.
  6. // Copyright © 2024 MS. All rights reserved.
  7. //
  8. #import "YMEditProfileAlbumView.h"
  9. #import "YMEditProfileViewModel.h"
  10. #import "YMEditProfileAlbumCell.h"
  11. #import "CHTCollectionViewWaterfallLayout.h"
  12. @interface YMEditProfileAlbumView ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout,CHTCollectionViewDelegateWaterfallLayout,TZImagePickerControllerDelegate,YBImageBrowserDelegate>
  13. /// 编辑资料VM
  14. @property (nonatomic, strong) YMEditProfileViewModel *viewModel;
  15. /// 相册标题标签
  16. @property (nonatomic, strong) UILabel *albumTitleLb;
  17. /// 相册排版
  18. @property (nonatomic, strong) CHTCollectionViewWaterfallLayout *albumLayout;
  19. /// 相册视图
  20. @property (nonatomic, strong) UICollectionView *albumCollectionView;
  21. /// 提示标签
  22. @property (nonatomic, strong) UILabel *albumTipsLb;
  23. @end
  24. @implementation YMEditProfileAlbumView
  25. - (void)ym_setupViews{
  26. [self addSubview:self.albumTitleLb];
  27. [self addSubview:self.albumCollectionView];
  28. [self addSubview:self.albumTipsLb];
  29. [self setNeedsUpdateConstraints];
  30. [self updateConstraintsIfNeeded];
  31. }
  32. - (void)updateConstraints{
  33. [self.albumTitleLb mas_makeConstraints:^(MASConstraintMaker *make) {
  34. make.top.equalTo(self).offset(adapt(10));
  35. make.left.equalTo(self).offset(adapt(15));
  36. }];
  37. [self.albumCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
  38. make.top.equalTo(self.albumTitleLb.mas_bottom).offset(adapt(10));
  39. make.left.equalTo(self);
  40. make.right.equalTo(self);
  41. }];
  42. [self.albumTipsLb mas_makeConstraints:^(MASConstraintMaker *make) {
  43. make.top.equalTo(self.albumCollectionView.mas_bottom).offset(adapt(10));
  44. make.left.equalTo(self).offset(adapt(15));
  45. make.right.equalTo(self).offset(adapt(-15));
  46. make.bottom.equalTo(self).offset(adapt(-10));
  47. }];
  48. [super updateConstraints];
  49. }
  50. - (void)ym_bindViewModel:(YMEditProfileViewModel *)viewModel{
  51. if (!viewModel) {
  52. return;
  53. }
  54. _viewModel = viewModel;
  55. @weakify(self)
  56. [[self.viewModel.refreshUISubject takeUntil:self.rac_willDeallocSignal] subscribeNext:^(id result) {
  57. @strongify(self)
  58. [self.albumCollectionView reloadData];
  59. [self.albumCollectionView layoutIfNeeded];
  60. //刷新高度
  61. CGFloat albumCollectionViewHeight = self.albumCollectionView.collectionViewLayout.collectionViewContentSize.height;
  62. [self.albumCollectionView mas_updateConstraints:^(MASConstraintMaker *make) {
  63. make.height.mas_equalTo(albumCollectionViewHeight);
  64. }];
  65. }];
  66. }
  67. - (NSInteger)calculationAddItem{
  68. NSInteger result = 0;
  69. for (int i = 1; i <= self.viewModel.albumDataArray.count; i++) {
  70. result = i;
  71. }
  72. return self.viewModel.albumDataArray.count + 6 - result;
  73. }
  74. #pragma mark - UICollectionViewDataSource
  75. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
  76. return 1;
  77. }
  78. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  79. return !self.viewModel.isHideAddCell ? [self calculationAddItem]: self.viewModel.albumDataArray.count;
  80. }
  81. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  82. if (!self.viewModel.isHideAddCell) {
  83. if (self.viewModel.albumDataArray.count <= indexPath.item) {
  84. YMEditProfileAlbumCell *addCell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([YMEditProfileAlbumCell class]) forIndexPath:indexPath];
  85. [addCell ym_bindViewModel:self.viewModel.addCellViewModel];
  86. return addCell;
  87. }
  88. }
  89. YMEditProfileAlbumCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([YMEditProfileAlbumCell class]) forIndexPath:indexPath];
  90. [cell ym_bindViewModel:self.viewModel.albumDataArray[indexPath.item]];
  91. return cell;
  92. }
  93. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
  94. UICollectionReusableView *reusableView = nil;
  95. reusableView.backgroundColor = [UIColor clearColor];
  96. return reusableView;
  97. }
  98. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
  99. return CGSizeZero;
  100. }
  101. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
  102. return CGSizeZero;
  103. }
  104. ///允许移动的item
  105. - (BOOL)collectionView:(UICollectionView*)collectionView canMoveItemAtIndexPath:(nonnull NSIndexPath *)indexPath{
  106. if (indexPath.item >= self.viewModel.albumDataArray.count){
  107. return NO;
  108. }
  109. return YES;
  110. }
  111. ///对交换过的item数据进行操作
  112. - (void)collectionView:(UICollectionView*)collectionView moveItemAtIndexPath:(nonnull NSIndexPath *)sourceIndexPath toIndexPath:(nonnull NSIndexPath *)destinationIndexPath{
  113. NSMutableArray *tempArr = [NSMutableArray arrayWithArray:self.viewModel.albumDataArray];
  114. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.35 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  115. if (destinationIndexPath.row >= tempArr.count){
  116. [self.albumCollectionView reloadData];
  117. return;
  118. }
  119. if (sourceIndexPath.row >= tempArr.count){
  120. [self.albumCollectionView reloadData];
  121. return;
  122. }
  123. [tempArr exchangeObjectAtIndex:sourceIndexPath.item withObjectAtIndex:destinationIndexPath.item];
  124. [self.viewModel.refreshAlbumSortSubject sendNext:tempArr];
  125. });
  126. }
  127. ///抑制item移动 指定位置禁止交换
  128. - (NSIndexPath *)collectionView:(UICollectionView *)collectionView targetIndexPathForMoveOfItemFromOriginalIndexPath:(NSIndexPath *)originalIndexPath atCurrentIndexPath:(NSIndexPath *)currentIndexPath toProposedIndexPath:(NSIndexPath *)proposedIndexPath {
  129. return proposedIndexPath;
  130. }
  131. #pragma mark - UICollectionViewDelegate
  132. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
  133. if (!self.viewModel.isHideAddCell && self.viewModel.albumDataArray.count <= indexPath.item) {
  134. [self.viewModel openAlbumPickerPopupView];
  135. }else{
  136. NSMutableArray *tempArr = [NSMutableArray array];
  137. for (YMEditProfileAlbumCellViewModel *model in self.viewModel.albumDataArray) {
  138. YBIBImageData *data = [YBIBImageData new];
  139. if (!OCStringIsEmpty(model.networkAlbum)) {
  140. data.imageURL = [LCTools getImageUrlWithAddress:model.networkAlbum];
  141. } else {
  142. data.image = ^UIImage * _Nullable{
  143. return model.localAlbum;
  144. };
  145. }
  146. data.projectiveView = [collectionView cellForItemAtIndexPath:indexPath];
  147. [tempArr addObject:data];
  148. }
  149. YBImageBrowser *browser = [YBImageBrowser new];
  150. browser.delegate = self;
  151. browser.dataSourceArray = tempArr;
  152. browser.currentPage = indexPath.item;
  153. [browser.defaultToolViewHandler.topView.operationButton setHidden:false];
  154. [browser.defaultToolViewHandler.topView.operationButton setTitle:@"删除" forState:UIControlStateNormal];
  155. [browser.defaultToolViewHandler.topView.operationButton setImage:NULL forState:UIControlStateNormal];
  156. WS(weakSelf)
  157. [browser.defaultToolViewHandler.topView setClickOperation:^(YBIBTopViewOperationType type) {
  158. YMTipsPopupView *customView = [[YMTipsPopupView alloc]init];
  159. customView.confirmTitle = @"删除";
  160. [customView configutationWithTips:@"是否删除这张照片" TipsAlignment:NSTextAlignmentCenter IsHideTitle:NO IsHideSingleButton:YES];
  161. YMPopupView *popupView = [YMPopupView initWithCustomView:customView parentView:nil popStyle:YMPopupStyleFade dismissStyle:YMDismissStyleFade];
  162. popupView.priority = 999;
  163. popupView.cornerRadius = adapt(10);
  164. popupView.rectCorners = UIRectCornerAllCorners;
  165. popupView.positionStyle = YMPositionStyleCenter;
  166. popupView.isHideBg = NO;
  167. popupView.bgAlpha = 0.3;
  168. @weakify(popupView)
  169. customView.buttonBlock = ^(BOOL isConfirm) {
  170. @strongify(popupView)
  171. if (isConfirm) {
  172. [weakSelf.viewModel.deleteAlbumSubject sendNext:@(browser.currentPage)];
  173. [browser hide];
  174. }
  175. [popupView dismissWithStyle:YMDismissStyleFade duration:2.0];
  176. };
  177. [popupView pop];
  178. }];
  179. [browser show];
  180. }
  181. }
  182. #pragma mark - CHTCollectionViewDelegateWaterfallLayout
  183. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  184. return CGSizeMake(adapt(100), adapt(100));
  185. }
  186. #pragma mark - 拖拽事件照片处理
  187. - (void)longPressAction:(UIGestureRecognizer *)gesture{
  188. UIGestureRecognizerState state = gesture.state;
  189. CGPoint location = [gesture locationInView:self.albumCollectionView];
  190. switch(state) {
  191. //手势开始
  192. case UIGestureRecognizerStateBegan:
  193. {
  194. //判断手势落点位置是否在路径上
  195. NSIndexPath *indexPath = [self.albumCollectionView indexPathForItemAtPoint:location];
  196. if(indexPath == nil) {
  197. break;
  198. }
  199. //在路径上则开始移动该路径上的cell
  200. [self.albumCollectionView beginInteractiveMovementForItemAtIndexPath:indexPath];
  201. YMEditProfileAlbumCell *cell = (YMEditProfileAlbumCell*)[self.albumCollectionView cellForItemAtIndexPath:indexPath];
  202. [UIView animateWithDuration:0.35 animations:^{
  203. cell.alpha = 0.6;
  204. }];
  205. break;
  206. }
  207. //手势变化
  208. case UIGestureRecognizerStateChanged:
  209. {
  210. //移动过程当中随时更新cell位置
  211. [self.albumCollectionView updateInteractiveMovementTargetPosition:location];
  212. break;
  213. }
  214. case UIGestureRecognizerStateEnded:
  215. {
  216. //移动结束后关闭cell移动
  217. [self.albumCollectionView endInteractiveMovement];
  218. NSIndexPath *indexPath = [self.albumCollectionView indexPathForItemAtPoint:location];
  219. if(indexPath == nil) {
  220. break;
  221. }
  222. YMEditProfileAlbumCell *cell = (YMEditProfileAlbumCell*)[self.albumCollectionView cellForItemAtIndexPath:indexPath];
  223. [UIView animateWithDuration:0.35 animations:^{
  224. cell.alpha = 1;
  225. }];
  226. break;
  227. }
  228. //手势结束
  229. default:
  230. {
  231. NSIndexPath *indexPath = [self.albumCollectionView indexPathForItemAtPoint:location];
  232. if(indexPath == nil) {
  233. break;
  234. }
  235. YMEditProfileAlbumCell *cell = (YMEditProfileAlbumCell*)[self.albumCollectionView cellForItemAtIndexPath:indexPath];
  236. [UIView animateWithDuration:0.35 animations:^{
  237. cell.alpha = 1;
  238. }];
  239. break;
  240. }
  241. }
  242. }
  243. - (UILabel *)albumTitleLb{
  244. if (!_albumTitleLb) {
  245. _albumTitleLb = [[UILabel alloc]init];
  246. _albumTitleLb.font = LCBoldFont(16);
  247. _albumTitleLb.textColor = HexColorFromRGB(0x333333);
  248. _albumTitleLb.textAlignment = NSTextAlignmentLeft;
  249. _albumTitleLb.text = @"展示照片";
  250. }
  251. return _albumTitleLb;
  252. }
  253. - (CHTCollectionViewWaterfallLayout *)albumLayout{
  254. if (!_albumLayout) {
  255. _albumLayout = [[CHTCollectionViewWaterfallLayout alloc] init];
  256. _albumLayout.columnCount = 3;
  257. _albumLayout.sectionInset = UIEdgeInsetsMake(adapt(10), adapt(10), adapt(10), adapt(10));
  258. }
  259. return _albumLayout;
  260. }
  261. - (UICollectionView *)albumCollectionView{
  262. if (!_albumCollectionView) {
  263. _albumCollectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:self.albumLayout];
  264. _albumCollectionView.delegate = self;
  265. _albumCollectionView.dataSource = self;
  266. _albumCollectionView.showsVerticalScrollIndicator = NO;
  267. _albumCollectionView.showsHorizontalScrollIndicator = NO;
  268. _albumCollectionView.backgroundColor = UIColor.whiteColor;
  269. [_albumCollectionView registerClass:[YMEditProfileAlbumCell class] forCellWithReuseIdentifier:NSStringFromClass([YMEditProfileAlbumCell class])];
  270. UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAction:)];
  271. [_albumCollectionView addGestureRecognizer:longPress];
  272. }
  273. return _albumCollectionView;
  274. }
  275. - (UILabel *)albumTipsLb{
  276. if (!_albumTipsLb) {
  277. _albumTipsLb = [[UILabel alloc]init];
  278. _albumTipsLb.font = LCFont(12);
  279. _albumTipsLb.textColor = HexColorFromRGB(0x9C9C9C);
  280. _albumTipsLb.textAlignment = NSTextAlignmentLeft;
  281. _albumTipsLb.text = @"1、拖拽相片可以更改排序,单击查看大图可删除相册;";
  282. //1、拖拽相片可以更改排序,单击查看大图可删除相册\n2、照片涉黄或者审核不通过将被删除,严重者冻结账号
  283. }
  284. return _albumTipsLb;
  285. }
  286. @end