WFCUMentionUserTableViewController.m 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. //
  2. // WFCUMentionUserTableViewController.m
  3. // WFChatUIKit
  4. //
  5. // Created by WF Chat on 2018/10/24.
  6. // Copyright © 2018 WF Chat. All rights reserved.
  7. //
  8. #import "WFCUMentionUserTableViewController.h"
  9. #import <WFChatClient/WFCChatClient.h>
  10. #import "SDWebImage.h"
  11. #import "WFCUContactTableViewCell.h"
  12. @interface WFCUMentionUserTableViewController () <UITableViewDelegate, UITableViewDataSource>
  13. @property (nonatomic, strong)UITableView *tableView;
  14. @property (nonatomic, strong)NSMutableArray<WFCCGroupMember *> *groupMembers;
  15. @property (nonatomic, strong)WFCCGroupMember *selectedMember;
  16. @end
  17. @implementation WFCUMentionUserTableViewController
  18. - (void)viewDidLoad {
  19. [super viewDidLoad];
  20. self.groupMembers = [[[WFCCIMService sharedWFCIMService] getGroupMembers:self.groupId forceUpdate:NO] mutableCopy];
  21. [self.groupMembers enumerateObjectsUsingBlock:^(WFCCGroupMember * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  22. if ([obj.memberId isEqualToString:[WFCCNetworkService sharedInstance].userId]) {
  23. [self.groupMembers removeObjectAtIndex:idx];
  24. *stop = YES;
  25. }
  26. }];
  27. self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
  28. self.tableView.delegate = self;
  29. self.tableView.dataSource = self;
  30. self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
  31. [self.view addSubview:self.tableView];
  32. [self.tableView reloadData];
  33. self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStyleDone target:self action:@selector(onCancel:)];
  34. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonItemStyleDone target:self action:@selector(onDone:)];
  35. // Uncomment the following line to preserve selection between presentations.
  36. // self.clearsSelectionOnViewWillAppear = NO;
  37. // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
  38. // self.navigationItem.rightBarButtonItem = self.editButtonItem;
  39. }
  40. - (void)onCancel:(id)sender {
  41. [self.delegate didCancelMentionAtRange:self.range];
  42. [self dismissViewControllerAnimated:YES completion:nil];
  43. }
  44. - (void)onDone:(id)sender {
  45. if (self.selectedMember) {
  46. NSString *name = self.selectedMember.alias;
  47. if (!name.length) {
  48. WFCCUserInfo *userInfo = [[WFCCIMService sharedWFCIMService] getUserInfo:self.selectedMember.memberId refresh:NO];
  49. name = userInfo.displayName;
  50. }
  51. NSString *text = [NSString stringWithFormat:@"@%@ ", name];
  52. self.range = NSMakeRange(self.range.location, text.length);
  53. [self.delegate didMentionType:1 user:self.selectedMember.memberId range:self.range text:text];
  54. [self dismissViewControllerAnimated:YES completion:nil];
  55. } else {
  56. [self onCancel:sender];
  57. }
  58. }
  59. #pragma mark - Table view data source
  60. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  61. return self.groupMembers.count;
  62. }
  63. #define REUSEIDENTIFY @"reuseIdentifier"
  64. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  65. WFCUContactTableViewCell *contactCell = [tableView dequeueReusableCellWithIdentifier:REUSEIDENTIFY];
  66. if (contactCell == nil) {
  67. contactCell = [[WFCUContactTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:REUSEIDENTIFY];
  68. contactCell.big = YES;
  69. }
  70. WFCCGroupMember *member = [self.groupMembers objectAtIndex:(indexPath.row)];
  71. if (member.alias.length) {
  72. contactCell.alterName = member.alias;
  73. }
  74. contactCell.userId = member.memberId;
  75. return contactCell;
  76. }
  77. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  78. self.selectedMember = [self.groupMembers objectAtIndex:indexPath.row];
  79. [self onDone:nil];
  80. }
  81. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  82. return 64.f;
  83. }
  84. /*
  85. // Override to support conditional editing of the table view.
  86. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
  87. // Return NO if you do not want the specified item to be editable.
  88. return YES;
  89. }
  90. */
  91. /*
  92. // Override to support editing the table view.
  93. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
  94. if (editingStyle == UITableViewCellEditingStyleDelete) {
  95. // Delete the row from the data source
  96. [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
  97. } else if (editingStyle == UITableViewCellEditingStyleInsert) {
  98. // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
  99. }
  100. }
  101. */
  102. /*
  103. // Override to support rearranging the table view.
  104. - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
  105. }
  106. */
  107. /*
  108. // Override to support conditional rearranging of the table view.
  109. - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
  110. // Return NO if you do not want the item to be re-orderable.
  111. return YES;
  112. }
  113. */
  114. /*
  115. #pragma mark - Navigation
  116. // In a storyboard-based application, you will often want to do a little preparation before navigation
  117. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  118. // Get the new view controller using [segue destinationViewController].
  119. // Pass the selected object to the new view controller.
  120. }
  121. */
  122. @end