XYChatRoomMainViewController.m 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. //
  2. // XYChatRoomMainViewController.m
  3. // Starbuds
  4. //
  5. // Created by 翟玉磊 on 2020/6/19.
  6. // Copyright © 2020 翟玉磊. All rights reserved.
  7. //
  8. #import "XYChatRoomMainViewController.h"
  9. #import "MLMSegmentManager.h"
  10. #import "XYChatRoomCollectionListViewController.h"
  11. #import "XYChatRoomCategoriesModel.h"
  12. #import "XYGesturesScrollView.h"
  13. #import "XYBannerListModel.h"
  14. @interface XYChatRoomMainViewController ()<UIScrollViewDelegate, SDCycleScrollViewDelegate, XYChatRoomCollectionListViewControllerDelegate>
  15. @property (nonatomic, strong) XYGesturesScrollView *mainScrollView;
  16. @property (nonatomic, strong) SDCycleScrollView *cycleScrollView;
  17. @property (nonatomic, strong) MLMSegmentHead *segmentHead;
  18. @property (nonatomic, strong) MLMSegmentScroll *segmentScroll;
  19. @property (nonatomic, strong) NSMutableArray *titles;
  20. // 语音分类数组
  21. @property (nonatomic, readwrite, strong) NSArray *voiceCategoryArray;
  22. @property (nonatomic, readwrite, strong) NSArray *categoryControllers;
  23. @property (nonatomic, strong) NSMutableArray *bannerDataScoure;
  24. // 主scrollView的顶部位移临界点,大于等于则不滚动主scrollView
  25. @property (nonatomic, assign) CGFloat scrollTopOffset;
  26. // 主scrollView是否可以滑动
  27. @property (nonatomic, assign) BOOL canScroll;
  28. // 从首页列表跳转过来第一次加载数据(防止index=0页数据为空)
  29. @property (nonatomic, assign) BOOL isFirstLoad;
  30. @end
  31. @implementation XYChatRoomMainViewController
  32. - (void)dealloc {
  33. [self removeChildViewControllerScrollTopNotification];
  34. }
  35. /// 重写init方法,配置你想要的属性
  36. - (instancetype)init
  37. {
  38. self = [super init];
  39. if (self) {
  40. // 是否隐藏该控制器的导航栏
  41. self.prefersNavigationBarHidden = YES;
  42. }
  43. return self;
  44. }
  45. - (void)viewWillAppear:(BOOL)animated {
  46. [super viewWillAppear:animated];
  47. if (self.cycleScrollView) {
  48. [self.cycleScrollView adjustWhenControllerViewWillAppera];
  49. }
  50. }
  51. - (void)viewDidLoad {
  52. if (self.isMyVoiceRoomCollect) {
  53. self.view.backgroundColor = Color_White;
  54. self.prefersNavigationBarHidden = NO;
  55. self.segmentHead.hidden = YES;
  56. self.title = @"我的收藏";
  57. }
  58. [super viewDidLoad];
  59. /// 设置
  60. [self _setup];
  61. /// 设置导航栏
  62. [self _setupNavigationItem];
  63. /// 设置子控件
  64. [self _setupSubViews];
  65. /// 布局子空间
  66. [self _makeSubViewsConstraints];
  67. // 滚动通知
  68. [self addChildViewControllerScrollTopNotification];
  69. // 添加下拉刷新
  70. [self addRefresh];
  71. //[self scrollViewDidTriggerHeaderRefresh:NO];
  72. [self base_reloadListData];
  73. }
  74. - (void)base_reloadListData {
  75. [self.mainScrollView.mj_header beginRefreshing];
  76. }
  77. - (void)addRefresh {
  78. /// 下拉刷新
  79. @weakify(self);
  80. [self.mainScrollView addImageHeaderRefresh:^(MJRefreshHeader *header) {
  81. /// 加载下拉刷新的数据
  82. @strongify(self);
  83. [self scrollViewDidTriggerHeaderRefresh:YES];
  84. }];
  85. }
  86. - (void)scrollViewDidTriggerHeaderRefresh:(BOOL)refreshList {
  87. if (self.isMyVoiceRoomCollect) {
  88. self.segmentScroll.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64-kiPhoneXTopEdge);
  89. self.mainScrollView.top = 64 + kiPhoneXTopEdge;
  90. self.mainScrollView.height = SCREEN_HEIGHT-64-kiPhoneXTopEdge;
  91. self.mainScrollView.contentSize = CGSizeMake(SCREEN_WIDTH, self.segmentScroll.f_top);
  92. [self scrollInitValue];
  93. [self refreshCurrentList];
  94. //[self.mainScrollView.mj_header endRefreshing];
  95. }else{
  96. /// 获取banner
  97. [[XYConfigAPIManager new] getBannersWithBannerPosition:102 categoryId:@"-1" successHandler:^(ZYLResponseModel *responseModel) {
  98. [self.bannerDataScoure removeAllObjects];
  99. for (NSDictionary *dict in responseModel.data[@"list"]) {
  100. XYBannerListModel *model = XYBannerListModel.new;
  101. [model yy_modelSetWithDictionary:dict];
  102. [self.bannerDataScoure addObject:model];
  103. }
  104. if (self.bannerDataScoure.count > 0) {
  105. // 允许显示且banner数据不为空则可以显示
  106. NSMutableArray *images = NSMutableArray.new;
  107. for (XYBannerListModel *model in self.bannerDataScoure) {
  108. [images addObject:model.bannerPic];
  109. }
  110. self.cycleScrollView.frame = CGRectMake(16.0f, 8.0f, SCREEN_WIDTH - 32.0f, 100.0f);
  111. self.cycleScrollView.imageURLStringsGroup = [images copy];
  112. self.segmentHead.frame = CGRectMake(10.0f, self.cycleScrollView.f_top + 8.0f, SCREEN_WIDTH - 20.0f, 48.0f);
  113. }else {
  114. self.cycleScrollView.frame = CGRectMake(16.0f, 0.0f, SCREEN_WIDTH - 32.0f, 0.0f);
  115. self.segmentHead.frame = CGRectMake(10.0f, self.cycleScrollView.f_top, SCREEN_WIDTH - 20.0f, 48.0f);
  116. }
  117. [UIView animateWithDuration:0.3 animations:^{
  118. self.cycleScrollView.alpha = 1;
  119. }];
  120. self.segmentScroll.frame = CGRectMake(0, self.segmentHead.f_top, SCREEN_WIDTH, SCREEN_HEIGHT- NAVGATION_HEIGHT - self.segmentHead.f_heigh);
  121. self.mainScrollView.contentSize = CGSizeMake(SCREEN_WIDTH, self.segmentScroll.f_top);
  122. [self scrollInitValue];
  123. if (refreshList) {[self refreshCurrentList];}
  124. //[self.mainScrollView.mj_header endRefreshing];
  125. } failureHandler:^(ZYLNetworkError *error) {
  126. [self scrollInitValue];
  127. if (refreshList) {[self refreshCurrentList];}
  128. //[self.mainScrollView.mj_header endRefreshing];
  129. }];
  130. }
  131. }
  132. - (void)refreshCurrentList {
  133. if (self->_segmentScroll) {
  134. XYChatRoomCollectionListViewController *currentVc = [self->_segmentScroll currentVcOrView];
  135. if ([currentVc isKindOfClass:[XYChatRoomCollectionListViewController class]]) {
  136. [currentVc collectionViewDidTriggerHeaderRefresh];
  137. if (!self.isFirstLoad) { self.isFirstLoad = YES;
  138. [self selectItemType:[AppDelegate sharedDelegate].tabbarVC.partyVCSelectType];
  139. }
  140. }
  141. }
  142. }
  143. #pragma mark — SDCycleScrollViewDelegate
  144. /** 点击图片回调 */
  145. - (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index {
  146. // 派对Tab,点[banner]的次数
  147. [StatisticsManager event:@"party_banner_click"];
  148. XYBannerListModel *model = self.bannerDataScoure[index];
  149. [XYBannerListModel bannerClickHandleWithModel:model];
  150. }
  151. #pragma mark — XYChatRoomCollectionListViewControllerDelegate, XYChatRoomListViewControllerDelegate
  152. - (void)headerRefreshEnding {
  153. [self.mainScrollView.mj_header endRefreshing];
  154. }
  155. #pragma mark — scrollView 滑动处理----------------begin
  156. - (void)scrollInitValue {
  157. self.canScroll = YES;
  158. if (self.bannerDataScoure.count > 0) {
  159. self.scrollTopOffset = self.cycleScrollView.f_top + 8.0f;
  160. }else {
  161. self.scrollTopOffset = self.cycleScrollView.f_top;
  162. }
  163. }
  164. /// 添加子控制器滑动控件到达顶部的通知
  165. - (void)addChildViewControllerScrollTopNotification {
  166. [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeStatus) name:@"chatroom_controller_leave_top" object:nil];
  167. }
  168. /// 删除子控制器滑动控件到达顶部的通知
  169. - (void)removeChildViewControllerScrollTopNotification {
  170. [[NSNotificationCenter defaultCenter] removeObserver:self name:@"chatroom_controller_leave_top" object:nil];
  171. }
  172. - (void)changeStatus {
  173. // 更新状态
  174. self.canScroll = YES;
  175. for (XYChatRoomScrollCollectionViewController *vc in self.categoryControllers) {
  176. vc.canScroll = NO;
  177. }
  178. }
  179. - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
  180. // NSLog(@"scrollView--------开始拖动");
  181. }
  182. - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
  183. // NSLog(@"scrollView--------结束拖动");
  184. }
  185. - (void)scrollViewDidScroll:(UIScrollView *)scrollView{
  186. // NSLog(@"scrollView=====滚动==%.2F",scrollView.contentOffset.y);
  187. /*
  188. 当 底层滚动式图滚动到指定位置时,
  189. 停止滚动,开始滚动子视图
  190. */
  191. if (scrollView.contentOffset.y >= self.scrollTopOffset) {
  192. scrollView.contentOffset = CGPointMake(0, self.scrollTopOffset);
  193. if (self.canScroll) {
  194. self.canScroll = NO;
  195. for (XYChatRoomScrollCollectionViewController *vc in self.categoryControllers) {
  196. vc.canScroll = YES;
  197. }
  198. }
  199. }else{
  200. if (!self.canScroll) {//子视图没到顶部
  201. scrollView.contentOffset = CGPointMake(0, self.scrollTopOffset);
  202. }
  203. }
  204. }
  205. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
  206. // NSLog(@"scrollView--------减速");
  207. }
  208. /// 获取当前子控制器
  209. - (XYChatRoomScrollCollectionViewController *)getCurrentChildViewController {
  210. XYChatRoomScrollCollectionViewController *currentVc = (XYChatRoomScrollCollectionViewController *)[self.segmentScroll currentVcOrView];
  211. return currentVc;
  212. }
  213. #pragma mark — scrollView 滑动处理----------------end
  214. #pragma mark - 事件处理Or辅助方法
  215. #pragma mark - 初始化
  216. - (void)_setup{
  217. // 获取分类数据
  218. [self getLiveCategoryData];
  219. }
  220. #pragma mark - 设置导航栏
  221. - (void)_setupNavigationItem{
  222. }
  223. #pragma mark - 设置子控件
  224. - (void)_setupSubViews{
  225. [self.view addSubview:self.mainScrollView];
  226. [self.mainScrollView addSubview:self.cycleScrollView];
  227. // 初始化分类列表
  228. [self addLiveCategoriesView];
  229. // 初始化滑动值
  230. [self scrollInitValue];
  231. }
  232. - (void)addLiveCategoriesView {
  233. if (self.segmentHead) {
  234. [self.segmentHead removeFromSuperview];
  235. self.segmentHead = nil;
  236. }
  237. if (self.segmentScroll) {
  238. [self.segmentScroll removeFromSuperview];
  239. self.segmentScroll = nil;
  240. }
  241. self.titles = [NSMutableArray array];
  242. NSMutableArray *controllers = [NSMutableArray array];
  243. for (XYChatRoomCategoriesModel *model in self.voiceCategoryArray) {
  244. [self.titles addObject:model.categoryName];
  245. XYChatRoomCollectionListViewController *vc = [XYChatRoomCollectionListViewController new];
  246. vc.delegate = self;
  247. vc.categoriesModel = model;
  248. [controllers addObject:vc];
  249. }
  250. self.categoryControllers = [NSArray arrayWithArray:controllers];
  251. if (_segmentHead == nil) {
  252. _segmentHead = [[MLMSegmentHead alloc] initWithFrame:CGRectMake(10.0f, self.cycleScrollView.f_top + 8.0f, SCREEN_WIDTH - 20.0f, 48.0f) titles:self.titles headStyle:SegmentHeadStyleSlide layoutStyle:MLMSegmentLayoutLeft];
  253. _segmentHead.headColor = Color_Clear;
  254. _segmentHead.fontSize = 14;
  255. _segmentHead.selectColor = Color_White;
  256. _segmentHead.deSelectColor = ColorFromHexString(@"#969699");
  257. _segmentHead.slideColor = Color_TextRed;
  258. _segmentHead.slideDeSelectColor = ColorFromHexString(@"#F7F7FA");
  259. _segmentHead.slideHeight = 28.0f;
  260. _segmentHead.slideCorner = 14.0f;
  261. _segmentHead.slideScale = 0.8f;
  262. _segmentHead.singleW_Add = 40.0f;
  263. _segmentHead.equalSize = NO;
  264. _segmentHead.bottomLineHeight = 0;
  265. if (self.isMyVoiceRoomCollect) { _segmentHead.hidden = YES; }
  266. }
  267. if (_segmentScroll == nil) {
  268. kWEAKSELF
  269. _segmentScroll = [[MLMSegmentScroll alloc] initWithFrame:CGRectMake(0, self.segmentHead.f_top, SCREEN_WIDTH, SCREEN_HEIGHT- NAVGATION_HEIGHT - _segmentHead.f_heigh) vcOrViews:controllers];
  270. _segmentScroll.backgroundColor = Color_White;
  271. _segmentScroll.scrollEnd = ^(NSInteger index) {
  272. NSString *title = weakSelf.titles[index];
  273. if ([title isEqualToString:kLocalizedString(@"收藏")]) {
  274. // 派对Tab,点分类-[收藏]的次数
  275. [StatisticsManager event:@"party_sort_collection_click"];
  276. }else if ([title isEqualToString:kLocalizedString(@"热门")]) {
  277. // 派对Tab,点分类-[热门]的次数
  278. [StatisticsManager event:@"party_sort_hot_click"];
  279. }else if ([title isEqualToString:kLocalizedString(@"男神")]) {
  280. // 派对Tab,点分类-[男神]的次数
  281. [StatisticsManager event:@"party_sort_malegod_click"];
  282. }else if ([title isEqualToString:kLocalizedString(@"女神")]) {
  283. // 派对Tab,点分类-[女神]的次数
  284. [StatisticsManager event:@"party_sort_femalegod_click"];
  285. }else if ([title isEqualToString:kLocalizedString(@"点唱")]) {
  286. // 派对Tab,点分类-[点唱]的次数
  287. [StatisticsManager event:@"party_sort_sing_click"];
  288. }else if ([title isEqualToString:kLocalizedString(@"游戏")]) {
  289. // 派对Tab,点分类-[游戏]的次数
  290. [StatisticsManager event:@"party_sort_game_click"];
  291. }
  292. };
  293. }
  294. [MLMSegmentManager associateHead:_segmentHead withScroll:_segmentScroll completion:^{
  295. [self.mainScrollView addSubview:self.segmentHead];
  296. [self.mainScrollView addSubview:self.segmentScroll];
  297. }];
  298. self.mainScrollView.contentSize = CGSizeMake(SCREEN_WIDTH, self.segmentScroll.f_top);
  299. }
  300. - (void)selectItemType:(XYPublicChatHallViewType )type{
  301. if (type == XYPublicChatHallViewType_HotRoom) {
  302. for (int i = 0 ; i < self.titles.count ; i++) {
  303. NSString *str = self.titles[i];
  304. if ([str isEqualToString:kLocalizedString(@"热门")]) {
  305. self.segmentHead.selectedIndex(i);
  306. }
  307. }
  308. }else if (type == XYPublicChatHallViewType_LoveRoom){
  309. for (int i = 0 ; i < self.titles.count ; i++) {
  310. NSString *str = self.titles[i];
  311. if ([str isEqualToString:kLocalizedString(@"相亲")]) {
  312. self.segmentHead.selectedIndex(i);
  313. }
  314. }
  315. }else if (type == XYPublicChatHallViewType_DispatchRoom){
  316. for (int i = 0 ; i < self.titles.count ; i++) {
  317. NSString *str = self.titles[i];
  318. if ([str isEqualToString:kLocalizedString(@"派单")]) {
  319. self.segmentHead.selectedIndex(i);
  320. }
  321. }
  322. }else if (type == XYPublicChatHallViewType_AuctionRoom){
  323. for (int i = 0 ; i < self.titles.count ; i++) {
  324. NSString *str = self.titles[i];
  325. if ([str isEqualToString:kLocalizedString(@"拍卖")]) {
  326. self.segmentHead.selectedIndex(i);
  327. }
  328. }
  329. }
  330. }
  331. - (void)getLiveCategoryData {
  332. if (self.isMyVoiceRoomCollect) {
  333. NSMutableArray *tempTitles = [NSMutableArray new];
  334. XYChatRoomCategoriesModel *collectModel = XYChatRoomCategoriesModel.new;
  335. collectModel.categoryName = kLocalizedString(@"收藏");
  336. collectModel.categoryId = @"";
  337. collectModel.chatRoom_Type = XYChatRoom_Type_Collect;
  338. collectModel.layout = XYVoiceRoomListLayoutOneGraphic;
  339. [tempTitles addObject:collectModel];
  340. self.voiceCategoryArray = [NSArray arrayWithArray:tempTitles];
  341. }else{
  342. NSMutableArray *tempTitles = [NSMutableArray new];
  343. NSArray *rtcRoomCategories = [ApplicationDelegate.appConfigModel.channelConfig.rtcRoomCategories copy];
  344. XYChatRoomCategoriesModel *hotModel = XYChatRoomCategoriesModel.new;
  345. hotModel.categoryName = kLocalizedString(@"热门");
  346. hotModel.categoryId = @"";
  347. hotModel.chatRoom_Type = XYChatRoom_Type_Hot;
  348. hotModel.layout = XYVoiceRoomListLayoutTwoBigImage;
  349. [tempTitles addObject:hotModel];
  350. for (NSDictionary *dict in rtcRoomCategories) {
  351. XYChatRoomCategoriesModel *defaultModel = XYChatRoomCategoriesModel.new;
  352. [defaultModel yy_modelSetWithDictionary:dict];
  353. defaultModel.chatRoom_Type = XYChatRoom_Type_Default;
  354. defaultModel.autoLoad = YES;
  355. if (defaultModel.makeFriend) {
  356. defaultModel.chatRoom_Type = XYChatRoom_Type_Friends;
  357. }
  358. [tempTitles addObject:defaultModel];
  359. }
  360. self.voiceCategoryArray = [NSArray arrayWithArray:tempTitles];
  361. }
  362. }
  363. #pragma mark - 布局子控件
  364. - (void)_makeSubViewsConstraints{
  365. }
  366. #pragma mark - Setter & Getter
  367. - (XYGesturesScrollView *)mainScrollView {
  368. if (!_mainScrollView) {
  369. _mainScrollView = [[XYGesturesScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.isMyVoiceRoomCollect ? SCREEN_HEIGHT : (SCREEN_HEIGHT - NAVGATION_HEIGHT))];
  370. _mainScrollView.delegate = self;
  371. _mainScrollView.showsVerticalScrollIndicator = NO;
  372. _mainScrollView.showsHorizontalScrollIndicator = NO;
  373. _mainScrollView.backgroundColor = [UIColor clearColor];
  374. if (@available(iOS 11.0, *)) {
  375. /// CoderMikeHe: 适配 iPhone X + iOS 11,
  376. AdjustsScrollViewInsets_Never(_mainScrollView);
  377. }
  378. }
  379. return _mainScrollView;
  380. }
  381. - (SDCycleScrollView *)cycleScrollView {
  382. if (!_cycleScrollView) {
  383. _cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(10.0f, 0.0f, SCREEN_WIDTH - 20.0f, 0.0f) delegate:self placeholderImage:placeholderBannerImage()];
  384. _cycleScrollView.backgroundColor = Color_Clear;
  385. _cycleScrollView.bannerImageViewContentMode = UIViewContentModeScaleAspectFill;
  386. // 是否无限循环
  387. _cycleScrollView.infiniteLoop = YES;
  388. // 是否自动滚动
  389. _cycleScrollView.autoScroll = YES;
  390. _cycleScrollView.pageControlStyle = SDCycleScrollViewPageContolStyleAnimated;
  391. _cycleScrollView.currentPageDotImage = [UIImage resizableImageWithColor:Color_White cornerRadius:4];
  392. _cycleScrollView.pageDotImage = [UIImage resizableImageWithColor:ColorFromHexStringWithAlpha(@"#000000", 0.4f) cornerRadius:4];
  393. [_cycleScrollView addViewBorder:Color_Clear redian:8.0f];
  394. _cycleScrollView.alpha = 0;
  395. }
  396. return _cycleScrollView;
  397. }
  398. - (NSMutableArray *)bannerDataScoure {
  399. if (!_bannerDataScoure) {
  400. _bannerDataScoure = [NSMutableArray array];
  401. }
  402. return _bannerDataScoure;
  403. }
  404. @end