| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549 |
- //
- // FriendTableViewController.m
- // NIMDemo
- //
- // Created by Fenix Wang on 2017/6/7.
- // Copyright © 2017年 Netease. All rights reserved.
- //
- #import "FriendViewController.h"
- #import "NTESSessionViewController.h"
- #import "NTESSessionPeekViewController.h"
- #import "UIView+NTES.h"
- #import "NTESBundleSetting.h"
- #import "NTESListHeader.h"
- #import "NTESClientsTableViewController.h"
- #import "NTESSnapchatAttachment.h"
- #import "NTESJanKenPonAttachment.h"
- #import "NTESChartletAttachment.h"
- #import "NTESWhiteboardAttachment.h"
- #import "NTESGiftAttachment.h"
- #import "NTESSessionUtil.h"
- #import "NTESPersonalCardViewController.h"
- #import "NTESContactViewController.h"
- #import "NTESContactAddFriendViewController.h"
- #import "User.h"
- #import "FriendListViewController.h"
- #import "NIMSessionListCell.h"
- #import "NIMAvatarImageView.h"
- #import "NTESShareAttachment.h"
- #import "HttpRequest.h"
- #import "NTESBadgeView.h"
- #define SessionListTitle @"谁说"
- @interface FriendViewController ()<NIMLoginManagerDelegate,NTESListHeaderDelegate,NIMEventSubscribeManagerDelegate,UIViewControllerPreviewingDelegate,UISearchBarDelegate>
- @property (nonatomic,strong) UILabel *titleLabel;
- @property (nonatomic,strong) NTESListHeader *header;
- @property (nonatomic,assign) BOOL supportsForceTouch;
- @property (nonatomic,strong) NSMutableDictionary *previews;
- @property (nonatomic, strong) NSMutableArray *searchArr;
- @property (nonatomic, strong) UISearchController *searchController;
- @property (nonatomic, strong) UIButton *contactBtn;
- @property (nonatomic, strong) NTESBadgeView *badgeView;
- @property (nonatomic, strong) NSMutableArray *requestArr;
- @end
- @implementation FriendViewController
- - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
- self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
- if (self) {
- _previews = [[NSMutableDictionary alloc] init];
- self.autoRemoveRemoteSession = [[NTESBundleSetting sharedConfig] autoRemoveRemoteSession];
- }
- return self;
- }
- - (void)dealloc{
- [[NIMSDK sharedSDK].loginManager removeDelegate:self];
- }
- - (void)viewDidLoad{
- [super viewDidLoad];
-
- self.view.backgroundColor = UIColor.whiteColor;
-
- self.supportsForceTouch = [self.traitCollection respondsToSelector:@selector(forceTouchCapability)] && self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable;
-
- [[NIMSDK sharedSDK].loginManager addDelegate:self];
- [[NIMSDK sharedSDK].subscribeManager addDelegate:self];
-
- self.header = [[NTESListHeader alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 0)];
- self.header.autoresizingMask = UIViewAutoresizingFlexibleWidth;
- self.header.delegate = self;
- [self.view addSubview:self.header];
-
- self.emptyTipLabel = [[UILabel alloc] init];
- self.emptyTipLabel.text = @"还没有会话,在通讯录中找个人聊聊吧";
- [self.emptyTipLabel sizeToFit];
- self.emptyTipLabel.hidden = self.recentSessions.count;
- [self.view addSubview:self.emptyTipLabel];
-
- NSString *userID = [[[NIMSDK sharedSDK] loginManager] currentAccount];
- self.navigationItem.titleView = [self titleView:userID];
-
- __weak typeof(self) wself = self;
- [[User sharedInfo] requestChatStickList:^{
- [wself sort];
- [wself refresh:YES];
- }];
-
- _searchArr = [[NSMutableArray alloc] init];
-
- _searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
- //_searchController.searchBar.barTintColor = User.orangeColor;
- _searchController.hidesNavigationBarDuringPresentation = NO;
- _searchController.searchBar.tintColor = UIColor.whiteColor;
- //_searchController.delegate = self;
- _searchController.dimsBackgroundDuringPresentation = NO;
- _searchController.searchBar.delegate = self;
- [self.tableView setTableHeaderView:_searchController.searchBar];
- }
- - (void)viewWillAppear:(BOOL)animated{
- [super viewWillAppear:animated];
-
- UINavigationItem *navItem = self.parentViewController.navigationItem;
-
- [navItem setTitle:@"朋友"];
-
- UIButton *emptyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
- [emptyBtn setTitle:@"" forState:UIControlStateNormal];
- [emptyBtn sizeToFit];
- UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:emptyBtn];
-
- [navItem setLeftBarButtonItems:@[leftItem]];
-
- _contactBtn = [UIButton buttonWithType:UIButtonTypeCustom];
- [_contactBtn setImage:[UIImage imageNamed:@"朋友_03"] forState:UIControlStateNormal];
- [_contactBtn addTarget:self action:@selector(showContact:) forControlEvents:UIControlEventTouchUpInside];
- [_contactBtn sizeToFit];
- UIBarButtonItem *contactItem = [[UIBarButtonItem alloc] initWithCustomView:_contactBtn];
-
- _badgeView = [NTESBadgeView viewWithBadgeTip:@""];
- _badgeView.right = 20;
- _badgeView.top = -10;
- [_contactBtn addSubview:_badgeView];
-
- UIButton *spaceBtn = [UIButton buttonWithType:UIButtonTypeCustom];
- spaceBtn.frame = CGRectMake(0, 2, 10, 10);
- UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithCustomView:spaceBtn];
-
- UIButton *addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
- [addBtn setImage:[UIImage imageNamed:@"创建群_07"] forState:UIControlStateNormal];
- [addBtn addTarget:self action:@selector(showAddFriend:) forControlEvents:UIControlEventTouchUpInside];
- [addBtn sizeToFit];
- UIBarButtonItem *addItem = [[UIBarButtonItem alloc] initWithCustomView:addBtn];
-
- [navItem setRightBarButtonItems:@[addItem, spaceItem, contactItem]];
-
- [[User sharedInfo] removeNotJoinTeamConversation];
- [super resetRecentSessions];
-
- __weak typeof(self) wself = self;
- [[HttpRequest shared] requestFriendList:^(NSMutableArray<FriendRequestData *> * _Nullable dataArr) {
- wself.requestArr = dataArr;
- NSInteger badge = dataArr.count;
- wself.badgeView.hidden = badge == 0;
- wself.badgeView.badgeValue = [NSString stringWithFormat:@"%ld", (long)badge];
- } failure:^{
- }];
- }
- - (void)viewWillDisappear:(BOOL)animated
- {
- [super viewWillDisappear:animated];
- [self.searchController setActive:NO];
- }
- - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
- {
- NSLog(@"textDidChange %@ %@", searchBar.text, searchText);
-
- if(searchText.length > 0)
- {
- [self.searchArr removeAllObjects];
- for(int i=0; i<self.recentSessions.count; i++)
- {
- NIMRecentSession *recent = [self.recentSessions objectAtIndex:i];
- NSString *name = [[self nameForRecentSession:recent] lowercaseString];
- if([name containsString:[searchText lowercaseString]])
- {
- [_searchArr addObject:recent];
- }
- }
- }
- else
- {
- [self.searchArr removeAllObjects];
- }
-
- [self.tableView reloadData];
- }
- - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
- {
- [self.searchArr removeAllObjects];
- [self.tableView reloadData];
- }
- - (void)refresh:(BOOL)reload{
- [super refresh:reload];
- self.emptyTipLabel.hidden = self.recentSessions.count;
- }
- - (void)onSelectedRecent:(NIMRecentSession *)recent atIndexPath:(NSIndexPath *)indexPath{
- NTESSessionViewController *vc = [[NTESSessionViewController alloc] initWithSession:recent.session];
- [self.navigationController pushViewController:vc animated:YES];
- }
- - (void)onSelectedAvatar:(NIMRecentSession *)recent
- atIndexPath:(NSIndexPath *)indexPath{
- if (recent.session.sessionType == NIMSessionTypeP2P) {
- [User showUserInfo:recent.session.sessionId viewController:self];
- }
- else
- {
- [User showTeamInfo:recent.session.sessionId viewController:self];
- }
- }
- - (void)onDeleteRecentAtIndexPath:(NIMRecentSession *)recent atIndexPath:(NSIndexPath *)indexPath{
- [super onDeleteRecentAtIndexPath:recent atIndexPath:indexPath];
- }
- - (void)viewDidLayoutSubviews{
- [super viewDidLayoutSubviews];
- [self refreshSubview];
- }
- - (NSString *)nameForRecentSession:(NIMRecentSession *)recent{
- if ([recent.session.sessionId isEqualToString:[[NIMSDK sharedSDK].loginManager currentAccount]]) {
- return @"我的电脑";
- }
- return [super nameForRecentSession:recent];
- }
- #pragma mark - SessionListHeaderDelegate
- - (void)didSelectRowType:(NTESListHeaderType)type{
- //多人登录
- switch (type) {
- case ListHeaderTypeLoginClients:{
- NTESClientsTableViewController *vc = [[NTESClientsTableViewController alloc] initWithNibName:nil bundle:nil];
- [self.navigationController pushViewController:vc animated:YES];
- break;
- }
- default:
- break;
- }
- }
- #pragma mark - NIMLoginManagerDelegate
- - (void)onLogin:(NIMLoginStep)step{
- [super onLogin:step];
- switch (step) {
- case NIMLoginStepLinkFailed:
- self.titleLabel.text = [SessionListTitle stringByAppendingString:@"(未连接)"];
- break;
- case NIMLoginStepLinking:
- self.titleLabel.text = [SessionListTitle stringByAppendingString:@"(连接中)"];
- break;
- case NIMLoginStepLinkOK:
- case NIMLoginStepSyncOK:
- self.titleLabel.text = SessionListTitle;
- break;
- case NIMLoginStepSyncing:
- self.titleLabel.text = [SessionListTitle stringByAppendingString:@"(同步数据)"];
- break;
- default:
- break;
- }
- [self.titleLabel sizeToFit];
- self.titleLabel.centerX = self.navigationItem.titleView.width * .5f;
- [self.header refreshWithType:ListHeaderTypeNetStauts value:@(step)];
- [self.view setNeedsLayout];
- }
- - (void)onMultiLoginClientsChanged
- {
- [self.header refreshWithType:ListHeaderTypeLoginClients value:[NIMSDK sharedSDK].loginManager.currentLoginClients];
- [self.view setNeedsLayout];
- }
- #pragma mark - UITableViewDelegate
- - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
- {
- if (self.supportsForceTouch) {
- id<UIViewControllerPreviewing> preview = [self registerForPreviewingWithDelegate:self sourceView:cell];
- [self.previews setObject:preview forKey:@(indexPath.row)];
- }
- }
- - (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
- {
- if (self.supportsForceTouch) {
- id<UIViewControllerPreviewing> preview = [self.previews objectForKey:@(indexPath.row)];
- [self unregisterForPreviewingWithContext:preview];
- [self.previews removeObjectForKey:@(indexPath.row)];
- }
- }
- - (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)context viewControllerForLocation:(CGPoint)point {
- UITableViewCell *touchCell = (UITableViewCell *)context.sourceView;
- if ([touchCell isKindOfClass:[UITableViewCell class]]) {
- NSIndexPath *indexPath = [self.tableView indexPathForCell:touchCell];
- NIMRecentSession *recent = self.recentSessions[indexPath.row];
- NTESSessionPeekNavigationViewController *nav = [NTESSessionPeekNavigationViewController instance:recent.session];
- return nav;
- }
- return nil;
- }
- - (void)previewingContext:(id <UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit
- {
- UITableViewCell *touchCell = (UITableViewCell *)previewingContext.sourceView;
- if ([touchCell isKindOfClass:[UITableViewCell class]]) {
- NSIndexPath *indexPath = [self.tableView indexPathForCell:touchCell];
- NIMRecentSession *recent = self.recentSessions[indexPath.row];
- NTESSessionViewController *vc = [[NTESSessionViewController alloc] initWithSession:recent.session];
- [self.navigationController showViewController:vc sender:nil];
- }
- }
- #pragma mark - NIMEventSubscribeManagerDelegate
- - (void)onRecvSubscribeEvents:(NSArray *)events
- {
- NSMutableSet *ids = [[NSMutableSet alloc] init];
- for (NIMSubscribeEvent *event in events) {
- [ids addObject:event.from];
- }
-
- NSMutableArray *indexPaths = [[NSMutableArray alloc] init];
- for (NSIndexPath *indexPath in self.tableView.indexPathsForVisibleRows) {
- NIMRecentSession *recent = self.recentSessions[indexPath.row];
- if (recent.session.sessionType == NIMSessionTypeP2P) {
- NSString *from = recent.session.sessionId;
- if ([ids containsObject:from]) {
- [indexPaths addObject:indexPath];
- }
- }
- }
-
- [self.tableView reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone];
- }
- #pragma mark - UITableViewDataSource
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
- if(_searchArr.count)
- return _searchArr.count;
- return self.recentSessions.count;
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
- NIMRecentSession *recent = nil;
- if(_searchArr.count)
- {
- recent = [_searchArr objectAtIndex:indexPath.row];
- }
- else
- {
- recent = self.recentSessions[indexPath.row];
- }
-
- static NSString * cellId = @"cellId";
- NIMSessionListCell * cell = [tableView dequeueReusableCellWithIdentifier:cellId];
- if (!cell) {
- cell = [[NIMSessionListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
- [cell.avatarImageView addTarget:self action:@selector(onTouchAvatar:) forControlEvents:UIControlEventTouchUpInside];
- }
- cell.nameLabel.text = [self nameForRecentSession:recent];
- if(recent.session.sessionType == NIMSessionTypeP2P)
- {
- [cell.avatarImageView setAvatarBySession:recent.session];
- }
- else if(recent.session.sessionType == NIMSessionTypeTeam)
- {
- NIMTeam *team = [[NIMSDK sharedSDK].teamManager teamById:recent.session.sessionId];
- NSURL *url = team.thumbAvatarUrl ? [NSURL URLWithString:team.thumbAvatarUrl] : nil;
- [cell.avatarImageView nim_setImageWithURL:url placeholderImage:[User defaultTeamAvatar:team]];
- }
- [cell.nameLabel sizeToFit];
- cell.messageLabel.attributedText = [self contentForRecentSession:recent];
- [cell.messageLabel sizeToFit];
- cell.timeLabel.text = [self timestampDescriptionForRecentSession:recent];
- [cell.timeLabel sizeToFit];
-
- [cell refresh:recent];
- return cell;
- }
- #pragma mark - Private
- - (void)refreshSubview{
- [self.titleLabel sizeToFit];
- self.titleLabel.centerX = self.navigationItem.titleView.width * .5f;
- self.tableView.top = self.header.height;
- self.tableView.height = self.view.height - self.tableView.top;
- self.header.bottom = self.tableView.top + self.tableView.contentInset.top;
- self.emptyTipLabel.centerX = self.view.width * .5f;
- self.emptyTipLabel.centerY = self.tableView.height * .5f;
- }
- - (UIView*)titleView:(NSString*)userID{
- self.titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
- self.titleLabel.text = SessionListTitle;
- self.titleLabel.font = [UIFont boldSystemFontOfSize:15.f];
- [self.titleLabel sizeToFit];
- UILabel *subLabel = [[UILabel alloc] initWithFrame:CGRectZero];
- subLabel.textColor = [UIColor grayColor];
- subLabel.font = [UIFont systemFontOfSize:12.f];
- subLabel.text = userID;
- subLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
- [subLabel sizeToFit];
-
- UIView *titleView = [[UIView alloc] init];
- titleView.width = subLabel.width;
- titleView.height = self.titleLabel.height + subLabel.height;
-
- subLabel.bottom = titleView.height;
- [titleView addSubview:self.titleLabel];
- [titleView addSubview:subLabel];
- return titleView;
- }
- - (NSAttributedString *)contentForRecentSession:(NIMRecentSession *)recent{
- NSAttributedString *content;
- if (recent.lastMessage.messageType == NIMMessageTypeCustom)
- {
- NIMCustomObject *object = recent.lastMessage.messageObject;
- NSString *text = @"";
- if ([object.attachment isKindOfClass:[NTESSnapchatAttachment class]]) {
- text = @"[阅后即焚]";
- }
- else if ([object.attachment isKindOfClass:[NTESJanKenPonAttachment class]]) {
- text = @"[猜拳]";
- }
- else if ([object.attachment isKindOfClass:[NTESChartletAttachment class]]) {
- text = @"[贴图]";
- }
- else if ([object.attachment isKindOfClass:[NTESWhiteboardAttachment class]]) {
- text = @"[白板]";
- }
- else if ([object.attachment isKindOfClass:[NTESGiftAttachment class]]) {
- text = @"[礼物]";
- }
- else if ([object.attachment isKindOfClass:[NTESShareAttachment class]]) {
- text = @"[分享]";
- }
- else{
- text = @"[未知消息]";
- }
- if (recent.session.sessionType != NIMSessionTypeP2P)
- {
- NSString *nickName = [NTESSessionUtil showNick:recent.lastMessage.from inSession:recent.lastMessage.session];
- text = nickName.length ? [nickName stringByAppendingFormat:@" : %@",text] : @"";
- }
- content = [[NSAttributedString alloc] initWithString:text];
- }
- else
- {
- content = [super contentForRecentSession:recent];
- }
- NSMutableAttributedString *attContent = [[NSMutableAttributedString alloc] initWithAttributedString:content];
- [self checkNeedAtTip:recent content:attContent];
- [self checkOnlineState:recent content:attContent];
- return attContent;
- }
- - (void)checkNeedAtTip:(NIMRecentSession *)recent content:(NSMutableAttributedString *)content
- {
- if ([NTESSessionUtil recentSessionIsAtMark:recent]) {
- NSAttributedString *atTip = [[NSAttributedString alloc] initWithString:@"[有人@你] " attributes:@{NSForegroundColorAttributeName:[UIColor redColor]}];
- [content insertAttributedString:atTip atIndex:0];
- }
- }
- - (void)checkOnlineState:(NIMRecentSession *)recent content:(NSMutableAttributedString *)content
- {
- if (recent.session.sessionType == NIMSessionTypeP2P) {
- NSString *state = [NTESSessionUtil onlineState:recent.session.sessionId detail:NO];
- if (state.length) {
- NSString *format = [NSString stringWithFormat:@"[%@] ",state];
- NSAttributedString *atTip = [[NSAttributedString alloc] initWithString:format attributes:nil];
- [content insertAttributedString:atTip atIndex:0];
- }
- }
-
- }
- - (void)showContact:(int)sender
- {
- UIStoryboard *board = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
- FriendListViewController *vc = [board instantiateViewControllerWithIdentifier:@"FriendList"];
- vc.requestArr = _requestArr;
- [self.navigationController pushViewController:vc animated:YES];
- }
- - (void)showAddFriend:(int)sender
- {
- NTESContactAddFriendViewController *vc = [[NTESContactAddFriendViewController alloc] init];
- [self.navigationController pushViewController:vc animated:YES];
- }
- - (void)sort{
- [self.recentSessions sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
- NIMRecentSession *item1 = obj1;
- NIMRecentSession *item2 = obj2;
-
- NSComparisonResult result = [[User sharedInfo] compareChatStick:item1.session.sessionId chatIdB:item2.session.sessionId];
-
- if(result != NSOrderedSame)
- return result;
-
- if (item1.lastMessage.timestamp < item2.lastMessage.timestamp) {
- return NSOrderedDescending;
- }
- if (item1.lastMessage.timestamp > item2.lastMessage.timestamp) {
- return NSOrderedAscending;
- }
- return NSOrderedSame;
- }];
- }
- - (void)onTouchAvatar:(id)sender{
- UIView *view = [sender superview];
- while (![view isKindOfClass:[UITableViewCell class]]) {
- view = view.superview;
- }
- UITableViewCell *cell = (UITableViewCell *)view;
- NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
- NIMRecentSession *recent = self.recentSessions[indexPath.row];
- [self onSelectedAvatar:recent atIndexPath:indexPath];
- }
- @end
|