// // SPSBAlbumItemCollectionViewController.m // 我的社保 // // Created by shanp on 2020/9/15. // #import "SPSBAlbumItemCollectionViewController.h" #import "SPSBUIGeneralHeader.h" #import "JXHPhotoPullManager.h" #import "UIView+SPSBLoadingTipsView.h" #import "JXHPhotoManager.h" #import "SPSBBusinessManager.h" #import "SPSBPhotoAlbumPreviewViewController.h" #import "SPSBVerificationProfile.h" #import "SPSBNotificationDelegateManager.h" //#import "SPSBTipsWordProfile.h" #define spsb_cellCountOfRow 3 #define spsb_cellSpace 2 #define spsb_cellWidth ((jxh_screenWidth() - (spsb_cellCountOfRow + 1) * spsb_cellSpace) / spsb_cellCountOfRow) @interface SPSBAlbumItemCollectionViewCell : UICollectionViewCell { UIImageView *_imageView; } @property (nonatomic, weak) JXHPhotoManager *spsb_photoManager; @end @implementation SPSBAlbumItemCollectionViewCell - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (!self) return nil; self.backgroundColor = spsb_FFFFFF_color(1.f); self.clipsToBounds = true; _imageView = UIImageView.new; _imageView.contentMode = UIViewContentModeScaleAspectFill; [self.contentView addSubview:_imageView]; [_imageView makeConstraints:^(JXHConstraintMaker *make) { make.edges.equalTo(self.contentView); }]; return self; } - (void)prepareForReuse { [super prepareForReuse]; _imageView.image = nil; } - (void)reloadImage:(UIImage *)image { _imageView.image = image; } @end @interface SPSBAlbumImageItemCollectionViewCell : SPSBAlbumItemCollectionViewCell { UIView *_blackCoverView; JXHCenterButton *_chooseButton; UIView *_writeCoverView; } @property (nonatomic, strong, nullable) void(^spsb_chooseAction)(SPSBAlbumItemCollectionViewCell *cell); @end @implementation SPSBAlbumImageItemCollectionViewCell - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (!self) return nil; _blackCoverView = UIView.new; _blackCoverView.hidden = true; _blackCoverView.backgroundColor = spsb_000000_color(0.28); [self.contentView addSubview:_blackCoverView]; [_blackCoverView makeConstraints:^(JXHConstraintMaker *make) { make.edges.equalTo(self.contentView); }]; _chooseButton = [JXHCenterButton convenienceWithAxis:JXHCenterButtonAxisHorizontal interval:-24 font:spsb_semiboldFont(16) target:self action:@selector(chooseAction)]; [_chooseButton setTitle:@"" titleColor:spsb_clear_color() image:jxh_getImage(photo_album_choose) backgroundImage:nil state:JXHButtonControlStateNormal | JXHButtonControlStateHighlighted]; [_chooseButton setTitleColor:spsb_FFFFFF_color(1.f) image:jxh_getImage(photo_album_chose) state:JXHButtonControlStateSelected | JXHButtonControlStateSelectedAndHighlighted]; [self.contentView addSubview:_chooseButton]; [_chooseButton.jxh_titleLabel remakeConstraints:^(JXHConstraintMaker *make) { make.center.equalTo(self->_chooseButton); }]; [_chooseButton.jxh_icon remakeConstraints:^(JXHConstraintMaker *make) { make.center.equalTo(self->_chooseButton); }]; [_chooseButton makeConstraints:^(JXHConstraintMaker *make) { make.size.equalTo(40); make.top.and.trailing.equalTo(0); }]; _writeCoverView = UIView.new; _writeCoverView.userInteractionEnabled = false; _writeCoverView.hidden = true; _writeCoverView.backgroundColor = spsb_FFFFFF_color(0.6); [self.contentView addSubview:_writeCoverView]; [_writeCoverView makeConstraints:^(JXHConstraintMaker *make) { make.edges.equalTo(self.contentView); }]; return self; } - (void)reloadDataWithIsChose:(bool)isChose choseNum:(nullable NSString *)choseNum canChoose:(bool)canChoose { if (isChose) { _blackCoverView.hidden = false; _chooseButton.selected = true; _writeCoverView.hidden = true; [_chooseButton setTitle:choseNum state:JXHButtonControlStateSelected | JXHButtonControlStateSelectedAndHighlighted]; } else { _blackCoverView.hidden = true; _chooseButton.selected = false; if (!canChoose) { _writeCoverView.hidden = false; } else { _writeCoverView.hidden = true; } } } - (void)chooseAction { if (_spsb_chooseAction) { _spsb_chooseAction(self); } } @end @interface SPSBAlbumVideoItemCollectionViewCell : SPSBAlbumItemCollectionViewCell { UIView *_writeCoverView; UILabel *_time; } @end @implementation SPSBAlbumVideoItemCollectionViewCell - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (!self) return nil; UIImageView *bottomCover = [[UIImageView alloc] initWithImage:jxh_getImage(play_video_bottom_cover)]; [self.contentView addSubview:bottomCover]; [bottomCover makeConstraints:^(JXHConstraintMaker *make) { make.leading.and.trailing.and.bottom.equalTo(0); make.height.equalTo(20); }]; _time = [UILabel convenienceWithFont:spsb_font(12) text:nil textColor:spsb_FFFFFF_color(1.f)]; [self.contentView addSubview:_time]; [_time makeConstraints:^(JXHConstraintMaker *make) { make.trailing.and.bottom.equalTo(-6); }]; _writeCoverView = UIView.new; _writeCoverView.hidden = true; _writeCoverView.backgroundColor = spsb_FFFFFF_color(0.6); [self.contentView addSubview:_writeCoverView]; [_writeCoverView makeConstraints:^(JXHConstraintMaker *make) { make.edges.equalTo(self.contentView); }]; return self; } - (void)reloadTime:(NSString *)time canChoose:(bool)canChoose { _time.text = time; _writeCoverView.hidden = canChoose; } @end @interface SPSBAlbumItemCollectionViewController () { JXHResultDictionary *_data; NSInteger _section; NSInteger _countOfSection; NSInteger _countOfLastSection; JXHPhotoPullManager *_pullManager; CGFloat _cellWidth; bool _isScrolling; bool _isForward; } @end @implementation SPSBAlbumItemCollectionViewController - (instancetype)init { self = [super initWithCollectionViewLayout:[self createCollectionViewFlowLayout]]; if (!self) return nil; if (@available(iOS 13.0, *)) { _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]; } else { _pullManager = [[JXHPhotoPullManager alloc] initWithRowOfScreen:(jxh_screenHeight() - jxh_statusBarHeight() - jxh_navigationViewHeight() - 56 - jxh_safeInsets(spsb_keyWindow()).bottom) / (spsb_cellWidth + spsb_cellSpace) countOfRow:spsb_cellCountOfRow]; } _data = NSMutableDictionary.new; return self; } - (void)viewDidLoad { [super viewDidLoad]; [self setupCollectionView]; spsb_setNotificationKey(SPSBChoosePublishPhotosNotificationKey); } - (void)dealloc { spsb_removeNotificationKey(SPSBChoosePublishPhotosNotificationKey); } #pragma mark - ACTION - (void)chooseCell:(SPSBAlbumItemCollectionViewCell *)cell { NSIndexPath *indexPath = [self.collectionView indexPathForCell:cell]; if (indexPath) { spsb_changeCurrentChosePublishPhotos(_data[@(indexPath.section)][indexPath.item], indexPath.section * _countOfSection + indexPath.item); } } - (void)initData { NSInteger section = _section - 1; NSInteger index = 0; do { [_pullManager pullDataWithSection:section completion:^(JXHResultDictionary * _Nonnull result) { self->_data = result; [self.collectionView reloadData]; [self.view dismissToast]; }]; section --; index ++; } while (section >= 0 && index < 3); } - (void)setSpsb_photoAlbum:(JXHPhotoAlbum *)spsb_photoAlbum { _spsb_photoAlbum = spsb_photoAlbum; [self.view showLoadingToastWithTitle:@"加载中" userClick:false]; @weakify(self) [_pullManager setAlbum:spsb_photoAlbum completion:^(NSInteger section, NSInteger countOfSection, NSInteger countOfLastSection) { @strongify(self) self->_section = section; self->_countOfSection = countOfSection; self->_countOfLastSection = countOfLastSection; self->_data = NSMutableDictionary.new; [self.collectionView reloadData]; dispatch_async(dispatch_get_main_queue(), ^{ [self scrollToBottom]; [self initData]; }); }]; } - (void)scrollToBottom { [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:_countOfLastSection - 1 inSection:_section - 1] atScrollPosition:UICollectionViewScrollPositionBottom animated:false]; } #pragma mark - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return _section; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { if (section == _section - 1) { return _countOfLastSection; } return _countOfSection; } static NSString * const image_reuseIdentifier = @"SPSBAlbumImageItemCollectionViewCell"; static NSString * const video_reuseIdentifier = @"SPSBAlbumVideoItemCollectionViewCell"; - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { NSNumber *key = @(indexPath.section); if (_data[key].count <= indexPath.item) { return [collectionView dequeueReusableCellWithReuseIdentifier:image_reuseIdentifier forIndexPath:indexPath]; } JXHPhotoManager *photoManager = _data[key][indexPath.item]; SPSBAlbumItemCollectionViewCell *tempCell; if (spsb_publishCanChooseVideo() && photoManager.jxh_phAsset.mediaType == PHAssetMediaTypeVideo) { // SPSBAlbumVideoItemCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:video_reuseIdentifier forIndexPath:indexPath]; // if (spsb_currentChosePublishPhotos().count > 0 || spsb_alreadyChosePublishPhoto() > 0) { // [cell reloadTime:spsb_changeSecondToTime(photoManager.jxh_phAsset.duration, false) canChoose:false]; // } else { // [cell reloadTime:spsb_changeSecondToTime(photoManager.jxh_phAsset.duration, false) canChoose:true]; // } // tempCell = cell; } else { SPSBAlbumImageItemCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:image_reuseIdentifier forIndexPath:indexPath]; if (!cell.spsb_chooseAction) { @weakify(self) cell.spsb_chooseAction = ^(SPSBAlbumItemCollectionViewCell *cell) { @strongify(self) [self chooseCell:cell]; }; } NSUInteger index = [spsb_currentChosePublishPhotos() indexOfObject:photoManager]; if (index < spsb_publishMaxCount() - spsb_alreadyChosePublishPhoto()) { [cell reloadDataWithIsChose:true choseNum:[NSString stringWithFormat:@"%ld", (long)index + 1] canChoose:spsb_currentChosePublishPhotos().count < spsb_publishMaxCount() - spsb_alreadyChosePublishPhoto()]; } else { [cell reloadDataWithIsChose:false choseNum:nil canChoose:spsb_currentChosePublishPhotos().count < spsb_publishMaxCount() - spsb_alreadyChosePublishPhoto()]; } tempCell = cell; } tempCell.spsb_photoManager = photoManager; [photoManager requestThumbnailImageWithSize:(CGSize){60, 60} contentMode:JXHImageContentModeAspectFill completion:^(UIImage * _Nullable image, NSDictionary * _Nullable info) { if (tempCell.spsb_photoManager == photoManager) { [tempCell reloadImage:image]; } }]; return tempCell; } #pragma mark - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { NSNumber *key = @(indexPath.section); if (_data[key].count <= indexPath.item) return; JXHPhotoManager *manager = _data[key][indexPath.item]; if (spsb_publishCanChooseVideo() && manager.jxh_phAsset.mediaType == PHAssetMediaTypeVideo) { if (spsb_currentChosePublishPhotos().count > 0 || spsb_alreadyChosePublishPhoto() > 0) return; SPSBPhotoAlbumPreviewViewController *vc = SPSBPhotoAlbumPreviewViewController.new; vc.spsb_dataArray = @[manager]; vc.spsb_startIndex = 0; vc.spsb_isAutoPlay = true; vc.spsb_finishChose = _spsb_finishChose; vc.spsb_finishActionFirst = _spsb_finishActionFirst; [self.navigationController pushViewController:vc animated:true]; } else { SPSBPhotoAlbumPreviewViewController *vc = SPSBPhotoAlbumPreviewViewController.new; vc.spsb_photoAlbum = _spsb_photoAlbum; vc.spsb_finishChose = _spsb_finishChose; vc.spsb_finishActionFirst = _spsb_finishActionFirst; vc.spsb_startIndex = indexPath.section * _countOfSection + indexPath.item; [self.navigationController pushViewController:vc animated:true]; } } - (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath { //防止未显示的cell也会走这个方法 if (!_isScrolling) { return; } [_pullManager judgeShouldPullDataWithForward:_isForward currentIndex:indexPath.item currentSection:indexPath.section completion:^(JXHResultDictionary * _Nonnull result, bool isDelete) { self->_data = result; }]; } - (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView { debugLog(@"top"); [_pullManager setAlbum:_spsb_photoAlbum completion:^(NSInteger section, NSInteger countOfSection, NSInteger countOfLastSection) { }]; self->_data = NSMutableDictionary.new; [self.collectionView reloadData]; NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:CGPointMake(0, scrollView.contentOffset.y)]; if (!indexPath || indexPath.section == 0) { for (NSInteger i = 0; i < 3; i ++) { if (i < _section) { [_pullManager pullDataWithSection:i completion:^(JXHResultDictionary * _Nonnull result) { self->_data = result; [self.collectionView reloadData]; }]; } } } else { NSArray *sectionArray = @[@(indexPath.section), @(indexPath.section + 1), @(indexPath.section - 1), @(indexPath.section + 2), @(indexPath.section - 2)]; for (NSInteger i = 0; i < sectionArray.count; i ++) { NSInteger section = [sectionArray[i] integerValue]; if (section < _section && section >= 0) { [_pullManager pullDataWithSection:i completion:^(JXHResultDictionary * _Nonnull result) { self->_data = result; [self.collectionView reloadData]; }]; } } } debugLog(@"%@", indexPath); } static float oldY = 0; static float newY = 0; - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { _isScrolling = true; } - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate { if (!decelerate) { debugLog(@"1"); _isScrolling = false; [self.collectionView reloadData]; } } - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { debugLog(@"3"); _isScrolling = false; [self.collectionView reloadData]; } - (void)scrollViewDidScroll:(UIScrollView *)scrollView { newY = scrollView.contentOffset.y; if (oldY > newY) { _isForward = true; } else { _isForward = false; } oldY = newY; } #pragma mark - SPSBNotificationDelegate - (void)spsb_currenChosePublishPhotosChanged:(NSArray *)currentArray chose:(JXHPhotoManager *)chose { [self.collectionView reloadData]; } #pragma mark - UI - (void)setupCollectionView { self.collectionView.backgroundColor = spsb_FFFFFF_color(1.f); self.collectionView.showsVerticalScrollIndicator = false; [self.collectionView registerClass:[SPSBAlbumImageItemCollectionViewCell class] forCellWithReuseIdentifier:image_reuseIdentifier]; [self.collectionView registerClass:[SPSBAlbumVideoItemCollectionViewCell class] forCellWithReuseIdentifier:video_reuseIdentifier]; } - (UICollectionViewFlowLayout *)createCollectionViewFlowLayout { _cellWidth = spsb_cellWidth; UICollectionViewFlowLayout *layout = UICollectionViewFlowLayout.new; layout.scrollDirection = UICollectionViewScrollDirectionVertical; layout.itemSize = CGSizeMake(_cellWidth, _cellWidth); layout.headerReferenceSize = CGSizeMake(jxh_screenWidth(), spsb_cellSpace); layout.minimumLineSpacing = spsb_cellSpace; layout.minimumInteritemSpacing = spsb_cellSpace; layout.sectionInset = UIEdgeInsetsMake(0, spsb_cellSpace, spsb_cellSpace, spsb_cellSpace); return layout; } @end #undef spsb_cellSpace #undef spsb_cellWidth #undef spsb_cellCountOfRow