ChatIndexController.m 7.7 KB

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