123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462 |
- //
- // 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 ()<SPSBNotificationDelegate> {
- 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 <UICollectionViewDataSource>
- - (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 <UICollectionViewDelegate>
- - (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<JXHPhotoManager *> *)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
|