123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 |
- //
- // FriendListViewController.m
- // NIMDemo
- //
- // Created by Fenix Wang on 2017/7/29.
- // Copyright © 2017年 Netease. All rights reserved.
- //
- #import "FriendListViewController.h"
- #import "User.h"
- #import "FriendListTableViewCell.h"
- #import "NTESContactUtilItem.h"
- #import "NTESContactDataMember.h"
- #import "NTESContactUtilCell.h"
- #import "NTESContactDataCell.h"
- #import "UIView+Toast.h"
- #import "ContactViewController.h"
- #import "HttpRequest.h"
- @implementation GroupedContacts
- - (instancetype)initWithMembers:(NSMutableArray *)members
- {
- self = [super init];
- if(self) {
- self.groupTitleComparator = ^NSComparisonResult(NSString *title1, NSString *title2) {
- if ([title1 isEqualToString:@"#"]) {
- return NSOrderedDescending;
- }
- if ([title2 isEqualToString:@"#"]) {
- return NSOrderedAscending;
- }
- return [title1 compare:title2];
- };
- self.groupMemberComparator = ^NSComparisonResult(NSString *key1, NSString *key2) {
- return [key1 compare:key2];
- };
- [self setMembers:members];
- }
- return self;
- }
- @end
- @interface FriendListViewController ()<NTESContactUtilCellDelegate,
- NIMContactDataCellDelegate>
- @property (nonatomic, strong) GroupedContacts *contacts;
- @property (nonatomic, strong) NSMutableArray *requestArr;
- @end
- @implementation FriendListViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
-
- __weak typeof(self) wself = self;
- [[User sharedInfo] requestFollowList:^{
-
- [[HttpRequest shared] requestFriendList:^(NSMutableArray<FriendRequestData *> * _Nullable dataArr) {
-
- wself.requestArr = dataArr;
- [wself fetchUserInfos];
-
- } failure:^{
- [wself fetchUserInfos];
- }];
-
- }];
- }
- - (void)fetchUserInfos{
- __weak typeof(self) wself = self;
- NSArray *followList = [[User sharedInfo] followList];
- [[NIMSDK sharedSDK].userManager fetchUserInfos:followList completion:^(NSArray<NIMUser *> * _Nullable users, NSError * _Nullable error) {
- [wself prepareData];
- }];
- }
- - (void)prepareData{
-
- NSMutableArray *infoArr = [NSMutableArray array];
- for (NSString *userId in [[User sharedInfo] followList]) {
- NIMKitInfo *info = [[NIMKit sharedKit] infoByUser:userId option:nil];
- NTESContactDataMember *contact = [[NTESContactDataMember alloc] init];
- contact.info = info;
- [infoArr addObject:contact];
- }
-
- _contacts = [[GroupedContacts alloc] initWithMembers:infoArr];
-
- NSString *contactCellUtilIcon = @"icon";
- NSString *contactCellUtilVC = @"vc";
- NSString *contactCellUtilStory = @"story";
- NSString *contactCellUtilBadge = @"badge";
- NSString *contactCellUtilTitle = @"title";
- NSString *contactCellUtilUid = @"uid";
- NSString *contactCellUtilSelectorName = @"selName";
-
- NSMutableArray *utils =
- [@[
- @{
- contactCellUtilIcon:@"通讯录_03",
- contactCellUtilTitle:@"新的朋友",
- contactCellUtilStory:@"FriendRequest",
- contactCellUtilBadge:@(_requestArr.count)
- },
- @{
- contactCellUtilIcon:@"通讯录_06",
- contactCellUtilTitle:@"手机联系人",
- contactCellUtilStory:@"Contact"
- },
- @{
- contactCellUtilIcon:@"通讯录_08",
- contactCellUtilTitle:@"群组",
- contactCellUtilVC:@"NTESAdvancedTeamListViewController"
- },
- ] mutableCopy];
-
-
- //构造显示的数据模型
- NTESContactUtilItem *contactUtil = [[NTESContactUtilItem alloc] init];
- NSMutableArray * members = [[NSMutableArray alloc] init];
- for (NSDictionary *item in utils) {
- NTESContactUtilMember *utilItem = [[NTESContactUtilMember alloc] init];
- utilItem.nick = item[contactCellUtilTitle];
- utilItem.icon = [UIImage imageNamed:item[contactCellUtilIcon]];
- utilItem.vcName = item[contactCellUtilVC];
- utilItem.badge = [item[contactCellUtilBadge] stringValue];
- utilItem.userId = item[contactCellUtilUid];
- utilItem.selName = item[contactCellUtilSelectorName];
- utilItem.storyboardId = item[contactCellUtilStory];
- [members addObject:utilItem];
- }
- contactUtil.members = members;
-
- [_contacts addGroupAboveWithTitle:@"" members:contactUtil.members];
-
- [self.tableView reloadData];
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- - (void)viewWillAppear:(BOOL)animated{
- [super viewWillAppear:animated];
- [self.navigationItem setTitle:@"通讯录"];
- }
- - (void)viewDidAppear:(BOOL)animated{
- [super viewDidAppear:animated];
- [self prepareData];
- }
- #pragma mark - Table view data source
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
- return [_contacts memberCountOfGroup:section];
- }
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
- return [_contacts groupCount];
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- id contactItem = [_contacts memberOfIndex:indexPath];
- NSString * cellId = [contactItem reuseId];
- UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellId];
- if (!cell) {
- Class cellClazz = NSClassFromString([contactItem cellName]);
- cell = [[cellClazz alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
- }
- if ([contactItem showAccessoryView]) {
- cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
- }else{
- cell.accessoryType = UITableViewCellAccessoryNone;
- }
- if ([cell isKindOfClass:[NTESContactUtilCell class]]) {
- [(NTESContactUtilCell *)cell refreshWithContactItem:contactItem];
- [(NTESContactUtilCell *)cell setDelegate:self];
- }else{
- [(NTESContactDataCell *)cell refreshUser:contactItem];
- [(NTESContactDataCell *)cell setDelegate:self];
- }
- return cell;
- }
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
- [tableView deselectRowAtIndexPath:indexPath animated:YES];
- id<NTESContactItem> contactItem = (id<NTESContactItem>)[_contacts memberOfIndex:indexPath];
- if ([contactItem respondsToSelector:@selector(selName)] && [contactItem selName].length) {
- SEL sel = NSSelectorFromString([contactItem selName]);
- SuppressPerformSelectorLeakWarning([self performSelector:sel withObject:nil]);
- }
- else if (contactItem.vcName.length) {
- Class clazz = NSClassFromString(contactItem.vcName);
- UIViewController * vc = [[clazz alloc] initWithNibName:nil bundle:nil];
- [self.navigationController pushViewController:vc animated:YES];
- }
- else if ([contactItem isKindOfClass:[NTESContactUtilMember class]] && ((NTESContactUtilMember *)contactItem).storyboardId.length > 0){
- UIStoryboard *board = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
- UIViewController *vc = [board instantiateViewControllerWithIdentifier:((NTESContactUtilMember *)contactItem).storyboardId];
- if([vc isKindOfClass:[FriendRequestViewController class]])
- {
- [((FriendRequestViewController *)vc) setDataArr:_requestArr];
- }
- [self.navigationController pushViewController:vc animated:YES];
- }
- else if([contactItem respondsToSelector:@selector(userId)]){
- NSString * friendId = contactItem.userId;
- [self onPressAvatar:friendId];
- }
-
- }
- - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
- id<NTESContactItem> contactItem = (id<NTESContactItem>)[_contacts memberOfIndex:indexPath];
- return contactItem.uiHeight;
- }
- - (NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
- return [_contacts titleOfGroup:section];
- }
- - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
- return _contacts.sortedGroupTitles;
- }
- - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
- return index + 1;
- }
- #pragma mark - NIMContactDataCellDelegate
- - (void)onPressAvatar:(NSString *)memberId{
- [User showUserInfo:memberId viewController:self];
- }
- #pragma mark - NTESContactUtilCellDelegate
- - (void)onPressUtilImage:(NSString *)content{
- [self.view makeToast:[NSString stringWithFormat:@"点我干嘛 我是<%@>",content] duration:2.0 position:CSToastPositionCenter];
- }
- @end
|