Files
peko-ios/YuMi/Modules/YMMine/View/Cell/MineInfo/XPMineAlbumTableViewCell.m
2024-06-29 14:26:50 +08:00

153 lines
5.4 KiB
Objective-C

//
// XPMineAlbumTableViewCell.m
// YuMi
//
// Created by P on 2024/6/24.
//
#import "XPMineAlbumTableViewCell.h"
#import "UserPhoto.h"
#import "SDPhotoBrowser.h"
@interface XPMineAlbumCell : UICollectionViewCell
@property (nonatomic, strong) NetImageView *imageView;
@end
@implementation XPMineAlbumCell
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
NetImageConfig *config = [[NetImageConfig alloc]init];
config.imageType = ImageTypeUserInfoAlbum;
config.placeHolder = [UIImageConstant defaultAvatarPlaceholder];
_imageView = [[NetImageView alloc] initWithConfig:config];
_imageView.layer.masksToBounds = YES;
_imageView.layer.cornerRadius = 12;
_imageView.contentMode = UIViewContentModeScaleAspectFill;
[self.contentView addSubview:_imageView];
[_imageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.mas_equalTo(self.contentView);
}];
}
return self;
}
@end
@interface XPMineAlbumTableViewCell() <UICollectionViewDelegateFlowLayout, UICollectionViewDataSource, SDPhotoBrowserDelegate>
@property (nonatomic, strong) UICollectionView *albumCollectionView;
@property (nonatomic, strong) UILabel *noDataLabel;
@end
@implementation XPMineAlbumTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
self.contentView.backgroundColor = [UIColor whiteColor];
[self.contentView addSubview:self.albumCollectionView];
[self.albumCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(self.contentView).offset(8);
make.edges.mas_equalTo(self.contentView);
}];
[self.contentView addSubview:self.noDataLabel];
[self.noDataLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.center.mas_equalTo(self.contentView);
}];
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
}
- (void)setAlbumDataSource:(NSArray *)albumDataSource {
_albumDataSource = albumDataSource;
self.noDataLabel.hidden = albumDataSource.count > 0;
[self.albumCollectionView reloadData];
}
#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.albumDataSource.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
XPMineAlbumCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Album Cell"
forIndexPath:indexPath];
UserPhoto *photo = [self.albumDataSource xpSafeObjectAtIndex:indexPath.row];
if (photo) {
cell.imageView.imageUrl = photo.photoUrl;
}
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView
didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
SDPhotoBrowser *browser = [[SDPhotoBrowser alloc]init];
browser.sourceImagesContainerView = self.albumCollectionView;
browser.delegate = self;
browser.imageCount = self.albumDataSource.count;
browser.currentImageIndex = indexPath.item - 1;
browser.isHaveUserAdd = YES;
[browser show];
}
#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return CGSizeMake(81, 81);
}
#pragma mark - SDPhotoBrowserDelegate
- (UIImage *)photoBrowser:(SDPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index {
return [UIImageConstant defaultAvatarPlaceholder];
}
- (NSURL *)photoBrowser:(SDPhotoBrowser *)browser highQualityImageURLForIndex:(NSInteger)index {
UserPhoto *photo = [self.albumDataSource xpSafeObjectAtIndex:index];
if (photo) {
return [NSURL URLWithString:photo.photoUrl];
}
return nil;
}
#pragma mark -
- (UICollectionView *)albumCollectionView {
if (!_albumCollectionView) {
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
layout.minimumInteritemSpacing = 4.5;
layout.minimumLineSpacing = 4.5;
_albumCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero
collectionViewLayout:layout];
_albumCollectionView.backgroundColor = [UIColor clearColor];
_albumCollectionView.delegate = self;
_albumCollectionView.dataSource = self;
[_albumCollectionView registerClass:[XPMineAlbumCell class]
forCellWithReuseIdentifier:@"Album Cell"];
}
return _albumCollectionView;
}
- (UILabel *)noDataLabel {
if (!_noDataLabel) {
_noDataLabel = [UILabel labelInitWithText:YMLocalizedString(@"XPMineUserInfoGiftWallViewController3") font:[UIFont systemFontOfSize:14 weight:UIFontWeightMedium] textColor:UIColorFromRGB(0x191919)];
_noDataLabel.textAlignment = NSTextAlignmentCenter;
_noDataLabel.alpha = 0.5;
_noDataLabel.hidden = YES;
}
return _noDataLabel;
}
@end