ChatIndexController.m 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. //
  2. // ChatIndexController.m
  3. // AIIM
  4. //
  5. // Created by gan on 2025/4/6.
  6. //
  7. #import <Foundation/Foundation.h>
  8. #import "ChatIndexController.h"
  9. #import "ChatListCell.h"
  10. #import "ChatListStore.h"
  11. #import "ChatController.h"
  12. #import "ChatNetApi.h"
  13. #import <Bugly/Bugly.h>
  14. @interface ChatIndexController()<UITableViewDelegate,UITableViewDataSource,ChatListStoreDelegate>
  15. @property (weak, nonatomic) IBOutlet UITableView *_tableView;
  16. @end
  17. @implementation ChatIndexController
  18. -(void)viewDidLoad{
  19. [super viewDidLoad];
  20. NSLog(@"ChatIndexController viewDidLoad");
  21. self._tableView.delegate = self;
  22. self._tableView.dataSource = self;
  23. self._tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  24. self._tableView.backgroundColor = [UIColor clearColor];
  25. UINib *nib = [UINib nibWithNibName:@"ChatListCell" bundle:nil];
  26. [self._tableView registerNib:nib forCellReuseIdentifier:@"chat"];
  27. //[self._tableView registerClass:[ChatListCell class] forCellReuseIdentifier:@"chat"]; // 使用 Storyboard 时不需要这行代码,除非你混合使用 Storyboard 和代码配置 Cell。
  28. // ChatListStore.shareInstance.delegate = self;
  29. //[ChatListStore.shareInstance reloadData:YES];
  30. }
  31. -(void)viewWillAppear:(BOOL)animated{
  32. [super viewWillAppear:animated];
  33. NSLog(@"ChatIndexController viewWillAppear");
  34. [self reloadList];
  35. }
  36. -(void)viewDidAppear:(BOOL)animated{
  37. [super viewDidAppear:animated];
  38. NSLog(@"ChatIndexController viewDidAppear");
  39. // NSDictionary *userinfo = [UDManager.shareInstance getDDManager:dkuserinfo];
  40. // [Bugly reportException:[NSException exceptionWithName:@"ChatIndexController viewDidAppear" reason:[NSString stringWithFormat:@"数量:%@",userinfo[@"name"]] userInfo:nil]];
  41. }
  42. -(void)reloadList{
  43. ChatListStore.shareInstance.delegate = self;
  44. ChatListStore.shareInstance.chatId =@"";
  45. [ChatListStore.shareInstance reloadData:true];
  46. NSLog(@"ChatIndexController reloadList");
  47. }
  48. -(void)viewDidDisappear:(BOOL)animated{
  49. [super viewDidDisappear:animated];
  50. NSLog(@"ChatIndexController viewDidDisappear");
  51. ChatListStore.shareInstance.delegate = nil;
  52. }
  53. #pragma mark ChatListStoreDelegate
  54. -(void)ChatListChange:(NSArray *)array{
  55. self.chatlist = [NSMutableArray arrayWithArray:array];
  56. NSLog(@"self.chatlist:%@",self.chatlist);
  57. [self._tableView reloadData];
  58. }
  59. #pragma mark UITableViewDelegate
  60. -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
  61. return 1;
  62. }
  63. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  64. return self.chatlist.count;
  65. }
  66. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  67. NSDictionary *data=self.chatlist[indexPath.row];
  68. if(data){
  69. ChatListCell *cell =[tableView dequeueReusableCellWithIdentifier:@"chat" forIndexPath:indexPath];
  70. if(cell==nil){
  71. cell=[[ChatListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"chat"];
  72. }
  73. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  74. cell.backgroundColor = [UIColor clearColor];
  75. [cell fillWithData:data];
  76. return cell;
  77. }
  78. return nil;
  79. }
  80. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  81. return 80;
  82. }
  83. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  84. NSDictionary *data=self.chatlist[indexPath.row];
  85. UIStoryboard *board = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
  86. ChatController *chatctr = [board instantiateViewControllerWithIdentifier:@"ChatController"];
  87. UINavigationController *uiNavC = [[UINavigationController alloc] initWithRootViewController:chatctr];
  88. uiNavC.modalPresentationStyle = UIModalPresentationFullScreen;
  89. //[self.navigationController pushViewController:chatctr animated:YES];
  90. NSString *strtype = data[@"type"];
  91. chatctr.chatId = data[@"id"];
  92. chatctr.type = strtype.intValue;
  93. ChatListStore.shareInstance.chatId =data[@"id"];
  94. chatctr.titlename = data[@"name"];
  95. chatctr.avatar = data[@"avatar"];
  96. //chatctr.modalPresentationStyle = UIModalPresentationFullScreen;
  97. // 打开新页面为模态形式
  98. //[self presentViewController:chatctr animated:YES completion:nil];
  99. [self presentViewController :uiNavC animated:YES completion:nil];
  100. }
  101. - (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath {
  102. weakSelf(self);
  103. NSDictionary *data=self.chatlist[indexPath.row];
  104. BOOL istopMessage = NO;
  105. if ([data jk_hasKey:@"top"]) {
  106. istopMessage = [data[@"top"] boolValue];
  107. }
  108. NSString * chatId = data[@"id"];
  109. // // 创建置顶操作
  110. UIContextualAction * topAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleNormal title:istopMessage?@"取消置顶":@"置顶" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
  111. if (istopMessage) {//取消置顶聊天
  112. [weakself cancelTopChatWithId:chatId];
  113. [ChatListStore.shareInstance cancelTopChat:data];
  114. }else{//置顶聊天
  115. [weakself topChatWithId:chatId];
  116. [ChatListStore.shareInstance topChat:data];
  117. }
  118. completionHandler(YES);
  119. }];
  120. topAction.backgroundColor = UIColor.orangeColor;
  121. // 创建删除操作
  122. UIContextualAction *deleteAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive
  123. title:@"删除"
  124. handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
  125. // 删除聊天
  126. [weakself deleteChatWith:chatId];
  127. // 更新数据
  128. [ChatListStore.shareInstance deleteChat:data];
  129. [weakself.chatlist removeObject:data];
  130. [weakself._tableView reloadData];
  131. // 告诉系统操作已完成
  132. completionHandler(YES);
  133. }];
  134. // 设置删除按钮颜色
  135. deleteAction.backgroundColor = [UIColor redColor];
  136. // 创建配置并返回
  137. UISwipeActionsConfiguration *config = [UISwipeActionsConfiguration configurationWithActions:@[topAction,deleteAction]];
  138. config.performsFirstActionWithFullSwipe = YES; // 完全滑动直接执行删除
  139. return config;
  140. }
  141. - (void)deleteChatWith:(NSString *)chatId{
  142. [ChatNetApi removechat:chatId succ:^(int code, NSDictionary * _Nullable result) {
  143. if (result[@"msg"]) {
  144. [MBProgressHUD showWithText:result[@"msg"]];
  145. }
  146. } fail:^(NSError * _Nonnull error) {
  147. }];
  148. }
  149. #pragma mark api
  150. - (void)topChatWithId:(NSString *)chatId{
  151. [ChatNetApi settopchats:chatId succ:^(int code, NSDictionary * _Nullable result) {
  152. if (result[@"msg"]) {
  153. [MBProgressHUD showWithText:result[@"msg"]];
  154. // 更新数据
  155. // [ChatListStore.shareInstance reloadData:YES];
  156. }
  157. } fail:^(NSError * _Nonnull error) {
  158. }];
  159. }
  160. - (void)cancelTopChatWithId:(NSString *)chatId{
  161. [ChatNetApi canceltopchat:chatId succ:^(int code, NSDictionary * _Nullable result) {
  162. if (result[@"msg"]) {
  163. [MBProgressHUD showWithText:result[@"msg"]];
  164. // 更新数据
  165. // [ChatListStore.shareInstance reloadData:YES];
  166. }
  167. } fail:^(NSError * _Nonnull error) {
  168. }];
  169. }
  170. @end