// // XPGiftUsersView.m // xplan-ios // // Created by 冯硕 on 2021/11/9. // #import "XPGiftUsersView.h" ///Third #import ///Tool #import "AccountInfoStorage.h" #import "NetImageView.h" #import "ThemeColor.h" #import "UIImage+Utils.h" #import "NSArray+Safe.h" ///Model #import "UserInfoModel.h" #import "XPGiftUserInfoModel.h" ///view #import "XPGiftUserCollectionViewCell.h" @interface XPGiftUsersView () ///麦上的送礼物 @property (nonatomic,strong) UIStackView *stackView; ///送给 @property (nonatomic,strong) UILabel *sendLabel; ///全麦送背景 @property (nonatomic,strong) UIImageView *allSendImageView; ///全麦 @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) UIView *devideView; ///原始的数据 @property (nonatomic,strong) NSArray *userArray; ///选中的人 @property (nonatomic,strong) NSMutableArray *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]; if (self.delegate && [self.delegate respondsToSelector:@selector(xPGiftUsersView:didSelectUsers:)]) { [self.delegate xPGiftUsersView:self didSelectUsers:self.selectUserArray]; } } #pragma mark - Private Method - (void)initSubViews { self.backgroundColor = [UIColor clearColor];; [self addSubview:self.stackView]; [self addSubview:self.oneStackView]; [self addSubview:self.devideView]; [self.stackView addArrangedSubview:self.sendLabel]; [self.stackView addArrangedSubview:self.collectionView]; [self.stackView addArrangedSubview:self.allSendImageView]; [self.allSendImageView addSubview:self.allMicroButton]; [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(0); make.top.bottom.mas_equalTo(self); }]; [self.sendLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.width.mas_equalTo(53); make.height.mas_equalTo(43); }]; [self.oneStackView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.mas_equalTo(self).inset(15); make.top.bottom.mas_equalTo(self); }]; [self.allSendImageView mas_makeConstraints:^(MASConstraintMaker *make) { make.size.mas_equalTo(CGSizeMake(84, 50)); }]; [self.allMicroButton mas_makeConstraints:^(MASConstraintMaker *make) { make.size.mas_equalTo(CGSizeMake(36, 18)); make.left.mas_equalTo(self.allSendImageView).offset(34); make.centerY.mas_equalTo(self.allSendImageView); }]; [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) { make.height.mas_equalTo(43); }]; [self.avatarImageView mas_makeConstraints:^(MASConstraintMaker *make) { make.size.mas_equalTo(CGSizeMake(38, 38)); }]; [self.devideView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.mas_equalTo(0); make.height.mas_equalTo(1); make.bottom.mas_equalTo(0); }]; } /// 查找麦序 坑位上有人的麦序 /// @param users 麦序列表 - (NSArray *)findSendGiftAllUsers:(NSArray *)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 safeObjectAtIndex1: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 *)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 safeObjectAtIndex1:indexPath.row]; cell.userInfo = queue; return cell; } - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { [collectionView deselectItemAtIndexPath:indexPath animated:YES]; XPGiftUserInfoModel * queue = [self.userArray safeObjectAtIndex1: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; } if (self.delegate && [self.delegate respondsToSelector:@selector(xPGiftUsersView:didSelectUsers:)]) { [self.delegate xPGiftUsersView:self didSelectUsers:self.selectUserArray]; } } #pragma mark - Getters And Setters - (UIStackView *)stackView { if (!_stackView) { _stackView = [[UIStackView alloc] init]; _stackView.axis = UILayoutConstraintAxisHorizontal; _stackView.distribution = UIStackViewDistributionFill; _stackView.alignment = UIStackViewAlignmentCenter; _stackView.spacing = 0; } 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 *)sendLabel { if (!_sendLabel) { _sendLabel = [[UILabel alloc] init]; _sendLabel.font = [UIFont systemFontOfSize:13 weight:UIFontWeightMedium]; _sendLabel.textColor = [UIColor whiteColor]; _sendLabel.text = @"送给"; _sendLabel.textAlignment = NSTextAlignmentCenter; } return _sendLabel; } - (UILabel *)nickLabel { if (!_nickLabel) { _nickLabel = [[UILabel alloc] init]; _nickLabel.font = [UIFont systemFontOfSize:15]; _nickLabel.textColor = [UIColor whiteColor]; } return _nickLabel; } - (UIButton *)allMicroButton { if (!_allMicroButton) { _allMicroButton = [UIButton buttonWithType:UIButtonTypeCustom]; [_allMicroButton setBackgroundImage:[UIImage gradientColorImageFromColors:@[[ThemeColor colorWithHexString:@"#878B9C"], [ThemeColor colorWithHexString:@"#878B9C"]] gradientType:GradientTypeLeftToRight imgSize:CGSizeMake(10, 10)] forState:UIControlStateNormal]; [_allMicroButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; [_allMicroButton setBackgroundImage:[UIImage gradientColorImageFromColors:@[[ThemeColor colorWithHexString:@"#FFE710"], [ThemeColor colorWithHexString:@"#FFE710"]] gradientType:GradientTypeLeftToRight imgSize:CGSizeMake(10, 10)] forState:UIControlStateSelected]; [_allMicroButton setTitleColor:[ThemeColor colorWithHexString:@"#282828"] forState:UIControlStateSelected]; [_allMicroButton setTitle:@"全选" forState:UIControlStateSelected]; [_allMicroButton setTitle:@"全选" forState:UIControlStateNormal]; _allMicroButton.titleLabel.font = [UIFont systemFontOfSize:13 weight:UIFontWeightMedium]; _allMicroButton.layer.masksToBounds = YES; _allMicroButton.layer.cornerRadius = 9; [_allMicroButton addTarget:self action:@selector(allMicroButtonAction:) forControlEvents:UIControlEventTouchUpInside]; } return _allMicroButton; } - (UIImageView *)allSendImageView { if (!_allSendImageView) { _allSendImageView = [[UIImageView alloc] init]; _allSendImageView.userInteractionEnabled = YES; _allSendImageView.image = [UIImage imageNamed:@"room_gift_all_mic_bg"]; } return _allSendImageView; } - (UICollectionView *)collectionView{ if (!_collectionView) { UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; layout.scrollDirection = UICollectionViewScrollDirectionHorizontal; 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 *)selectUserArray { if (!_selectUserArray) { _selectUserArray = [NSMutableArray array]; } return _selectUserArray; } - (UIView *)devideView { if (!_devideView) { _devideView = [[UIView alloc] init]; _devideView.backgroundColor = UIColorRGBAlpha(0xffffff, 0.1); } return _devideView; } @end