2
0

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