MSmallVideoVC.m 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. //
  2. // MSmallVideoVC.m
  3. // BuguLive
  4. //
  5. // Created by 丁凯 on 2017/8/17.
  6. // Copyright © 2017年 xfg. All rights reserved.
  7. //
  8. #import "MSmallVideoVC.h"
  9. #import "SSearchVC.h"
  10. #import "SmallVideoCell.h"
  11. #import "SmallVideoListModel.h"
  12. #import "BGVideoDetailController.h"
  13. #import "HMVideoPlayerViewController.h"
  14. #import "BogoVideoPlayViewController.h"
  15. #import "XRWaterfallLayout.h"
  16. #import "XRImage.h"
  17. @interface MSmallVideoVC ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UIScrollViewDelegate,XRWaterfallLayoutDelegate>
  18. @property ( nonatomic,strong) XRWaterfallLayout *myCollectionLayout;
  19. @property ( nonatomic,strong) NSMutableArray *dataArray;
  20. @property ( nonatomic,assign) int currentPage;
  21. @property ( nonatomic,assign) int has_next;
  22. @property (nonatomic, strong) NSMutableArray<XRImage *> *images;
  23. @property(nonatomic, assign) NSInteger page;
  24. @end
  25. @implementation MSmallVideoVC
  26. -(NSMutableArray<XRImage *> *)images{
  27. if (!_images) {
  28. _images = [NSMutableArray array];
  29. }
  30. return _images;
  31. }
  32. - (void)viewDidLoad
  33. {
  34. [super viewDidLoad];
  35. }
  36. - (NSMutableArray *)dataArray
  37. {
  38. if (!_dataArray)
  39. {
  40. _dataArray = [NSMutableArray arrayWithCapacity:0];
  41. }
  42. return _dataArray;
  43. }
  44. - (void)viewWillAppear:(BOOL)animated
  45. {
  46. [super viewWillAppear:animated];
  47. if (_isHaveNavBar) {
  48. [self.navigationController setNavigationBarHidden:NO animated:NO];
  49. self.navigationController.navigationBar.hidden = NO;
  50. }
  51. // [self.navigationController setNavigationBarHidden:NO animated:NO];
  52. // [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:animated];
  53. }
  54. - (void)initFWUI
  55. {
  56. [super initFWUI];
  57. [self setupBackBtnWithBlock:nil];
  58. self.title = ASLocalizedString(@"小视频");
  59. // self.view.backgroundColor = kGreenColor;
  60. self.myCollectionLayout = [XRWaterfallLayout waterFallLayoutWithColumnCount:2];
  61. //或者一次性设置
  62. [self.myCollectionLayout setColumnSpacing:15 rowSpacing:15 sectionInset:UIEdgeInsetsMake(10, 10, 10, 10)];
  63. self.myCollectionLayout.delegate = self;
  64. // self.myCollectionLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
  65. // self.myCollectionLayout.minimumInteritemSpacing = 5;
  66. // self.myCollectionLayout.itemSize = );
  67. if (!self.notHaveTabbar)
  68. {
  69. self.videoCollectionV = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH-FD_Top_Height-FD_Bottom_Height) collectionViewLayout:self.myCollectionLayout];
  70. }else
  71. {
  72. self.videoCollectionV = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH-FD_Top_Height) collectionViewLayout:self.myCollectionLayout];
  73. }
  74. self.videoCollectionV.delegate = self;
  75. self.videoCollectionV.dataSource = self;
  76. self.videoCollectionV.backgroundColor = UIColor.clearColor;
  77. [self.videoCollectionV registerNib:[UINib nibWithNibName:@"SmallVideoCell" bundle:nil] forCellWithReuseIdentifier:@"SmallVideoCell"];
  78. [self.view addSubview:self.videoCollectionV];
  79. [self.videoCollectionV registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"foot"];
  80. [BGMJRefreshManager refresh:self.videoCollectionV target:self headerRereshAction:@selector(refreshHeader) footerRereshAction:@selector(refreshFooter)];
  81. }
  82. - (void)returnCenterVC
  83. {
  84. [[AppDelegate sharedAppDelegate]popViewController];
  85. }
  86. - (void)refreshHeader
  87. {
  88. [self requestNetDataWithPage:1];
  89. }
  90. - (void)refreshFooter
  91. {
  92. if (_has_next == 1)
  93. {
  94. _currentPage ++;
  95. [self requestNetDataWithPage:_currentPage];
  96. }
  97. else
  98. {
  99. [self.videoCollectionV.mj_footer endRefreshingWithNoMoreData];
  100. }
  101. }
  102. - (void)requestNetDataWithPage:(int)Page
  103. {
  104. if (kIsCheckingVersion())
  105. {
  106. [self.dataArray removeAllObjects];
  107. [self.images removeAllObjects];
  108. [self.videoCollectionV reloadData];
  109. [BGMJRefreshManager endRefresh:self.videoCollectionV];
  110. return;
  111. }
  112. NSMutableDictionary *parmDict = [NSMutableDictionary dictionary];
  113. [parmDict setObject:@"svideo" forKey:@"ctl"];
  114. if (!self.notHaveTabbar)
  115. {
  116. [parmDict setObject:@"index" forKey:@"act"];
  117. }else
  118. {
  119. [parmDict setObject:@"video" forKey:@"act"];
  120. }
  121. [parmDict setObject:[NSNumber numberWithInt:Page] forKey:@"page"];
  122. if (self.paramDict) {
  123. [parmDict setValuesForKeysWithDictionary:self.paramDict];
  124. }
  125. FWWeakify(self)
  126. [self.httpsManager POSTWithParameters:parmDict SuccessBlock:^(NSDictionary *responseJson) {
  127. FWStrongify(self)
  128. if ([responseJson toInt:@"status"] == 1)
  129. {
  130. if (Page == 1)
  131. {
  132. [self.dataArray removeAllObjects];
  133. [self.images removeAllObjects];
  134. }
  135. _has_next = [responseJson toInt:@"has_next"];
  136. _currentPage = [responseJson toInt:@"page"];
  137. NSString *focus = [responseJson toString:@"is_focus"];
  138. NSArray *list = responseJson[@"data"];
  139. for ( NSDictionary *dict in list)
  140. {
  141. SmallVideoListModel *model = [SmallVideoListModel mj_objectWithKeyValues:dict];
  142. XRImage *image = [XRImage new];
  143. image.imageURL = [NSURL URLWithString:model.photo_image];
  144. model.has_focus = focus;
  145. [self.images addObject:image];
  146. [self.dataArray addObject:model];
  147. }
  148. [self.videoCollectionV reloadData];
  149. }
  150. [BGMJRefreshManager endRefresh:self.videoCollectionV];
  151. if (!self.dataArray.count)
  152. {
  153. [self showNoContentView];
  154. }
  155. else
  156. {
  157. [self hideNoContentView];
  158. }
  159. } FailureBlock:^(NSError *error) {
  160. FWStrongify(self)
  161. [BGMJRefreshManager endRefresh:self.videoCollectionV];
  162. }];
  163. }
  164. #pragma mark collectionView delegate
  165. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  166. {
  167. return self.dataArray.count;
  168. }
  169. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  170. {
  171. SmallVideoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SmallVideoCell" forIndexPath:indexPath];
  172. SmallVideoListModel *model = self.dataArray[indexPath.row];
  173. [cell creatCellWithModel:model andRow:(int)indexPath.row];
  174. return cell;
  175. }
  176. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  177. {
  178. if (indexPath.row < self.dataArray.count)
  179. {
  180. // SmallVideoListModel *model = _dataArray[indexPath.row];
  181. // BGVideoDetailController *VideoVC = [[BGVideoDetailController alloc]init];
  182. // VideoVC.weibo_id = model.weibo_id;
  183. // [[AppDelegate sharedAppDelegate] pushViewController:VideoVC];
  184. // BogoVideoPlayViewController *vc = [[BogoVideoPlayViewController alloc]initWithModelArr:_dataArray];
  185. HMVideoPlayerViewController *vc = [[HMVideoPlayerViewController alloc]initWithVideos:_dataArray index:indexPath.item IsPushed:YES requestDict:nil];
  186. WeakSelf
  187. vc.isRefreshVideoBlock = ^(BOOL isRefresh) {
  188. [weakSelf refreshHeader];
  189. };
  190. [[AppDelegate sharedAppDelegate] pushViewController:vc animated:YES];
  191. }
  192. }
  193. ////每个item之间的横间距
  194. //- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
  195. //
  196. // return 10;
  197. //}
  198. ////每个item之间的纵间距
  199. // - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
  200. //{
  201. // if (section == 0 || section % 3 == 0) {
  202. // return 10;
  203. // }
  204. // return 0;
  205. //}
  206. //
  207. //- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
  208. //{
  209. // if (section == 1) {
  210. // return UIEdgeInsetsMake(0, 10, 0, 10);
  211. // }
  212. // return UIEdgeInsetsMake(-10, 10, 0, 10);
  213. //}
  214. //根据item的宽度与indexPath计算每一个item的高度
  215. - (CGFloat)waterfallLayout:(XRWaterfallLayout *)waterfallLayout itemHeightForWidth:(CGFloat)itemWidth atIndexPath:(NSIndexPath *)indexPath {
  216. //根据图片的原始尺寸,及显示宽度,等比例缩放来计算显示高度
  217. XRImage *image = self.images[indexPath.item];
  218. if (indexPath.row % 2 == 0) {
  219. return (kScreenW-30)/2.0f * 1.8;
  220. }
  221. return (kScreenW-30)/2.0f * 1.4;
  222. // image.imageH / image.imageW * itemWidth;
  223. }
  224. //-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
  225. //
  226. // if (indexPath.row == 1) {
  227. // return CGSizeMake((kScreenW-30)/2.0f,(kScreenW-30)/2.0f * 1.5);
  228. // }
  229. //
  230. // return CGSizeMake((kScreenW-30)/2.0f,(kScreenW-30)/2.0f * 1.2);
  231. //}
  232. #pragma mark - ----------------------- scrollViewDelegate -----------------------
  233. - (void)scrollViewDidScroll:(UIScrollView *)scrollView
  234. {
  235. }
  236. @end