261 lines
8.6 KiB
Objective-C
261 lines
8.6 KiB
Objective-C
//
|
|
// XPGiftUsersView.m
|
|
// xplan-ios
|
|
//
|
|
// Created by 冯硕 on 2021/11/9.
|
|
//
|
|
|
|
#import "XPGiftUsersView.h"
|
|
///Third
|
|
#import <Masonry/Masonry.h>
|
|
///Tool
|
|
#import "AccountInfoStorage.h"
|
|
#import "NetImageView.h"
|
|
#import "ThemeColor.h"
|
|
///Model
|
|
#import "UserInfoModel.h"
|
|
#import "XPGiftUserInfoModel.h"
|
|
///view
|
|
#import "XPGiftUserCollectionViewCell.h"
|
|
@interface XPGiftUsersView ()<UICollectionViewDelegate, UICollectionViewDataSource>
|
|
///麦上的送礼物
|
|
@property (nonatomic,strong) UIStackView *stackView;
|
|
///全麦
|
|
@property (nonatomic,strong) UIButton *allMicroButton;
|
|
///列表
|
|
@property (nonatomic,strong) UICollectionView *collectionView;
|
|
///单独送一个人礼物
|
|
@property (nonatomic,strong) UIStackView *oneStackView;
|
|
///昵称
|
|
@property (nonatomic,strong) NetImageView *avatarImageView;
|
|
///头像
|
|
@property (nonatomic,strong) UILabel *nickLabel;
|
|
///原始的数据
|
|
@property (nonatomic,strong) NSArray<XPGiftUserInfoModel *> *userArray;
|
|
///选中的人
|
|
@property (nonatomic,strong) NSMutableArray<NSString *> *selectUserArray;
|
|
///是不是选择了全部麦上的人
|
|
@property (nonatomic,assign) BOOL isSelectAll;
|
|
@end
|
|
|
|
@implementation XPGiftUsersView
|
|
|
|
- (instancetype)initWithFrame:(CGRect)frame {
|
|
self = [super initWithFrame:frame];
|
|
if (self) {
|
|
[self initSubViews];
|
|
[self initSubViewConstraints];
|
|
}
|
|
return self;
|
|
}
|
|
#pragma mark - Response
|
|
- (void)allMicroButtonAction:(UIButton *)sender {
|
|
sender.selected = !sender.selected;
|
|
[self.userArray enumerateObjectsUsingBlock:^(XPGiftUserInfoModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
|
|
obj.isSelect = sender.selected;
|
|
NSString * selectUid = [NSString stringWithFormat:@"%ld", obj.uid];
|
|
if (obj.isSelect) {
|
|
if (![self.selectUserArray containsObject:selectUid]) {
|
|
[self.selectUserArray addObject:selectUid];
|
|
}
|
|
} else {
|
|
if ([self.selectUserArray containsObject:selectUid]) {
|
|
[self.selectUserArray removeObject:selectUid];
|
|
}
|
|
}
|
|
}];
|
|
self.isSelectAll = sender.selected;
|
|
[self.collectionView reloadData];
|
|
}
|
|
|
|
#pragma mark - Private Method
|
|
- (void)initSubViews {
|
|
self.backgroundColor = [UIColor clearColor];;
|
|
[self addSubview:self.stackView];
|
|
[self addSubview:self.oneStackView];
|
|
[self.stackView addArrangedSubview:self.allMicroButton];
|
|
[self.stackView addArrangedSubview:self.collectionView];
|
|
|
|
[self.oneStackView addArrangedSubview:self.avatarImageView];
|
|
[self.oneStackView addArrangedSubview:self.nickLabel];
|
|
|
|
}
|
|
|
|
- (void)initSubViewConstraints {
|
|
[self mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.height.mas_equalTo(43 + 15 * 2);
|
|
}];
|
|
|
|
[self.stackView mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.left.right.mas_equalTo(self).inset(15);
|
|
make.top.bottom.mas_equalTo(self);
|
|
}];
|
|
|
|
[self.oneStackView mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.left.right.mas_equalTo(self).inset(15);
|
|
make.top.bottom.mas_equalTo(self);
|
|
}];
|
|
|
|
|
|
[self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.height.mas_equalTo(43);
|
|
}];
|
|
|
|
[self.avatarImageView mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.size.mas_equalTo(CGSizeMake(38, 38));
|
|
}];
|
|
}
|
|
|
|
/// 查找麦序 坑位上有人的麦序
|
|
/// @param users 麦序列表
|
|
- (NSArray *)findSendGiftAllUsers:(NSArray<XPGiftUserInfoModel *> *)users {
|
|
NSMutableArray * tempArray = [NSMutableArray array];
|
|
NSString * uid = [AccountInfoStorage instance].getUid;
|
|
NSArray * newArray = [users sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
|
|
XPGiftUserInfoModel * model1 = obj1;
|
|
XPGiftUserInfoModel * model2 = obj2;
|
|
NSComparisonResult resuest = [model1.position compare:model2.position];
|
|
return resuest;
|
|
}];
|
|
|
|
for (int i = 0; i < newArray.count; i++) {
|
|
XPGiftUserInfoModel * userInfo = [newArray objectAtIndex:i];
|
|
if (userInfo && userInfo.uid > 0 && userInfo.uid != uid.integerValue) { ///自己在麦上不显示在送礼物列表中
|
|
NSString * uid = [NSString stringWithFormat:@"%ld", userInfo.uid];
|
|
if (userInfo.isSelect) {
|
|
[self.selectUserArray addObject:uid];
|
|
}
|
|
[tempArray addObject:userInfo];
|
|
}
|
|
}
|
|
return [tempArray copy];
|
|
}
|
|
|
|
#pragma mark - Public Method
|
|
- (void)configGiftUsers:(NSArray<XPGiftUserInfoModel *> *)users {
|
|
self.userArray = [self findSendGiftAllUsers:users];
|
|
if (users.count == 1 && users.firstObject.position == nil) {///只有一个 并且用户不再麦序中的话
|
|
XPGiftUserInfoModel *userInfo = users.firstObject;
|
|
self.avatarImageView.imageUrl = userInfo.avatar;
|
|
self.nickLabel.text = userInfo.nick;
|
|
self.stackView.hidden = YES;
|
|
self.oneStackView.hidden = NO;
|
|
} else {
|
|
[self.collectionView reloadData];
|
|
self.oneStackView.hidden = YES;
|
|
self.stackView.hidden = NO;
|
|
}
|
|
}
|
|
|
|
#pragma mark - UICollectionViewDataSource And UICollectionViewDelegate
|
|
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
|
|
return self.userArray.count;
|
|
}
|
|
|
|
-(__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
|
|
XPGiftUserCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([XPGiftUserCollectionViewCell class]) forIndexPath:indexPath];
|
|
XPGiftUserInfoModel * queue = [self.userArray objectAtIndex:indexPath.row];
|
|
cell.userInfo = queue;
|
|
return cell;
|
|
}
|
|
|
|
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
|
|
[collectionView deselectItemAtIndexPath:indexPath animated:YES];
|
|
XPGiftUserInfoModel * queue = [self.userArray objectAtIndex:indexPath.row];
|
|
queue.isSelect = !queue.isSelect;
|
|
[self.collectionView reloadData];
|
|
NSString * selectUid = [NSString stringWithFormat:@"%ld", queue.uid];
|
|
if (queue.isSelect) {
|
|
[self.selectUserArray addObject:selectUid];
|
|
} else {
|
|
if ([self.selectUserArray containsObject:selectUid]) {
|
|
[self.selectUserArray removeObject:selectUid];
|
|
}
|
|
}
|
|
if (self.selectUserArray.count == self.userArray.count) {
|
|
self.allMicroButton.selected = YES;
|
|
self.isSelectAll = YES;
|
|
} else {
|
|
self.allMicroButton.selected = NO;
|
|
self.isSelectAll = NO;
|
|
}
|
|
}
|
|
#pragma mark - Getters And Setters
|
|
- (UIStackView *)stackView {
|
|
if (!_stackView) {
|
|
_stackView = [[UIStackView alloc] init];
|
|
_stackView.axis = UILayoutConstraintAxisHorizontal;
|
|
_stackView.distribution = UIStackViewDistributionFill;
|
|
_stackView.alignment = UIStackViewAlignmentCenter;
|
|
_stackView.spacing = 10;
|
|
}
|
|
return _stackView;
|
|
}
|
|
|
|
- (UIStackView *)oneStackView {
|
|
if (!_oneStackView) {
|
|
_oneStackView = [[UIStackView alloc] init];
|
|
_oneStackView.axis = UILayoutConstraintAxisHorizontal;
|
|
_oneStackView.distribution = UIStackViewDistributionFill;
|
|
_oneStackView.alignment = UIStackViewAlignmentCenter;
|
|
_oneStackView.spacing = 10;
|
|
}
|
|
return _oneStackView;
|
|
}
|
|
|
|
- (NetImageView *)avatarImageView {
|
|
if (!_avatarImageView) {
|
|
NetImageConfig * config = [[NetImageConfig alloc]init];
|
|
config.imageType = ImageTypeUserIcon;
|
|
config.placeHolder = [UIImageConstant defaultAvatarPlaceholder];
|
|
_avatarImageView = [[NetImageView alloc] initWithConfig:config];
|
|
_avatarImageView.layer.masksToBounds = YES;
|
|
_avatarImageView.layer.cornerRadius = 38/2;
|
|
}
|
|
return _avatarImageView;
|
|
}
|
|
|
|
- (UILabel *)nickLabel {
|
|
if (!_nickLabel) {
|
|
_nickLabel = [[UILabel alloc] init];
|
|
_nickLabel.font = [UIFont systemFontOfSize:12];
|
|
_nickLabel.textColor = [ThemeColor mainTextColor];
|
|
}
|
|
return _nickLabel;
|
|
}
|
|
|
|
- (UIButton *)allMicroButton {
|
|
if (!_allMicroButton) {
|
|
_allMicroButton = [UIButton buttonWithType:UIButtonTypeCustom];
|
|
[_allMicroButton setImage:[UIImage imageNamed:@"gift_all_micro_normal"] forState:UIControlStateNormal];
|
|
[_allMicroButton setImage:[UIImage imageNamed:@"gift_all_micro_select"] forState:UIControlStateSelected];
|
|
[_allMicroButton addTarget:self action:@selector(allMicroButtonAction:) forControlEvents:UIControlEventTouchUpInside];
|
|
}
|
|
return _allMicroButton;
|
|
}
|
|
|
|
- (UICollectionView *)collectionView{
|
|
if (!_collectionView) {
|
|
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
|
|
layout.itemSize = CGSizeMake(38, 43);
|
|
layout.minimumInteritemSpacing = 4;
|
|
layout.sectionInset = UIEdgeInsetsMake(0, 0, 0,0);
|
|
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
|
|
_collectionView.dataSource = self;
|
|
_collectionView.delegate = self;
|
|
_collectionView.backgroundColor = [UIColor clearColor];
|
|
_collectionView.showsHorizontalScrollIndicator = NO;
|
|
[_collectionView registerClass:[XPGiftUserCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([XPGiftUserCollectionViewCell class])];
|
|
}
|
|
return _collectionView;
|
|
}
|
|
|
|
- (NSMutableArray<NSString *> *)selectUserArray {
|
|
if (!_selectUserArray) {
|
|
_selectUserArray = [NSMutableArray array];
|
|
}
|
|
return _selectUserArray;
|
|
}
|
|
|
|
@end
|