BGConversationSegmentController.m 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. //
  2. // BGConversationSegmentController.m
  3. // BuguLive
  4. //
  5. // Created by 朱庆彬 on 2017/8/22.
  6. // Copyright © 2017年 xfg. All rights reserved.
  7. //
  8. #import "BGConversationSegmentController.h"
  9. #import "BGChatSegmentScrollController.h"
  10. #import "BGConversationListController.h"
  11. #import "BGConversationStrangerController.h"
  12. #import "BGConversationTradeController.h"
  13. static NSString const *kChatTrade = @"交易";
  14. static NSString const *kChatFriend = @"好友";
  15. static NSString const *kChatStranger = @"未关注";
  16. NSInteger kSelectedSegmentIndex = 1; // segmentedControl默认选中的index
  17. @interface BGConversationSegmentController () <UIScrollViewDelegate, ConversationListViewDelegate, FWConversationStrangerViewDelegate>
  18. {
  19. int _selectIndex; //1.交易 2.好友 3.未关注
  20. BGConversationListController *_conversationListVC;
  21. BGConversationStrangerController *_conversationStrangerVC;
  22. BGConversationTradeController *_conversationTradeVC;
  23. }
  24. @property (nonatomic, assign) int tag;
  25. @property (nonatomic, assign) BOOL isClickedSegmented;// 是否点击了Segmented的滑块
  26. @property (nonatomic, strong) BGChatSegmentScrollController *scrollView;
  27. @property (nonatomic, strong) HMSegmentedControl *segmentedControl;
  28. @property (nonatomic, strong) UIView *navView;
  29. @property (nonatomic, strong) NSMutableArray *itemMutableArray;
  30. @end
  31. @implementation BGConversationSegmentController
  32. - (void)viewWillAppear:(BOOL)animated
  33. {
  34. [super viewWillAppear:animated];
  35. [self.navigationController setNavigationBarHidden:YES animated:NO];
  36. }
  37. - (void)viewWillDisappear:(BOOL)animated
  38. {
  39. [super viewWillAppear:animated];
  40. if (!_mbhalf)
  41. {
  42. [self.navigationController setNavigationBarHidden:NO animated:NO];
  43. }
  44. }
  45. - (void)viewDidLoad
  46. {
  47. [super viewDidLoad];
  48. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(IMChatMsgNotficationInList:) name:g_notif_chatmsg object:nil];
  49. }
  50. #pragma mrak-- ------------------------消息通知-- ------------------------
  51. - (void)IMChatMsgNotficationInList:(NSNotification *)notfication
  52. {
  53. if (![NSThread isMainThread])
  54. {
  55. [self performSelectorOnMainThread:@selector(IMChatMsgNotficationInList:) withObject:notfication waitUntilDone:NO];
  56. return;
  57. }
  58. [self loadBtnBadageData];
  59. }
  60. - (void)initFWUI
  61. {
  62. [super initFWUI];
  63. self.BuguLive = [GlobalVariables sharedInstance];
  64. if ([self.BuguLive.appModel.open_pai_module intValue] == 1 || [self.BuguLive.appModel.shopping_goods integerValue] == 1)
  65. {
  66. self.itemMutableArray = [NSMutableArray arrayWithObjects:kChatTrade, ASLocalizedString(kChatFriend), ASLocalizedString(kChatStranger), nil];
  67. kSelectedSegmentIndex = 1;
  68. }
  69. else
  70. {
  71. self.itemMutableArray = [NSMutableArray arrayWithObjects:ASLocalizedString(kChatFriend), ASLocalizedString(kChatStranger), nil];
  72. kSelectedSegmentIndex = 0;
  73. }
  74. CGFloat viewWidth = kScreenW;
  75. CGFloat viewHeight = kScreenH;
  76. CGFloat navPointY = 0.0f;
  77. CGFloat scrollViewHeight = viewHeight - 64;
  78. CGFloat childViewPointY = 0;
  79. CGFloat navHeight;
  80. if (_mbhalf == YES)
  81. {
  82. navHeight = 44;
  83. scrollViewHeight = viewHeight / 2 - 44;
  84. }
  85. else
  86. {
  87. navHeight = kNavigationBarHeight+kStatusBarHeight;
  88. }
  89. _navView = [[UIView alloc] initWithFrame:CGRectMake(0, navPointY, kScreenW, navHeight)];
  90. _navView.backgroundColor = [UIColor whiteColor];
  91. [self.view addSubview:_navView];
  92. UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, navHeight - 0.5, kScreenW, 0.5f)];
  93. lineView.backgroundColor = kAppSpaceColor;
  94. [_navView addSubview:lineView];
  95. UIButton *backBtn = [[UIButton alloc] initWithFrame:CGRectMake(10, navHeight - 44, 22, 44)];
  96. [backBtn setImage:[UIImage imageNamed:@"com_arrow_vc_back"] forState:UIControlStateNormal];
  97. [backBtn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
  98. [_navView addSubview:backBtn];
  99. self.segmentedControl = [[HMSegmentedControl alloc] initWithFrame:CGRectMake(kScreenW / 2 - 100, navHeight - 41, 200, 40)];
  100. self.segmentedControl.sectionTitles = self.itemMutableArray;
  101. self.segmentedControl.selectedSegmentIndex = kSelectedSegmentIndex;
  102. self.segmentedControl.backgroundColor = kWhiteColor;
  103. self.segmentedControl.titleTextAttributes = @{NSForegroundColorAttributeName: kAppGrayColor3, NSFontAttributeName: [UIFont systemFontOfSize:15]};
  104. self.segmentedControl.selectedTitleTextAttributes = @{NSForegroundColorAttributeName: kAppGrayColor1, NSBackgroundColorAttributeName: kClearColor, NSFontAttributeName: [UIFont systemFontOfSize:15]};
  105. self.segmentedControl.selectionIndicatorColor = kAppGrayColor1;
  106. self.segmentedControl.selectionIndicatorHeight = 3;
  107. self.segmentedControl.selectionIndicatorBoxColor = kClearColor;
  108. self.segmentedControl.selectionStyle = HMSegmentedControlSelectionStyleTextWidthStripe;
  109. self.segmentedControl.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationDown;
  110. self.segmentedControl.segmentEdgeInset = UIEdgeInsetsMake(0, 10, 0, 10);
  111. FWWeakify(self)
  112. [self.segmentedControl setIndexChangeBlock:^(NSInteger index) {
  113. FWStrongify(self)
  114. [self.scrollView scrollRectToVisible:CGRectMake(viewWidth * index, 0, viewWidth, CGRectGetHeight(self.scrollView.frame))
  115. animated:YES];
  116. self.isClickedSegmented = YES;
  117. }];
  118. [_navView addSubview:_segmentedControl];
  119. UIButton *rightBtn = [[UIButton alloc] initWithFrame:CGRectMake(kScreenW - 70, navHeight - 36, 60, 30)];
  120. [rightBtn setTitle:ASLocalizedString(@"忽略未读")forState:UIControlStateNormal];
  121. [rightBtn.titleLabel setFont:[UIFont systemFontOfSize:13]];
  122. [rightBtn setTitleColor:kAppMainColor forState:UIControlStateNormal];
  123. [rightBtn addTarget:self action:@selector(clickedunread:) forControlEvents:UIControlEventTouchUpInside];
  124. [_navView addSubview:rightBtn];
  125. self.scrollView = [[BGChatSegmentScrollController alloc] initWithFrame:CGRectMake(0, lineView.bottom, viewWidth, scrollViewHeight)];
  126. self.scrollView.delegate = self;
  127. self.scrollView.backgroundColor = kClearColor;
  128. self.scrollView.pagingEnabled = YES;
  129. self.scrollView.showsHorizontalScrollIndicator = NO;
  130. self.scrollView.bounces = NO;
  131. [self.scrollView setDelaysContentTouches:NO];
  132. [self.scrollView setCanCancelContentTouches:NO];
  133. self.scrollView.contentSize = CGSizeMake(viewWidth * [self.itemMutableArray count], CGRectGetHeight(self.scrollView.frame));
  134. [self.scrollView scrollRectToVisible:CGRectMake(kSelectedSegmentIndex * viewWidth, 0, viewWidth, CGRectGetHeight(self.scrollView.frame)) animated:NO];
  135. [self.view addSubview:self.scrollView];
  136. // 交易
  137. if ([self.itemMutableArray containsObject:kChatTrade])
  138. {
  139. _conversationTradeVC = [[BGConversationTradeController alloc] init];
  140. _conversationTradeVC.isHaveLive = _mbhalf;
  141. _conversationTradeVC.view.frame = CGRectMake(viewWidth * [self.itemMutableArray indexOfObject:kChatTrade], childViewPointY, viewWidth, _scrollView.bounds.size.height);
  142. [_conversationTradeVC updateTableViewFrame];
  143. [_scrollView addSubview:_conversationTradeVC.view];
  144. }
  145. // 好友
  146. if ([self.itemMutableArray containsObject:ASLocalizedString(kChatFriend)])
  147. {
  148. _conversationListVC = [[BGConversationListController alloc] init];
  149. _conversationListVC.view.frame = CGRectMake(viewWidth * [self.itemMutableArray indexOfObject:ASLocalizedString(kChatFriend)], childViewPointY, viewWidth, _scrollView.bounds.size.height);
  150. _conversationListVC.isHaveLive = _mbhalf;
  151. _conversationListVC.delegate = self;
  152. [_conversationListVC updateTableViewFrame];
  153. [_scrollView addSubview:_conversationListVC.view];
  154. }
  155. if ([self.itemMutableArray containsObject:ASLocalizedString(kChatStranger)])
  156. {
  157. _conversationStrangerVC = [[BGConversationStrangerController alloc] init];
  158. _conversationStrangerVC.view.frame = CGRectMake(viewWidth * [self.itemMutableArray indexOfObject:ASLocalizedString(kChatStranger)], childViewPointY, viewWidth, _scrollView.bounds.size.height);
  159. _conversationStrangerVC.isHaveLive = _mbhalf;
  160. _conversationStrangerVC.delegate = self;
  161. [_conversationStrangerVC updateTableViewFrame];
  162. [_scrollView addSubview:_conversationStrangerVC.view];
  163. }
  164. CGFloat badgeBtnPointX;
  165. CGFloat badgeBtn2PointX;
  166. if (_itemMutableArray.count == 2)
  167. {
  168. badgeBtnPointX = self.segmentedControl.width / 2 - 35;
  169. badgeBtn2PointX = badgeBtnPointX +105;
  170. }
  171. else
  172. {
  173. badgeBtnPointX = self.segmentedControl.width / 3 * 2 - 20;
  174. badgeBtn2PointX = badgeBtnPointX +75;
  175. }
  176. UIButton *friendBtn = [[UIButton alloc] initWithFrame:CGRectMake(badgeBtnPointX, 10, 10, 10)];
  177. friendBtn.backgroundColor = [UIColor clearColor];
  178. [_segmentedControl addSubview:friendBtn];
  179. [self initFriendBadgeBtn:friendBtn];
  180. UIButton *strangerBtn = [[UIButton alloc] initWithFrame:CGRectMake(badgeBtn2PointX, 10, 10, 10)];
  181. strangerBtn.backgroundColor = [UIColor clearColor];
  182. [_segmentedControl addSubview:strangerBtn];
  183. [self initStrangerBadgeBtn:strangerBtn];
  184. //[self loadBtnBadageData];
  185. }
  186. //忽略未读点击事件
  187. - (void)clickedunread:(UIButton *)sender
  188. {
  189. [SVProgressHUD showWithStatus:ASLocalizedString(@"操作中...")];
  190. NSArray *friendConversationArr = _conversationListVC.conversationArr;
  191. NSArray *strangerConversationArr = _conversationStrangerVC.conversationArr;
  192. [SFriendObj ignoreMsg:friendConversationArr
  193. block:^(SResBase *resb) {
  194. if (resb.msuccess)
  195. {
  196. [SVProgressHUD showSuccessWithStatus:resb.mmsg];
  197. kNotifPost(@"clearJsbadge", nil);
  198. self.badgeFriend.badgeText = nil;
  199. [_conversationListVC.mTableView reloadData];
  200. }
  201. else
  202. [SVProgressHUD showErrorWithStatus:resb.mmsg];
  203. }];
  204. [SFriendObj ignoreMsg:strangerConversationArr
  205. block:^(SResBase *resb) {
  206. if (resb.msuccess)
  207. {
  208. [SVProgressHUD showSuccessWithStatus:resb.mmsg];
  209. self.badgeStranger.badgeText = nil;
  210. [_conversationStrangerVC.mTableView reloadData];
  211. }
  212. else
  213. [SVProgressHUD showErrorWithStatus:resb.mmsg];
  214. }];
  215. }
  216. #pragma mark - ----------------------- segment代理 -----------------------
  217. #pragma mark 解决Segmented的滑块快速滑动时的延迟,同时把点击滑块的情况排除在外
  218. - (void)scrollViewDidScroll:(UIScrollView *)scrollView
  219. {
  220. if (!self.isClickedSegmented)
  221. {
  222. CGFloat pageWidth = scrollView.frame.size.width;
  223. NSInteger tmpPage = scrollView.contentOffset.x / pageWidth;
  224. float tmpPage2 = scrollView.contentOffset.x / pageWidth;
  225. NSInteger page = tmpPage2 - tmpPage >= 0.5 ? tmpPage + 1 : tmpPage;
  226. if (kSelectedSegmentIndex != page)
  227. {
  228. [self.segmentedControl setSelectedSegmentIndex:page animated:YES];
  229. kSelectedSegmentIndex = page;
  230. }
  231. }
  232. }
  233. #pragma mark 页面滚动,同时调起Segmented的滑块滑动起来等
  234. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
  235. {
  236. CGFloat pageWidth = scrollView.frame.size.width;
  237. NSInteger page = scrollView.contentOffset.x / pageWidth;
  238. self.isClickedSegmented = NO;
  239. [self.segmentedControl setSelectedSegmentIndex:page animated:YES];
  240. }
  241. - (void)backClick
  242. {
  243. //1
  244. kNotifPost(@"reloadunread", nil);
  245. //2
  246. if (self.mbhalf == YES)
  247. {
  248. [self goNextVCBlock:1:nil]; //1 返回
  249. }
  250. else
  251. {
  252. [self.navigationController popViewControllerAnimated:YES];
  253. }
  254. }
  255. #pragma mark----------------------- 半VC 相关部分 ----------------------------
  256. #pragma mark -goNextVC 半VC去下个半界面block
  257. /**
  258. * @author Yue
  259. *
  260. * @brief 半VC 去下个界面
  261. * @discussion 1.不能push/present,因弹出半Vc,保证直播VC还能点击,addChildVC处理的
  262. * 2.vc。view 切动画弹出,达到push效果
  263. */
  264. - (void)goNextVCBlock:(int)tag:(SFriendObj *)friend_Obj
  265. {
  266. NSLog(@"%s", __func__);
  267. if (self.goNextVCBlock)
  268. {
  269. self.goNextVCBlock(tag, friend_Obj);
  270. }
  271. }
  272. #pragma mark - 创建半ChatVC
  273. /**
  274. * @author Yue
  275. *
  276. * @brief 创建半VC,成为直播VC的子VC,并把View加到直播view,动画切出来半VC.View
  277. * @prama1.0 imChat_VC nib加载
  278. * @prama1.1 mbhalf 最好创建完就设置,如果写在block完成后,会有问题
  279. * @prama1.2 frame 先放到屏幕下方
  280. * @prama1.3 animate 动画切出。类似模态弹出效果
  281. *
  282. * @prama2.0 live_VC 不直接传,是因为要考虑重播。。这个为啥没区分重播???
  283. * @prama2.1 child 子VC 子View 并 置前
  284. */
  285. //ykk 半VC 成为子VC 加载到直播VC
  286. + (BGConversationSegmentController *)createIMChatVCWithHalf:(UIViewController *)full_VC isRelive:(BOOL)sender
  287. {
  288. //1.0
  289. BGConversationSegmentController *conversationListVC = [[BGConversationSegmentController alloc] init];
  290. //1.2
  291. conversationListVC.mbhalf = YES;
  292. //1.1
  293. conversationListVC.view.frame = CGRectMake(0, kScreenH, kScreenW, kScreenH / 2);
  294. //1.3
  295. [UIView animateWithDuration:kHalfVCViewanimation
  296. animations:^{
  297. //移动到 半下方 不用transform 容易出问题
  298. conversationListVC.view.y = kScreenH / 2;
  299. }
  300. completion:^(BOOL finished){
  301. }];
  302. //2.
  303. BGLiveServiceController *live_VC = (BGLiveServiceController *) full_VC;
  304. [live_VC addChildViewController:conversationListVC];
  305. [live_VC.view addSubview:conversationListVC.view];
  306. [live_VC.view bringSubviewToFront:conversationListVC.view];
  307. return conversationListVC;
  308. }
  309. + (void)showIMChatInVCWithHalf:(UIViewController *)vc inView:(UIView *)inView
  310. {
  311. BGConversationSegmentController *conListVC = [[BGConversationSegmentController alloc] init];
  312. conListVC.mbhalf = YES;
  313. UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:conListVC];
  314. //[vc addChildViewController:nav];
  315. nav.navigationBarHidden = YES;
  316. nav.view.backgroundColor = [UIColor clearColor];
  317. //[vc.view addSubview:nav.view];
  318. nav.modalPresentationStyle = UIModalPresentationCustom;
  319. [vc presentViewController:nav
  320. animated:NO
  321. completion:^{
  322. CGRect fff = nav.view.frame;
  323. fff.origin.y = inView.bounds.size.height / 3.0f;
  324. fff.size.height = inView.bounds.size.height * (2.0f / 3.0f);
  325. nav.view.frame = fff;
  326. UIButton *bt = [[UIButton alloc] initWithFrame:CGRectMake(0, -fff.origin.y, inView.bounds.size.width, fff.origin.y)];
  327. bt.backgroundColor = [UIColor yellowColor];
  328. [bt addTarget:conListVC action:@selector(halfbtclicked:) forControlEvents:UIControlEventTouchUpInside];
  329. // [nav.view.window addSubview:bt];
  330. }];
  331. }
  332. - (void)loadBtnBadageData
  333. {
  334. [SFriendObj getMyFocusFriendUnReadMsgNumIsFriend:1
  335. block:^(int unReadNum) {
  336. int scount = unReadNum;
  337. if (scount)
  338. {
  339. if (scount > 98)
  340. {
  341. self.badgeFriend.badgeText = @"99+";
  342. }
  343. else
  344. {
  345. self.badgeFriend.badgeText = [NSString stringWithFormat:@"%d", scount];
  346. }
  347. }
  348. else
  349. {
  350. self.badgeFriend.badgeText = nil;
  351. }
  352. }];
  353. [SFriendObj getMyFocusFriendUnReadMsgNumIsFriend:2
  354. block:^(int unReadNum) {
  355. int scount = unReadNum;
  356. if (scount)
  357. {
  358. if (scount > 98)
  359. {
  360. self.badgeStranger.badgeText = @"99+";
  361. }
  362. else
  363. {
  364. self.badgeStranger.badgeText = [NSString stringWithFormat:@"%d", scount];
  365. }
  366. }
  367. else
  368. {
  369. self.badgeStranger.badgeText = nil;
  370. }
  371. }];
  372. }
  373. /**
  374. 设置 角标
  375. @param sender 对应的控件
  376. */
  377. - (void)initFriendBadgeBtn:(UIButton *)sender
  378. {
  379. //-好友
  380. self.badgeFriend = [[JSBadgeView alloc] initWithParentView:sender alignment:JSBadgeViewAlignmentTopRight];
  381. }
  382. - (void)initStrangerBadgeBtn:(UIButton *)sender
  383. {
  384. self.badgeStranger = [[JSBadgeView alloc] initWithParentView:sender alignment:JSBadgeViewAlignmentTopRight];
  385. }
  386. - (void)clickFriendItem:(SFriendObj *)obj
  387. {
  388. [self goNextVCBlock:2:obj];
  389. }
  390. - (void)reloadChatBadge:(int)selectItem
  391. {
  392. [SFriendObj getMyFocusFriendUnReadMsgNumIsFriend:selectItem-1
  393. block:^(int unReadNum) {
  394. int scount = unReadNum;
  395. if (selectItem == 2)
  396. {
  397. if (scount)
  398. {
  399. if (scount > 98)
  400. {
  401. self.badgeFriend.badgeText = @"99+";
  402. }
  403. else
  404. {
  405. self.badgeFriend.badgeText = [NSString stringWithFormat:@"%d", scount];
  406. }
  407. }
  408. else
  409. {
  410. self.badgeFriend.badgeText = nil;
  411. }
  412. }
  413. else if (selectItem == 3)
  414. {
  415. if (scount)
  416. {
  417. if (scount > 98)
  418. {
  419. self.badgeStranger.badgeText = @"99+";
  420. }
  421. else
  422. {
  423. self.badgeStranger.badgeText = [NSString stringWithFormat:@"%d", scount];
  424. }
  425. }
  426. else
  427. {
  428. self.badgeStranger.badgeText = nil;
  429. }
  430. }
  431. }];
  432. }
  433. - (void)updateChatFriendBadge:(int)unReadNum
  434. {
  435. if (unReadNum)
  436. {
  437. if (unReadNum > 98)
  438. {
  439. self.badgeFriend.badgeText = @"99+";
  440. }
  441. else
  442. {
  443. self.badgeFriend.badgeText = [NSString stringWithFormat:@"%d", unReadNum];
  444. }
  445. }
  446. else
  447. {
  448. self.badgeFriend.badgeText = nil;
  449. }
  450. }
  451. - (void)updateChatStrangerBadge:(int)unReadNum
  452. {
  453. if (unReadNum)
  454. {
  455. if (unReadNum > 98)
  456. {
  457. self.badgeStranger.badgeText = @"99+";
  458. }
  459. else
  460. {
  461. self.badgeStranger.badgeText = [NSString stringWithFormat:@"%d", unReadNum];
  462. }
  463. }
  464. else
  465. {
  466. self.badgeStranger.badgeText = nil;
  467. }
  468. }
  469. - (void)didReceiveMemoryWarning
  470. {
  471. [super didReceiveMemoryWarning];
  472. // Dispose of any resources that can be recreated.
  473. }
  474. /*
  475. #pragma mark - Navigation
  476. // In a storyboard-based application, you will often want to do a little preparation before navigation
  477. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  478. // Get the new view controller using [segue destinationViewController].
  479. // Pass the selected object to the new view controller.
  480. }
  481. */
  482. @end