Files
peko-ios/YuMi/Modules/YMMessage/View/SessionList/SessionListViewController.m
edwinQQQ 99db078b62 feat(勋章): 新增多等级高亮功能并优化勋章展示逻辑
refactor(勋章排行): 重构排行榜分页加载和刷新逻辑

fix(会话): 优化官方账号判断逻辑和跳转处理

style(UI): 调整勋章排行榜和游戏菜单UI布局

chore: 更新Podfile配置和bitcode框架列表
2025-07-03 19:35:17 +08:00

404 lines
15 KiB
Objective-C

//
// SessionListViewController.m
// xplan-ios
//
// Created by zu on 2021/11/25.
//
#import "SessionListViewController.h"
#import "SessionListCell.h"
#import "SessionDiscoverNewTableViewCell.h"
#import "SessionViewController.h"
#import "XPMineFriendEmptyTableViewCell.h"
#import "ClientConfig.h"
#import "Api+Mine.h"
#import "AccountInfoStorage.h"
///Model
#import "UserInfoModel.h"
///View
#import "XPSessionFindNewViewController.h"
#import "TTPopUp.h"
#import "XPSkillCardPlayerManager.h"
#import "MessagePresenter.h"
#import "MessageProtocol.h"
#import "NIMMessageUtils.h"
NSString * const kMessageShowReadDotKey = @"kMessageShowReadDotKey";
#import <Masonry/Masonry.h>
@interface SessionListViewController ()<UITableViewDataSource, UITableViewDelegate, NIMLoginManagerDelegate, NIMConversationManagerDelegate, MessageProtocol>
/**
* 会话列表
*/
@property (nonatomic, strong) UITableView *sessionListView;
/**
* 最近会话集合
*/
@property (nonatomic, strong) NSMutableArray<NIMRecentSession *> *recentSessions;
@property (nonatomic, assign) SessionListOpenType openType;
///用户信息
@property (nonatomic,strong) UserInfoModel *userInfo;
@property (nonatomic, copy) void(^scrollCallback)(UIScrollView *scrollView);
@property (nonatomic, strong) NIMRecentSession *recentSession;
@end
@implementation SessionListViewController
- (void)dealloc {
[[NIMSDK sharedSDK].conversationManager removeDelegate:self];
[[NIMSDK sharedSDK].loginManager removeDelegate:self];
[[NSNotificationCenter defaultCenter]removeObserver:self];
}
- (BOOL)isHiddenNavBar {
return YES;
}
- (MessagePresenter *)createPresenter {
return [[MessagePresenter alloc] init];
}
- (instancetype)initWithType:(SessionListOpenType)type {
self = [self init];
if (self) {
_openType = type;
}
return self;
}
- (instancetype)init {
self = [super init];
if (self) {
_openType = SessionListOpenTypeDefault;
[self initDatas];
[[NIMSDK sharedSDK].conversationManager addDelegate:self];
[[NIMSDK sharedSDK].loginManager addDelegate:self];
///置顶会话同步
[[NIMSDKConfig sharedConfig] setShouldSyncStickTopSessionInfos:YES];
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self getUserInfo];
[self initViews];
[self initLayout];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(allReadButtonClick) name:@"kAllReadAction" object:nil];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
}
- (void)getUserInfo {
NSString * uid = [AccountInfoStorage instance].getUid;
@kWeakify(self);
[Api getUserInfo:^(BaseModel * _Nullable data, NSInteger code, NSString * _Nullable msg) {
@kStrongify(self);
if (code == 200) {
UserInfoModel * userInfo = [UserInfoModel modelWithDictionary:data.data];
self.userInfo = userInfo;
[[XPSkillCardPlayerManager shareInstance] setUserInfoModel:userInfo];
[self.sessionListView reloadData];
}
} uid:uid];
}
- (void)initViews {
self.view.backgroundColor = [UIColor clearColor];
[self.view addSubview:self.sessionListView];
}
- (void)initLayout {
[self.sessionListView mas_makeConstraints:^(MASConstraintMaker *make) {
make.leading.trailing.bottom.mas_equalTo(self.view);
if (self.openType == SessionListOpenTypeDefault) {
make.top.mas_equalTo(self.view);
} else {
make.top.mas_equalTo(self.view);
}
}];
}
- (void)initDatas {
[self.recentSessions removeAllObjects];
_recentSessions = [[NIMSDK sharedSDK].conversationManager.allRecentSessions mutableCopy];
__block int unreadCount = 0;
NSMutableArray<NSString *> * uids = [[NSMutableArray alloc] init];
[self.recentSessions enumerateObjectsUsingBlock:^(NIMRecentSession * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
[uids addObject:obj.session.sessionId];
unreadCount += obj.unreadCount;
}];
[self setTabBarItemBadge:unreadCount];
[[NIMSDK sharedSDK].userManager fetchUserInfos:uids completion:nil];
NIMLoadRecentSessionsOptions *options = [[NIMLoadRecentSessionsOptions alloc] init];
options.sortByStickTopInfos = YES;
[NIMSDK.sharedSDK.chatExtendManager loadRecentSessionsWithOptions:options completion:^(NSError * _Nullable error, NSArray<NIMRecentSession *> * _Nullable recentSessions) {
NSLog(@"%@", recentSessions);
}];
}
- (void)onGetUserInfoSuccess:(UserInfoModel *)userInfo {
if (userInfo) {
if (self.openType == SessionListOpenTypeRoom) {
SessionViewController * sessionVC =[[SessionViewController alloc] initWithSession:self.recentSession.session];
sessionVC.userInfo = userInfo;
sessionVC.openType = self.openType;
CATransition *transition = [CATransition animation];
transition.duration = 0.3f;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromRight;
[self.mainController.view.layer addAnimation:transition forKey:nil];
[self.mainController.view addSubview:sessionVC.view];
[self.mainController addChildViewController:sessionVC];
} else {
SessionViewController *vc = [[SessionViewController alloc] initWithSession:self.recentSession.session];
vc.openType = self.openType;
vc.userInfo = userInfo;
[self.navigationController pushViewController:vc animated:YES];
}
}
}
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if (self.recentSessions.count == 0) {
return;
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NIMRecentSession *recentSession = self.recentSessions[indexPath.row];
self.recentSession = recentSession;
if ([NIMMessageUtils isOfficalAccount:self.recentSession.session.sessionId]) {
if (self.openType == SessionListOpenTypeRoom) {
SessionViewController * sessionVC =[[SessionViewController alloc] initWithSession:self.recentSession.session];
sessionVC.openType = self.openType;
CATransition *transition = [CATransition animation];
transition.duration = 0.3f;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromRight;
[self.mainController.view.layer addAnimation:transition forKey:nil];
[self.mainController.view addSubview:sessionVC.view];
[self.mainController addChildViewController:sessionVC];
} else {
SessionViewController *vc = [[SessionViewController alloc] initWithSession:self.recentSession.session];
vc.openType = self.openType;
[self.navigationController pushViewController:vc animated:YES];
}
} else {
[self.presenter getUserInfoWithUid:recentSession.session.sessionId];
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return self.recentSessions.count == 0 ? (KScreenHeight - 200 - kNavigationHeight) : 92.f;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return self.recentSessions.count == 0 ? NO : YES;
}
- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewRowAction * deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:YMLocalizedString(@"SessionListViewController0") handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
NIMRecentSession * session = [self.recentSessions xpSafeObjectAtIndex:indexPath.row];
[[NIMSDK sharedSDK].conversationManager deleteRecentSession:session];
}];
deleteAction.title = YMLocalizedString(@"SessionListViewController1");
deleteAction.backgroundColor = [UIColor redColor];
return @[deleteAction];
}
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.recentSessions.count == 0 ? 1 : self.recentSessions.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if (self.recentSessions.count == 0) {
XPMineFriendEmptyTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([XPMineFriendEmptyTableViewCell class])];
if (cell == nil) {
cell = [[XPMineFriendEmptyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([XPMineFriendEmptyTableViewCell class])];
}
cell.emptyTitle = YMLocalizedString(@"XPMineAttentionViewController0");
return cell;
} else {
static NSString * cellId = @"cellId";
SessionListCell * cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if (!cell) {
cell = [[SessionListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
}
NIMRecentSession *recent = [self.recentSessions xpSafeObjectAtIndex:indexPath.row];
[cell renderWithSession:recent];
return cell;
}
}
#pragma mark - NIMConversationManagerDelegate
- (void)didLoadAllRecentSessionCompletion {
[self initDatas];
[self.sessionListView reloadData];
}
- (void)didAddRecentSession:(NIMRecentSession *)recentSession
totalUnreadCount:(NSInteger)totalUnreadCount {
[self.recentSessions addObject:recentSession];
[self.recentSessions sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
NIMRecentSession *item1 = obj1;
NIMRecentSession *item2 = obj2;
if (item1.lastMessage.timestamp < item2.lastMessage.timestamp) {
return NSOrderedDescending;
}
if (item1.lastMessage.timestamp > item2.lastMessage.timestamp) {
return NSOrderedAscending;
}
return NSOrderedSame;
}];
[self.sessionListView reloadData];
[self setTabBarItemBadge:totalUnreadCount];
}
- (void)didUpdateRecentSession:(NIMRecentSession *)recentSession
totalUnreadCount:(NSInteger)totalUnreadCount {
for (NIMRecentSession *recent in self.recentSessions) {
if ([recentSession.session.sessionId isEqualToString:recent.session.sessionId]) {
[self.recentSessions removeObject:recent];
break;
}
}
NSInteger insert = [self findInsertPlace:recentSession];
[self.recentSessions insertObject:recentSession atIndex:insert];
[self.sessionListView reloadData];
[self setTabBarItemBadge:totalUnreadCount];
}
- (void)didRemoveRecentSession:(NIMRecentSession *)recentSession
totalUnreadCount:(NSInteger)totalUnreadCount {
// 清理本地数据
NSUInteger index = [self.recentSessions indexOfObject:recentSession];
[self.recentSessions xpSafeRemoveObjectAtIndex:index];
// 如果删除本地会话后就不允许漫游当前会话,则需要进行一次删除服务器会话的操作
BOOL deleteRemote = NO;
if (deleteRemote) {
[[NIMSDK sharedSDK].conversationManager deleteRemoteSessions:@[recentSession.session]
completion:nil];
}
[self.sessionListView reloadData];
[self setTabBarItemBadge:totalUnreadCount];
}
#pragma mark - NIMLoginManagerDelegate
- (void)onLogin:(NIMLoginStep)step {
if (step == NIMLoginStepSyncOK) {
[self initDatas];
}
}
#pragma mark - JXCategoryListContentViewDelegate
- (UIView *)listView {
return self.view;
}
- (UIScrollView *)listScrollView {
return self.sessionListView;
}
- (void)listViewDidScrollCallback:(void (^)(UIScrollView *))callback {
self.scrollCallback = callback;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (self.scrollCallback){
self.scrollCallback(scrollView);
}
}
#pragma mark - Private
- (NSInteger)findInsertPlace:(NIMRecentSession *)recentSession{
__block NSUInteger matchIdx = 0;
__block BOOL find = NO;
[self.recentSessions enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NIMRecentSession *item = obj;
if (item.lastMessage.timestamp <= recentSession.lastMessage.timestamp) {
*stop = YES;
find = YES;
matchIdx = idx;
}
}];
if (find) {
return matchIdx;
} else {
return self.recentSessions.count;
}
}
- (void)sort{
[self.recentSessions sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
NIMRecentSession *item1 = obj1;
NIMRecentSession *item2 = obj2;
if (item1.lastMessage.timestamp < item2.lastMessage.timestamp) {
return NSOrderedDescending;
}
if (item1.lastMessage.timestamp > item2.lastMessage.timestamp) {
return NSOrderedAscending;
}
return NSOrderedSame;
}];
}
- (void)setTabBarItemBadge:(NSInteger)value {
[[NSNotificationCenter defaultCenter]postNotificationName:@"kGetTabBarItemBadge" object:nil userInfo:@{@"BadgeValue":@(value)}];
}
#pragma mark - action
- (void)allReadButtonClick {
NSInteger count = [NIMSDK sharedSDK].conversationManager.allUnreadCount;
if (count<1) {
[self showErrorToast:YMLocalizedString(@"SessionListViewController2")];
return;
}
TTAlertConfig * config = [[TTAlertConfig alloc] init];
config.confirmButtonConfig.title = YMLocalizedString(@"SessionListViewController3");
config.message =YMLocalizedString(@"SessionListViewController4");
[TTPopup alertWithConfig:config confirmHandler:^{
[[NIMSDK sharedSDK].conversationManager markAllMessagesRead];
[self setTabBarItemBadge:0];
[self.sessionListView reloadData];
} cancelHandler:^{
}];
}
- (UITableView *)sessionListView {
if (!_sessionListView) {
_sessionListView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
_sessionListView.separatorStyle = UITableViewCellSeparatorStyleNone;
_sessionListView.backgroundColor = UIColor.clearColor;
_sessionListView.delegate = self;
_sessionListView.dataSource = self;
_sessionListView.showsVerticalScrollIndicator = NO;
}
return _sessionListView;
}
- (NSMutableArray<NIMRecentSession *> *)recentSessions {
if (!_recentSessions) {
_recentSessions = [NSMutableArray array];
}
return _recentSessions;
}
@end