KSSelectConversationViewController.m 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. //
  2. // KSSelectConversationViewController.m
  3. // StudentDaya
  4. //
  5. // Created by Kyle on 2021/8/5.
  6. // Copyright © 2021 DayaMusic. All rights reserved.
  7. //
  8. #import "KSSelectConversationViewController.h"
  9. #import <RongIMKit/RCKitUtility.h>
  10. #import <RongIMKit/RCKitCommonDefine.h>
  11. #import <RongIMKit/RCKitConfig.h>
  12. #import <RongIMKit/RCIM.h>
  13. @interface RCSelectConversationCell : UITableViewCell
  14. - (void)setConversation:(RCConversation *)conversation ifSelected:(BOOL)ifSelected;
  15. @end
  16. typedef void (^CompleteBlock)(NSArray *conversationList);
  17. @interface KSSelectConversationViewController ()<UITableViewDataSource, UITableViewDelegate>
  18. @property (nonatomic, strong) NSMutableArray *selectedConversationArray;
  19. @property (nonatomic, strong) UITableView *conversationTableView;
  20. @property (nonatomic, strong) UIBarButtonItem *rightBarButtonItem;
  21. @property (nonatomic, strong) NSArray *listingConversationArray;
  22. @property (nonatomic, strong) CompleteBlock completeBlock;
  23. @end
  24. @implementation KSSelectConversationViewController
  25. - (instancetype)initSelectConversationViewControllerCompleted:
  26. (void (^)(NSArray<RCConversation *> *conversationList))completedBlock {
  27. if (self = [super init]) {
  28. self.completeBlock = completedBlock;
  29. }
  30. return self;
  31. }
  32. - (void)viewDidLoad {
  33. [super viewDidLoad];
  34. [self setupNavi];
  35. self.view.backgroundColor = RCDYCOLOR(0xf0f0f6, 0x000000);
  36. [self.view addSubview:self.conversationTableView];
  37. self.selectedConversationArray = [[NSMutableArray alloc] init];
  38. NSMutableArray *listArray = [NSMutableArray array];
  39. NSArray *conversationArray = [[RCIMClient sharedRCIMClient] getConversationList:@[ @(ConversationType_PRIVATE), @(ConversationType_GROUP)]];
  40. for (RCConversation *conversation in conversationArray) {
  41. if (conversation.conversationType == ConversationType_GROUP && ![conversation.targetId containsString:@"FAN"] && ![conversation.targetId containsString:@"COURSE"]) { // 视频聊天群不添加
  42. }
  43. else {
  44. [listArray addObject:conversation];
  45. }
  46. }
  47. self.listingConversationArray = [NSArray arrayWithArray:listArray];
  48. // self.listingConversationArray =
  49. // [[RCIMClient sharedRCIMClient] getConversationList:@[ @(ConversationType_PRIVATE), @(ConversationType_GROUP) ]];
  50. // NSLog(@"--");
  51. }
  52. #pragma mark - UITableViewDataSource
  53. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  54. if (!self.listingConversationArray) {
  55. return 0;
  56. } else {
  57. return self.listingConversationArray.count;
  58. }
  59. }
  60. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  61. if (self.listingConversationArray.count <= indexPath.row) {
  62. return nil;
  63. }
  64. static NSString *reusableID = @"RCSelectConversationCell";
  65. RCSelectConversationCell *cell = [tableView dequeueReusableCellWithIdentifier:reusableID];
  66. if (!cell) {
  67. cell = [[RCSelectConversationCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reusableID];
  68. }
  69. RCConversation *conversation = self.listingConversationArray[indexPath.row];
  70. BOOL ifSelected = [self.selectedConversationArray containsObject:conversation];
  71. [cell setConversation:conversation ifSelected:ifSelected];
  72. return cell;
  73. }
  74. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  75. return 70;
  76. }
  77. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  78. if (indexPath.row >= self.listingConversationArray.count) {
  79. return;
  80. }
  81. NSString *userId = self.listingConversationArray[indexPath.row];
  82. if ([self.selectedConversationArray containsObject:userId]) {
  83. [self.selectedConversationArray removeObject:userId];
  84. } else if (userId) {
  85. [self.selectedConversationArray addObject:userId];
  86. }
  87. [self updateRightButton];
  88. [UIView performWithoutAnimation:^{
  89. [self.conversationTableView reloadRowsAtIndexPaths:@[ indexPath ] withRowAnimation:UITableViewRowAnimationNone];
  90. }];
  91. }
  92. #pragma mark - Target Action
  93. - (void)onLeftButtonClick:(id)sender {
  94. [self.navigationController popViewControllerAnimated:NO];
  95. }
  96. - (void)onRightButtonClick:(id)sender {
  97. if (!self.selectedConversationArray) {
  98. return;
  99. }
  100. if (self.completeBlock) {
  101. self.completeBlock(self.selectedConversationArray);
  102. }
  103. [self.navigationController popViewControllerAnimated:NO];
  104. }
  105. #pragma mark - Private Methods
  106. - (void)setupNavi {
  107. self.title = RCLocalizedString(@"SelectContact");
  108. UIBarButtonItem *leftBarItem =
  109. [[UIBarButtonItem alloc] initWithTitle:RCLocalizedString(@"Cancel")
  110. style:UIBarButtonItemStylePlain
  111. target:self
  112. action:@selector(onLeftButtonClick:)];
  113. leftBarItem.tintColor = RCKitConfigCenter.ui.globalNavigationBarTintColor;
  114. self.navigationItem.leftBarButtonItem = leftBarItem;
  115. self.rightBarButtonItem =
  116. [[UIBarButtonItem alloc] initWithTitle:RCLocalizedString(@"OK")
  117. style:UIBarButtonItemStylePlain
  118. target:self
  119. action:@selector(onRightButtonClick:)];
  120. self.rightBarButtonItem.tintColor = RCKitConfigCenter.ui.globalNavigationBarTintColor;
  121. self.navigationItem.rightBarButtonItem = self.rightBarButtonItem;
  122. [self updateRightButton];
  123. }
  124. - (void)updateRightButton {
  125. [self.rightBarButtonItem setEnabled:self.selectedConversationArray.count > 0];
  126. }
  127. #pragma mark - Getters and Setters
  128. - (UITableView *)conversationTableView {
  129. if (!_conversationTableView) {
  130. _conversationTableView = [[UITableView alloc] init];
  131. CGFloat navBarHeight = 64;
  132. CGFloat homeBarHeight = [RCKitUtility getWindowSafeAreaInsets].bottom;
  133. if (homeBarHeight > 0) {
  134. navBarHeight = 88;
  135. }
  136. _conversationTableView.frame =
  137. CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - navBarHeight - homeBarHeight);
  138. _conversationTableView.dataSource = self;
  139. _conversationTableView.delegate = self;
  140. _conversationTableView.backgroundColor = [UIColor clearColor];
  141. _conversationTableView.tableFooterView = [[UIView alloc] init];
  142. }
  143. return _conversationTableView;
  144. }
  145. /*
  146. #pragma mark - Navigation
  147. // In a storyboard-based application, you will often want to do a little preparation before navigation
  148. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  149. // Get the new view controller using [segue destinationViewController].
  150. // Pass the selected object to the new view controller.
  151. }
  152. */
  153. @end