Files
yinmeng-ios/xplan-ios/Main/ModuleKit/SendGiftView/View/XPGiftUsersView.m

338 lines
12 KiB
Mathematica
Raw Normal View History

2021-11-10 18:42:27 +08:00
//
// 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"
2021-12-17 19:31:26 +08:00
#import "UIImage+Utils.h"
2022-11-11 17:46:37 +08:00
#import "NSArray+Safe.h"
2021-11-10 18:42:27 +08:00
///Model
#import "UserInfoModel.h"
2021-11-17 19:29:14 +08:00
#import "XPGiftUserInfoModel.h"
2021-11-10 18:42:27 +08:00
///view
#import "XPGiftUserCollectionViewCell.h"
@interface XPGiftUsersView ()<UICollectionViewDelegate, UICollectionViewDataSource>
///
2021-11-10 18:42:27 +08:00
@property (nonatomic,strong) UIStackView *stackView;
2023-03-13 18:27:16 +08:00
///
@property (nonatomic,strong) UILabel *sendLabel;
///
@property (nonatomic,strong) UIImageView *allSendImageView;
2021-11-10 18:42:27 +08:00
///
@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;
2023-03-20 18:04:16 +08:00
///线
@property (nonatomic, strong) UIView *devideView;
///
2021-11-17 19:29:14 +08:00
@property (nonatomic,strong) NSArray<XPGiftUserInfoModel *> *userArray;
2021-11-10 18:42:27 +08:00
///
@property (nonatomic,strong) NSMutableArray<NSString *> *selectUserArray;
2021-11-16 19:56:37 +08:00
///
@property (nonatomic,assign) BOOL isSelectAll;
2021-11-10 18:42:27 +08:00
@end
@implementation XPGiftUsersView
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
2021-11-10 18:42:27 +08:00
[self initSubViews];
[self initSubViewConstraints];
}
return self;
}
#pragma mark - Response
- (void)allMicroButtonAction:(UIButton *)sender {
sender.selected = !sender.selected;
2021-11-17 19:29:14 +08:00
[self.userArray enumerateObjectsUsingBlock:^(XPGiftUserInfoModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
2021-11-16 19:56:37 +08:00
obj.isSelect = sender.selected;
2021-11-17 19:29:14 +08:00
NSString * selectUid = [NSString stringWithFormat:@"%ld", obj.uid];
2021-11-16 19:56:37 +08:00
if (obj.isSelect) {
if (![self.selectUserArray containsObject:selectUid]) {
[self.selectUserArray addObject:selectUid];
}
2021-11-16 19:56:37 +08:00
} else {
if ([self.selectUserArray containsObject:selectUid]) {
[self.selectUserArray removeObject:selectUid];
}
}
2021-11-10 18:42:27 +08:00
}];
2021-11-16 16:31:24 +08:00
self.isSelectAll = sender.selected;
2021-11-10 18:42:27 +08:00
[self.collectionView reloadData];
if (self.delegate && [self.delegate respondsToSelector:@selector(xPGiftUsersView:didSelectUsers:)]) {
[self.delegate xPGiftUsersView:self didSelectUsers:self.selectUserArray];
}
2021-11-10 18:42:27 +08:00
}
#pragma mark - Private Method
- (void)initSubViews {
2021-11-15 18:59:44 +08:00
self.backgroundColor = [UIColor clearColor];;
2021-11-10 18:42:27 +08:00
[self addSubview:self.stackView];
[self addSubview:self.oneStackView];
2023-03-20 18:04:16 +08:00
[self addSubview:self.devideView];
2023-03-13 18:27:16 +08:00
[self.stackView addArrangedSubview:self.sendLabel];
2021-11-10 18:42:27 +08:00
[self.stackView addArrangedSubview:self.collectionView];
2023-03-13 18:27:16 +08:00
[self.stackView addArrangedSubview:self.allSendImageView];
[self.allSendImageView addSubview:self.allMicroButton];
[self.oneStackView addArrangedSubview:self.avatarImageView];
[self.oneStackView addArrangedSubview:self.nickLabel];
2021-11-10 18:42:27 +08:00
}
- (void)initSubViewConstraints {
[self mas_makeConstraints:^(MASConstraintMaker *make) {
2021-11-16 19:56:37 +08:00
make.height.mas_equalTo(43 + 15 * 2);
2021-11-10 18:42:27 +08:00
}];
[self.stackView mas_makeConstraints:^(MASConstraintMaker *make) {
2023-03-13 18:27:16 +08:00
make.left.right.mas_equalTo(self).inset(0);
2021-11-15 18:59:44 +08:00
make.top.bottom.mas_equalTo(self);
}];
2023-03-13 18:27:16 +08:00
[self.sendLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.mas_equalTo(53);
make.height.mas_equalTo(43);
}];
2021-11-15 18:59:44 +08:00
[self.oneStackView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.mas_equalTo(self).inset(15);
make.top.bottom.mas_equalTo(self);
}];
2023-03-13 18:27:16 +08:00
[self.allSendImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(84, 50));
}];
2021-12-17 19:31:26 +08:00
[self.allMicroButton mas_makeConstraints:^(MASConstraintMaker *make) {
2023-03-13 18:27:16 +08:00
make.size.mas_equalTo(CGSizeMake(36, 18));
make.left.mas_equalTo(self.allSendImageView).offset(34);
make.centerY.mas_equalTo(self.allSendImageView);
2021-12-17 19:31:26 +08:00
}];
2021-11-15 18:59:44 +08:00
[self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
2021-11-16 19:56:37 +08:00
make.height.mas_equalTo(43);
2021-11-10 18:42:27 +08:00
}];
[self.avatarImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(38, 38));
}];
2023-03-20 18:04:16 +08:00
[self.devideView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.mas_equalTo(0);
make.height.mas_equalTo(1);
make.bottom.mas_equalTo(0);
}];
2021-11-10 18:42:27 +08:00
}
///
/// @param users
2021-11-17 19:29:14 +08:00
- (NSArray *)findSendGiftAllUsers:(NSArray<XPGiftUserInfoModel *> *)users {
2021-11-10 18:42:27 +08:00
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++) {
2022-11-11 17:46:37 +08:00
XPGiftUserInfoModel * userInfo = [newArray safeObjectAtIndex1:i];
2021-11-10 18:42:27 +08:00
if (userInfo && userInfo.uid > 0 && userInfo.uid != uid.integerValue) { ///
2021-11-16 16:31:24 +08:00
NSString * uid = [NSString stringWithFormat:@"%ld", userInfo.uid];
if (userInfo.isSelect) {
2021-11-16 16:31:24 +08:00
[self.selectUserArray addObject:uid];
}
2021-11-17 19:29:14 +08:00
[tempArray addObject:userInfo];
2021-11-10 18:42:27 +08:00
}
}
return [tempArray copy];
}
#pragma mark - Public Method
2021-11-17 19:29:14 +08:00
- (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;
}
2021-11-10 18:42:27 +08:00
}
#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];
2022-11-11 17:46:37 +08:00
XPGiftUserInfoModel * queue = [self.userArray safeObjectAtIndex1:indexPath.row];
2021-11-17 19:29:14 +08:00
cell.userInfo = queue;
2021-11-10 18:42:27 +08:00
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
[collectionView deselectItemAtIndexPath:indexPath animated:YES];
2022-11-11 17:46:37 +08:00
XPGiftUserInfoModel * queue = [self.userArray safeObjectAtIndex1:indexPath.row];
2021-11-10 18:42:27 +08:00
queue.isSelect = !queue.isSelect;
[self.collectionView reloadData];
2021-11-17 19:29:14 +08:00
NSString * selectUid = [NSString stringWithFormat:@"%ld", queue.uid];
2021-11-10 18:42:27 +08:00
if (queue.isSelect) {
[self.selectUserArray addObject:selectUid];
} else {
if ([self.selectUserArray containsObject:selectUid]) {
[self.selectUserArray removeObject:selectUid];
}
}
2021-11-15 18:59:44 +08:00
if (self.selectUserArray.count == self.userArray.count) {
self.allMicroButton.selected = YES;
2021-11-16 19:56:37 +08:00
self.isSelectAll = YES;
2021-11-15 18:59:44 +08:00
} else {
self.allMicroButton.selected = NO;
2021-11-16 19:56:37 +08:00
self.isSelectAll = NO;
2021-11-15 18:59:44 +08:00
}
if (self.delegate && [self.delegate respondsToSelector:@selector(xPGiftUsersView:didSelectUsers:)]) {
[self.delegate xPGiftUsersView:self didSelectUsers:self.selectUserArray];
}
2021-11-10 18:42:27 +08:00
}
#pragma mark - Getters And Setters
- (UIStackView *)stackView {
if (!_stackView) {
_stackView = [[UIStackView alloc] init];
_stackView.axis = UILayoutConstraintAxisHorizontal;
_stackView.distribution = UIStackViewDistributionFill;
_stackView.alignment = UIStackViewAlignmentCenter;
2023-03-13 18:27:16 +08:00
_stackView.spacing = 0;
2021-11-10 18:42:27 +08:00
}
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;
}
2023-03-13 18:27:16 +08:00
- (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;
}
2021-11-10 18:42:27 +08:00
- (UIButton *)allMicroButton {
if (!_allMicroButton) {
_allMicroButton = [UIButton buttonWithType:UIButtonTypeCustom];
2023-03-13 18:27:16 +08:00
[_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];
2021-12-17 19:31:26 +08:00
_allMicroButton.layer.masksToBounds = YES;
2023-03-13 18:27:16 +08:00
_allMicroButton.layer.cornerRadius = 9;
2021-11-10 18:42:27 +08:00
[_allMicroButton addTarget:self action:@selector(allMicroButtonAction:) forControlEvents:UIControlEventTouchUpInside];
}
return _allMicroButton;
}
2023-03-13 18:27:16 +08:00
- (UIImageView *)allSendImageView {
if (!_allSendImageView) {
_allSendImageView = [[UIImageView alloc] init];
_allSendImageView.userInteractionEnabled = YES;
_allSendImageView.image = [UIImage imageNamed:@"room_gift_all_mic_bg"];
}
return _allSendImageView;
}
2021-11-10 18:42:27 +08:00
- (UICollectionView *)collectionView{
if (!_collectionView) {
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
layout.itemSize = CGSizeMake(38, 43);
layout.minimumInteritemSpacing = 4;
2021-11-10 18:42:27 +08:00
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;
}
2023-03-20 18:04:16 +08:00
- (UIView *)devideView {
if (!_devideView) {
_devideView = [[UIView alloc] init];
_devideView.backgroundColor = UIColorRGBAlpha(0xffffff, 0.1);
}
return _devideView;
}
2021-11-10 18:42:27 +08:00
@end