123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474 |
- //
- // XYChatRoomManager.m
- // Starbuds
- //
- // Created by 翟玉磊 on 2020/6/29.
- // Copyright © 2020 翟玉磊. All rights reserved.
- //
- #import "XYChatRoomManager.h"
- #import "XYFloatingDragView.h"
- #import "XYChatRoomShowAnimationView.h"
- #import "XYPeiPlayCommendAlertView.h"
- #import "XYCrossRoomPkManager.h"
- #import "XYBankIdentityCardVerIfyViewController.h"
- @interface XYChatRoomManager ()<XYFloatingDragViewDelegate>
- /// 语音悬浮窗
- @property (nonatomic, strong) XYFloatingDragView *floatingDragView;
- @property (nonatomic, strong) XYChatRoomShowAnimationView *showAnimationView;
- @property (nonatomic, copy) NSString *roomId;
- @end
- @implementation XYChatRoomManager
- #pragma mark — 单例
- static XYChatRoomManager *_singleInstance = nil;
- + (instancetype)sharedInstance {
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
- if (_singleInstance == nil) {
- _singleInstance = [[self alloc]init];
- }
- });
- return _singleInstance;
- }
- + (instancetype)allocWithZone:(struct _NSZone *)zone {
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
- _singleInstance = [super allocWithZone:zone];
- });
- return _singleInstance;
- }
- - (id)copyWithZone:(NSZone *)zone {
- return _singleInstance;
- }
- - (id)mutableCopyWithZone:(NSZone *)zone {
- return _singleInstance;
- }
- #pragma mark — Public
- - (void)showChatRoomWithRoomId:(NSString *)roomId roomCoverUrl:(NSString *)roomCoverUrl completion:(void (^ __nullable)(void))completion {
-
- if (![XYUserInfoManager nowUser]) {
- [ApplicationDelegate createQulickLoginPageVC];
- return;
- }
-
- if (StringIsEmpty(roomId)) {
- !completion?:completion();
- return;
- }
- if (!ApplicationDelegate.tabbarVC) {
- return;
- }
- // 所有进入语音房的次数,加载房间信息的时候统计
- [StatisticsManager event:@"voiceroom_load"];
- // 从外部点击进入语音房时上报,区分不同来源
- [StatisticsManager event:@"goto_voiceroom"];
-
- // 获取房间信息 判断是否需要密码
- if (StringIsNotEmpty(self.roomId)) {
- if (![self.roomId isEqualToString:roomId]) {
- // 先销毁悬浮窗
- [self closeChatRoomBlock:^{
- [self.showAnimationView showChatRoomAnimationWithRoomCoverUrl:roomCoverUrl completion:^{
- [self jainRoomWithRoomId:roomId pass:@"" completion:completion];
- }];
- }];
- }else {
- // 因为id一直且存在直播间 说明是打开的同一个房间 则先打开直播间界面再关闭悬浮窗
- [self openFloatingDragWindow];
- }
- }else {
- // 记录进入房间的时间戳
- [[XYPeiPlayCommendAlertView shareInstance] joinRoom];
- // 显示进场动画
- [self.showAnimationView showChatRoomAnimationWithRoomCoverUrl:roomCoverUrl completion:^{
- [self jainRoomWithRoomId:roomId pass:@"" completion:completion];
- }];
- }
- }
- - (void)jainRoomWithRoomId:(NSString *)roomId pass:(NSString *)pass completion:(void (^ __nullable)(void))completion {
- // 不相等则需要请求房间信息,确定是否需要密码
- [[XYVoiceRoomAPIManager new] getRoomInfoWithRoomId:roomId pass:pass successHandler:^(ZYLResponseModel *responseModel) {
-
- self.liveRoomVCInfo = [[XYLiveRoomVCInfo alloc] init];
- [self.liveRoomVCInfo create:responseModel.data];
- [XYChatRoomManager sharedInstance].liveRoomVCInfo = self.liveRoomVCInfo;
-
- BOOL needPass = [responseModel.data[@"needPass"] boolValue];
- if (needPass) {
- // 关闭进场动画
- [self.showAnimationView dismissCompletion:nil];
- self.showAnimationView = nil;
-
- UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"该房间已上锁" message:nil preferredStyle:UIAlertControllerStyleAlert];
- [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
- [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
- UITextField *userNameTextField = alertController.textFields.firstObject;
- NSLog(@"房间密码:%@",userNameTextField.text);
- [self jainRoomWithRoomId:roomId pass:userNameTextField.text completion:completion];
- }]];
- [alertController addTextFieldWithConfigurationHandler:^(UITextField*_Nonnull textField) {
- textField.placeholder = @"请输入房间密码";
- textField.secureTextEntry = YES;
- }];
- [ApplicationDelegate.tabbarVC presentViewController:alertController animated:YES completion:nil];
- }else {
- if (self.chatRoomController==nil) {
- // 初始化控制器
- self.chatRoomController = XYChatRoomViewController.new;
- XYChatRoomInfoManager *infoManager = XYChatRoomInfoManager.new;
- infoManager.delegate = self.chatRoomController;
- self.chatRoomController.infoManager = infoManager;
- [self.chatRoomController roomRTCManager];
- }
- self.roomId = [roomId copy];
-
- // 设置直播间信息
- XYChatRoomInfoModel *model = XYChatRoomInfoModel.new;
- [model yy_modelSetWithDictionary:responseModel.data];
- [model yy_modelSetWithDictionary:responseModel.data[@"room"]];
- self.chatRoomController.infoManager.roomInfoModel = model;
-
- // 麦位信息
- self.chatRoomController.infoManager.seatsArray = [NSMutableArray array];
- NSMutableArray *seats = [NSMutableArray array];
- if (self.chatRoomController.infoManager.roomInfoModel.roomType == XYVoiceRoomTypeVoiceLive) {
- NSArray *tempSeats = responseModel.data[@"seats"];
- if (tempSeats.count > 5) {
- [seats addObjectsFromArray:[tempSeats subarrayWithRange:NSMakeRange(0, 5)]];
- }else {
- [seats addObjectsFromArray:responseModel.data[@"seats"]];
- }
- }else {
- [seats addObjectsFromArray:responseModel.data[@"seats"]];
- }
- // 先重置用户的上麦状态
- self.chatRoomController.infoManager.isCurrentOnMicrophone = NO;
- self.chatRoomController.infoManager.isCurrentHost = NO;
- for (NSDictionary *dict in seats) {
- XYChatRoomSeatsModel *seatModel = XYChatRoomSeatsModel.new;
- [seatModel yy_modelSetWithDictionary:dict];
- seatModel.isShowScore = self.chatRoomController.infoManager.roomInfoModel.roomScoreEnabled;
- [self.chatRoomController.infoManager.seatsArray addObject:seatModel];
- if (seatModel.user) {
- // 麦位用户
- [self.chatRoomController.infoManager.seatUserArray addObject:seatModel.user];
- // 获取当前用户的上麦的状态
- if ([seatModel.user.userId isEqualToString:[XYUserInfoManager nowUser].userId]) {
- self.chatRoomController.infoManager.isCurrentOnMicrophone = YES;
- if (seatModel.seatNo == 0) {
- self.chatRoomController.infoManager.isCurrentHost = YES;
- }
- }
- }
- }
-
- self.chatRoomController.infoManager.role = [responseModel.data[@"role"] integerValue];
- self.chatRoomController.infoManager.stage = [responseModel.data[@"stage"] integerValue];
- self.chatRoomController.infoManager.isFav = [responseModel.data[@"isFav"] boolValue];
-
- NSDictionary *profile = responseModel.data[@"profile"];
- if (profile) {
- NSMutableDictionary *profileDict = [[NSMutableDictionary alloc] init];
- [profileDict setDictionary:profile];
- [profileDict setObject:[XYUserInfoManager nowUser].headWearAnimationId forKey:@"headWearAnimationId"];
- self.chatRoomController.infoManager.profile = profileDict;
- }
-
- self.chatRoomController.infoManager.timestamp = responseModel.data[@"timestamp"];
-
- // 获取rtc的认证信息
- XYChatRoomRTCAuthInfoModel *rtcModel = XYChatRoomRTCAuthInfoModel.new;
- [rtcModel yy_modelSetWithDictionary:responseModel.data[@"authInfo"]];
- self.chatRoomController.infoManager.rtcAuthInfo = rtcModel;
-
- // pk相关
- self.chatRoomController.infoManager.battleEnabled = self.chatRoomController.infoManager.KFBattleEnabled = [responseModel.data[@"battleEnabled"] boolValue];
- NSDictionary *battleRoomIn = responseModel.data[@"battleRoomIn"];
- if (battleRoomIn) {
- XYChatRoomBattleRoomInModel *battleRoomInModel = XYChatRoomBattleRoomInModel.new;
- [battleRoomInModel yy_modelSetWithDictionary:battleRoomIn];
-
- self.chatRoomController.infoManager.battleRoomIn = battleRoomInModel;
- if (battleRoomInModel.battleStatus == 201) {
- self.chatRoomController.infoManager.pkState = YES;
- }else {
- self.chatRoomController.infoManager.pkState = NO;
- }
- }
- // 跨房PK
- NSDictionary *battleRoomCross = responseModel.data[@"battleRoomCross"];
- if (battleRoomCross) {
- NSMutableDictionary *dataDict = [[NSMutableDictionary alloc] init];
-
- NSMutableDictionary *msgDict = [[NSMutableDictionary alloc] initWithDictionary:battleRoomCross];
- [msgDict setObject:RTC_BATTLE_ROOM_CROSS_STATUS forKey:@"type"];
-
- [dataDict setObject:msgDict forKey:@"msg"];
-
- [XYCrossRoomPkManager sharedInstance].battleRoomCrossDict = dataDict;
-
- XYChatRoomKFBattleRoomInModel *battleRoomInModel = XYChatRoomKFBattleRoomInModel.new;
- [battleRoomInModel yy_modelSetWithDictionary:battleRoomCross];
- self.chatRoomController.infoManager.battleRoomCross = battleRoomInModel;
- if (battleRoomInModel.battleStatus == 201) {
- self.chatRoomController.infoManager.KFPkState = YES;
- }else {
- self.chatRoomController.infoManager.KFPkState = NO;
- }
- }
-
- // 跨房PK
- NSDictionary *auction = responseModel.data[@"auction"];
- if (auction) {
- XYChatRoomAuctionInfoModel *auctionModel = XYChatRoomAuctionInfoModel.new;
- [auctionModel yy_modelSetWithDictionary:auction];
- self.chatRoomController.infoManager.auctionInfoModel = auctionModel;
- self.chatRoomController.infoManager.isAuctioning = YES;
- }
-
- // 小时榜开关
- self.chatRoomController.infoManager.hourRankEnabled = [responseModel.data[@"hourRankEnabled"] boolValue];
-
- // 审核中不显示规则
- if (ApplicationDelegate.isVersionStatus) {
- self.chatRoomController.infoManager.roomInfoModel.roomRule = @"";
- }
-
- // 个播相关
- if (self.chatRoomController.infoManager.roomInfoModel.roomType == XYVoiceRoomTypeVoiceLive) {
- self.chatRoomController.infoManager.singleRtcHisId = [BaseMethod toString:responseModel.data[@"singleRtcHisId"]];
- // 主播是否暂离
- self.chatRoomController.infoManager.isSingleRtcAnchorOnline = [responseModel.data[@"isSingleRtcAnchorOnline"] boolValue];
- }
-
- // 是否开播
- self.chatRoomController.infoManager.isLive = [responseModel.data[@"isLive"] boolValue];
-
- // 房间类型统计
- if (self.chatRoomController.infoManager.roomInfoModel.roomType == XYVoiceRoomTypeLove) {
- // 进入[相亲房]的次数,在进入语音房时上报
- [StatisticsManager event:@"goto_dateroom"];
- }else if (self.chatRoomController.infoManager.roomInfoModel.roomType == XYVoiceRoomTypeOrder) {
- // 进入[派单房]的次数,在进入语音房时上报
- [StatisticsManager event:@"goto_dispatchroom"];
- }else if (self.chatRoomController.infoManager.roomInfoModel.roomType == XYVoiceRoomTypeAuction) {
- // 进入[拍卖房]的次数,在进入语音房时上报
- [StatisticsManager event:@"goto_auctionroom"];
- }else if (self.chatRoomController.infoManager.roomInfoModel.roomType == XYVoiceRoomTypeVoiceLive) {
- // 进入[个播房]的次数,在进入语音房时上报
- [StatisticsManager event:@"goto_personalliveroom"];
- }
-
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
- // 更新进入房间
- [self.chatRoomController enterRoom];
- });
- // 显示动画
- [self showAnimationCompletion:completion];
- }
- } failureHandler:^(ZYLNetworkError *error) {
- // 关闭进场动画
- [self.showAnimationView dismissCompletion:^{
- [SVProgressHUD showInfoWithStatus:error.domain];
- self.showAnimationView = nil;
- }];
-
- NSDictionary *data1 = error.responseObject;
- if ([data1[@"data"][@"goAuth"] isEqualToString:@"1"]) {//需要认证
- CustomActionAlertController *al = [[CustomActionAlertController alloc] initWithTitle:nil message:kLocalizedString(@"根据《网络游戏管理暂行办法》要求,未实名认证部分功能的使用将受到限制") sure:kLocalizedString(@"去认证") cancel:kLocalizedString(@"取消") selctedBlock:^(NSInteger index, NSString * _Nonnull title) {
- if ([title isEqualToString:kLocalizedString(@"去认证")]) {
- XYBankIdentityCardVerIfyViewController *controller = XYBankIdentityCardVerIfyViewController.new;
- controller.targetAction = 2;
- [[AppDelegate sharedDelegate].getCurrentController.navigationController pushViewController:controller animated:YES];
- }
- }];
- [al show];
- }
-
- }];
- }
- - (void)showAnimationCompletion:(void (^ __nullable)(void))completion {
-
- UINavigationController *currentNav = [ApplicationDelegate getCurrentController].navigationController;
- if ([currentNav.topViewController isKindOfClass:XYChatRoomViewController.class]) {
- !completion?:completion();
- return;
- }
-
- [currentNav pushViewController:self.chatRoomController animated:YES];
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
- // 阻止设备锁屏
- [self.showAnimationView dismissCompletion:^{
- self.showAnimationView = nil;
- [UIApplication sharedApplication].idleTimerDisabled = YES;
- !completion?:completion();
- }];
- });
- }
- /// 转变成悬浮按钮 showFloatingDrag YES显示小窗口
- - (void)dismissChatRoomWithShowFloatingDrag:(BOOL)showFloatingDrag completion:(void (^ __nullable)(void))completion {
-
- if (!self.isChatRooming) {
- !completion?:completion();
- return;
- }
-
- if (showFloatingDrag) {
- [self.chatRoomController.navigationController popViewControllerAnimated:YES];
- [self showVoiceFloatingDrag];
- // 开启设备锁屏
- [UIApplication sharedApplication].idleTimerDisabled = NO;
- !completion?:completion();
- }else {
- // 1.设置关闭状态
- self.chatRoomController.infoManager.enterClosedState = YES;
- // 2.显示loading
- [SVProgressHUD showWithStatus:kLocalizedString(@"关闭中")];
- // 3.关闭rtc、rtm、下麦
- [self.chatRoomController requestCloseRoomBlock:^{
- // 4.回收栈位
- UINavigationController *currentNavigation = self.chatRoomController.navigationController;
- NSMutableArray *marr = [[NSMutableArray alloc]initWithArray:currentNavigation.viewControllers];
- if ([[currentNavigation.viewControllers lastObject] isKindOfClass:XYChatRoomViewController.class]) {
- [self.chatRoomController.navigationController popViewControllerAnimated:YES];
- }else {
- for (NSInteger i = 0; i < marr.count; i++) {
- UIViewController *vc = marr[i];
- //防止删掉当前控制器
- if (i != (marr.count-1)) {
- if ([vc isKindOfClass:XYChatRoomViewController.class]) {
- [marr removeObject:vc];
- break;
- }
- }
- }
- currentNavigation.viewControllers = marr;
- if (currentNavigation.viewControllers.count == marr.count) {
- // 没删除则使用系统回收
- [self.chatRoomController.navigationController popViewControllerAnimated:YES];
- }
- }
- // 5.销毁语聊房
- [self clearChatRoom];
- !completion?:completion();
- }];
- }
- }
- - (BOOL)isChatRooming {
- if (_chatRoomController) {
- return YES;
- }
- return NO;
- }
- - (BOOL)isShrink {
- // 是否在聊天室
- if (self.isChatRooming) {
- if (_floatingDragView) {
- return YES;
- }
- }
- return NO;
- }
- - (void)showVoiceFloatingDrag {
- if (_floatingDragView == nil) {
- _floatingDragView = [XYFloatingDragView floatingDragViewWithFloatingDragType:XYFloatingDragTypeVoiceChatRoom];
- _floatingDragView.delegate = self;
- [_floatingDragView setupViewDataWithModel:self.chatRoomController.infoManager.roomInfoModel];
- }
- [_floatingDragView showAnimation:YES];
- }
- #pragma mark — XYFloatingDragViewDelegate 悬浮窗回调协议
- /// 展开聊天室
- - (void)openFloatingDragWindow {
- // 显示聊天室
- if (self.isShrink) {
- [self showAnimationCompletion:^{
- }];
- }
- // 销毁悬浮窗
- [self clearFloatingDragWindow];
- }
- /// 关闭聊天室
- - (void)closeFloatingDragWindow {
- [self closeChatRoomBlock:nil];
- }
- /// 销毁悬浮窗
- - (void)clearFloatingDragWindow {
- if (_floatingDragView) {
- [_floatingDragView clear];
- [_floatingDragView removeFromSuperview];
- _floatingDragView = nil;
- }
- }
- #pragma mark — 关闭房间
- - (void)closeChatRoomBlock:(void(^ __nullable)(void))block {
- [self dismissChatRoomWithShowFloatingDrag:NO completion:block];
- }
- - (void)clearChatRoom {
- // 销毁悬浮窗
- [self clearFloatingDragWindow];
- // 销毁控制器
- if (_chatRoomController) {
- [_chatRoomController clear];
- [_chatRoomController.view removeFromSuperview];
- _chatRoomController = nil;
- }
- // 开启设备锁屏
- [UIApplication sharedApplication].idleTimerDisabled = NO;
-
- [self reset];
-
- [SVProgressHUD dismiss];
- }
- //保存玩法已自动显示
- - (void)savePlayMethodShowState:(NSString *)roomId{
- [[NSUserDefaults standardUserDefaults] setObject:@"roomId" forKey:roomId];
- }
- //获取玩法是否已自动显示
- - (BOOL )getPlayMethodShowState:(NSString *)roomId{
- NSString *haveRoomId = [[NSUserDefaults standardUserDefaults] objectForKey:roomId];
- if (haveRoomId.length) {
- return YES;
- }
- return NO;
- }
- #pragma mark — 重置状态
- - (void)reset {
- _roomId = nil;
- }
- #pragma mark — Getter
- - (XYChatRoomShowAnimationView *)showAnimationView {
- if (!_showAnimationView) {
- _showAnimationView = [[XYChatRoomShowAnimationView alloc] initWithFrame:[UIScreen mainScreen].bounds];
- }
- return _showAnimationView;
- }
- @end
|