WFCUFriendRequestViewController.m 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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.h"
  12. #import "WFCUFriendRequestTableViewCell.h"
  13. #import "MBProgressHUD.h"
  14. #import "WFCUAddFriendViewController.h"
  15. #import "UIView+Toast.h"
  16. @interface WFCUFriendRequestViewController () <UITableViewDataSource, UITableViewDelegate, WFCUFriendRequestTableViewCellDelegate>
  17. @property (nonatomic, strong) UITableView *tableView;
  18. @property (nonatomic, strong) NSArray *dataList;
  19. @end
  20. @implementation WFCUFriendRequestViewController
  21. - (void)viewDidLoad {
  22. [super viewDidLoad];
  23. [self initSearchUIAndData];
  24. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onUserInfoUpdated:) name:kUserInfoUpdated object:nil];
  25. [[WFCCIMService sharedWFCIMService] clearUnreadFriendRequestStatus];
  26. }
  27. - (void)onUserInfoUpdated:(NSNotification *)notification {
  28. WFCCUserInfo *userInfo = notification.userInfo[@"userInfo"];
  29. NSArray *dataSource = self.dataList;
  30. for (int i = 0; i < dataSource.count; i++) {
  31. WFCCFriendRequest *request = dataSource[i];
  32. if ([request.target isEqualToString:userInfo.userId]) {
  33. [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:i inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
  34. }
  35. }
  36. }
  37. - (void)viewWillDisappear:(BOOL)animated {
  38. [super viewWillDisappear:animated];
  39. [[WFCCIMService sharedWFCIMService] clearUnreadFriendRequestStatus];
  40. }
  41. - (void)initSearchUIAndData {
  42. self.view.backgroundColor = [UIColor whiteColor];
  43. self.navigationItem.title = @"新朋友";
  44. //初始化数据源
  45. [[WFCCIMService sharedWFCIMService] loadFriendRequestFromRemote];
  46. _dataList = [[WFCCIMService sharedWFCIMService] getIncommingFriendRequest];
  47. CGFloat screenWidth = self.view.frame.size.width;
  48. CGFloat screenHeight = self.view.frame.size.height;
  49. _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight)];
  50. //设置代理
  51. _tableView.delegate = self;
  52. _tableView.dataSource = self;
  53. _tableView.allowsSelection = YES;
  54. _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
  55. [self.view addSubview:_tableView];
  56. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"添加朋友" style:UIBarButtonItemStyleDone target:self action:@selector(onRightBarBtn:)];
  57. }
  58. - (void)onRightBarBtn:(UIBarButtonItem *)sender {
  59. UIViewController *addFriendVC = [[WFCUAddFriendViewController alloc] init];
  60. addFriendVC.hidesBottomBarWhenPushed = YES;
  61. [self.navigationController pushViewController:addFriendVC animated:YES];
  62. }
  63. - (void)viewWillAppear:(BOOL)animated {
  64. [super viewWillAppear:animated];
  65. self.tabBarController.tabBar.hidden = YES;
  66. }
  67. #pragma mark - UITableViewDataSource
  68. //table 返回的行数
  69. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  70. return [self.dataList count];
  71. }
  72. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  73. }
  74. //返回单元格内容
  75. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  76. static NSString *requestFlag = @"request_cell";
  77. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:requestFlag];
  78. if (cell == nil) {
  79. cell = [[WFCUFriendRequestTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:requestFlag];
  80. }
  81. WFCUFriendRequestTableViewCell *frCell = (WFCUFriendRequestTableViewCell *)cell;
  82. frCell.delegate = self;
  83. WFCCFriendRequest *request = self.dataList[indexPath.row];
  84. frCell.friendRequest = request;
  85. cell.userInteractionEnabled = YES;
  86. return cell;
  87. }
  88. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  89. return 56;
  90. }
  91. #pragma mark - FriendRequestTableViewCellDelegate
  92. - (void)onAcceptBtn:(NSString *)targetUserId {
  93. __block MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  94. hud.label.text = @"更新中...";
  95. [hud showAnimated:YES];
  96. __weak typeof(self) ws = self;
  97. [[WFCCIMService sharedWFCIMService] handleFriendRequest:targetUserId accept:YES success:^{
  98. dispatch_async(dispatch_get_main_queue(), ^{
  99. hud.hidden = YES;
  100. [ws.view makeToast:@"成功"
  101. duration:2
  102. position:CSToastPositionCenter];
  103. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  104. [[WFCCIMService sharedWFCIMService] loadFriendRequestFromRemote];
  105. dispatch_async(dispatch_get_main_queue(), ^{
  106. ws.dataList = [[WFCCIMService sharedWFCIMService] getIncommingFriendRequest];
  107. for (WFCCFriendRequest *request in ws.dataList) {
  108. if ([request.target isEqualToString:targetUserId]) {
  109. request.status = 1;
  110. break;
  111. }
  112. }
  113. [ws.tableView reloadData];
  114. });
  115. });
  116. });
  117. } error:^(int error_code) {
  118. dispatch_async(dispatch_get_main_queue(), ^{
  119. hud.hidden = YES;
  120. [ws.view makeToast:@"请求发送失败"
  121. duration:2
  122. position:CSToastPositionCenter];
  123. });
  124. }];
  125. }
  126. - (void)dealloc {
  127. [[NSNotificationCenter defaultCenter] removeObserver:self];
  128. _tableView = nil;
  129. _dataList = nil;
  130. }
  131. @end