FriendListController.m 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. #import "AppDelegate.h"
  16. @interface FriendListController()<UITableViewDelegate,UITableViewDataSource>
  17. @property (weak, nonatomic) IBOutlet UILabel *titleLb;
  18. @property (weak, nonatomic) IBOutlet UITableView *_tableView;
  19. @property (weak, nonatomic) IBOutlet UIView *topBview;
  20. @property (weak, nonatomic) IBOutlet UILabel *countLb;
  21. @property (nonatomic,strong) UIView *markView;
  22. @property (strong, nonatomic) NSTimer *timer;
  23. @end
  24. @implementation FriendListController
  25. - (UIStatusBarStyle)preferredStatusBarStyle {
  26. return UIStatusBarStyleLightContent;
  27. }
  28. -(void)viewDidLoad{
  29. [super viewDidLoad];
  30. _titleLb.text = NSLocalizedString(@"TabBarItem2", @"");
  31. self._tableView.delegate = self;
  32. self._tableView.dataSource = self;
  33. self._tableView.backgroundColor = [UIColor clearColor];
  34. [self._tableView registerClass:[FriendListCell class] forCellReuseIdentifier:@"friend"]; // 使用 Storyboard 时不需要这行代码,除非你混合使用 Storyboard 和代码配置 Cell。
  35. _topBview.backgroundColor=globalColor(GCTypeDark3);
  36. _topBview.layer.cornerRadius = 8.0f;
  37. _countLb.backgroundColor = globalColor(GCTypeOrangeR);
  38. _countLb.textColor = [UIColor whiteColor];
  39. _countLb.layer.cornerRadius = 15.0f; // 设置圆角
  40. _countLb.layer.masksToBounds = YES; // 防止子视图超出边界
  41. _countLb.alpha = 0;
  42. _markView=[[UIView alloc] init];
  43. [self.view addSubview:_markView];
  44. [self initpopView];
  45. }
  46. -(void)viewWillAppear:(BOOL)animated{
  47. [self getFriendlist];
  48. [self getexfriends];
  49. [self startTimer];
  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. AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
  114. [app resetAddFriendBadgeCount];
  115. }
  116. - (IBAction)addNewFriend:(id)sender {
  117. UIStoryboard *board = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
  118. addFriendController *addfriendctr = [board instantiateViewControllerWithIdentifier:@"addFriendController"];
  119. UINavigationController *uiNavC = [[UINavigationController alloc] initWithRootViewController:addfriendctr];
  120. uiNavC.modalPresentationStyle = UIModalPresentationFullScreen;
  121. [self presentViewController :uiNavC animated:YES completion:nil];
  122. // [self showpopView];
  123. }
  124. #pragma mark UITableViewDelegate
  125. -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
  126. return 1;
  127. }
  128. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  129. NSLog(@"numberOfRowsInSection:%lu",(unsigned long)self.friendlist.count);
  130. return self.friendlist.count;
  131. }
  132. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  133. NSDictionary *data=self.friendlist[indexPath.row];
  134. NSLog(@"cellForRowAtIndexPath:%@",data);
  135. if(data){
  136. FriendListCell *cell =[tableView dequeueReusableCellWithIdentifier:@"friend" forIndexPath:indexPath];
  137. if(cell==nil){
  138. cell=[[FriendListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"friend"];
  139. }
  140. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  141. cell.backgroundColor = [UIColor clearColor];
  142. [cell fillWithData:data];
  143. return cell;
  144. }
  145. return nil;
  146. }
  147. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  148. return 70;
  149. }
  150. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  151. NSLog(@"didSelectRowAtIndexPath:%@",indexPath);
  152. NSDictionary *data=self.friendlist[indexPath.row];
  153. UIStoryboard *board = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
  154. FriendController *friendctr = [board instantiateViewControllerWithIdentifier:@"FriendController"];
  155. UINavigationController *uiNavC = [[UINavigationController alloc] initWithRootViewController:friendctr];
  156. uiNavC.modalPresentationStyle = UIModalPresentationFullScreen;
  157. friendctr.friendMsg = data;
  158. [self presentViewController :uiNavC animated:YES completion:nil];
  159. }
  160. -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
  161. return UITableViewCellEditingStyleDelete;
  162. }
  163. -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
  164. if (editingStyle == UITableViewCellEditingStyleDelete) {
  165. // 从数据源中移除数据项
  166. NSLog(@"indexPath:%ld",(long)indexPath.row);
  167. [self deleteFriend:indexPath.row];
  168. // 更新表格视图
  169. //[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
  170. }
  171. NSLog(@"1111111");
  172. }
  173. -(void)deleteFriend:(NSInteger)index{
  174. if(self.friendlist.count>index){
  175. NSDictionary *dic = self.friendlist[index];
  176. [FriendNetApi deletefriend:dic[@"id"] succ:^(int code, NSDictionary * res) {
  177. NSMutableArray *tempArray = [NSMutableArray arrayWithArray:self.friendlist];
  178. [tempArray removeObjectAtIndex:index];
  179. self.friendlist = tempArray;
  180. [self._tableView reloadData];
  181. } fail:^(NSError * _Nonnull error) {
  182. ;
  183. }];
  184. }
  185. }
  186. @end