TCLiveListViewController.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. //
  2. // TCLiveListViewController.m
  3. // TCLVBIMDemo
  4. //
  5. // Created by annidyfeng on 16/7/29.
  6. // Copyright © 2016年 tencent. All rights reserved.
  7. //
  8. #import "TCLiveListViewController.h"
  9. #import "TCLiveListCell.h"
  10. #import "TCLiveListModel.h"
  11. #import <MJRefresh/MJRefresh.h>
  12. #import <AFNetworking.h>
  13. #import "HUDHelper.h"
  14. #import <MJExtension/MJExtension.h>
  15. #import <BlocksKit/BlocksKit.h>
  16. #import "TCLiveListModel.h"
  17. #import "UIColor+MLPFlatColors.h"
  18. #import "TCVodPlayViewController.h"
  19. @interface TCLiveListViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
  20. @property TCLiveListMgr *liveListMgr;
  21. @property(nonatomic, strong) NSMutableArray *lives;
  22. @property(nonatomic, strong) UICollectionView *collectionView;
  23. @property BOOL isLoading;
  24. @end
  25. @implementation TCLiveListViewController
  26. {
  27. BOOL _hasEnterplayVC;
  28. UIButton *_ugcVideoBtn;
  29. UIView *_scrollView;
  30. UIView *_nullDataView;
  31. CGFloat scrollViewWidth;
  32. CGFloat scrollViewHeight;
  33. }
  34. - (instancetype)init {
  35. self = [super init];
  36. if (self) {
  37. self.lives = [NSMutableArray array];
  38. _liveListMgr = [TCLiveListMgr sharedMgr];
  39. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(newDataAvailable:) name:kTCLiveListNewDataAvailable object:nil];
  40. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(listDataUpdated:) name:kTCLiveListUpdated object:nil];
  41. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(svrError:) name:kTCLiveListSvrError object:nil];
  42. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playError:) name:kTCLivePlayError object:nil];
  43. }
  44. return self;
  45. }
  46. - (void)viewDidLoad {
  47. [super viewDidLoad];
  48. self.automaticallyAdjustsScrollViewInsets = YES;
  49. self.edgesForExtendedLayout = UIRectEdgeAll;
  50. self.view.backgroundColor = UIColorFromRGB(0xF6F2F4);
  51. self.navigationItem.title = NSLocalizedString(@"TCLiveListView.NewLive", nil);
  52. CGFloat btnWidth = 38;
  53. CGFloat btnHeight = 24;
  54. CGFloat statuBarHeight = [UIApplication sharedApplication].statusBarFrame.size.height;
  55. scrollViewWidth = 70;
  56. scrollViewHeight = 3;
  57. UIView *tabView = [[UIView alloc] initWithFrame:CGRectMake(0,statuBarHeight,SCREEN_WIDTH, 44)];
  58. tabView.backgroundColor = [UIColor whiteColor];
  59. _ugcVideoBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  60. [_ugcVideoBtn setTitle:NSLocalizedString(@"Common.App", nil) forState:UIControlStateNormal];
  61. [_ugcVideoBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  62. _ugcVideoBtn.titleLabel.font = [UIFont systemFontOfSize:18];
  63. [_ugcVideoBtn sizeToFit];
  64. btnWidth = _ugcVideoBtn.width - 20;
  65. [_ugcVideoBtn setFrame:CGRectMake(self.view.width / 2 - (btnWidth + 20) / 2 , 11, btnWidth + 20, btnHeight)];
  66. [_ugcVideoBtn addTarget:self action:@selector(videoBtnClick:) forControlEvents:UIControlEventTouchUpInside];
  67. _scrollView = [[UIView alloc] initWithFrame:CGRectMake(_ugcVideoBtn.left - (scrollViewWidth - _ugcVideoBtn.width)/2, _ugcVideoBtn.bottom + 5, scrollViewWidth, scrollViewHeight)];
  68. _scrollView.backgroundColor = UIColorFromRGB(0xFF0ACBAB);
  69. UIView *boomView = [[UIView alloc] initWithFrame:CGRectMake(0, _scrollView.bottom, SCREEN_WIDTH, 1)];
  70. boomView.backgroundColor = UIColorFromRGB(0xD8D8D8);
  71. [tabView addSubview:_ugcVideoBtn];
  72. [tabView addSubview:_scrollView];
  73. [tabView addSubview:boomView];
  74. [self.view addSubview:tabView];
  75. //self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,tabView.bottom, SCREEN_WIDTH, self.view.height - tabView.height - statuBarHeight) //style:UITableViewStylePlain];
  76. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  77. [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
  78. self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0,tabView.bottom, self.view.width, self.view.height - tabView.bottom) collectionViewLayout:layout];
  79. self.collectionView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  80. [self.collectionView registerClass:[TCLiveListCell class] forCellWithReuseIdentifier:@"TCLiveListCell"];
  81. self.collectionView.dataSource = self;
  82. self.collectionView.delegate = self;
  83. self.collectionView.backgroundColor = [UIColor blackColor];
  84. [self.view addSubview:self.collectionView];
  85. CGFloat nullViewWidth = 90;
  86. CGFloat nullViewHeight = 115;
  87. CGFloat imageViewWidth = 68;
  88. CGFloat imageViewHeight = 74;
  89. UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake((nullViewWidth - imageViewWidth)/2, 0, imageViewWidth, imageViewHeight)];
  90. imageView.image = [UIImage imageNamed:@"null_image"];
  91. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, imageView.bottom + 5, nullViewWidth, 22)];
  92. label.text = NSLocalizedString(@"TCLiveListView.NoContent", nil);
  93. label.font = [UIFont systemFontOfSize:16];
  94. label.textColor = UIColorFromRGB(0x777777);
  95. _nullDataView = [[UIView alloc] initWithFrame:CGRectMake((SCREEN_WIDTH - nullViewWidth)/2, (self.view.height - nullViewHeight)/2, nullViewWidth, nullViewHeight)];
  96. [_nullDataView addSubview:imageView];
  97. [_nullDataView addSubview:label];
  98. _nullDataView.hidden = YES;
  99. [self.view addSubview:_nullDataView];
  100. [self setup];
  101. }
  102. -(void)viewWillAppear:(BOOL)animated{
  103. [super viewWillAppear:animated];
  104. [self.navigationController setNavigationBarHidden:NO];
  105. self.navigationController.navigationBar.hidden = YES;
  106. UIView *statusBar;
  107. if (@available(iOS 13.0, *)) {
  108. statusBar = [[UIView alloc] initWithFrame:[UIApplication sharedApplication].statusBarFrame];
  109. } else {
  110. statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
  111. }
  112. if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
  113. statusBar.backgroundColor = [UIColor whiteColor];
  114. }
  115. }
  116. -(void)viewWillDisappear:(BOOL)animated
  117. {
  118. UIView *statusBar;
  119. if (@available(iOS 13.0, *)) {
  120. statusBar = [[UIView alloc] initWithFrame:[UIApplication sharedApplication].statusBarFrame];
  121. } else {
  122. statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
  123. }
  124. if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
  125. statusBar.backgroundColor = [UIColor clearColor];
  126. }
  127. }
  128. - (void)viewDidAppear:(BOOL)animated {
  129. [super viewDidAppear:animated];
  130. _playVC = nil;
  131. _hasEnterplayVC = NO;
  132. }
  133. - (void)viewDidDisappear:(BOOL)animated {
  134. [super viewDidDisappear:animated];
  135. }
  136. - (void)setup
  137. {
  138. [self.collectionView.mj_header endRefreshing];
  139. [self.collectionView.mj_footer endRefreshing];
  140. if(self.lives) [self.lives removeAllObjects];
  141. self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
  142. self.isLoading = YES;
  143. self.lives = [NSMutableArray array];
  144. [_liveListMgr queryVideoList:GetType_Up];
  145. }];
  146. self.collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
  147. self.isLoading = YES;
  148. [_liveListMgr queryVideoList:GetType_Down];
  149. }];
  150. // 先加载缓存的数据,然后再开始网络请求,以防用户打开是看到空数据
  151. [self.liveListMgr loadVodsFromArchive];
  152. [self doFetchList];
  153. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  154. [self.collectionView.mj_header beginRefreshing];
  155. });
  156. [(MJRefreshHeader *)self.collectionView.mj_header endRefreshingWithCompletionBlock:^{
  157. self.isLoading = NO;
  158. }];
  159. [(MJRefreshHeader *)self.collectionView.mj_footer endRefreshingWithCompletionBlock:^{
  160. self.isLoading = NO;
  161. }];
  162. }
  163. -(void)videoBtnClick:(UIButton *)button
  164. {
  165. [UIView animateWithDuration:0.5 animations:^{
  166. _scrollView.frame = CGRectMake(_ugcVideoBtn.left - (scrollViewWidth - _ugcVideoBtn.width)/2, _ugcVideoBtn.bottom + 5, scrollViewWidth, scrollViewHeight);
  167. }];
  168. [self setup];
  169. }
  170. #pragma mark - UICollectionView datasource
  171. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
  172. return (self.lives.count + 1) / 2;
  173. }
  174. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  175. if (self.lives.count % 2 != 0 && section == (self.lives.count + 1) / 2 - 1) {
  176. return 1;
  177. } else {
  178. return 2;
  179. }
  180. }
  181. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  182. TCLiveListCell *cell = (TCLiveListCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"TCLiveListCell" forIndexPath:indexPath];
  183. if (cell == nil) {
  184. cell = [[TCLiveListCell alloc] initWithFrame:CGRectZero];
  185. }
  186. NSInteger index = indexPath.section * 2 + indexPath.row;
  187. if (self.lives.count > index) {
  188. TCLiveInfo *live = self.lives[index];
  189. cell.model = live;
  190. }
  191. return cell;
  192. }
  193. //设置每个item的尺寸
  194. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  195. // 图片的宽高比为9:16
  196. CGFloat width = (self.view.width - 1) / 2;
  197. CGFloat height = width;
  198. return CGSizeMake(width, height);
  199. }
  200. //设置每个item的UIEdgeInsets
  201. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
  202. return UIEdgeInsetsMake(0, 0, 0, 0);
  203. }
  204. //设置每个item水平间距
  205. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
  206. return 1;
  207. }
  208. //设置每个item垂直间距
  209. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
  210. return 0;
  211. }
  212. #pragma mark - UICollectionView delegate
  213. //点击item方法
  214. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  215. // 此处一定要用cell的数据,live中的对象可能已经清空了
  216. TCLiveListCell *cell = (TCLiveListCell *)[collectionView cellForItemAtIndexPath:indexPath];
  217. TCLiveInfo *info = cell.model;
  218. // MARK: 打开播放界面
  219. if (_playVC == nil) {
  220. if (self.lives && self.lives.count > 0 && info) {
  221. _playVC = [[TCVodPlayViewController alloc] initWithPlayInfoS:self.lives liveInfo:info videoIsReady:^{
  222. if (!_hasEnterplayVC) {
  223. [[BGBaseAppDelegate sharedAppDelegate]pushViewController:_playVC animated:YES];
  224. _hasEnterplayVC = YES;
  225. }
  226. }];
  227. }
  228. }
  229. [self performSelector:@selector(enterPlayVC:) withObject:_playVC afterDelay:0.5];
  230. }
  231. -(void)enterPlayVC:(NSObject *)obj{
  232. if (!_hasEnterplayVC) {
  233. [[BGBaseAppDelegate sharedAppDelegate] pushViewController:_playVC animated:YES];
  234. _hasEnterplayVC = YES;
  235. if (self.listener) {
  236. [self.listener onEnterPlayViewController];
  237. }
  238. }
  239. }
  240. #pragma mark - Net fetch
  241. /**
  242. * 拉取直播列表。TCLiveListMgr在启动是,会将所有数据下载下来。在未全部下载完前,通过loadLives借口,
  243. * 能取到部分数据。通过finish接口,判断是否已取到最后的数据
  244. *
  245. */
  246. - (void)doFetchList {
  247. NSRange range = NSMakeRange(self.lives.count, 20);
  248. BOOL finish;
  249. NSArray *result = [_liveListMgr readVods:range finish:&finish];
  250. if (result.count) {
  251. result = [self mergeResult:result];
  252. [self.lives addObjectsFromArray:result];
  253. } else {
  254. if (finish) {
  255. MBProgressHUD *hud = [[HUDHelper sharedInstance] tipMessage:NSLocalizedString(@"TCLiveListView.NoMore", nil)];
  256. hud.userInteractionEnabled = NO;
  257. }
  258. }
  259. self.collectionView.mj_footer.hidden = finish;
  260. [self.collectionView reloadData];
  261. [self.collectionView.mj_header endRefreshing];
  262. [self.collectionView.mj_footer endRefreshing];
  263. if (self.lives.count == 0) {
  264. _nullDataView.hidden = NO;
  265. }else{
  266. _nullDataView.hidden = YES;
  267. }
  268. }
  269. /**
  270. * 将取到的数据于已存在的数据进行合并。
  271. *
  272. * @param result 新拉取到的数据
  273. *
  274. * @return 新数据去除已存在记录后,剩余的数据
  275. */
  276. - (NSArray *)mergeResult:(NSArray *)result {
  277. // 每个直播的播放地址不同,通过其进行去重处理
  278. NSArray *existArray = [self.lives bk_map:^id(TCLiveInfo *obj) {
  279. return obj.playurl;
  280. }];
  281. NSArray *newArray = [result bk_reject:^BOOL(TCLiveInfo *obj) {
  282. return [existArray containsObject:obj.playurl];
  283. }];
  284. return newArray;
  285. }
  286. /**
  287. * TCLiveListMgr有新数据过来
  288. *
  289. * @param noti
  290. */
  291. - (void)newDataAvailable:(NSNotification *)noti {
  292. [self doFetchList];
  293. }
  294. /**
  295. * TCLiveListMgr数据有更新
  296. *
  297. * @param noti
  298. */
  299. - (void)listDataUpdated:(NSNotification *)noti {
  300. [self setup];
  301. }
  302. /**
  303. * TCLiveListMgr内部出错
  304. *
  305. * @param noti
  306. */
  307. - (void)svrError:(NSNotification *)noti {
  308. NSError *e = noti.object;
  309. if ([e isKindOfClass:[NSError class]]) {
  310. if ([e localizedFailureReason]) {
  311. [HUDHelper alert:[e localizedFailureReason]];
  312. }
  313. else if ([e localizedDescription]) {
  314. [HUDHelper alert:[e localizedDescription]];
  315. }
  316. }
  317. // 如果还在加载,停止加载动画
  318. if (self.isLoading) {
  319. [self.collectionView.mj_header endRefreshing];
  320. [self.collectionView.mj_footer endRefreshing];
  321. self.isLoading = NO;
  322. }
  323. }
  324. /**
  325. * TCPlayViewController出错,加入房间失败
  326. *
  327. */
  328. - (void)playError:(NSNotification *)noti {
  329. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  330. // [self.tableView.mj_header beginRefreshing];
  331. //加房间失败后,刷新列表,不需要刷新动画
  332. self.lives = [NSMutableArray array];
  333. self.isLoading = YES;
  334. [_liveListMgr queryVideoList:GetType_Up];
  335. });
  336. }
  337. - (void)dealloc {
  338. [[NSNotificationCenter defaultCenter] removeObserver:self];
  339. }
  340. @end