MOGiftCollectionView.m 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. //
  2. // MOGiftCollectionView.m
  3. // MiMoLive
  4. //
  5. // Created by SuperC on 2023/11/23.
  6. //
  7. //一页的size
  8. #define kPageSize 60
  9. #import "MOGiftCollectionView.h"
  10. #import "MOShowBubbleTool.h"
  11. @interface MOGiftCollectionView ()
  12. <UICollectionViewDelegate,
  13. UICollectionViewDataSource,
  14. MOGiftListViewModelDelegate>
  15. @property (nonatomic, strong) UICollectionView *collectionView;
  16. /** 没有更多的数据视图 */
  17. @property (nonatomic, strong) MONoMoreDataView *noMoreDataView;
  18. @property (nonatomic, strong) MOGiftListViewModel *listViewModel;
  19. @property (nonatomic, assign) BOOL isOnShow;
  20. @end
  21. @implementation MOGiftCollectionView
  22. - (instancetype)initWith:(MOGiftListViewModel *)listViewModel {
  23. self = [super init];
  24. if (self) {
  25. [self setupUI];
  26. self.listViewModel = listViewModel;
  27. [listViewModel addObserver:self];
  28. }
  29. return self;
  30. }
  31. - (void)setupUI{
  32. [self addSubview:self.collectionView];
  33. [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
  34. make.edges.equalTo(self);
  35. }];
  36. }
  37. - (void)getHttpDataWithDict:(BOOL)firstPage {
  38. __weak typeof(self) weakSelf = self;
  39. [self.listViewModel loadGiftListWithCategory:self.type reload:firstPage completion:^(BOOL success) {
  40. __strong typeof(self) strongSelf = weakSelf; if (!strongSelf) return;
  41. if ([strongSelf.listViewModel isGiftListHasNext:strongSelf.type]) {
  42. [strongSelf.collectionView.mj_footer endRefreshing];
  43. } else {
  44. [strongSelf.collectionView.mj_footer endRefreshingWithNoMoreData];
  45. }
  46. }];
  47. }
  48. - (void)handleCellClick:(NSIndexPath *)indexPath {
  49. NSArray *giftList = [self.listViewModel giftListFor:self.type];
  50. if (self.selectIndexPath == indexPath) return;
  51. if (indexPath.row >= giftList.count) return;
  52. MOGiftlist *modle = giftList[indexPath.row];
  53. if (modle.lock) return;
  54. for (MOGiftlist *giftModel in giftList) {
  55. giftModel.isSelect = NO;
  56. }
  57. modle.isSelect = YES;
  58. self.selectIndexPath = indexPath;
  59. self.selectCellBlock(modle, self.type);
  60. }
  61. #pragma mark - JXCategoryListContentViewDelegate
  62. - (void)listWillAppear {
  63. self.isOnShow = YES;
  64. NSArray *giftList = [self.listViewModel giftListFor:self.type];
  65. self.noMoreDataView.isHaveData = giftList.count > 0;
  66. }
  67. - (void)listDidAppear {
  68. NSArray *giftList = [self.listViewModel giftListFor:self.type];
  69. if (giftList.count == 0) {
  70. [self getHttpDataWithDict:YES];
  71. } else{
  72. for (MOGiftlist *giftModel in giftList) {
  73. giftModel.isSelect = NO;
  74. }
  75. [self toChangeTheFirstChooseWith:giftList];//默认选中第一个
  76. [self toSelectTheHitGift:giftList];//跳转Hit礼物
  77. [self.collectionView reloadData];
  78. }
  79. }
  80. - (void)listWillDisappear {
  81. self.isOnShow = NO;
  82. }
  83. - (void)toChangeTheFirstChooseWith:(NSArray *)dataArr{
  84. if (![MOShowBubbleTool hadShowSendGiftGuideView]) {
  85. return;
  86. }
  87. if(dataArr.count == 0){
  88. return;
  89. }
  90. BOOL isHaveHit = NO;
  91. //是否有冲榜礼物对象
  92. if([MOSvgaSourceManage shareManager].hitGiftModel){
  93. NSString *theHitGiftId = [MOSvgaSourceManage shareManager].hitGiftModel.giftId;
  94. NSInteger theIndex = 0;
  95. for (MOGiftlist *object in dataArr) {
  96. if([object.giftInfo.id isEqualToString:theHitGiftId]){
  97. isHaveHit = YES;
  98. break;
  99. }
  100. theIndex ++;
  101. }
  102. }
  103. if(isHaveHit){
  104. return;
  105. }
  106. MOGiftlist *giftModel = dataArr.firstObject;
  107. if(!giftModel.lock){
  108. giftModel.isSelect = YES;
  109. self.selectIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];//选中第一个
  110. self.selectCellBlock(giftModel, self.type);
  111. }
  112. else{
  113. self.theFirstGiftBlock ? self.theFirstGiftBlock() : nil;
  114. }
  115. }
  116. - (void)toSelectTheHitGift:(NSArray *)giftList {
  117. //是否有冲榜礼物对象
  118. if([MOSvgaSourceManage shareManager].hitGiftModel){
  119. NSString *theHitGiftId = [MOSvgaSourceManage shareManager].hitGiftModel.giftId;
  120. NSInteger theIndex = 0;
  121. for (MOGiftlist *object in giftList) {
  122. if([object.giftInfo.id isEqualToString:theHitGiftId]){
  123. object.isSelect = YES;
  124. self.selectIndexPath = [NSIndexPath indexPathForRow:theIndex inSection:0];//选中第一个
  125. self.selectCellBlock(object, self.type);
  126. [self.collectionView scrollToItemAtIndexPath:self.selectIndexPath atScrollPosition:UICollectionViewScrollPositionTop animated:NO];
  127. [MOSvgaSourceManage shareManager].hitGiftModel = nil;
  128. break;
  129. }
  130. theIndex ++;
  131. }
  132. }
  133. }
  134. - (UIView *)listView{
  135. return self;
  136. }
  137. #pragma mark MOGiftListViewModelDelegate
  138. - (void)onGiftListChangedWithCategory:(NSInteger)category list:(NSArray<MOGiftlist *> *)list {
  139. if (self.type != category || !self.isOnShow) return;
  140. BOOL isHaveSelet = NO;
  141. for (MOGiftlist *giftModel in list) {
  142. if(giftModel.isSelect){
  143. isHaveSelet = YES;
  144. break;
  145. }
  146. }
  147. if(!isHaveSelet){
  148. [self toChangeTheFirstChooseWith:list];
  149. }
  150. [self.collectionView reloadData];
  151. self.noMoreDataView.isHaveData = list.count > 0 ? YES : NO;
  152. [self toSelectTheHitGift:list];//跳转Hit礼物
  153. }
  154. #pragma mark UICollectionViewDelegate,UICollectionViewDataSource
  155. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
  156. return 1;
  157. }
  158. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  159. return [self.listViewModel giftListFor:self.type].count;
  160. }
  161. -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  162. NSArray *giftList = [self.listViewModel giftListFor:self.type];
  163. WEAKSELF
  164. MOGiftlist *modle = giftList[indexPath.row];
  165. MOGiftItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:MOGiftItemCell_ID forIndexPath:indexPath];
  166. cell.isBag = false;
  167. cell.listModel = modle;
  168. cell.selectSkin = self.selectSkin;
  169. return cell;
  170. }
  171. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  172. [collectionView deselectItemAtIndexPath:indexPath animated:YES];
  173. [self handleCellClick:indexPath];
  174. [self.collectionView reloadData];
  175. }
  176. - (void)toUpdataTheSelectIndexPathCell{
  177. if(self.selectIndexPath){
  178. MOGiftItemCell *cell = (MOGiftItemCell *)[self.collectionView cellForItemAtIndexPath:self.selectIndexPath];
  179. if(cell){
  180. cell.selectSkin = self.selectSkin;
  181. [cell updataCellShowData];
  182. }
  183. }
  184. }
  185. - (void)oneSecondPassed{
  186. }
  187. #pragma mark - Lazy
  188. - (UICollectionView *)collectionView
  189. {
  190. if (!_collectionView)
  191. {
  192. UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
  193. CGFloat width = SCREENWIDTH / 4.0;
  194. CGFloat height = 104;
  195. flow.itemSize = CGSizeMake(width, height);
  196. flow.minimumLineSpacing = 4.0;//行间距
  197. flow.minimumInteritemSpacing = 0.0;//列间距
  198. flow.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
  199. _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 100) collectionViewLayout:flow];
  200. _collectionView.backgroundColor = [UIColor clearColor];
  201. _collectionView.dataSource = self;
  202. _collectionView.delegate = self;
  203. [_collectionView registerClass:[MOGiftItemCell class] forCellWithReuseIdentifier:MOGiftItemCell_ID];
  204. WEAKSELF
  205. MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
  206. _collectionView.mj_footer = footer;
  207. [footer setTitle:@" " forState:MJRefreshStateNoMoreData]; // 没有更多数据
  208. _collectionView.mj_footer.hidden = YES;
  209. _collectionView.backgroundView = self.noMoreDataView;
  210. }
  211. return _collectionView;
  212. }
  213. - (void)loadMoreData{
  214. if (![self.listViewModel isGiftListHasNext:self.type]) {
  215. return;
  216. }
  217. [self getHttpDataWithDict:NO];
  218. }
  219. - (MONoMoreDataView *)noMoreDataView{
  220. if(!_noMoreDataView){
  221. _noMoreDataView = [MONoMoreDataView new];
  222. _noMoreDataView.topImg.alpha = 0.6;
  223. _noMoreDataView.tipLab.textColor = [MOTools colorWithHexString:@"#FFFFFF" alpha:0.5];
  224. }
  225. return _noMoreDataView;
  226. }
  227. @end