Files
yinmeng-ios/xplan-ios/Main/Room/View/XPRoomViewController.m

488 lines
18 KiB
Mathematica
Raw Normal View History

//
// XPRoomViewController.m
// xplan-ios
//
// Created by on 2021/10/11.
//
#import "XPRoomViewController.h"
///Third
#import <Masonry/Masonry.h>
2021-12-02 00:09:04 +08:00
#import <NIMSDK/NIMSDK.h>
///Tool
#import "XPMacro.h"
#import "XPConstant.h"
2021-10-19 18:30:03 +08:00
#import "AccountInfoStorage.h"
2021-10-22 17:40:52 +08:00
#import "RtcManager.h"
#import "XPRoomMiniManager.h"
2021-12-20 21:17:12 +08:00
#import "TTPopup.h"
#import "Api+ArrangeMic.h"
///Model
#import "RoomInfoModel.h"
2021-10-19 18:30:03 +08:00
#import "UserInfoModel.h"
2021-12-02 00:09:04 +08:00
#import "AttachmentModel.h"
2021-12-21 12:05:31 +08:00
#import "ArrangeMicModel.h"
///View
2021-10-29 18:17:27 +08:00
#import "BaseNavigationController.h"
#import "XPRoomActivityContainerView.h"
#import "XPRoomBackContainerView.h"
#import "XPRoomMenuContainerView.h"
#import "XPRoomMessageContainerView.h"
#import "RoomHeaderView.h"
2021-10-26 19:14:01 +08:00
#import "SocialStageView.h"
#import "XPRoomAnimationView.h"
2021-12-15 18:12:55 +08:00
#import "XPRoomFunctionContainerView.h"
2021-12-20 21:17:12 +08:00
#import "XPArrangeMicViewController.h"
2021-10-19 18:30:03 +08:00
///P
#import "XPRoomPresenter.h"
#import "XPRoomProtocol.h"
#import "RoomHostDelegate.h"
#import "RoomGuestDelegate.h"
2021-12-21 12:05:31 +08:00
@interface XPRoomViewController ()<XPRoomProtocol, RoomHostDelegate, NIMChatroomManagerDelegate, NIMChatManagerDelegate, NIMLoginManagerDelegate>
///
@property (nonatomic,strong) XPRoomBackContainerView *backContainerView;
///
@property (nonatomic,strong) RoomHeaderView *roomHeaderView;
///
2021-10-26 19:14:01 +08:00
@property (nonatomic,strong) SocialStageView *stageView;
///
@property (nonatomic,strong) XPRoomMessageContainerView *messageContainerView;
///
@property (nonatomic,strong) XPRoomMenuContainerView *menuContainerView;
///
@property (nonatomic,strong) XPRoomActivityContainerView *activityContainerView;
///view
@property (nonatomic,strong) XPRoomAnimationView *animationView;
2021-12-15 18:12:55 +08:00
///view
@property (nonatomic,strong) XPRoomFunctionContainerView *functionView;
///Uid
2021-10-29 18:17:27 +08:00
@property (nonatomic,copy) NSString * roomUid;
2021-10-19 18:30:03 +08:00
///
2021-10-26 19:14:01 +08:00
@property (nonatomic,strong) UserInfoModel *userInfo;
2021-10-19 18:30:03 +08:00
///
@property (nonatomic,strong) RoomInfoModel *roomInfo;
@end
@implementation XPRoomViewController
2021-10-29 18:17:27 +08:00
+ (BOOL)openRoom:(NSString *)roomUid viewController:(UIViewController *)viewController {
XPRoomViewController * 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;
}
2021-10-19 18:30:03 +08:00
- (void)dealloc {
#warning to dox -
2021-11-04 17:48:32 +08:00
2021-10-19 18:30:03 +08:00
}
- (XPRoomPresenter *)createPresenter {
2021-11-04 17:48:32 +08:00
return [[XPRoomPresenter alloc] init];
2021-10-19 18:30:03 +08:00
}
- (BOOL)isHiddenNavBar {
2021-11-04 17:48:32 +08:00
return YES;
}
- (void)viewDidLoad {
[super viewDidLoad];
2021-12-03 19:10:35 +08:00
[self exitOldRoom];
2021-11-04 17:48:32 +08:00
[self initSubViews];
[self initSubViewConstraints];
2021-11-04 20:56:54 +08:00
[self.presenter initEnterRoom:self.roomUid user:[AccountInfoStorage instance].getUid];
2021-11-17 19:29:14 +08:00
///
[self.presenter getNormalGiftList:self.roomUid];
2021-12-02 00:09:04 +08:00
//
[[NIMSDK sharedSDK].chatroomManager addDelegate:self];
[[NIMSDK sharedSDK].chatManager addDelegate:self];
[[NIMSDK sharedSDK].loginManager addDelegate:self];
2021-10-21 17:55:51 +08:00
}
2021-10-29 15:53:44 +08:00
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
2021-11-04 17:48:32 +08:00
[self.view endEditing:YES];
2021-10-29 15:53:44 +08:00
}
2021-11-04 17:48:32 +08:00
2021-11-18 18:43:20 +08:00
#pragma mark - Private Method
- (void)initSubViews {
2021-11-04 17:48:32 +08:00
[self.view addSubview:self.backContainerView];
[self.view addSubview:self.roomHeaderView];
[self.view addSubview:self.stageView];
[self.view addSubview:self.messageContainerView];
[self.view addSubview:self.menuContainerView];
[self.view addSubview:self.activityContainerView];
2021-12-15 18:12:55 +08:00
[self.view addSubview:self.functionView];
[self.view addSubview:self.animationView];
}
- (void)initSubViewConstraints {
2021-11-04 17:48:32 +08:00
[self.backContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.mas_equalTo(self.view);
}];
[self.roomHeaderView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.top.mas_equalTo(self.view);
make.height.mas_equalTo(kNavigationHeight);
}];
[self.stageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.mas_equalTo(self.view);
make.top.mas_equalTo(self.roomHeaderView.mas_bottom);
}];
[self.messageContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.stageView.mas_bottom);
make.bottom.equalTo(self.menuContainerView.mas_top).offset(-5);
make.left.equalTo(self.view);
make.right.equalTo(self.activityContainerView.mas_left).offset(-10);
}];
[self.menuContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.mas_equalTo(self.view);
make.bottom.mas_equalTo(-kSafeAreaBottomHeight- 8);
make.height.mas_equalTo(40);
}];
[self.activityContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.messageContainerView);
make.right.mas_equalTo(self.view);
make.bottom.mas_equalTo(self.menuContainerView.mas_top).offset(-5);
make.width.mas_equalTo(80);
}];
[self.animationView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.mas_equalTo(self.view);
}];
2021-12-15 18:12:55 +08:00
[self.functionView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.mas_equalTo(self.view);
}];
}
2021-10-26 19:14:01 +08:00
2021-12-03 19:10:35 +08:00
- (void)exitOldRoom {
NSString * roomUid = [NSString stringWithFormat:@"%ld", [XPRoomMiniManager shareManager].getRoomInfo.uid];
2021-12-03 19:10:35 +08:00
if (roomUid.integerValue > 0 && roomUid.integerValue != self.roomUid.integerValue) {
// 退
NSString * roomId = [NSString stringWithFormat:@"%ld", [XPRoomMiniManager shareManager].getRoomInfo.roomId];
[self.presenter exitNIMRoom:roomId];
2021-12-03 19:10:35 +08:00
[[RtcManager instance] exitRoom];
}
}
2021-12-16 20:22:01 +08:00
- (void)userEnterRoomSuccess {
if (self.userInfo.carEffect.length > 0) {
AttachmentModel *attachment = [[AttachmentModel alloc]init];
attachment.first = CustomMessageType_Car_Notify;
attachment.second = Custom_Message_Sub_Car_EnterRoom;
NSMutableDictionary *att = [NSMutableDictionary dictionary];
[att setValue:self.userInfo.carEffect forKey:@"effect"];
[att setValue:self.userInfo.nick forKey:@"nick"];
attachment.data = att;
NSString *sessionID = [NSString stringWithFormat:@"%ld",self.roomInfo.roomId];
NIMMessage *message = [[NIMMessage alloc]init];
NIMCustomObject *object = [[NIMCustomObject alloc] init];
object.attachment = attachment;
message.messageObject = object;
//
NIMSession *session = [NIMSession session:sessionID type:NIMSessionTypeChatroom];
[[NIMSDK sharedSDK].chatManager sendMessage:message toSession:session error:nil];
}
}
2021-10-19 18:30:03 +08:00
#pragma mark - XPRoomProtocol
2021-11-04 17:48:32 +08:00
- (void)initEnterRoomSuccess:(RoomInfoModel *)roomInfo user:(UserInfoModel *)userInfo {
self.roomInfo = roomInfo;
self.userInfo = userInfo;
if (roomInfo.valid) { //
2021-12-01 18:27:08 +08:00
if (roomInfo.roomPwd.length > 0) {//
[self showErrorToast:@"房间已上锁"];
[self enterRoomFail];
} else {
[self.roomHeaderView onRoomUpdate];
2021-12-11 17:02:42 +08:00
[self.activityContainerView onRoomUpdate];
2021-12-13 16:08:24 +08:00
[self.menuContainerView onRoomUpdate];
if ([XPRoomMiniManager shareManager].getRoomInfo.uid != self.roomUid.integerValue) {// 退
2021-12-03 19:10:35 +08:00
[self.presenter enterNIMRoom:[NSString stringWithFormat:@"%ld", self.roomInfo.roomId] user:self.userInfo];
} else {
//退
2021-12-09 15:36:59 +08:00
[self.stageView onRoomEntered];
2021-12-03 19:10:35 +08:00
}
[[XPRoomMiniManager shareManager] configRoomInfo:nil];
2021-12-01 18:27:08 +08:00
}
2021-11-04 17:48:32 +08:00
} else { //
if ([self.roomUid isEqualToString:[NSString stringWithFormat:@"%ld", userInfo.uid]]) { //
if (roomInfo.roomId > 0) { // 使
[self.presenter openRoom:roomInfo.title type:roomInfo.type roomPwd:roomInfo.roomPwd roomDesc:roomInfo.roomDesc backPic:@""];
} else { //
NSString* title = [NSString stringWithFormat:@"%@的房间", userInfo.nick];
[self.presenter openRoom:title type:RoomType_Game roomPwd:@"" roomDesc:@"" backPic:@""];
}
} else { //
2021-10-21 17:55:51 +08:00
// TODO: 线
2021-11-04 17:48:32 +08:00
[self showSuccessToast:@"房主已下线"];
[self enterRoomFail];
2021-10-21 17:55:51 +08:00
}
}
2021-10-19 18:30:03 +08:00
}
- (void)openRoomSuccess:(RoomInfoModel *)roomInfo {
2021-10-26 19:14:01 +08:00
self.roomInfo = roomInfo;
[self.roomHeaderView onRoomUpdate];
2021-12-11 17:02:42 +08:00
[self.activityContainerView onRoomUpdate];
2021-10-26 19:14:01 +08:00
[self.presenter enterNIMRoom:[NSString stringWithFormat:@"%ld", self.roomInfo.roomId] user:self.userInfo];
}
2021-12-09 15:36:59 +08:00
- (void)enterRoomSuccess:(NIMChatroom *)chatRoom {
// room/get线
self.roomInfo.onlineNum = chatRoom.onlineUserCount;
[self.stageView onRoomEntered];
2021-10-19 18:30:03 +08:00
}
2021-10-21 17:55:51 +08:00
- (void)enterRoomFail {
[self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - NIMChatroomManagerDelegate
2021-12-02 00:09:04 +08:00
- (void)chatroomBeKicked:(NIMChatroomBeKickedResult *)result {
if (result.reason == 2) {
[self showErrorToast:@"您被管理员踢出直播间"];
} else if (result.reason == 5) {
[self showErrorToast:@"您已被管理员拉黑"];
}
2021-12-20 21:17:12 +08:00
///退
2021-12-21 12:05:31 +08:00
if (self.roomInfo.roomModeType == RoomModeType_Open_Micro_Mode) {
2021-12-20 21:17:12 +08:00
NSString * roomUid = [NSString stringWithFormat:@"%ld", self.roomInfo.uid];
NSString * uid = [AccountInfoStorage instance].getUid;
2021-12-21 12:05:31 +08:00
[Api getRoomArrangeMicList:^(BaseModel * _Nullable data, NSInteger code, NSString * _Nullable msg) {
if (code == 200) {
ArrangeMicModel * arrangeMicModel= [ArrangeMicModel modelWithJSON:data.data];
if (arrangeMicModel.myPos.intValue > 0) {
[Api cancelArrangeMic:^(BaseModel * _Nullable data, NSInteger code, NSString * _Nullable msg) {} roomUid:roomUid operUid:uid groupType:@"0"];
}
}
} roomUid:roomUid operUid:uid page:@"1" pageSize:@"50"];
2021-12-20 21:17:12 +08:00
}
[[RtcManager instance] exitRoom];
[self dismissViewControllerAnimated:YES completion:nil];
2021-12-02 00:09:04 +08:00
}
#pragma mark - NIMChatManagerDelegate
- (void)onRecvMessages:(NSArray<NIMMessage *> *)messages {
for (NIMMessage * message in messages) {
//
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;
2021-12-09 15:36:59 +08:00
NSInteger onLineNumber = self.roomInfo.onlineNum;
switch (content.eventType) {
case NIMChatroomEventTypeInfoUpdated: //
{
NSDictionary *data = [content.notifyExt toJSONObject];
int type = [data[@"type"] intValue];
switch (type) {
case 1:
case 3:
{
RoomInfoModel * roomInfo =[RoomInfoModel modelWithJSON:data[@"roomInfo"]];
self.roomInfo.leaveMode = roomInfo.leaveMode;
self.roomInfo.showGiftValue = roomInfo.showGiftValue;
2021-12-21 12:05:31 +08:00
self.roomInfo.roomModeType = roomInfo.roomModeType;
[self.stageView onRoomUpdate];
2021-12-20 21:17:12 +08:00
[self.menuContainerView onRoomUpdate];
}
break;
}
}
2021-12-09 15:36:59 +08:00
break;
case NIMChatroomEventTypeEnter:
if (content.source.userId.integerValue != [AccountInfoStorage instance].getUid.integerValue) {
onLineNumber += 1;
2021-12-16 20:22:01 +08:00
}else {///
[self userEnterRoomSuccess];
2021-12-09 15:36:59 +08:00
}
break;
case NIMChatroomEventTypeAddBlack:
case NIMChatroomEventTypeKicked:
case NIMChatroomEventTypeExit:
onLineNumber -= 1;
break;
default:
break;
}
2021-12-09 15:36:59 +08:00
self.roomInfo.onlineNum = onLineNumber;
[self.roomHeaderView onRoomUpdate];
2021-12-17 15:18:34 +08:00
[self.stageView handleNIMNotificationMessage:message];
[self.animationView handleNIMNotificationMessage:message];
} else if (message.messageType == NIMMessageTypeCustom) {
NIMCustomObject *obj = (NIMCustomObject *)message.messageObject;
if (obj.attachment != nil && [obj.attachment isKindOfClass:[AttachmentModel class]]) {
2021-12-24 18:01:30 +08:00
AttachmentModel * attachment = obj.attachment;
[self.stageView handleNIMCustomMessage:attachment];
[self.animationView handleNIMCustomMessage:attachment];
[self.menuContainerView handleNIMCustomMessage:attachment];
[self.activityContainerView handleNIMCustomMessage:attachment];
if (attachment.first == CustomMessageType_First_Recharge_Reward && attachment.second == Custom_Message_Sub_Room_First_Recharge_Reward) {
self.userInfo.isFirstCharge = NO;
[self.menuContainerView onRoomUpdate];
[self.activityContainerView onRoomUpdate];
}
}
}
}
}
2021-12-13 18:45:24 +08:00
//
- (void)sendMessage:(NIMMessage *)message didCompleteWithError:(NSError *)error {
if (error) return;
if (message.messageType == NIMMessageTypeCustom) {
NIMCustomObject *obj = (NIMCustomObject *)message.messageObject;
if (obj.attachment != nil && [obj.attachment isKindOfClass:[AttachmentModel class]]) {
[self.stageView handleNIMCustomMessage:obj.attachment];
2021-12-16 20:22:01 +08:00
[self.animationView handleNIMCustomMessage:obj.attachment];
2021-12-13 18:45:24 +08:00
}
}
}
#pragma mark - NIMLoginManagerDelegate
- (void)onKickout:(NIMLoginKickoutResult *)result {
[self exitRoom];
[self dismissViewControllerAnimated:YES completion:nil];
}
2021-10-26 19:14:01 +08:00
#pragma mark - RoomDelegate
- (RoomInfoModel *)getRoomInfo {
return self.roomInfo;
}
- (UserInfoModel *)getUserInfo {
return self.userInfo;
}
- (void)exitRoom {
2021-12-21 12:05:31 +08:00
if (self.roomInfo.roomModeType == RoomModeType_Open_Micro_Mode) {
NSString * roomUid = [NSString stringWithFormat:@"%ld", self.roomInfo.uid];
NSString * uid = [AccountInfoStorage instance].getUid;
[Api getRoomArrangeMicList:^(BaseModel * _Nullable data, NSInteger code, NSString * _Nullable msg) {
if (code == 200) {
ArrangeMicModel * arrangeMicModel= [ArrangeMicModel modelWithJSON:data.data];
if (arrangeMicModel.myPos.intValue > 0) {
[TTPopup alertWithMessage:@"退出房间后将退出目前排麦,再次进入需要重新排麦,确认退出房间吗?" confirmHandler:^{
[Api cancelArrangeMic:^(BaseModel * _Nullable data, NSInteger code, NSString * _Nullable msg) {
} roomUid:roomUid operUid:uid groupType:@"0"];
[self.presenter exitNIMRoom:[NSString stringWithFormat:@"%ld", self.roomInfo.roomId]];
[[RtcManager instance] exitRoom];
[self dismissViewControllerAnimated:YES completion:nil];
} cancelHandler:^{
}];
2021-12-21 18:14:10 +08:00
return;
2021-12-21 12:05:31 +08:00
}
}
2021-12-21 18:14:10 +08:00
[self.presenter exitNIMRoom:[NSString stringWithFormat:@"%ld", self.roomInfo.roomId]];
[[RtcManager instance] exitRoom];
[self dismissViewControllerAnimated:YES completion:nil];
2021-12-21 12:05:31 +08:00
} roomUid:roomUid operUid:uid page:@"1" pageSize:@"50"];
2021-12-20 21:17:12 +08:00
} else {
[self.presenter exitNIMRoom:[NSString stringWithFormat:@"%ld", self.roomInfo.roomId]];
[[RtcManager instance] exitRoom];
[self dismissViewControllerAnimated:YES completion:nil];
}
}
- (void)miniRoom {
[[XPRoomMiniManager shareManager] configRoomInfo:self.roomInfo];
NSDictionary * dic = @{@"roomInfo":self.roomInfo.model2dictionary, @"userInfo":self.userInfo.model2dictionary, @"microQueue":self.getMicroQueue};
[[NSNotificationCenter defaultCenter] postNotificationName:kRoomMiniNotificationKey object:nil userInfo:dic];
[self.view endEditing:YES];
[self dismissViewControllerAnimated:YES completion:nil];
}
2021-11-15 18:59:44 +08:00
- (UINavigationController *)getCurrentNav {
return self.navigationController;
}
2021-11-04 19:01:19 +08:00
- (NSMutableDictionary<NSString *,MicroQueueModel *> *)getMicroQueue {
return [self.stageView getMicroQueue];
}
- (void)onMicroQueueUpdate:(NSMutableDictionary<NSString *,MicroQueueModel *> *)queue {
[self.menuContainerView onMicroQueueUpdate:queue];
2021-11-04 19:01:19 +08:00
}
- (CGPoint)centerForUserAtUid:(NSString *)uid {
return [self.stageView centerForUserAtUid:uid];
2021-12-03 17:07:21 +08:00
}
- (XPRoomBackContainerView *)backContainerView {
2021-11-04 17:48:32 +08:00
if (!_backContainerView) {
_backContainerView = [[XPRoomBackContainerView alloc] initWithdelegate:self];
2021-11-04 17:48:32 +08:00
}
return _backContainerView;
}
- (RoomHeaderView *)roomHeaderView {
2021-11-04 17:48:32 +08:00
if (!_roomHeaderView) {
_roomHeaderView = [[RoomHeaderView alloc] initWithDelegate:self];
}
return _roomHeaderView;
}
2021-10-26 19:14:01 +08:00
- (SocialStageView *)stageView {
2021-11-04 17:48:32 +08:00
if (!_stageView) {
_stageView = [[SocialStageView alloc] initWithDelegate:self];
}
return _stageView;
}
- (XPRoomMessageContainerView *)messageContainerView {
2021-11-04 17:48:32 +08:00
if (!_messageContainerView) {
_messageContainerView = [[XPRoomMessageContainerView alloc] initWithDelegate:self];
}
return _messageContainerView;
}
- (XPRoomMenuContainerView *)menuContainerView {
2021-11-04 17:48:32 +08:00
if (!_menuContainerView) {
_menuContainerView = [[XPRoomMenuContainerView alloc] initWithDelegate:self];
}
return _menuContainerView;
}
- (XPRoomActivityContainerView *)activityContainerView {
2021-11-04 17:48:32 +08:00
if (!_activityContainerView) {
2021-12-09 20:58:54 +08:00
_activityContainerView = [[XPRoomActivityContainerView alloc] initWithdelegate:self];
2021-11-04 17:48:32 +08:00
}
return _activityContainerView;
}
- (XPRoomAnimationView *)animationView {
if (!_animationView) {
_animationView = [[XPRoomAnimationView alloc] initWithdelegate:self];
}
return _animationView;
}
2021-12-15 18:12:55 +08:00
- (XPRoomFunctionContainerView *)functionView {
if (!_functionView) {
_functionView = [[XPRoomFunctionContainerView alloc] initWithdelegate:self];
}
return _functionView;
}
@end