FriendListController.m 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. //
  2. // FriendListController.m
  3. // AIIM
  4. //
  5. // Created by gan on 2025/4/6.
  6. //
  7. #import <Foundation/Foundation.h>
  8. #import "FriendListController.h"
  9. #import "FriendListCell.h"
  10. #import "FriendNetApi.h"
  11. #import "config.h"
  12. #import "FriendController.h"
  13. #import "EXFriendController.h"
  14. #import "addFriendController.h"
  15. @interface FriendListController()<UITableViewDelegate,UITableViewDataSource>
  16. @property (weak, nonatomic) IBOutlet UILabel *titleLb;
  17. @property (weak, nonatomic) IBOutlet UITableView *_tableView;
  18. @property (weak, nonatomic) IBOutlet UIView *topBview;
  19. @property (weak, nonatomic) IBOutlet UILabel *countLb;
  20. @property (nonatomic,strong) UIView *markView;
  21. @property (strong, nonatomic) NSTimer *timer;
  22. @end
  23. @implementation FriendListController
  24. -(void)viewDidLoad{
  25. [super viewDidLoad];
  26. _titleLb.text = NSLocalizedString(@"TabBarItem2", @"");
  27. self._tableView.delegate = self;
  28. self._tableView.dataSource = self;
  29. self._tableView.backgroundColor = [UIColor clearColor];
  30. [self._tableView registerClass:[FriendListCell class] forCellReuseIdentifier:@"friend"]; // 使用 Storyboard 时不需要这行代码,除非你混合使用 Storyboard 和代码配置 Cell。
  31. _topBview.backgroundColor=globalColor(GCTypeDark3);
  32. _topBview.layer.cornerRadius = 8.0f;
  33. _countLb.backgroundColor = globalColor(GCTypeOrangeR);
  34. _countLb.textColor = [UIColor whiteColor];
  35. _countLb.layer.cornerRadius = 15.0f; // 设置圆角
  36. _countLb.layer.masksToBounds = YES; // 防止子视图超出边界
  37. _countLb.alpha = 0;
  38. _markView=[[UIView alloc] init];
  39. [self.view addSubview:_markView];
  40. [self initpopView];
  41. }
  42. -(void)viewWillAppear:(BOOL)animated{
  43. [self getFriendlist];
  44. [self getexfriends];
  45. [self startTimer];
  46. // 获取TabBarController
  47. UITabBarController *tabBarController = (UITabBarController *)self.parentViewController;
  48. // 移除红点(使用系统方法)
  49. tabBarController.tabBar.items[1].badgeValue = nil;
  50. }
  51. -(void)viewWillDisappear:(BOOL)animated{
  52. [super viewWillDisappear:animated];
  53. [self endTimer];
  54. }
  55. #pragma mark 定时服务
  56. -(void)startTimer{
  57. if(self.timer==nil){
  58. self.timer = [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(TimerAction) userInfo:nil repeats:YES];
  59. [self.timer setFireDate:[NSDate distantPast]];
  60. [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
  61. }
  62. }
  63. -(void)endTimer{
  64. [self.timer invalidate];
  65. self.timer = nil;
  66. }
  67. -(void)TimerAction{
  68. //处理定时触发事件
  69. [self getexfriends];
  70. }
  71. -(void)getFriendlist{
  72. [FriendNetApi getfriends:nil succ:^(int code, NSDictionary * res) {
  73. NSLog(@"self.friendlist:%@",res);
  74. NSNumber *codeD = res[@"code"];
  75. if([codeD intValue]==401){
  76. [[NSNotificationCenter defaultCenter] postNotificationName: nkonLogoutSucc object:nil];
  77. return;
  78. }
  79. self.friendlist = res[@"data"];
  80. NSLog(@"self.friendlist:%@",self.friendlist);
  81. if(self.friendlist){
  82. [self._tableView reloadData];
  83. }
  84. } fail:^(NSError * _Nonnull error) {
  85. }];
  86. }
  87. -(void)getexfriends{
  88. [FriendNetApi getvalidateList:nil succ:^(int code, NSDictionary * res) {
  89. NSLog(@"getvalidateList res:%@",res);
  90. NSArray *array = res[@"data"];
  91. if(array.count>0){
  92. self.countLb.alpha = 1;
  93. self.countLb.text = [NSString stringWithFormat:@"%lu",(unsigned long)array.count];
  94. }
  95. else{
  96. self.countLb.alpha = 0;
  97. }
  98. } fail:^(NSError * _Nonnull error) {
  99. NSLog(@"error:%@",error);
  100. }];
  101. }
  102. -(void)initpopView{
  103. UITapGestureRecognizer *gotoex = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gotoexFriend)];
  104. // 将手势添加到主视图上
  105. [_topBview addGestureRecognizer:gotoex];
  106. }
  107. -(void)gotoexFriend{
  108. UIStoryboard *board = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
  109. EXFriendController *friendctr = [board instantiateViewControllerWithIdentifier:@"EXFriendController"];
  110. UINavigationController *uiNavC = [[UINavigationController alloc] initWithRootViewController:friendctr];
  111. uiNavC.modalPresentationStyle = UIModalPresentationFullScreen;
  112. [self presentViewController :uiNavC animated:YES completion:nil];
  113. }
  114. - (IBAction)addNewFriend:(id)sender {
  115. UIStoryboard *board = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
  116. addFriendController *addfriendctr = [board instantiateViewControllerWithIdentifier:@"addFriendController"];
  117. UINavigationController *uiNavC = [[UINavigationController alloc] initWithRootViewController:addfriendctr];
  118. uiNavC.modalPresentationStyle = UIModalPresentationFullScreen;
  119. [self presentViewController :uiNavC animated:YES completion:nil];
  120. // [self showpopView];
  121. }
  122. #pragma mark UITableViewDelegate
  123. -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
  124. return 1;
  125. }
  126. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  127. NSLog(@"numberOfRowsInSection:%lu",(unsigned long)self.friendlist.count);
  128. return self.friendlist.count;
  129. }
  130. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  131. NSDictionary *data=self.friendlist[indexPath.row];
  132. NSLog(@"cellForRowAtIndexPath:%@",data);
  133. if(data){
  134. FriendListCell *cell =[tableView dequeueReusableCellWithIdentifier:@"friend" forIndexPath:indexPath];
  135. if(cell==nil){
  136. cell=[[FriendListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"friend"];
  137. }
  138. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  139. cell.backgroundColor = [UIColor clearColor];
  140. [cell fillWithData:data];
  141. return cell;
  142. }
  143. return nil;
  144. }
  145. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  146. return 70;
  147. }
  148. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  149. NSLog(@"didSelectRowAtIndexPath:%@",indexPath);
  150. NSDictionary *data=self.friendlist[indexPath.row];
  151. UIStoryboard *board = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
  152. FriendController *friendctr = [board instantiateViewControllerWithIdentifier:@"FriendController"];
  153. UINavigationController *uiNavC = [[UINavigationController alloc] initWithRootViewController:friendctr];
  154. uiNavC.modalPresentationStyle = UIModalPresentationFullScreen;
  155. friendctr.friendMsg = data;
  156. [self presentViewController :uiNavC animated:YES completion:nil];
  157. }
  158. -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
  159. return UITableViewCellEditingStyleDelete;
  160. }
  161. -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
  162. if (editingStyle == UITableViewCellEditingStyleDelete) {
  163. // 从数据源中移除数据项
  164. NSLog(@"indexPath:%ld",(long)indexPath.row);
  165. [self deleteFriend:indexPath.row];
  166. // 更新表格视图
  167. //[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
  168. }
  169. NSLog(@"1111111");
  170. }
  171. -(void)deleteFriend:(NSInteger)index{
  172. if(self.friendlist.count>index){
  173. NSDictionary *dic = self.friendlist[index];
  174. [FriendNetApi deletefriend:dic[@"id"] succ:^(int code, NSDictionary * res) {
  175. NSMutableArray *tempArray = [NSMutableArray arrayWithArray:self.friendlist];
  176. [tempArray removeObjectAtIndex:index];
  177. self.friendlist = tempArray;
  178. [self._tableView reloadData];
  179. } fail:^(NSError * _Nonnull error) {
  180. ;
  181. }];
  182. }
  183. }
  184. @end