SHomeSVideoV.m 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. //
  2. // SHomeSVideoV.m
  3. // BuguLive
  4. //
  5. // Created by 丁凯 on 2017/8/26.
  6. // Copyright © 2017年 xfg. All rights reserved.
  7. //
  8. #import "SHomeSVideoV.h"
  9. #import "SmallVideoCell.h"
  10. #import "SmallVideoListModel.h"
  11. #import "BGVideoDetailController.h"
  12. #import "HMVideoPlayerViewController.h"
  13. @implementation SHomeSVideoV
  14. -(NSMutableArray<XRImage *> *)images{
  15. if (!_images) {
  16. _images = [NSMutableArray array];
  17. }
  18. return _images;
  19. }
  20. - (instancetype)initWithFrame:(CGRect)frame andUserId:(NSString *)userId
  21. {
  22. self = [super initWithFrame:frame];
  23. if (self)
  24. {
  25. self.backgroundColor = kBackGroundColor;
  26. self.user_id = userId;
  27. [self creatMainView];
  28. [self requestNetDataWithPage:1];
  29. }
  30. return self;
  31. }
  32. - (void)creatMainView
  33. {
  34. // self.myCollectionLayout = [[UICollectionViewFlowLayout alloc]init];
  35. // self.myCollectionLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
  36. // self.myCollectionLayout.minimumInteritemSpacing = 5;
  37. // self.myCollectionLayout.itemSize = CGSizeMake((kScreenW-5*kScaleWidth)/2,(kScreenW-5*kScaleWidth)/2);
  38. self.myCollectionLayout = [XRWaterfallLayout waterFallLayoutWithColumnCount:2];
  39. //或者一次性设置
  40. [self.myCollectionLayout setColumnSpacing:10 rowSpacing:10 sectionInset:UIEdgeInsetsMake(10, 10, 10, 10)];
  41. self.myCollectionLayout.delegate = self;
  42. self.videoCollectionV = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, self.height) collectionViewLayout:self.myCollectionLayout];
  43. self.videoCollectionV.delegate = self;
  44. self.videoCollectionV.dataSource = self;
  45. self.videoCollectionV.backgroundColor = kBackGroundColor;
  46. self.videoCollectionV.pagingEnabled = YES;
  47. [self.videoCollectionV registerNib:[UINib nibWithNibName:@"SmallVideoCell" bundle:nil] forCellWithReuseIdentifier:@"SmallVideoCell"];
  48. [self addSubview:self.videoCollectionV];
  49. [self.videoCollectionV registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"foot"];
  50. // [BGMJRefreshManager refresh:self.videoCollectionV target:self headerRereshAction:nil footerRereshAction:@selector(refreshFooter)];
  51. [BGMJRefreshManager refresh:self.videoCollectionV target:self headerRereshAction:@selector(refreshHeader) footerRereshAction:@selector(refreshFooter)];
  52. }
  53. //根据item的宽度与indexPath计算每一个item的高度
  54. - (CGFloat)waterfallLayout:(XRWaterfallLayout *)waterfallLayout itemHeightForWidth:(CGFloat)itemWidth atIndexPath:(NSIndexPath *)indexPath {
  55. //根据图片的原始尺寸,及显示宽度,等比例缩放来计算显示高度
  56. XRImage *image = self.images[indexPath.item];
  57. if (indexPath.row % 2 == 0) {
  58. return (kScreenW-30)/2.0f * 1.8;
  59. }
  60. return (kScreenW-30)/2.0f * 1.4;
  61. // image.imageH / image.imageW * itemWidth;
  62. }
  63. -(void)refreshHeader{
  64. [self requestNetDataWithPage:1];
  65. }
  66. - (void)refreshFooter
  67. {
  68. if (_has_next == 1)
  69. {
  70. _currentPage ++;
  71. [self requestNetDataWithPage:_currentPage];
  72. }
  73. else
  74. {
  75. [self.videoCollectionV.mj_footer endRefreshingWithNoMoreData];
  76. }
  77. }
  78. - (void)requestNetDataWithPage:(int)page
  79. {
  80. NSMutableDictionary *parmDict = [NSMutableDictionary dictionary];
  81. [parmDict setObject:@"svideo" forKey:@"ctl"];
  82. if (self.user_id)
  83. {
  84. [parmDict setObject:self.user_id forKey:@"to_user_id"];
  85. }
  86. [parmDict setObject:@"video" forKey:@"act"];
  87. [parmDict setObject:[NSString stringWithFormat:@"%d",page] forKey:@"page"];
  88. FWWeakify(self)
  89. [self.httpsManager POSTWithParameters:parmDict SuccessBlock:^(NSDictionary *responseJson) {
  90. FWStrongify(self)
  91. if ([responseJson toInt:@"status"] == 1)
  92. {
  93. if (page == 1)
  94. {
  95. [self.dataArray removeAllObjects];
  96. [self.images removeAllObjects];
  97. }
  98. _has_next = [responseJson toInt:@"has_next"];
  99. _currentPage = [responseJson toInt:@"page"];
  100. NSString *focus = [responseJson toString:@"is_focus"];
  101. NSArray *list = responseJson[@"data"];
  102. for ( NSDictionary *dict in list)
  103. {
  104. SmallVideoListModel *model = [SmallVideoListModel mj_objectWithKeyValues:dict];
  105. XRImage *image = [XRImage new];
  106. image.imageURL = [NSURL URLWithString:model.photo_image];
  107. model.has_focus = focus;
  108. [self.images addObject:image];
  109. [self.dataArray addObject:model];
  110. }
  111. if (self.dataArray.count)
  112. {
  113. [self hideNoContentViewOnView:self.videoCollectionV];
  114. }else
  115. {
  116. [self showNoContentViewOnView:self.videoCollectionV];
  117. }
  118. [self.videoCollectionV reloadData];
  119. if (list.count < 20) {
  120. [self.videoCollectionV.mj_header endRefreshing];
  121. [self.videoCollectionV.mj_footer endRefreshingWithNoMoreData];
  122. }else{
  123. [BGMJRefreshManager endRefresh:self.videoCollectionV];
  124. }
  125. }
  126. else
  127. {
  128. [[BGHUDHelper sharedInstance] tipMessage:responseJson[@"error"]];
  129. [BGMJRefreshManager endRefresh:self.videoCollectionV];
  130. }
  131. } FailureBlock:^(NSError *error)
  132. {
  133. FWStrongify(self)
  134. [BGMJRefreshManager endRefresh:self.videoCollectionV];
  135. }];
  136. }
  137. #pragma mark collectionView delegate
  138. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  139. {
  140. return self.dataArray.count;
  141. }
  142. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  143. {
  144. SmallVideoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SmallVideoCell" forIndexPath:indexPath];
  145. SmallVideoListModel *model = self.dataArray[indexPath.row];
  146. [cell creatCellWithModel:model andRow:(int)indexPath.row];
  147. return cell;
  148. }
  149. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
  150. {
  151. return 5.0*kScaleWidth;
  152. }
  153. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  154. {
  155. if (indexPath.row < self.dataArray.count)
  156. {
  157. // HMVideoPlayerViewController *vc = [[HMVideoPlayerViewController alloc]initWithVideos:_dataArray index:indexPath.row IsPushed:YES requestDict:nil];
  158. // vc.isRefreshVideoBlock = ^(BOOL isRefresh) {
  159. // [self refreshHeader];
  160. // };
  161. NSLog(@"navigationController%@",[AppDelegate sharedAppDelegate].topViewController.navigationController);
  162. // [self presentViewController:nav animated:YES completion:nil];
  163. // [[AppDelegate sharedAppDelegate] pushViewController:nav animated:YES];
  164. // [[AppDelegate sharedAppDelegate].topViewController.navigationController pushViewController:vc animated:YES];
  165. // [[AppDelegate sharedAppDelegate] pushViewController:vc animated:YES];
  166. // SmallVideoListModel *model = _dataArray[indexPath.row];
  167. if (self.VDelegate)
  168. {
  169. [self.VDelegate pushToVideoDetailWithArr:_dataArray index:indexPath.row IsPushed:YES requestDict:nil];
  170. }
  171. }
  172. }
  173. -(void)scrollViewDidScroll:(UIScrollView *)scrollView{
  174. if (self.VDelegate && [self.VDelegate respondsToSelector:@selector(didVideoCollectionViewScrollView:)]) {
  175. [self.VDelegate didVideoCollectionViewScrollView:scrollView];
  176. }
  177. }
  178. - (NSMutableArray *)dataArray
  179. {
  180. if (!_dataArray)
  181. {
  182. _dataArray = [NSMutableArray arrayWithCapacity:0];
  183. }
  184. return _dataArray;
  185. }
  186. @end