HMHotViewController.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. //
  2. // HMHotViewController.m
  3. // BuguLive
  4. //
  5. // Created by xfg on 2017/7/5.
  6. // Copyright © 2017年 xfg. All rights reserved.
  7. //
  8. #import "HMHotViewController.h"
  9. #import "HMHotModel.h"
  10. #import "HMHotTableViewCell.h"
  11. #import "SDCycleScrollView.h"
  12. #import "ChooseAreaCell.h"
  13. #import "AdJumpViewModel.h"
  14. #import "PublishLivestViewController.h"
  15. #import "AgreementViewController.h"
  16. #import "SChooseAreaView.h"
  17. // 这样子写主要是为了方便拓展
  18. typedef NS_ENUM(NSInteger, HMHotViewSections)
  19. {
  20. HMHotViewSectionsBanner, // 广告栏
  21. HMHotViewSectionsArea, // 地区选择
  22. HMHotViewSectionsLiveItem, // 直播项
  23. };
  24. static NSString *const cellReuseIdentifier0 = @"cellReuseIdentifier0";
  25. static NSString *const cellReuseIdentifier1 = @"cellReuseIdentifier1";
  26. // 广告图默认滚动时间
  27. static float const bannerAutoScrollTimeInterval = 7;
  28. @interface HMHotViewController () <SDCycleScrollViewDelegate, HMHotTableViewCellDelegate, UITableViewDelegate, UITableViewDataSource>
  29. {
  30. ChooseAreaCell *_chooseAreaCell;
  31. }
  32. @property (nonatomic, strong) NSMutableArray *mainDataMArray;
  33. @property (nonatomic, strong) HMHotModel *hotModel;
  34. @property (nonatomic, strong) SDCycleScrollView *cycleScrollView;
  35. @property (nonatomic, assign) BOOL canClickItem; // 防止视频列表被重复点击
  36. @end
  37. @implementation HMHotViewController
  38. - (void)viewDidLoad
  39. {
  40. [super viewDidLoad];
  41. }
  42. - (void)viewWillAppear:(BOOL)animated
  43. {
  44. [super viewWillAppear:animated];
  45. [self.navigationController setNavigationBarHidden:NO animated:NO];
  46. }
  47. - (void)initFWVariables
  48. {
  49. [super initFWVariables];
  50. self.canClickItem = YES;
  51. }
  52. - (void)initFWUI
  53. {
  54. [super initFWUI];
  55. self.view.backgroundColor = kBackGroundColor;
  56. CGRect tmpFrame;
  57. if (_tableViewFrame.size.height)
  58. {
  59. tmpFrame = _tableViewFrame;
  60. }
  61. else
  62. {
  63. tmpFrame = CGRectMake(0, 0, kScreenW, self.view.frame.size.height);
  64. }
  65. self.tableView = [[UITableView alloc] initWithFrame:tmpFrame];
  66. self.tableView.delegate = self;
  67. self.tableView.dataSource = self;
  68. self.tableView.backgroundColor = kBackGroundColor;
  69. [self.view addSubview:self.tableView];
  70. self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  71. [self.tableView registerNib:[UINib nibWithNibName:@"ChooseAreaCell" bundle:nil] forCellReuseIdentifier:cellReuseIdentifier1];
  72. [self.tableView registerNib:[UINib nibWithNibName:@"HMHotTableViewCell" bundle:nil] forCellReuseIdentifier:cellReuseIdentifier];
  73. // 话题页相关
  74. if (![BGUtils isBlankString:self.topicName])
  75. {
  76. self.navigationItem.title = self.topicName;
  77. }
  78. if (![BGUtils isBlankString:self.cate_id])
  79. {
  80. [self initJoinTopicView];
  81. [self setupBackBtnWithBlock:nil];
  82. }
  83. }
  84. - (void)initFWData
  85. {
  86. [super initFWData];
  87. [BGMJRefreshManager refresh:self.tableView target:self headerRereshAction:@selector(headerRefresh) footerRereshAction:nil];
  88. // 刷新该页面(主要为了删除最新页已经退出的直播间)
  89. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshHome:) name:kRefreshHomeItem object:nil];
  90. }
  91. - (void)refreshHome:(NSNotification *)noti
  92. {
  93. if (noti)
  94. {
  95. NSDictionary *tmpDict = (NSDictionary *)noti.object;
  96. NSString *room_id = [tmpDict toString:@"room_id"];
  97. @synchronized (self.mainDataMArray)
  98. {
  99. NSMutableArray *tmpArray = self.mainDataMArray;
  100. for (HMHotItemModel *model in tmpArray)
  101. {
  102. if (model.room_id == [room_id intValue])
  103. {
  104. [tmpArray removeObject:model];
  105. self.mainDataMArray = tmpArray;
  106. [_tableView reloadData];
  107. return;
  108. }
  109. }
  110. }
  111. }
  112. }
  113. - (void)headerRefresh
  114. {
  115. [self loadDataFromNet:1];
  116. }
  117. - (void)initBanner
  118. {
  119. if (!_cycleScrollView)
  120. {
  121. // 网络加载 创建带标题的图片轮播器
  122. self.cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, kScreenW, [self bannerHeight]) delegate:self placeholderImage:nil];
  123. self.cycleScrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
  124. self.cycleScrollView.currentPageDotColor = kAppMainColor; // 自定义分页控件小圆标颜色
  125. self.cycleScrollView.autoScrollTimeInterval = bannerAutoScrollTimeInterval;
  126. self.cycleScrollView.backgroundColor = kWhiteColor;
  127. }
  128. NSMutableArray *tmpMArray = [NSMutableArray array];
  129. for (HMHotBannerModel *bannerModel in self.hotModel.banner)
  130. {
  131. [tmpMArray addObject:bannerModel.image];
  132. }
  133. // 加载延迟
  134. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  135. self.cycleScrollView.imageURLStringsGroup = tmpMArray;
  136. });
  137. }
  138. #pragma mark 点击图片回调
  139. - (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
  140. {
  141. HMHotBannerModel *hotBannerModel = [self.hotModel.banner objectAtIndex:index];
  142. if ([AdJumpViewModel adToOthersWith:hotBannerModel])
  143. {
  144. [[AppDelegate sharedAppDelegate]pushViewController:[AdJumpViewModel adToOthersWith:hotBannerModel] animated:YES];
  145. }
  146. }
  147. - (CGFloat)bannerHeight
  148. {
  149. if (self.hotModel.banner)
  150. {
  151. if ([self.hotModel.banner count])
  152. {
  153. HMHotBannerModel *bannerModel = [self.hotModel.banner firstObject];
  154. return bannerModel.bannerHeight;
  155. }
  156. else
  157. {
  158. return 0;
  159. }
  160. }
  161. else
  162. {
  163. return 0;
  164. }
  165. }
  166. #pragma mark 加载热门页数据
  167. - (void)loadDataFromNet:(int)page
  168. {
  169. NSMutableDictionary *parmDict = [NSMutableDictionary dictionary];
  170. [parmDict setObject:@"index" forKey:@"ctl"];
  171. [parmDict setObject:@"index" forKey:@"act"];
  172. if (![BGUtils isBlankString:_sexString])
  173. {
  174. [parmDict setObject:_sexString forKey:@"sex"];
  175. }
  176. if (![BGUtils isBlankString:_areaString])
  177. {
  178. [parmDict setObject:_areaString forKey:@"city"];
  179. }
  180. if (![BGUtils isBlankString:self.cate_id])
  181. {
  182. [parmDict setObject:self.cate_id forKey:@"cate_id"];
  183. }
  184. [parmDict setObject:@(page) forKey:@"p"];
  185. FWWeakify(self)
  186. [NetWorkManager asyncPostWithParameters:parmDict successBlock:^(NSDictionary *responseJson, AppNetWorkModel *netWorkModel) {
  187. FWStrongify(self)
  188. if ([responseJson toInt:@"status"] == 1)
  189. {
  190. self.hotModel = [HMHotModel mj_objectWithKeyValues:responseJson];
  191. self.mainDataMArray = self.hotModel.list;
  192. [self initBanner];
  193. [self.tableView reloadData];
  194. }
  195. [BGMJRefreshManager endRefresh:self.tableView];
  196. } failureBlock:^(NSError *error, AppNetWorkModel *netWorkModel) {
  197. FWStrongify(self)
  198. [BGMJRefreshManager endRefresh:self.tableView];
  199. }];
  200. }
  201. #pragma mark - Table view data source
  202. - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
  203. {
  204. if (section == 2)
  205. {
  206. return kDefaultMargin;
  207. }
  208. else
  209. {
  210. return 0;
  211. }
  212. }
  213. - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
  214. {
  215. if (section == 2)
  216. {
  217. UIView *tmpView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, kDefaultMargin)];
  218. tmpView.backgroundColor = kClearColor;
  219. return tmpView;
  220. }
  221. else
  222. {
  223. return nil;
  224. }
  225. }
  226. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  227. {
  228. if (!_hotModel)
  229. {
  230. return 0;
  231. }
  232. else
  233. {
  234. return 3;
  235. }
  236. }
  237. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  238. {
  239. if (section == HMHotViewSectionsBanner)
  240. {
  241. if (![BGUtils isBlankString:self.cate_id])
  242. {
  243. return 0;
  244. }
  245. else
  246. {
  247. return 1;
  248. }
  249. }
  250. else if (section == HMHotViewSectionsArea)
  251. {
  252. if (![BGUtils isBlankString:self.cate_id])
  253. {
  254. return 0;
  255. }
  256. else
  257. {
  258. return 0;
  259. }
  260. }
  261. else
  262. {
  263. return [self.mainDataMArray count];
  264. }
  265. }
  266. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  267. {
  268. if (indexPath.section == HMHotViewSectionsBanner)
  269. {
  270. return [self bannerHeight];
  271. }
  272. else if (indexPath.section == HMHotViewSectionsArea)
  273. {
  274. return 44;
  275. }
  276. else
  277. {
  278. if ([self.mainDataMArray count])
  279. {
  280. HMHotItemModel *itemModel = self.mainDataMArray[indexPath.row];
  281. if ([itemModel.title isEqualToString:@""])
  282. {
  283. return kScreenW + 70;
  284. }
  285. else
  286. {
  287. return kScreenW + 110;
  288. }
  289. }
  290. else
  291. {
  292. return 0;
  293. }
  294. }
  295. }
  296. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  297. {
  298. if (indexPath.section == HMHotViewSectionsBanner)
  299. {
  300. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellReuseIdentifier0];
  301. if (!cell)
  302. {
  303. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellReuseIdentifier0];
  304. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  305. }
  306. [cell.contentView addSubview:self.cycleScrollView];
  307. return cell;
  308. }
  309. else if (indexPath.section == HMHotViewSectionsArea)
  310. {
  311. _chooseAreaCell = [tableView dequeueReusableCellWithIdentifier:cellReuseIdentifier1 forIndexPath:indexPath];
  312. _chooseAreaCell.selectionStyle = UITableViewCellSelectionStyleNone;
  313. if ([BGUtils isBlankString:_areaString])
  314. {
  315. _chooseAreaCell.areaLabel.text = ASLocalizedString(@"热门");
  316. }
  317. else
  318. {
  319. _chooseAreaCell.areaLabel.text = _areaString;
  320. }
  321. return _chooseAreaCell;
  322. }
  323. else
  324. {
  325. HMHotTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellReuseIdentifier forIndexPath:indexPath];
  326. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  327. cell.delegate = self;
  328. if ([self.mainDataMArray count] > indexPath.row)
  329. {
  330. HMHotItemModel *tmpModel = [self.mainDataMArray objectAtIndex:indexPath.row];
  331. [cell initWidthModel:tmpModel rowIndex:indexPath.row];
  332. }
  333. return cell;
  334. }
  335. }
  336. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  337. {
  338. if (indexPath.section == HMHotViewSectionsArea)
  339. {
  340. //if need,could to do
  341. }
  342. else if (indexPath.section == HMHotViewSectionsLiveItem)
  343. {
  344. if ([self.mainDataMArray count] > indexPath.row)
  345. {
  346. HMHotItemModel *tmpModel = [self.mainDataMArray objectAtIndex:indexPath.row];
  347. [self joinLivingRoom:tmpModel];
  348. }
  349. }
  350. }
  351. - (void)changeClickState
  352. {
  353. }
  354. #pragma mark 加入直播间
  355. - (void)joinLivingRoom:(HMHotItemModel *)model
  356. {
  357. // 防止重复点击
  358. if (self.canClickItem)
  359. {
  360. self.canClickItem = NO;
  361. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  362. self.canClickItem = YES;
  363. });
  364. }
  365. else
  366. {
  367. return;
  368. }
  369. if (![BGUtils isNetConnected])
  370. {
  371. return;
  372. }
  373. // model 转为 dic
  374. NSDictionary *dic = model.mj_keyValues;
  375. // 直播管理中心开启观众直播
  376. BOOL isSusWindow = [[LiveCenterManager sharedInstance] judgeIsSusWindow];
  377. [[LiveCenterManager sharedInstance] showLiveOfAudienceLiveofPramaDic:dic.mutableCopy isSusWindow:isSusWindow isSmallScreen:NO block:^(BOOL finished) {
  378. }];
  379. }
  380. #pragma mark - ----------------------- 代理 -----------------------
  381. #pragma mark 热门搜索的代理
  382. - (void)sentAreaWithName:(NSString *)name andType:(NSString *)typeString
  383. {
  384. _chooseAreaCell.areaLabel.text = name;
  385. NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
  386. [dict setObject:name forKey:@"name"];
  387. [dict setObject:typeString forKey:@"sexType"];
  388. [[NSNotificationCenter defaultCenter]postNotificationName:@"updateTitleName" object:dict];
  389. _areaString = name;
  390. _sexString = typeString;
  391. [self loadDataFromNet:1];
  392. }
  393. /**
  394. 点击跳转到话题
  395. @param rowIndex 当前行的下标
  396. */
  397. - (void)pushToTopic:(NSInteger)rowIndex
  398. {
  399. // if ([self.mainDataMArray count] > rowIndex)
  400. // {
  401. // HMHotItemModel *tmpModel = [self.mainDataMArray objectAtIndex:rowIndex];
  402. // HMHotViewController *tmpController = [[HMHotViewController alloc]init];
  403. // tmpController.topicName = tmpModel.title;
  404. // tmpController.cate_id = tmpModel.cate_id;
  405. // [[AppDelegate sharedAppDelegate] pushViewController:tmpController];
  406. // }
  407. }
  408. /**
  409. 点击用户头像
  410. @param rowIndex 当前行的下标
  411. */
  412. - (void)clickUserIcon:(NSInteger)rowIndex
  413. {
  414. if ([self.delegate respondsToSelector:@selector(goToMainPage:)])
  415. {
  416. if ([self.mainDataMArray count] > rowIndex)
  417. {
  418. HMHotItemModel *tmpModel = [self.mainDataMArray objectAtIndex:rowIndex];
  419. [self.delegate goToMainPage:tmpModel.user_id];
  420. }
  421. }
  422. }
  423. #pragma mark - ----------------------- 其他 -----------------------
  424. #pragma mark 话题点击按钮
  425. - (void)initJoinTopicView
  426. {
  427. UIImageView *jionView = [UIImageView imageViewWithImage:[UIImage imageNamed:@"ic_create_topic_room"]];
  428. jionView.frame = CGRectMake((kScreenW - 50) / 2, kScreenH - 180, 50, 50);
  429. jionView.layer.cornerRadius = 25;
  430. jionView.layer.masksToBounds = YES;
  431. jionView.layer.borderWidth = 0.5;
  432. jionView.layer.borderColor = kAppBorderColor;
  433. [self.view addSubview:jionView];
  434. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleToTopic)];
  435. jionView.userInteractionEnabled = YES;
  436. [jionView addGestureRecognizer:tap];
  437. }
  438. #pragma mark 点击事件
  439. - (void)handleToTopic
  440. {
  441. IMALoginParam *loginParam = [IMALoginParam loadFromLocal];
  442. if (loginParam.isAgree ==1)
  443. {
  444. if (AppDelegate.sharedAppDelegate.isInAudioVideoChatVc) {
  445. [[BGHUDHelper sharedInstance] tipMessage:ASLocalizedString(@"通话中,请结束通话后再试")];
  446. return;
  447. }
  448. PublishLivestViewController *pvc = [[PublishLivestViewController alloc] init];
  449. pvc.titleTopic = self.topicName;
  450. [self presentViewController:pvc animated:YES completion:nil];
  451. }
  452. else
  453. {
  454. loginParam.isAgree = 1;
  455. [loginParam saveToLocal];
  456. AgreementViewController *agreeVC = [AgreementViewController webControlerWithUrlStr:[GlobalVariables sharedInstance].appModel.agreement_link isShowIndicator:YES isShowNavBar:YES];
  457. BGNavigationController *nav = [[BGNavigationController alloc]initWithRootViewController:agreeVC];
  458. [self presentViewController:nav animated:YES completion:nil];
  459. }
  460. }
  461. #pragma mark - ----------------------- GET方法 -----------------------
  462. - (NSMutableArray *)mainDataMArray
  463. {
  464. if (!_mainDataMArray)
  465. {
  466. _mainDataMArray = [NSMutableArray array];
  467. }
  468. return _mainDataMArray;
  469. }
  470. @end