Files
yinmeng-ios-store/yinmeng-ios/yinmeng-ios/Main/Mew/Room/View/MewRoomViewController.m
2023-12-04 15:44:30 -08:00

608 lines
23 KiB
Objective-C

//
// MewRoomViewController.m
// mew-ios
//
// Created by 触海 on 2023/11/7.
//
///View
#import "MewRoomViewController.h"
#import "BaseNavigationController.h"
#import "MewRoomHeaderView.h"
#import "MewRoomRightView.h"
#import "MewSocialStageView.h"
#import "YMRoomMessageContainerView.h"
#import "YMRoomSendTextView.h"
#import "YMRoomAnimationView.h"
///Model
#import "UserInfoModel.h"
#import "RoomInfoModel.h"
#import "YMMessageInfoModel.h"
#import "AttachmentModel.h"
#import "MicroQueueModel.h"
#import "GiftInfoModel.h"
#import "GiftReceiveInfoModel.h"
#import "YMMessageRemoteExtModel.h"
///Tool
#import "AccountInfoStorage.h"
#import "MEWHUDTool.h"
#import "YMMacro.h"
#import "NSArray+MEWSafe.h"
#import "RtcManager.h"
///P
#import "YMRoomPresenter.h"
#import "YMRoomProtocol.h"
#import "CHRoomHostDelegate.h"
/// Third
#import <Masonry/Masonry.h>
#import <NIMSDK/NIMSDK.h>
@interface MewRoomViewController ()<YMRoomProtocol,
NIMChatroomManagerDelegate, NIMChatManagerDelegate, NIMConversationManagerDelegate, NIMLoginManagerDelegate,
CHRoomHostDelegate, MewRoomRightViewDelegate, MewRoomHeaderViewDelegate>
// 背景
@property (nonatomic, strong) UIImageView *roomBgImageView;
@property (nonatomic, strong) MewRoomHeaderView *roomHeaderView;
@property (nonatomic, strong) MewRoomRightView *roomRightView;
@property (nonatomic, strong) CHStageView *roomStageView;
@property (nonatomic, strong) YMRoomMessageContainerView *roomMessageView;
///动画的view
@property (nonatomic,strong) YMRoomAnimationView *roomAnimationView;
@property (nonatomic, strong) UIImageView *emojiImageView;
/// 房间的Uid
@property (nonatomic, strong) NSString *roomUid;
/// 用户信息
@property (nonatomic, strong) UserInfoModel *userInfo;
///房间信息
@property (nonatomic, strong) RoomInfoModel *roomInfo;
/// emoji
@property (nonatomic, assign) BOOL isPlayEmojiAnimation;
@property (nonatomic, copy) NSArray<NSString *> *emojiArrays;
@property (nonatomic, strong) NSMutableArray<NSString *> *playEmojiArrays;
@end
@implementation MewRoomViewController
#pragma mark - Super
- (YMRoomPresenter *)createPresenter {
return [[YMRoomPresenter alloc] init];
}
- (BOOL)isHiddenNavBar {
return YES;
}
#pragma mark - Life Cycle
- (void)viewDidLoad {
[super viewDidLoad];
[self initView];
self.emojiArrays = @[@"mew_room_emoji_deyi",@"mew_room_emoji_happy",@"mew_room_emoji_sex",@"mew_room_emoji_suprise"];
self.playEmojiArrays = [NSMutableArray array];
self.isPlayEmojiAnimation = NO;
[self.presenter initEnterCurrentRoom:self.roomUid user:[AccountInfoStorage instance].getUid];
//监听云信消息
[[NIMSDK sharedSDK].chatroomManager addDelegate:self];
[[NIMSDK sharedSDK].chatManager addDelegate:self];
[[NIMSDK sharedSDK].loginManager addDelegate:self];
[[NIMSDK sharedSDK].conversationManager addDelegate:self];
}
#pragma mark - dealloc
- (void)dealloc {
#warning to dox - 时刻注意这个方法 功能做完的时候删除此方法
NSLog(@"房间销毁了");
[[NIMSDK sharedSDK].chatroomManager removeDelegate:self];
[[NIMSDK sharedSDK].chatManager removeDelegate:self];
[[NIMSDK sharedSDK].loginManager removeDelegate:self];
[[NIMSDK sharedSDK].conversationManager removeDelegate:self];
// [[NSNotificationCenter defaultCenter] removeObserver:self];
}
#pragma mark - NIMChatManagerDelegate
- (void)onRecvMessages:(NSArray<NIMMessage *> *)messages {
for (NIMMessage * message in messages) {
///房间内收到p2p的消息 比如升级消息
// 非房间内消息不处理
if (message.session.sessionType != NIMSessionTypeChatroom) {
continue;
}
// 非本房间不处理
if (![message.session.sessionId isEqualToString:[NSString stringWithFormat:@"%ld", self.roomInfo.roomId]]) {
continue;
}
if (message.messageType == NIMMessageTypeNotification) {
NIMNotificationObject *notiMsg = (NIMNotificationObject *)message.messageObject;
NIMChatroomNotificationContent *content = (NIMChatroomNotificationContent *)notiMsg.content;
NSInteger onLineNumber = self.roomInfo.onlineNum;
switch (content.eventType) {
case NIMChatroomEventTypeInfoUpdated: // 麦序状态更新
{
NSDictionary *data = [content.notifyExt mewToJSONObject];
int type = [data[@"type"] intValue];
switch (type) {
case 1:
case 3:
{
NSMutableDictionary *lastRoomInfoDic = [NSMutableDictionary dictionaryWithDictionary:[self.roomInfo mewModel2dictionary]];
[lastRoomInfoDic addEntriesFromDictionary: ((NSString *)data[@"roomInfo"]).mewToJSONObject];
[self.roomStageView onRoomInfoUpdate];
// [self.roomMessageView onRoomUpdate];
}
break;
}
}
break;
case NIMChatroomEventTypeEnter:
if (content.source.userId.integerValue != [AccountInfoStorage instance].getUid.integerValue && self.userInfo.platformRole != 1) {
// onLineNumber += 1;
}
break;
case NIMChatroomEventTypeAddBlack:
case NIMChatroomEventTypeKicked:
case NIMChatroomEventTypeExit:
onLineNumber -= 1;
break;
case NIMChatroomEventTypeQueueChange: // 麦序上下麦
{
NSDictionary* data = (NSDictionary *)content.ext;
UserInfoModel* userInfo = [UserInfoModel mewModelWithJSON:[data objectForKey:NIMChatroomEventInfoQueueChangeItemValueKey]];
NSInteger changeType = [data[NIMChatroomEventInfoQueueChangeTypeKey] integerValue];
if (changeType == 1 && userInfo.uid == [AccountInfoStorage instance].getUid.integerValue) {
// [self cancelRoomArrangeMic];
}
}
break;
default:
break;
}
self.roomInfo.onlineNum = onLineNumber;
// [self.roomHeaderView onRoomUpdate];
[self.roomStageView handleNIMNotificationMessage:message];
[self.roomAnimationView handleNIMNotificationMessage:message];
[self.roomMessageView handleNIMNotificationMessage:message];
} else if (message.messageType == NIMMessageTypeCustom) {
NIMCustomObject *obj = (NIMCustomObject *)message.messageObject;
if (obj.attachment != nil && [obj.attachment isKindOfClass:[AttachmentModel class]]) {
[self.roomStageView handleNIMCustomMessage:message];
[self.roomAnimationView handleNIMCustomMessage:message];
[self.roomMessageView handleNIMCustomMessage:message];
}
} else if(message.messageType == NIMMessageTypeText) {
[self.roomMessageView handleNIMTextMessage:message];
} else if(message.messageType == NIMMessageTypeTip) {
[self.roomMessageView handleNIMTextMessage:message];
}
}
}
- (void)sendMessage:(NIMMessage *)message didCompleteWithError:(NSError *)error {
if (![message.session.sessionId isEqualToString:[NSString stringWithFormat:@"%ld",self.roomInfo.roomId]]) {
return;
}
if (message.yidunAntiSpamRes) {
NSDictionary * spamRes = message.yidunAntiSpamRes.mewToJSONObject;
NSDictionary * spamResExt = ((NSString *)spamRes[@"ext"]).mewToJSONObject;
if ([spamResExt[@"antispam"][@"suggestion"] intValue] == 2) {
[self showErrorToast:@"消息中可能含有违规信息,请停止发送类似信息!"];
return;
}
}
if (error) return;
if (message.messageType == NIMMessageTypeCustom) {
NIMCustomObject *obj = (NIMCustomObject *)message.messageObject;
if (obj.attachment != nil && [obj.attachment isKindOfClass:[AttachmentModel class]]) {
[self.roomStageView handleNIMCustomMessage:message];
[self.roomAnimationView handleNIMCustomMessage:message];
[self.roomMessageView handleNIMCustomMessage:message];
}
} else if(message.messageType == NIMMessageTypeText) {
[self.roomMessageView handleNIMTextMessage:message];
}
}
#pragma mark - Public Method
/// 进入房间
+ (BOOL)openRoom:(NSString *)roomUid viewController:(UIViewController *)viewController {
MewRoomViewController *roomVC = [[self alloc] init];
roomVC.roomUid = roomUid;
BaseNavigationController *baseNav = [[BaseNavigationController alloc] initWithRootViewController:roomVC];
baseNav.modalPresentationStyle = UIModalPresentationFullScreen;
[viewController presentViewController:baseNav animated:YES completion:nil];
return YES;
}
#pragma mark - CHRoomHostDelegate
- (RoomInfoModel *)getRoomInformation {
return self.roomInfo;
}
- (UserInfoModel *)getUserInformation {
return self.userInfo;
}
/// 退出房间
- (void)exitCurrentRoom {
//其他模式下
[self.roomAnimationView resumeAnimationTimer];
// 默认模式下
[self.presenter exitNIMCurrentRoom:[NSString stringWithFormat:@"%ld", self.roomInfo.roomId]];
[[RtcManager instance] exitRoom];
[self.presenter reportUserOutRoom:[NSString stringWithFormat:@"%ld",self.roomInfo.uid]];
[self dismissViewControllerAnimated:YES completion:nil];
}
- (UINavigationController *)getCurrentNavigationController {
return self.navigationController;
}
- (void)microQueueUpdate:(NSMutableDictionary<NSString *,MicroQueueModel *> *)queue {
BOOL isOnMic = false;
for (MicroQueueModel * info in queue.allValues) {
if (info.userInfo.uid > 0 && [AccountInfoStorage instance].getUid.integerValue == info.userInfo.uid) {
isOnMic = YES;
break;
}
}
}
- (CGPoint)animationPointAtStageViewByUid:(NSString *)uid {
return [self.roomStageView animationPointAtStageViewByUid:uid];
}
#pragma mark - YMRoomProtocol
/// 进入房间成功
- (void)initEnterCurrentRoomSuccess:(RoomInfoModel *)roomInfo user:(UserInfoModel *)userInfo {
[MewHUDTool hideHUDInView:self.navigationController.view];
self.userInfo = userInfo;
self.roomInfo = roomInfo;
if (roomInfo.valid) {
[self.roomHeaderView onRoomEntered];
[self.presenter enterNIMCurrentRoom:[NSString stringWithFormat:@"%ld",self.roomInfo.roomId] user:self.userInfo];
} else {
if ([self.roomUid isEqualToString:[NSString stringWithFormat:@"%ld", userInfo.uid]]) {
//用户进入自己的房间
if (roomInfo.roomId > 0) {
[self.presenter openUserRoom:roomInfo.title type:roomInfo.type roomPwd:@"" roomDesc:roomInfo.roomDesc backPic:@"" mgId:@""];
} else {
NSString *title = [NSString stringWithFormat:@"%@的房间",userInfo.nick];
RoomType type = RoomType_Game;
[self.presenter openUserRoom:title type:type roomPwd:@"" roomDesc:@"" backPic:@"" mgId:@""];
}
} else {
[self showSuccessToast:@"房主已下线"];
[self enterCurrentRoomFail:-1];
}
}
}
// 开房成功
- (void)openCurrentRoomSuccess:(RoomInfoModel *)roomInfo {
self.roomInfo = roomInfo;
[self.roomHeaderView onRoomEntered];
[self.presenter enterNIMCurrentRoom:[NSString stringWithFormat:@"%ld", self.roomInfo.roomId] user:self.userInfo];
}
// IM进房成功
- (void)enterCurrentRoomSuccess:(NIMChatroom *)chatRoom {
[MewHUDTool hideHUDInView:self.navigationController.view];
[self.roomStageView onRoomEntered];
if (self.roomInfo != nil) {
[self.presenter reportUserInterRoom:[NSString stringWithFormat:@"%zd", self.roomInfo.uid]];
}
self.roomInfo.onlineNum = chatRoom.onlineUserCount;
}
// 进房失败
- (void)enterCurrentRoomFail:(NSInteger)code {
[MewHUDTool hideHUDInView:self.navigationController.view];
[self hideHUD];
if (code == 13003) {
[self showErrorToast:@"您已被管理员拉黑"];
}
[self dismissViewControllerAnimated:YES completion:nil];
}
/// 送礼物成功
- (void)sendGiftSuccess:(GiftReceiveInfoModel *)receiveInfo originDic:(NSDictionary *)originDic uidCount:(NSInteger)uidCount {
[MewHUDTool hideHUD];
[self sendCustomMessage:receiveInfo oringinDic:originDic];
}
/// 送礼物失败
- (void)sendGiftFailWithCode:(NSInteger)code msg:(NSString *)msg {
[MewHUDTool hideHUD];
if (code == 31005) {// 余额不足
[self showErrorToast:msg];
} else {
[self showErrorToast:msg];
}
}
/// 发送云信IM
- (void)sendCustomMessage:(GiftReceiveInfoModel *)eceiveModel oringinDic:(NSDictionary *)originDic {
///单人
NSMutableDictionary *data = [NSMutableDictionary dictionary];
[data addEntriesFromDictionary:originDic];
AttachmentModel *attchment = [[AttachmentModel alloc] init];
attchment.first = CustomMessageType_Gift;
attchment.second = Custom_Message_Sub_Gift_Send;
NSDictionary *targetUsers = ((NSArray *)[data objectForKey:@"targetUsers"]).firstObject;
[data setObject:[targetUsers valueForKeyPath:@"uid"] forKey:@"targetUid"];
[data setObject:[targetUsers valueForKeyPath:@"nick"] forKey:@"targetNick"];
[data setObject:[targetUsers valueForKeyPath:@"avatar"] forKey:@"targetAvatar"];
attchment.data = data;
[self sendCustomMessage:attchment];
}
- (void)sendCustomMessage:(AttachmentModel *)attachment {
NSString *sessionID = [NSString stringWithFormat:@"%ld", [self getRoomInformation].roomId];
NIMMessage *message = [[NIMMessage alloc] init];
NIMCustomObject *object = [[NIMCustomObject alloc] init];
object.attachment = attachment;
message.messageObject = object;
UserInfoModel *userInfo = [self getUserInformation];
YMMessageRemoteExtModel *extModel = [[YMMessageRemoteExtModel alloc] init];
NSMutableDictionary *remoteExt = [NSMutableDictionary dictionaryWithObject:extModel.mewModel2dictionary forKey:[NSString stringWithFormat:@"%ld",userInfo.uid]];
message.remoteExt = remoteExt;
/// 构造会话
NIMSessionType sessionType = NIMSessionTypeChatroom;
NIMSession *session = [NIMSession session:sessionID type:sessionType];
[[NIMSDK sharedSDK].chatManager sendMessage:message toSession:session error:nil];
}
#pragma mark - MewRoomHeaderViewDelegate (header事件回调)
- (void)didSelectMewRoomHeaderViewTarget:(NSInteger)target {
if (target == 0) {
// 收藏
} else if (target == 1) {
// 退出房间
[self exitCurrentRoom];
} else {
// 举报房间
[self reportButton];
}
}
#pragma mark - MewRoomRightViewDelegate(右边事件回调)
- (void)didSelectMewRoomRightViewWithTarget:(NSInteger)target {
if (target == 0) {
// 聊天
[YMRoomSendTextView showTextView:self.view.superview.superview delegate:self];
} else if (target == 1) {
// 表情
[self randomEmoji];
} else {
// 礼物
[self sendRoomGift];
}
}
#pragma mark - Private Method
/// 表情
- (void)randomEmoji {
int t = arc4random() % self.emojiArrays.count;
[self.playEmojiArrays addObject:self.emojiArrays[t]];
[self startPlayEmojiAnimation:self.playEmojiArrays[0]];
}
- (void)startPlayEmojiAnimation:(NSString *)emoji {
if (self.playEmojiArrays.count == 0) {
return;
};
if (self.isPlayEmojiAnimation) return;
self.isPlayEmojiAnimation = YES;
self.emojiImageView.hidden = NO;
if (self.playEmojiArrays.count > 0) {
self.emojiImageView.image = [UIImage imageNamed:self.playEmojiArrays[0]];
}
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
self.isPlayEmojiAnimation = NO;
self.emojiImageView.hidden = YES;
if (self.playEmojiArrays.count > 0) {
[self.playEmojiArrays removeObjectAtIndex:0];
}
if (self.playEmojiArrays.count > 0) {
[self startPlayEmojiAnimation:self.playEmojiArrays[0]];
}
});
}
/// 礼物
- (void)sendRoomGift {
MewSocialStageView *mewStageView = (MewSocialStageView *)self.roomStageView;
NSString *selectId = mewStageView.selectUid;
if (selectId == [AccountInfoStorage instance].getUid) {
[self showErrorToast:@"不能赠送礼物给自己"];
return;
}
if (mewStageView.selectUid.length == 0 || [mewStageView.selectUid isEqualToString:@""]) {
[self showErrorToast:@"请点击麦位头像,选择送礼的人"];
return;
}
[MewHUDTool showLoadingWithMessage:@"赠送中..."];
[self.presenter sendGift:mewStageView.selectUid roomUid:self.roomUid];
}
- (void)reportButton {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"举报内容" message:@""
preferredStyle:UIAlertControllerStyleActionSheet];
[alert addAction:[UIAlertAction actionWithTitle:@"色情低俗,以任何形式传播淫秽、色情、低俗擦边的内" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
[self showSuccessToast:@"举报成功"];
}]];
[alert addAction:[UIAlertAction actionWithTitle:@"政治敏感,进行反党反政府或带有侮辱诋毁党和国家的内容" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
[self showSuccessToast:@"举报成功"];
}]];
[alert addAction:[UIAlertAction actionWithTitle:@"垃圾广告,传播垃圾广告或违法违规广告信息" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
[self showSuccessToast:@"举报成功"];
}]];
[alert addAction:[UIAlertAction actionWithTitle:@"言语攻击,恶意抹黑、诋毁或辱骂他人" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
[self showSuccessToast:@"举报成功"];
}]];
[alert addAction:[UIAlertAction actionWithTitle:@"违规交易,发布线下交易或可能涉及诈骗的内容" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
[self showSuccessToast:@"举报成功"];
}]];
[alert addAction:[UIAlertAction actionWithTitle:@"泄露隐私,以任何形式泄露他人隐私" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
[self showSuccessToast:@"举报成功"];
}]];
[alert addAction:[UIAlertAction actionWithTitle:@"暴力恐怖,传播暴力、血腥、威胁生命健康的内容或展示枪支、刀具" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
[self showSuccessToast:@"举报成功"];
}]];
[alert addAction:[UIAlertAction actionWithTitle:@"侵权盗版,侵犯他人合法版权" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
[self showSuccessToast:@"举报成功"];
}]];
[alert addAction:[UIAlertAction actionWithTitle:@"其它" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
[self showSuccessToast:@"举报成功"];
}]];
[alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action){
// [self showSuccessToast:@"举报成功"];
}]];
[self presentViewController:alert animated:YES completion:nil];
}
#pragma mark - Init
- (void)initView {
[self.view addSubview:self.roomBgImageView];
[self.view addSubview:self.roomHeaderView];
[self.view addSubview:self.roomStageView];
[self.view insertSubview:self.roomRightView aboveSubview:self.roomStageView];
[self.view addSubview:self.roomMessageView];
[self.view insertSubview:self.emojiImageView aboveSubview:self.roomRightView];
[self.view addSubview:self.roomAnimationView];
[self initLayout];
}
- (void)initLayout {
[self.roomBgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.view);
}];
[self.roomHeaderView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.equalTo(self.view);
make.top.mas_equalTo(kStatusBarHeight+20);
make.height.mas_equalTo(28);
}];
[self.roomRightView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.roomHeaderView.mas_bottom).offset(35);
make.right.equalTo(self.view).offset(-32);
}];
[self.roomStageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.roomRightView);
make.left.right.equalTo(self.view);
make.height.mas_equalTo(self.roomStageView.heightForStageView);
}];
[self.roomMessageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.roomStageView.mas_bottom);
make.left.right.equalTo(self.view);
make.bottom.mas_equalTo(-kSafeAreaBottomHeight);
}];
[self.emojiImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self.roomStageView);
make.bottom.equalTo(self.roomStageView).offset(-60);
}];
[self.roomAnimationView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.mas_equalTo(self.view);
}];
}
#pragma mark - Get
- (UIImageView *)roomBgImageView {
if (!_roomBgImageView) {
_roomBgImageView = [[UIImageView alloc] init];
_roomBgImageView.image = [UIImage imageNamed:@"mew_home_bg"];
}
return _roomBgImageView;
}
- (MewRoomHeaderView *)roomHeaderView {
if (!_roomHeaderView) {
_roomHeaderView = [[MewRoomHeaderView alloc] initWithDelegate:self];
_roomHeaderView.delegate = self;
}
return _roomHeaderView;
}
- (MewRoomRightView *)roomRightView {
if (!_roomRightView) {
_roomRightView = [[MewRoomRightView alloc] init];
_roomRightView.delegate = self;
}
return _roomRightView;
}
- (CHStageView *)roomStageView {
if (!_roomStageView) {
_roomStageView = [[MewSocialStageView alloc] initWithDelegate:self];
}
return _roomStageView;
}
- (YMRoomMessageContainerView *)roomMessageView {
if (!_roomMessageView) {
_roomMessageView = [[YMRoomMessageContainerView alloc] initWithDelegate:self];
}
return _roomMessageView;
}
- (UIImageView *)emojiImageView {
if (!_emojiImageView) {
_emojiImageView = [[UIImageView alloc] init];
}
return _emojiImageView;
}
- (YMRoomAnimationView *)roomAnimationView {
if (!_roomAnimationView) {
_roomAnimationView = [[YMRoomAnimationView alloc] initWithDelegate:self];
}
return _roomAnimationView;
}
@end