YBUserListView.m 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. //
  2. // YBUserListView.m
  3. // YBVideo
  4. //
  5. // Created by YB007 on 2019/11/30.
  6. // Copyright © 2019 cat. All rights reserved.
  7. //
  8. #import "YBUserListView.h"
  9. #import "YBUserListModel.h"
  10. #import "YBUserListCell.h"
  11. @interface YBUserListView()<UICollectionViewDelegate,UICollectionViewDataSource>
  12. {
  13. int _userCount; //预留
  14. }
  15. @property(nonatomic,strong)UICollectionView *collectionView;
  16. @property(nonatomic,strong)NSMutableArray *listArray;
  17. @property(nonatomic,strong)NSMutableArray *listModel;
  18. @end
  19. @implementation YBUserListView
  20. - (instancetype)initWithFrame:(CGRect)frame {
  21. self = [super initWithFrame:frame];
  22. if (self) {
  23. _listArray = [NSMutableArray array];
  24. _listModel = [NSMutableArray array];
  25. [self addSubview:self.collectionView];
  26. }
  27. return self;
  28. }
  29. - (void)layoutSubviews {
  30. [super layoutSubviews];
  31. [_collectionView mas_remakeConstraints:^(MASConstraintMaker *make) {
  32. make.width.height.centerX.centerY.equalTo(self);
  33. }];
  34. }
  35. - (NSMutableArray *)listModel {
  36. NSMutableArray *array = [NSMutableArray array];
  37. for (NSDictionary *dic in _listArray) {
  38. YBUserListModel *model = [YBUserListModel modelWithDic:dic];
  39. [array addObject:model];
  40. }
  41. _listModel = [array mutableCopy];
  42. return _listModel;
  43. }
  44. #pragma mark - 用户第一次进房间、请求僵尸粉数组赋值
  45. -(void)updateListCount:(NSArray *)listArray {
  46. if(_listArray.count > 0){
  47. [_listArray removeAllObjects];
  48. }
  49. [_listArray addObjectsFromArray:listArray];
  50. _userCount = (int)_listArray.count;
  51. }
  52. #pragma mark - 用户进入、离开
  53. -(void)userEventOfType:(UserEventType)eventType andInfo:(NSDictionary *)eventDic {
  54. if (eventType == UserEvent_Enter) {
  55. //进入
  56. _userCount +=1;
  57. NSString *ID = [[eventDic valueForKey:@"ct"] valueForKey:@"id"];
  58. [_listArray enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  59. for (NSDictionary *dic in _listArray) {
  60. int a = [[dic valueForKey:@"id"] intValue];
  61. int bsss = [ID intValue];
  62. if ([[dic valueForKey:@"id"] isEqual:ID] || a == bsss) {
  63. [_listArray removeObject:dic];
  64. break;
  65. }
  66. }
  67. }];
  68. NSDictionary *subdic = [eventDic valueForKey:@"ct"];
  69. [self.listArray addObject:subdic];
  70. [_collectionView reloadData];
  71. }else {
  72. //离开
  73. _userCount -=1;
  74. if (_userCount <=0) {
  75. _userCount = 0;
  76. }
  77. NSDictionary *SUBdIC =[eventDic valueForKey:@"ct"];
  78. NSString *ID = [SUBdIC valueForKey:@"id"];
  79. [_listArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  80. for (NSDictionary *dic in _listArray) {
  81. if ([[dic valueForKey:@"id"] isEqual:ID]) {
  82. [_listArray removeObject:dic];
  83. [_collectionView reloadData];
  84. return ;
  85. }
  86. }
  87. }];
  88. }
  89. }
  90. #pragma mark - /** 计时器刷新 */
  91. -(void)timerReloadList {
  92. if ([PublicObj checkNull:_liveUid] || [PublicObj checkNull:_liveStream]) {
  93. [MBProgressHUD showPop:YZMsg(@"缺少信息")];
  94. return;
  95. }
  96. NSDictionary *postDic = @{
  97. @"liveuid":_liveUid,
  98. @"stream":_liveStream,
  99. };
  100. [YBNetworking postWithUrl:@"Live.getUserLists" Dic:postDic Suc:^(int code, id info, NSString *msg) {
  101. if (code == 0) {
  102. NSArray *infos = [info firstObject];
  103. NSArray *list = [infos valueForKey:@"userlist"];
  104. if ([list isEqual:[NSNull null]]) {
  105. return ;
  106. }
  107. [_listArray removeAllObjects];
  108. [_listArray addObjectsFromArray:list];
  109. [_collectionView reloadData];
  110. }
  111. } Fail:^(id fail) {
  112. }];
  113. }
  114. -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
  115. return self.listModel.count;
  116. }
  117. -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
  118. return 1;
  119. }
  120. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
  121. return 0;
  122. }
  123. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
  124. return 0;
  125. }
  126. -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
  127. YBUserListCell *cell = (YBUserListCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"YBUserListCell" forIndexPath:indexPath];
  128. YBUserListModel *model = _listModel[indexPath.row];
  129. cell.model = model;
  130. cell.backgroundColor = [UIColor clearColor];
  131. if (indexPath.row < 3 && [model.contribution intValue]>0) {
  132. cell.kuang.image = [UIImage imageNamed:[NSString stringWithFormat:@"userlist_no%ld",indexPath.row+1]];
  133. }else{
  134. cell.kuang.image = [UIImage new];
  135. }
  136. return cell;
  137. }
  138. -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  139. YBUserListModel *model = _listModel[indexPath.row];
  140. NSString *ID = model.userID;
  141. NSDictionary *subdic = [NSDictionary dictionaryWithObjects:@[ID,model.user_nickname] forKeys:@[@"id",@"name"]];
  142. if (self.listEvent) {
  143. self.listEvent(@"用户列表-用户信息", subdic);
  144. }
  145. }
  146. -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
  147. return CGSizeMake(40,40);
  148. }
  149. -(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
  150. return UIEdgeInsetsMake(0,5,0,5);
  151. }
  152. - (UICollectionView *)collectionView {
  153. if (!_collectionView) {
  154. UICollectionViewFlowLayout *flowlayoutt = [[UICollectionViewFlowLayout alloc]init];
  155. flowlayoutt.itemSize = CGSizeMake(40,40);
  156. flowlayoutt.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  157. _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0,0,self.width,self.height) collectionViewLayout:flowlayoutt];
  158. _collectionView.showsHorizontalScrollIndicator = NO;
  159. _collectionView.delegate = self;
  160. _collectionView.dataSource = self;
  161. [_collectionView registerClass:[YBUserListCell class] forCellWithReuseIdentifier:@"YBUserListCell"];
  162. _collectionView.backgroundColor = [UIColor clearColor];
  163. }
  164. return _collectionView;
  165. }
  166. @end