Files
yinmeng-ios/xplan-ios/Base/UI/SendGiftView/View/XPGiftUsersView.m
2021-11-19 14:13:08 +08:00

186 lines
6.3 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"
///Model
#import "UserInfoModel.h"
#import "MicroQueueModel.h"
#import "XPGiftMicroUserModel.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,assign) SendGiftType type;
///
@property (nonatomic,strong) NSArray<XPGiftMicroUserModel *> *userArray;
///选中的人
@property (nonatomic,strong) NSMutableArray<NSString *> *selectUserArray;
@end
@implementation XPGiftUsersView
- (instancetype)initWithType:(SendGiftType)type {
if (self = [super init]) {
[self initSubViews];
[self initSubViewConstraints];
}
return self;
}
#pragma mark - Response
- (void)allMicroButtonAction:(UIButton *)sender {
sender.selected = !sender.selected;
[self.userArray enumerateObjectsUsingBlock:^(XPGiftMicroUserModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
obj.isSelect = YES;
}];
self.isSelectAll = sender.selected;
[self.collectionView reloadData];
}
#pragma mark - Private Method
- (void)initSubViews {
self.backgroundColor = [UIColor clearColor];;
[self addSubview:self.stackView];
[self.stackView addArrangedSubview:self.allMicroButton];
[self.stackView addArrangedSubview:self.collectionView];
if (self.type == SendGiftType_User) {
self.allMicroButton.hidden = YES;
}
}
- (void)initSubViewConstraints {
[self mas_makeConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(28 + 15 * 2);
}];
[self.stackView 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(35);
}];
}
/// 查找麦序 坑位上有人的麦序
/// @param users 麦序列表
- (NSArray *)findSendGiftAllUsers:(NSArray<MicroQueueModel *> *)users {
NSMutableArray * tempArray = [NSMutableArray array];
NSString * uid = [AccountInfoStorage instance].getUid;
for (int i = 0; i < users.count; i++) {
MicroQueueModel * micModel = [users objectAtIndex:i];
UserInfoModel * userInfo = micModel.userInfo;
if (userInfo && userInfo.uid > 0 && userInfo.uid != uid.integerValue) { ///自己在麦上不显示在送礼物列表中
NSString * uid = [NSString stringWithFormat:@"%ld", userInfo.uid];
XPGiftMicroUserModel * giftUserModel = [[XPGiftMicroUserModel alloc] init];
giftUserModel.microQueue = micModel;
if (self.allMicroButton.selected && ![self.selectUserArray containsObject:uid]) {
[self.selectUserArray addObject:uid];
}
if ([self.selectUserArray containsObject:uid]){
giftUserModel.isSelect = YES;
}
[tempArray addObject:giftUserModel];
}
}
return [tempArray copy];
}
#pragma mark - Public Method
- (void)microQueueUpdate:(NSArray<MicroQueueModel *> *)queue {
self.userArray = [self findSendGiftAllUsers:queue];
[self.collectionView reloadData];
}
#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];
XPGiftMicroUserModel * queue = [self.userArray objectAtIndex:indexPath.row];
cell.microModel = queue;
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
[collectionView deselectItemAtIndexPath:indexPath animated:YES];
XPGiftMicroUserModel * queue = [self.userArray objectAtIndex:indexPath.row];
queue.isSelect = !queue.isSelect;
[self.collectionView reloadData];
NSString * selectUid = [NSString stringWithFormat:@"%ld", queue.microQueue.userInfo.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;
} else {
self.allMicroButton.selected = 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;
}
- (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(35, 35);
layout.minimumLineSpacing = 6;
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