SPSBAlbumItemCollectionViewController.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. //
  2. // SPSBAlbumItemCollectionViewController.m
  3. // 我的社保
  4. //
  5. // Created by shanp on 2020/9/15.
  6. //
  7. #import "SPSBAlbumItemCollectionViewController.h"
  8. #import "SPSBUIGeneralHeader.h"
  9. #import "JXHPhotoPullManager.h"
  10. #import "UIView+SPSBLoadingTipsView.h"
  11. #import "JXHPhotoManager.h"
  12. #import "SPSBBusinessManager.h"
  13. #import "SPSBPhotoAlbumPreviewViewController.h"
  14. #import "SPSBVerificationProfile.h"
  15. #import "SPSBNotificationDelegateManager.h"
  16. //#import "SPSBTipsWordProfile.h"
  17. #define spsb_cellCountOfRow 3
  18. #define spsb_cellSpace 2
  19. #define spsb_cellWidth ((jxh_screenWidth() - (spsb_cellCountOfRow + 1) * spsb_cellSpace) / spsb_cellCountOfRow)
  20. @interface SPSBAlbumItemCollectionViewCell : UICollectionViewCell {
  21. UIImageView *_imageView;
  22. }
  23. @property (nonatomic, weak) JXHPhotoManager *spsb_photoManager;
  24. @end
  25. @implementation SPSBAlbumItemCollectionViewCell
  26. - (instancetype)initWithFrame:(CGRect)frame {
  27. self = [super initWithFrame:frame];
  28. if (!self) return nil;
  29. self.backgroundColor = spsb_FFFFFF_color(1.f);
  30. self.clipsToBounds = true;
  31. _imageView = UIImageView.new;
  32. _imageView.contentMode = UIViewContentModeScaleAspectFill;
  33. [self.contentView addSubview:_imageView];
  34. [_imageView 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. @end
  47. @interface SPSBAlbumImageItemCollectionViewCell : SPSBAlbumItemCollectionViewCell {
  48. UIView *_blackCoverView;
  49. JXHCenterButton *_chooseButton;
  50. UIView *_writeCoverView;
  51. }
  52. @property (nonatomic, strong, nullable) void(^spsb_chooseAction)(SPSBAlbumItemCollectionViewCell *cell);
  53. @end
  54. @implementation SPSBAlbumImageItemCollectionViewCell
  55. - (instancetype)initWithFrame:(CGRect)frame {
  56. self = [super initWithFrame:frame];
  57. if (!self) return nil;
  58. _blackCoverView = UIView.new;
  59. _blackCoverView.hidden = true;
  60. _blackCoverView.backgroundColor = spsb_000000_color(0.28);
  61. [self.contentView addSubview:_blackCoverView];
  62. [_blackCoverView makeConstraints:^(JXHConstraintMaker *make) {
  63. make.edges.equalTo(self.contentView);
  64. }];
  65. _chooseButton = [JXHCenterButton convenienceWithAxis:JXHCenterButtonAxisHorizontal interval:-24 font:spsb_semiboldFont(16) target:self action:@selector(chooseAction)];
  66. [_chooseButton setTitle:@"" titleColor:spsb_clear_color() image:jxh_getImage(photo_album_choose) backgroundImage:nil state:JXHButtonControlStateNormal | JXHButtonControlStateHighlighted];
  67. [_chooseButton setTitleColor:spsb_FFFFFF_color(1.f) image:jxh_getImage(photo_album_chose) state:JXHButtonControlStateSelected | JXHButtonControlStateSelectedAndHighlighted];
  68. [self.contentView addSubview:_chooseButton];
  69. [_chooseButton.jxh_titleLabel remakeConstraints:^(JXHConstraintMaker *make) {
  70. make.center.equalTo(self->_chooseButton);
  71. }];
  72. [_chooseButton.jxh_icon remakeConstraints:^(JXHConstraintMaker *make) {
  73. make.center.equalTo(self->_chooseButton);
  74. }];
  75. [_chooseButton makeConstraints:^(JXHConstraintMaker *make) {
  76. make.size.equalTo(40);
  77. make.top.and.trailing.equalTo(0);
  78. }];
  79. _writeCoverView = UIView.new;
  80. _writeCoverView.userInteractionEnabled = false;
  81. _writeCoverView.hidden = true;
  82. _writeCoverView.backgroundColor = spsb_FFFFFF_color(0.6);
  83. [self.contentView addSubview:_writeCoverView];
  84. [_writeCoverView makeConstraints:^(JXHConstraintMaker *make) {
  85. make.edges.equalTo(self.contentView);
  86. }];
  87. return self;
  88. }
  89. - (void)reloadDataWithIsChose:(bool)isChose choseNum:(nullable NSString *)choseNum canChoose:(bool)canChoose {
  90. if (isChose) {
  91. _blackCoverView.hidden = false;
  92. _chooseButton.selected = true;
  93. _writeCoverView.hidden = true;
  94. [_chooseButton setTitle:choseNum state:JXHButtonControlStateSelected | JXHButtonControlStateSelectedAndHighlighted];
  95. } else {
  96. _blackCoverView.hidden = true;
  97. _chooseButton.selected = false;
  98. if (!canChoose) {
  99. _writeCoverView.hidden = false;
  100. } else {
  101. _writeCoverView.hidden = true;
  102. }
  103. }
  104. }
  105. - (void)chooseAction {
  106. if (_spsb_chooseAction) {
  107. _spsb_chooseAction(self);
  108. }
  109. }
  110. @end
  111. @interface SPSBAlbumVideoItemCollectionViewCell : SPSBAlbumItemCollectionViewCell {
  112. UIView *_writeCoverView;
  113. UILabel *_time;
  114. }
  115. @end
  116. @implementation SPSBAlbumVideoItemCollectionViewCell
  117. - (instancetype)initWithFrame:(CGRect)frame {
  118. self = [super initWithFrame:frame];
  119. if (!self) return nil;
  120. UIImageView *bottomCover = [[UIImageView alloc] initWithImage:jxh_getImage(play_video_bottom_cover)];
  121. [self.contentView addSubview:bottomCover];
  122. [bottomCover makeConstraints:^(JXHConstraintMaker *make) {
  123. make.leading.and.trailing.and.bottom.equalTo(0);
  124. make.height.equalTo(20);
  125. }];
  126. _time = [UILabel convenienceWithFont:spsb_font(12) text:nil textColor:spsb_FFFFFF_color(1.f)];
  127. [self.contentView addSubview:_time];
  128. [_time makeConstraints:^(JXHConstraintMaker *make) {
  129. make.trailing.and.bottom.equalTo(-6);
  130. }];
  131. _writeCoverView = UIView.new;
  132. _writeCoverView.hidden = true;
  133. _writeCoverView.backgroundColor = spsb_FFFFFF_color(0.6);
  134. [self.contentView addSubview:_writeCoverView];
  135. [_writeCoverView makeConstraints:^(JXHConstraintMaker *make) {
  136. make.edges.equalTo(self.contentView);
  137. }];
  138. return self;
  139. }
  140. - (void)reloadTime:(NSString *)time canChoose:(bool)canChoose {
  141. _time.text = time;
  142. _writeCoverView.hidden = canChoose;
  143. }
  144. @end
  145. @interface SPSBAlbumItemCollectionViewController ()<SPSBNotificationDelegate> {
  146. JXHResultDictionary *_data;
  147. NSInteger _section;
  148. NSInteger _countOfSection;
  149. NSInteger _countOfLastSection;
  150. JXHPhotoPullManager *_pullManager;
  151. CGFloat _cellWidth;
  152. bool _isScrolling;
  153. bool _isForward;
  154. }
  155. @end
  156. @implementation SPSBAlbumItemCollectionViewController
  157. - (instancetype)init {
  158. self = [super initWithCollectionViewLayout:[self createCollectionViewFlowLayout]];
  159. if (!self) return nil;
  160. if (@available(iOS 13.0, *)) {
  161. _pullManager = [[JXHPhotoPullManager alloc] initWithRowOfScreen:(jxh_screenHeight() - jxh_safeInsets(spsb_keyWindow()).top - jxh_navigationViewHeight() - 56 - jxh_safeInsets(spsb_keyWindow()).bottom) / (spsb_cellWidth + spsb_cellSpace) countOfRow:spsb_cellCountOfRow];
  162. } else {
  163. _pullManager = [[JXHPhotoPullManager alloc] initWithRowOfScreen:(jxh_screenHeight() - jxh_statusBarHeight() - jxh_navigationViewHeight() - 56 - jxh_safeInsets(spsb_keyWindow()).bottom) / (spsb_cellWidth + spsb_cellSpace) countOfRow:spsb_cellCountOfRow];
  164. }
  165. _data = NSMutableDictionary.new;
  166. return self;
  167. }
  168. - (void)viewDidLoad {
  169. [super viewDidLoad];
  170. [self setupCollectionView];
  171. spsb_setNotificationKey(SPSBChoosePublishPhotosNotificationKey);
  172. }
  173. - (void)dealloc {
  174. spsb_removeNotificationKey(SPSBChoosePublishPhotosNotificationKey);
  175. }
  176. #pragma mark - ACTION
  177. - (void)chooseCell:(SPSBAlbumItemCollectionViewCell *)cell {
  178. NSIndexPath *indexPath = [self.collectionView indexPathForCell:cell];
  179. if (indexPath) {
  180. spsb_changeCurrentChosePublishPhotos(_data[@(indexPath.section)][indexPath.item], indexPath.section * _countOfSection + indexPath.item);
  181. }
  182. }
  183. - (void)initData {
  184. NSInteger section = _section - 1;
  185. NSInteger index = 0;
  186. do {
  187. [_pullManager pullDataWithSection:section completion:^(JXHResultDictionary * _Nonnull result) {
  188. self->_data = result;
  189. [self.collectionView reloadData];
  190. [self.view dismissToast];
  191. }];
  192. section --;
  193. index ++;
  194. } while (section >= 0 && index < 3);
  195. }
  196. - (void)setSpsb_photoAlbum:(JXHPhotoAlbum *)spsb_photoAlbum {
  197. _spsb_photoAlbum = spsb_photoAlbum;
  198. [self.view showLoadingToastWithTitle:@"加载中" userClick:false];
  199. @weakify(self)
  200. [_pullManager setAlbum:spsb_photoAlbum completion:^(NSInteger section, NSInteger countOfSection, NSInteger countOfLastSection) {
  201. @strongify(self)
  202. self->_section = section;
  203. self->_countOfSection = countOfSection;
  204. self->_countOfLastSection = countOfLastSection;
  205. self->_data = NSMutableDictionary.new;
  206. [self.collectionView reloadData];
  207. dispatch_async(dispatch_get_main_queue(), ^{
  208. [self scrollToBottom];
  209. [self initData];
  210. });
  211. }];
  212. }
  213. - (void)scrollToBottom {
  214. [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:_countOfLastSection - 1 inSection:_section - 1] atScrollPosition:UICollectionViewScrollPositionBottom animated:false];
  215. }
  216. #pragma mark <UICollectionViewDataSource>
  217. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
  218. return _section;
  219. }
  220. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  221. if (section == _section - 1) {
  222. return _countOfLastSection;
  223. }
  224. return _countOfSection;
  225. }
  226. static NSString * const image_reuseIdentifier = @"SPSBAlbumImageItemCollectionViewCell";
  227. static NSString * const video_reuseIdentifier = @"SPSBAlbumVideoItemCollectionViewCell";
  228. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  229. NSNumber *key = @(indexPath.section);
  230. if (_data[key].count <= indexPath.item) {
  231. return [collectionView dequeueReusableCellWithReuseIdentifier:image_reuseIdentifier forIndexPath:indexPath];
  232. }
  233. JXHPhotoManager *photoManager = _data[key][indexPath.item];
  234. SPSBAlbumItemCollectionViewCell *tempCell;
  235. if (spsb_publishCanChooseVideo() && photoManager.jxh_phAsset.mediaType == PHAssetMediaTypeVideo) {
  236. // SPSBAlbumVideoItemCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:video_reuseIdentifier forIndexPath:indexPath];
  237. // if (spsb_currentChosePublishPhotos().count > 0 || spsb_alreadyChosePublishPhoto() > 0) {
  238. // [cell reloadTime:spsb_changeSecondToTime(photoManager.jxh_phAsset.duration, false) canChoose:false];
  239. // } else {
  240. // [cell reloadTime:spsb_changeSecondToTime(photoManager.jxh_phAsset.duration, false) canChoose:true];
  241. // }
  242. // tempCell = cell;
  243. } else {
  244. SPSBAlbumImageItemCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:image_reuseIdentifier forIndexPath:indexPath];
  245. if (!cell.spsb_chooseAction) {
  246. @weakify(self)
  247. cell.spsb_chooseAction = ^(SPSBAlbumItemCollectionViewCell *cell) {
  248. @strongify(self)
  249. [self chooseCell:cell];
  250. };
  251. }
  252. NSUInteger index = [spsb_currentChosePublishPhotos() indexOfObject:photoManager];
  253. if (index < spsb_publishMaxCount() - spsb_alreadyChosePublishPhoto()) {
  254. [cell reloadDataWithIsChose:true choseNum:[NSString stringWithFormat:@"%ld", (long)index + 1] canChoose:spsb_currentChosePublishPhotos().count < spsb_publishMaxCount() - spsb_alreadyChosePublishPhoto()];
  255. } else {
  256. [cell reloadDataWithIsChose:false choseNum:nil canChoose:spsb_currentChosePublishPhotos().count < spsb_publishMaxCount() - spsb_alreadyChosePublishPhoto()];
  257. }
  258. tempCell = cell;
  259. }
  260. tempCell.spsb_photoManager = photoManager;
  261. [photoManager requestThumbnailImageWithSize:(CGSize){60, 60} contentMode:JXHImageContentModeAspectFill completion:^(UIImage * _Nullable image, NSDictionary * _Nullable info) {
  262. if (tempCell.spsb_photoManager == photoManager) {
  263. [tempCell reloadImage:image];
  264. }
  265. }];
  266. return tempCell;
  267. }
  268. #pragma mark <UICollectionViewDelegate>
  269. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  270. NSNumber *key = @(indexPath.section);
  271. if (_data[key].count <= indexPath.item) return;
  272. JXHPhotoManager *manager = _data[key][indexPath.item];
  273. if (spsb_publishCanChooseVideo() && manager.jxh_phAsset.mediaType == PHAssetMediaTypeVideo) {
  274. if (spsb_currentChosePublishPhotos().count > 0 || spsb_alreadyChosePublishPhoto() > 0) return;
  275. SPSBPhotoAlbumPreviewViewController *vc = SPSBPhotoAlbumPreviewViewController.new;
  276. vc.spsb_dataArray = @[manager];
  277. vc.spsb_startIndex = 0;
  278. vc.spsb_isAutoPlay = true;
  279. vc.spsb_finishChose = _spsb_finishChose;
  280. vc.spsb_finishActionFirst = _spsb_finishActionFirst;
  281. [self.navigationController pushViewController:vc animated:true];
  282. } else {
  283. SPSBPhotoAlbumPreviewViewController *vc = SPSBPhotoAlbumPreviewViewController.new;
  284. vc.spsb_photoAlbum = _spsb_photoAlbum;
  285. vc.spsb_finishChose = _spsb_finishChose;
  286. vc.spsb_finishActionFirst = _spsb_finishActionFirst;
  287. vc.spsb_startIndex = indexPath.section * _countOfSection + indexPath.item;
  288. [self.navigationController pushViewController:vc animated:true];
  289. }
  290. }
  291. - (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
  292. //防止未显示的cell也会走这个方法
  293. if (!_isScrolling) {
  294. return;
  295. }
  296. [_pullManager judgeShouldPullDataWithForward:_isForward currentIndex:indexPath.item currentSection:indexPath.section completion:^(JXHResultDictionary * _Nonnull result, bool isDelete) {
  297. self->_data = result;
  298. }];
  299. }
  300. - (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView {
  301. debugLog(@"top");
  302. [_pullManager setAlbum:_spsb_photoAlbum completion:^(NSInteger section, NSInteger countOfSection, NSInteger countOfLastSection) {
  303. }];
  304. self->_data = NSMutableDictionary.new;
  305. [self.collectionView reloadData];
  306. NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:CGPointMake(0, scrollView.contentOffset.y)];
  307. if (!indexPath || indexPath.section == 0) {
  308. for (NSInteger i = 0; i < 3; i ++) {
  309. if (i < _section) {
  310. [_pullManager pullDataWithSection:i completion:^(JXHResultDictionary * _Nonnull result) {
  311. self->_data = result;
  312. [self.collectionView reloadData];
  313. }];
  314. }
  315. }
  316. } else {
  317. NSArray *sectionArray = @[@(indexPath.section), @(indexPath.section + 1), @(indexPath.section - 1), @(indexPath.section + 2), @(indexPath.section - 2)];
  318. for (NSInteger i = 0; i < sectionArray.count; i ++) {
  319. NSInteger section = [sectionArray[i] integerValue];
  320. if (section < _section && section >= 0) {
  321. [_pullManager pullDataWithSection:i completion:^(JXHResultDictionary * _Nonnull result) {
  322. self->_data = result;
  323. [self.collectionView reloadData];
  324. }];
  325. }
  326. }
  327. }
  328. debugLog(@"%@", indexPath);
  329. }
  330. static float oldY = 0;
  331. static float newY = 0;
  332. - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
  333. _isScrolling = true;
  334. }
  335. - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
  336. if (!decelerate) {
  337. debugLog(@"1");
  338. _isScrolling = false;
  339. [self.collectionView reloadData];
  340. }
  341. }
  342. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
  343. debugLog(@"3");
  344. _isScrolling = false;
  345. [self.collectionView reloadData];
  346. }
  347. - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
  348. newY = scrollView.contentOffset.y;
  349. if (oldY > newY) {
  350. _isForward = true;
  351. } else {
  352. _isForward = false;
  353. }
  354. oldY = newY;
  355. }
  356. #pragma mark - SPSBNotificationDelegate
  357. - (void)spsb_currenChosePublishPhotosChanged:(NSArray<JXHPhotoManager *> *)currentArray chose:(JXHPhotoManager *)chose {
  358. [self.collectionView reloadData];
  359. }
  360. #pragma mark - UI
  361. - (void)setupCollectionView {
  362. self.collectionView.backgroundColor = spsb_FFFFFF_color(1.f);
  363. self.collectionView.showsVerticalScrollIndicator = false;
  364. [self.collectionView registerClass:[SPSBAlbumImageItemCollectionViewCell class] forCellWithReuseIdentifier:image_reuseIdentifier];
  365. [self.collectionView registerClass:[SPSBAlbumVideoItemCollectionViewCell class] forCellWithReuseIdentifier:video_reuseIdentifier];
  366. }
  367. - (UICollectionViewFlowLayout *)createCollectionViewFlowLayout {
  368. _cellWidth = spsb_cellWidth;
  369. UICollectionViewFlowLayout *layout = UICollectionViewFlowLayout.new;
  370. layout.scrollDirection = UICollectionViewScrollDirectionVertical;
  371. layout.itemSize = CGSizeMake(_cellWidth, _cellWidth);
  372. layout.headerReferenceSize = CGSizeMake(jxh_screenWidth(), spsb_cellSpace);
  373. layout.minimumLineSpacing = spsb_cellSpace;
  374. layout.minimumInteritemSpacing = spsb_cellSpace;
  375. layout.sectionInset = UIEdgeInsetsMake(0, spsb_cellSpace, spsb_cellSpace, spsb_cellSpace);
  376. return layout;
  377. }
  378. @end
  379. #undef spsb_cellSpace
  380. #undef spsb_cellWidth
  381. #undef spsb_cellCountOfRow