2
0

WFCUFriendRequestViewController.m 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. //
  2. // FriendRequestViewController.m
  3. // WFChat UIKit
  4. //
  5. // Created by WF Chat on 2017/10/7.
  6. // Copyright © 2017年 WildFireChat. All rights reserved.
  7. //
  8. #import "WFCUFriendRequestViewController.h"
  9. #import <WFChatClient/WFCChatClient.h>
  10. #import "WFCUProfileTableViewController.h"
  11. #import <SDWebImage/SDWebImage.h>
  12. #import "WFCUFriendRequestTableViewCell.h"
  13. #import "MBProgressHUD.h"
  14. #import "WFCUAddFriendViewController.h"
  15. #import "UIView+Toast.h"
  16. #import "WFCUConfigManager.h"
  17. @interface WFCUFriendRequestViewController () <UITableViewDataSource, UITableViewDelegate, WFCUFriendRequestTableViewCellDelegate>
  18. @property (nonatomic, strong) UITableView *tableView;
  19. @property (nonatomic, strong) NSArray *dataList;
  20. @end
  21. @implementation WFCUFriendRequestViewController
  22. - (void)viewDidLoad {
  23. [super viewDidLoad];
  24. [self initSearchUIAndData];
  25. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onUserInfoUpdated:) name:kUserInfoUpdated object:nil];
  26. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onFriendRequestUpdated:) name:kFriendRequestUpdated object:nil];
  27. [[WFCCIMService sharedWFCIMService] clearUnreadFriendRequestStatus];
  28. }
  29. - (void)onUserInfoUpdated:(NSNotification *)notification {
  30. NSArray<WFCCUserInfo *> *userInfoList = notification.userInfo[@"userInfoList"];
  31. for (int i = 0; i < self.dataList.count; ++i) {
  32. WFCCFriendRequest *request = self.dataList[i];
  33. for (WFCCUserInfo *userInfo in userInfoList) {
  34. if([userInfo.userId isEqualToString:request.target]) {
  35. [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:i inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
  36. }
  37. }
  38. }
  39. }
  40. - (void)onFriendRequestUpdated:(NSNotification *)notification {
  41. self.dataList = [[WFCCIMService sharedWFCIMService] getIncommingFriendRequest];
  42. [self.tableView reloadData];
  43. }
  44. - (void)viewWillDisappear:(BOOL)animated {
  45. [super viewWillDisappear:animated];
  46. [[WFCCIMService sharedWFCIMService] clearUnreadFriendRequestStatus];
  47. }
  48. - (void)initSearchUIAndData {
  49. self.view.backgroundColor = [WFCUConfigManager globalManager].backgroudColor;
  50. self.navigationItem.title = WFCString(@"NewFriend");
  51. //初始化数据源
  52. [[WFCCIMService sharedWFCIMService] loadFriendRequestFromRemote];
  53. _dataList = [[WFCCIMService sharedWFCIMService] getIncommingFriendRequest];
  54. CGFloat screenWidth = self.view.frame.size.width;
  55. CGFloat screenHeight = self.view.frame.size.height;
  56. _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight)];
  57. //设置代理
  58. _tableView.delegate = self;
  59. _tableView.dataSource = self;
  60. _tableView.allowsSelection = YES;
  61. _tableView.backgroundColor = [WFCUConfigManager globalManager].backgroudColor;
  62. _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
  63. [self.view addSubview:_tableView];
  64. self.navigationItem.rightBarButtonItems = @[[[UIBarButtonItem alloc] initWithTitle:WFCString(@"Clear") style:UIBarButtonItemStyleDone target:self action:@selector(onClearBarBtn:)], [[UIBarButtonItem alloc] initWithTitle:WFCString(@"Add") style:UIBarButtonItemStyleDone target:self action:@selector(onAddBarBtn:)]];
  65. }
  66. - (void)onAddBarBtn:(UIBarButtonItem *)sender {
  67. UIViewController *addFriendVC = [[WFCUAddFriendViewController alloc] init];
  68. addFriendVC.hidesBottomBarWhenPushed = YES;
  69. [self.navigationController pushViewController:addFriendVC animated:YES];
  70. }
  71. - (void)onClearBarBtn:(UIBarButtonItem *)sender {
  72. [[WFCCIMService sharedWFCIMService] clearFriendRequest:1 beforeTime:0];
  73. _dataList = [[WFCCIMService sharedWFCIMService] getIncommingFriendRequest];
  74. [self.tableView reloadData];
  75. }
  76. - (void)viewWillAppear:(BOOL)animated {
  77. [super viewWillAppear:animated];
  78. self.tabBarController.tabBar.hidden = YES;
  79. }
  80. #pragma mark - UITableViewDataSource
  81. //table 返回的行数
  82. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  83. return [self.dataList count];
  84. }
  85. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  86. }
  87. //返回单元格内容
  88. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  89. static NSString *requestFlag = @"request_cell";
  90. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:requestFlag];
  91. if (cell == nil) {
  92. cell = [[WFCUFriendRequestTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:requestFlag];
  93. }
  94. WFCUFriendRequestTableViewCell *frCell = (WFCUFriendRequestTableViewCell *)cell;
  95. frCell.delegate = self;
  96. WFCCFriendRequest *request = self.dataList[indexPath.row];
  97. frCell.friendRequest = request;
  98. cell.userInteractionEnabled = YES;
  99. return cell;
  100. }
  101. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
  102. if (editingStyle == UITableViewCellEditingStyleDelete) {
  103. WFCCFriendRequest *request = self.dataList[indexPath.row];
  104. [[WFCCIMService sharedWFCIMService] deleteFriendRequest:request.target direction:request.direction];
  105. _dataList = [[WFCCIMService sharedWFCIMService] getIncommingFriendRequest];
  106. [self.tableView reloadData];
  107. }
  108. }
  109. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  110. return 60;
  111. }
  112. #pragma mark - FriendRequestTableViewCellDelegate
  113. - (void)onAcceptBtn:(NSString *)targetUserId {
  114. __block MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  115. hud.label.text = WFCString(@"Updating");
  116. [hud showAnimated:YES];
  117. __weak typeof(self) ws = self;
  118. [[WFCCIMService sharedWFCIMService] handleFriendRequest:targetUserId accept:YES extra:nil success:^{
  119. dispatch_async(dispatch_get_main_queue(), ^{
  120. hud.hidden = YES;
  121. [ws.view makeToast:WFCString(@"UpdateDone")
  122. duration:2
  123. position:CSToastPositionCenter];
  124. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  125. [[WFCCIMService sharedWFCIMService] loadFriendRequestFromRemote];
  126. dispatch_async(dispatch_get_main_queue(), ^{
  127. ws.dataList = [[WFCCIMService sharedWFCIMService] getIncommingFriendRequest];
  128. for (WFCCFriendRequest *request in ws.dataList) {
  129. if ([request.target isEqualToString:targetUserId]) {
  130. request.status = 1;
  131. break;
  132. }
  133. }
  134. [ws.tableView reloadData];
  135. });
  136. });
  137. });
  138. } error:^(int error_code) {
  139. dispatch_async(dispatch_get_main_queue(), ^{
  140. hud.hidden = YES;
  141. if(error_code == 19) {
  142. [ws.view makeToast:WFCString(@"Expired")
  143. duration:2
  144. position:CSToastPositionCenter];
  145. } else {
  146. [ws.view makeToast:WFCString(@"UpdateFailure")
  147. duration:2
  148. position:CSToastPositionCenter];
  149. }
  150. });
  151. }];
  152. }
  153. - (void)dealloc {
  154. [[NSNotificationCenter defaultCenter] removeObserver:self];
  155. _tableView = nil;
  156. _dataList = nil;
  157. }
  158. @end