ChatViewController.m 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. //
  2. // ChatViewController.m
  3. // KulexiuForTeacher
  4. //
  5. // Created by Kyle on 2022/3/17.
  6. //
  7. #import "ChatViewController.h"
  8. #import "ChatNavView.h"
  9. #import "CreateFansGroupViewController.h"
  10. #import "ChatAddressViewController.h"
  11. #import "KSChatListViewController.h"
  12. #import "KSTabBarViewController.h"
  13. #import "TXIMLinsenter.h"
  14. @interface ChatViewController ()<JXCategoryViewDelegate>
  15. @property (nonatomic, strong) ChatNavView *navView;
  16. @property (nonatomic, strong) JXCategoryTitleView *myCategoryView;
  17. @property (nonatomic, assign) NSInteger currentIndex;
  18. @property (nonatomic, strong) KSChatListViewController *chatListCtrl;
  19. @property (nonatomic, strong) ChatAddressViewController *contractList;
  20. @end
  21. @implementation ChatViewController
  22. - (void)viewDidLoad {
  23. [super viewDidLoad];
  24. // Do any additional setup after loading the view.
  25. self.ks_prefersNavigationBarHidden = YES;
  26. self.titles = @[@"聊天", @"联系人"];
  27. [self getUnreadCount];
  28. [self configUI];
  29. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshChatList) name:CHATVIEW_REFRESH object:nil];
  30. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshMessageStatus:) name:CHATVIEW_REFRESHSTATUS object:nil];
  31. }
  32. - (void)getUnreadCount {
  33. [TXIM_LINSENTER getUnReadCountCallback:^(NSInteger count) {
  34. dispatch_async(dispatch_get_main_queue(),^{
  35. if (count >= 1) {
  36. self.navView.dotView.hidden = NO;
  37. } else {
  38. self.navView.dotView.hidden = YES;
  39. }
  40. });
  41. }];
  42. }
  43. - (void)refreshMessageStatus:(NSNotification *)notification {
  44. NSDictionary *msgDic = notification.object; ///
  45. NSInteger unreadMsgCount = [msgDic ks_integerValueForKey:@"unreadCount"];
  46. if (unreadMsgCount >= 1) {
  47. self.navView.dotView.hidden = NO;
  48. } else {
  49. self.navView.dotView.hidden = YES;
  50. }
  51. }
  52. - (void)refreshChatList {
  53. if (self.chatListCtrl && self.currentIndex == 0) {
  54. [self.chatListCtrl refreshChatListMessage];
  55. }
  56. }
  57. - (void)configUI {
  58. [self.scrollView removeFromSuperview];
  59. [self.view addSubview:self.navView];
  60. CGFloat topSpace = kNaviBarHeight;
  61. self.myCategoryView.frame = CGRectMake(0, topSpace, KPortraitWidth, 10);
  62. self.myCategoryView.backgroundColor = HexRGB(0xf6f8f9);
  63. self.myCategoryView.titles = self.titles;
  64. self.myCategoryView.titleFont = [UIFont systemFontOfSize:16.0f];
  65. self.myCategoryView.titleSelectedFont = [UIFont systemFontOfSize:16.0f weight:UIFontWeightMedium];
  66. self.myCategoryView.titleSelectedColor = THEMECOLOR;
  67. self.myCategoryView.titleColor = HexRGB(0x777777);
  68. self.myCategoryView.titleColorGradientEnabled = YES;
  69. self.myCategoryView.hidden = YES;
  70. [self.view addSubview:self.categoryView];
  71. [self.view addSubview:self.listContainerView];
  72. }
  73. - (void)viewDidLayoutSubviews {
  74. [super viewDidLayoutSubviews];
  75. CGFloat topSpace = kNaviBarHeight;
  76. self.categoryView.frame = CGRectMake(0, topSpace, self.view.bounds.size.width, [self preferredCategoryViewHeight]);
  77. self.listContainerView.frame = CGRectMake(0, topSpace + [self preferredCategoryViewHeight], self.view.bounds.size.width, self.view.bounds.size.height - topSpace - [self preferredCategoryViewHeight]);
  78. }
  79. - (void)viewDidAppear:(BOOL)animated {
  80. [super viewDidAppear:animated];
  81. // 处于第一个item的时候,才允许屏幕边缘手势返回
  82. self.navigationController.interactivePopGestureRecognizer.enabled = (self.categoryView.selectedIndex == 0);
  83. }
  84. - (void)scrollToFirstPage {
  85. [self.myCategoryView selectItemAtIndex:0];
  86. }
  87. - (void)viewWillDisappear:(BOOL)animated {
  88. [super viewWillDisappear:animated];
  89. // 离开页面的时候,需要恢复屏幕边缘手势,不能影响其他页面
  90. self.navigationController.interactivePopGestureRecognizer.enabled = YES;
  91. }
  92. - (JXCategoryTitleView *)myCategoryView {
  93. return (JXCategoryTitleView *)self.categoryView;
  94. }
  95. - (CGFloat)preferredCategoryViewHeight {
  96. return CGFLOAT_MIN;
  97. }
  98. - (JXCategoryBaseView *)preferredCategoryView {
  99. return [[JXCategoryTitleView alloc] init];
  100. }
  101. #pragma mark - JXCategoryViewDelegate
  102. // 点击选中或者滚动选中都会调用该方法。适用于只关心选中事件,不关心具体是点击还是滚动选中的。
  103. - (void)categoryView:(JXCategoryBaseView *)categoryView didSelectedItemAtIndex:(NSInteger)index {
  104. NSLog(@"%@", NSStringFromSelector(_cmd));
  105. // bool scrollChatTable = index == 0 ? YES : NO;
  106. // [self.navView scrollChatTable:scrollChatTable];
  107. // 侧滑手势处理
  108. self.navigationController.interactivePopGestureRecognizer.enabled = (index == 0);
  109. }
  110. #pragma mark - JXCategoryListContainerViewDelegate
  111. - (id<JXCategoryListContentViewDelegate>)listContainerView:(JXCategoryListContainerView *)listContainerView initListForIndex:(NSInteger)index {
  112. if (index == 0) {
  113. KSChatListViewController *listVC = [[KSChatListViewController alloc] init];
  114. listVC.view.frame = CGRectMake(0, 0, KPortraitWidth, KPortraitHeight - kNaviBarHeight - kTabBarHeight);
  115. self.chatListCtrl = listVC;
  116. return listVC;
  117. }
  118. else {
  119. ChatAddressViewController *list = [[ChatAddressViewController alloc] init];
  120. MJWeakSelf;
  121. [list scrollActionCallback:^(NSInteger scrollIndex) {
  122. [weakSelf contractScrollIndex:scrollIndex];
  123. }];
  124. self.contractList = list;
  125. list.titles = @[@"群组",@"联系人"];
  126. return list;
  127. }
  128. }
  129. - (void)contractScrollIndex:(NSInteger)scrollIndex {
  130. [self.navView scrollChatTableIndex:scrollIndex+1];
  131. }
  132. // 返回列表的数量
  133. - (NSInteger)numberOfListsInlistContainerView:(JXCategoryListContainerView *)listContainerView {
  134. return self.titles.count;
  135. }
  136. #pragma mark - JXCategoryListContainerViewDelegate
  137. - (void)listContainerViewDidScroll:(UIScrollView *)scrollView{
  138. if ([self isKindOfClass:[ChatAddressViewController class]]) {
  139. CGFloat index = scrollView.contentOffset.x/scrollView.bounds.size.width;
  140. CGFloat absIndex = fabs(index - self.currentIndex);
  141. if (absIndex >= 1) {
  142. //”快速滑动的时候,只响应最外层VC持有的scrollView“,说实话,完全可以不用处理这种情况。如果你们的产品经理坚持认为这是个问题,就把这块代码加上吧。
  143. //嵌套使用的时候,最外层的VC持有的scrollView在翻页之后,就断掉一次手势。解决快速滑动的时候,只响应最外层VC持有的scrollView。子VC持有的scrollView却没有响应
  144. self.listContainerView.scrollView.panGestureRecognizer.enabled = NO;
  145. self.listContainerView.scrollView.panGestureRecognizer.enabled = YES;
  146. _currentIndex = floor(index);
  147. }
  148. }
  149. }
  150. - (ChatNavView *)navView {
  151. if (!_navView) {
  152. _navView = [ChatNavView shareInstance];
  153. _navView.frame = CGRectMake(0, 0, KPortraitWidth, kNaviBarHeight);
  154. MJWeakSelf;
  155. [_navView chatNavAction:^(CHATNAVACTION action) {
  156. [weakSelf navAction:action];
  157. }];
  158. }
  159. return _navView;
  160. }
  161. - (void)navAction:(CHATNAVACTION)action {
  162. switch (action) {
  163. case CHATNAVACTION_LIST:
  164. {
  165. [self.categoryView selectItemAtIndex:0];
  166. }
  167. break;
  168. case CHATNAVACTION_GROUP: // 群组
  169. {
  170. [self.categoryView selectItemAtIndex:1];
  171. [self.contractList scrollToChatList:NO];
  172. }
  173. break;
  174. case CHATNAVACTION_CONTACTS: // 联系人
  175. {
  176. [self.categoryView selectItemAtIndex:1];
  177. [self.contractList scrollToChatList:YES];
  178. }
  179. break;
  180. case CHATNAVACTION_SEND:
  181. {
  182. }
  183. break;
  184. default:
  185. break;
  186. }
  187. }
  188. #pragma mark - Custom Accessors
  189. // 分页菜单视图
  190. - (JXCategoryBaseView *)categoryView {
  191. if (!_categoryView) {
  192. _categoryView = [self preferredCategoryView];
  193. _categoryView.delegate = self;
  194. // !!!: 将列表容器视图关联到 categoryView
  195. _categoryView.listContainer = self.listContainerView;
  196. }
  197. return _categoryView;
  198. }
  199. // 列表容器视图
  200. - (JXCategoryListContainerView *)listContainerView {
  201. if (!_listContainerView) {
  202. _listContainerView = [[JXCategoryListContainerView alloc] initWithType:JXCategoryListContainerType_ScrollView delegate:self];
  203. _listContainerView.scrollView.scrollEnabled = NO;
  204. }
  205. return _listContainerView;
  206. }
  207. /*
  208. #pragma mark - Navigation
  209. // In a storyboard-based application, you will often want to do a little preparation before navigation
  210. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  211. // Get the new view controller using [segue destinationViewController].
  212. // Pass the selected object to the new view controller.
  213. }
  214. */
  215. @end