ChatIndexController.m 7.7 KB

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