@@ -15,13 +15,19 @@
# import "NetImageView.h"
# import "NetImageView.h"
# import "RtcManager.h"
# import "RtcManager.h"
# import "UIButton+EnlargeTouchArea.h"
# import "UIButton+EnlargeTouchArea.h"
# import "XPNoteView .h"
# import "TTPopup .h"
# import "XCHudTool.h"
// / Model
// / Model
# import "RoomInfoModel.h"
# import "RoomInfoModel.h"
# import "MicroQueueModel.h"
# import "MicroStateModel.h"
# import "UserInfoModel.h"
# import "AttachmentModel.h"
// / View
// / View
# import "XPRoomViewController.h"
# import "XPRoomViewController.h"
# import "XPNoteView.h"
@ interface XPMiniRoomView ( )
@ interface XPMiniRoomView ( ) < NIMChatroomManagerDelegate , NIMChatManagerDelegate >
// / 底 部 的 底 图
// / 底 部 的 底 图
@ property ( nonatomic , strong ) UIImageView * backImageView ;
@ property ( nonatomic , strong ) UIImageView * backImageView ;
// / 头 像
// / 头 像
@@ -36,10 +42,14 @@
@ property ( nonatomic , strong ) XPNoteView * noteView ;
@ property ( nonatomic , strong ) XPNoteView * noteView ;
// / 房 间 最 小 化
// / 房 间 最 小 化
@ property ( nonatomic , strong ) RoomInfoModel * roomInfo ;
@ property ( nonatomic , strong ) RoomInfoModel * roomInfo ;
// / 房 间 最 小 化 的 时 候 传 入 的 用 户 信 息
@ property ( nonatomic , strong ) UserInfoModel * userInfo ;
// / 当 前 最 小 化 的 房 间 的 uid 如 果 没 有 的 话 为 nil
// / 当 前 最 小 化 的 房 间 的 uid 如 果 没 有 的 话 为 nil
@ property ( nonatomic , strong ) NSString * currentRoomUid ;
@ property ( nonatomic , strong ) NSString * currentRoomUid ;
// / 房 间 的 id
// / 房 间 的 id
@ property ( nonatomic , strong ) NSString * currentRoomId ;
@ property ( nonatomic , strong ) NSString * currentRoomId ;
// / 麦 序
@ property ( nonatomic , strong ) NSMutableDictionary < NSString * , MicroQueueModel * > * micQueue ;
@ end
@ end
@@ -57,6 +67,8 @@
- ( instancetype ) initWithFrame : ( CGRect ) frame {
- ( instancetype ) initWithFrame : ( CGRect ) frame {
self = [ super initWithFrame : frame ] ;
self = [ super initWithFrame : frame ] ;
if ( self ) {
if ( self ) {
[ [ NIMSDK sharedSDK ] . chatroomManager addDelegate : self ] ;
[ [ NIMSDK sharedSDK ] . chatManager addDelegate : self ] ;
[ self initEvents ] ;
[ self initEvents ] ;
[ self initSubViews ] ;
[ self initSubViews ] ;
[ self initSubViewConstraints ] ;
[ self initSubViewConstraints ] ;
@@ -64,11 +76,13 @@
return self ;
return self ;
}
}
# pragma mark - Public Method
# pragma mark - Public Method
- ( void ) configRoomMiniView : ( RoomInfoModel * ) roomInfo {
- ( void ) configRoomMiniView : ( RoomInfoModel * ) roomInfo userInfo : ( UserInfoModel * ) userInfo micQueue : ( NSMutableDictionary * ) micQueue {
self . hidden = NO ;
self . hidden = NO ;
self . currentRoomUid = [ NSString stringWithFormat : @ "%ld" , roomInfo . uid ] ;
self . currentRoomUid = [ NSString stringWithFormat : @ "%ld" , roomInfo . uid ] ;
self . currentRoomId = [ NSString stringWithFormat : @ "%ld" , roomInfo . roomId ] ;
self . currentRoomId = [ NSString stringWithFormat : @ "%ld" , roomInfo . roomId ] ;
self . roomInfo = roomInfo ;
self . roomInfo = roomInfo ;
self . userInfo = userInfo ;
self . micQueue = micQueue ;
[ self startAvatarAnimation ] ;
[ self startAvatarAnimation ] ;
[ self . noteView startAnimation ] ;
[ self . noteView startAnimation ] ;
}
}
@@ -78,6 +92,9 @@
[ self . noteView stopAnimation ] ;
[ self . noteView stopAnimation ] ;
self . currentRoomUid = nil ;
self . currentRoomUid = nil ;
self . currentRoomId = nil ;
self . currentRoomId = nil ;
self . roomInfo = nil ;
self . userInfo = nil ;
self . micQueue = nil ;
self . hidden = YES ;
self . hidden = YES ;
}
}
@@ -145,6 +162,177 @@
rotationAnimation . removedOnCompletion = NO ;
rotationAnimation . removedOnCompletion = NO ;
[ self . avatarImageView . layer addAnimation : rotationAnimation forKey : @ "rotationAnimation" ] ;
[ self . avatarImageView . layer addAnimation : rotationAnimation forKey : @ "rotationAnimation" ] ;
}
}
# pragma mark - NIMChatroomManagerDelegate
- ( void ) chatroomBeKicked : ( NIMChatroomBeKickedResult * ) result {
if ( result . reason = = 2 ) {
[ XCHUDTool showErrorWithMessage : @ "您被管理员踢出直播间" ] ;
} else if ( result . reason = = 5 ) {
[ XCHUDTool showErrorWithMessage : @ "您已被管理员拉黑" ] ;
}
[ [ RtcManager instance ] exitRoom ] ;
[ self hiddenRoomMiniView ] ;
}
# 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 ;
switch ( content . eventType ) {
case NIMChatroomEventTypeInfoUpdated : // 麦 序 状 态 更 新
{
NSDictionary * data = [ content . notifyExt toJSONObject ] ;
int type = [ data [ @ "type" ] intValue ] ;
switch ( type ) {
case 1 :
break ;
case 2 :
case 3 :
[ self handleNIMNotificationMessage : content ] ;
break ;
}
}
default :
break ;
}
} else if ( message . messageType = = NIMMessageTypeCustom ) {
NIMCustomObject * obj = ( NIMCustomObject * ) message . messageObject ;
if ( obj . attachment ! = nil && [ obj . attachment isKindOfClass : [ AttachmentModel class ] ] ) {
[ self handleNIMCustomMessage : obj . attachment ] ;
}
}
}
}
- ( void ) handleNIMNotificationMessage : ( NIMChatroomNotificationContent * ) content {
BOOL microQueueChanged = NO ;
switch ( content . eventType ) {
case NIMChatroomEventTypeInfoUpdated : // 麦 序 状 态 更 新
{
NSDictionary * data = [ content . notifyExt toJSONObject ] ;
int type = [ data [ @ "type" ] intValue ] ;
NSArray * microStates ;
switch ( type ) {
case 2 :
microStates = @ [ [ MicroStateModel modelWithJSON : data [ @ "micInfo" ] ] ] ;
break ;
case 3 :
microStates = [ MicroStateModel modelsWithArray : data [ @ "micInfo" ] ] ;
break ;
}
if ( microStates && microStates . count > 0 ) {
for ( MicroStateModel * microState in microStates ) {
MicroQueueModel * micSequence = [ self . micQueue objectForKey : [ NSString stringWithFormat : @ "%d" , microState . position ] ] ;
micSequence . microState = microState ;
}
microQueueChanged = YES ;
}
}
break ;
case NIMChatroomEventTypeQueueChange : // 麦 序 上 下 麦
{
NSDictionary * data = ( NSDictionary * ) content . ext ;
NSString * position = [ data objectForKey : NIMChatroomEventInfoQueueChangeItemKey ] ;
UserInfoModel * userInfo = [ UserInfoModel modelWithJSON : [ data objectForKey : NIMChatroomEventInfoQueueChangeItemValueKey ] ] ;
NSInteger changeType = [ data [ NIMChatroomEventInfoQueueChangeTypeKey ] integerValue ] ;
// 先 清 除 该 用 户 旧 的 麦 位
for ( MicroQueueModel * sequence in self . micQueue . allValues ) {
if ( userInfo . uid = = sequence . userInfo . uid ) {
sequence . userInfo = nil ;
}
}
if ( changeType = = 1 ) { // 上 麦
MicroQueueModel * sequence = [ self . micQueue objectForKey : position ] ;
sequence . userInfo = userInfo ;
}
microQueueChanged = YES ;
}
break ;
case NIMChatroomEventTypeExit :
case NIMChatroomEventTypeKicked :
{
for ( NIMChatroomNotificationMember * member in content . targets ) {
for ( MicroQueueModel * sequence in self . micQueue . allValues ) {
if ( member . userId . integerValue = = sequence . userInfo . uid ) {
sequence . userInfo = nil ;
microQueueChanged = YES ;
}
}
}
}
break ;
default :
break ;
}
if ( microQueueChanged ) {
[ self microQueueUpdated ] ;
}
}
- ( void ) microQueueUpdated {
BOOL selfNeedBroadcast = NO ;
for ( int i = 0 ; i < self . micQueue . allKeys . count ; i + + ) {
MicroQueueModel * model = [ self . micQueue objectForKey : [ NSString stringWithFormat : @ "%d" , i ] ] ;
if ( self . userInfo . uid && model . userInfo . uid = = self . userInfo . uid ) {
selfNeedBroadcast = model . microState . micState = = MicroMicStateType_Open ;
}
}
[ [ RtcManager instance ] broadcast : selfNeedBroadcast ] ;
}
- ( void ) handleNIMCustomMessage : ( AttachmentModel * ) attachment {
if ( attachment . first = = CustomMessageType_Queue && attachment . second = = Custom_Message _Sub _Queue _Invite ) {
NSDictionary * dic = attachment . data ;
NSString * uid = dic [ @ "uid" ] ;
if ( uid . integerValue = = self . userInfo . uid ) {
NSNumber * micPosition = dic [ @ "micPosition" ] ;
NSString * position = micPosition . stringValue ;
MicroQueueModel * micro = [ self . micQueue objectForKey : position ] ;
if ( ! micro || micro . userInfo ) return ; // 当 前 麦 位 有 人 , 什 么 都 不 做 。
RoomInfoModel * roomInfo = self . roomInfo ;
NSString * roomId = [ NSString stringWithFormat : @ "%ld" , roomInfo . roomId ] ;
UserInfoModel * userInfo = self . userInfo ;
NIMChatroomQueueUpdateRequest * request = [ [ NIMChatroomQueueUpdateRequest alloc ] init ] ;
request . key = position ;
request . value = [ userInfo toJSONString ] ;
request . roomId = roomId ;
request . transient = YES ;
[ [ NIMSDK sharedSDK ] . chatroomManager updateChatroomQueueObject : request completion : ^ ( NSError * _Nullable error ) {
if ( error ) return ;
[ RtcManager instance ] . localMuted = YES ;
TTAlertConfig * config = [ [ TTAlertConfig alloc ] init ] ;
config . title = @ "房主或管理员拉你上麦" ;
config . message = @ "你已被房主或管理员拉上麦,但并未开启麦克风。如需要说话,请打开麦克风。" ;
[ TTPopup alertWithConfig : config confirmHandler : ^ {
} cancelHandler : ^ {
} ] ;
} ] ;
}
}
}
# pragma mark - Event Response
# pragma mark - Event Response
@@ -157,7 +345,6 @@
- ( void ) enterRomRecognizer : ( UITapGestureRecognizer * ) tap {
- ( void ) enterRomRecognizer : ( UITapGestureRecognizer * ) tap {
if ( self . currentRoomUid > 0 && self . controller ) {
if ( self . currentRoomUid > 0 && self . controller ) {
[ XPRoomViewController openRoom : self . currentRoomUid viewController : self . controller ] ;
[ XPRoomViewController openRoom : self . currentRoomUid viewController : self . controller ] ;
[ self hiddenRoomMiniView ] ;
}
}
}
}
@@ -209,12 +396,8 @@
self . center = newCenter ;
self . center = newCenter ;
}
}
} ] ;
} ] ;
} else {
NSLog ( @ "pan state : %zd" , p . state ) ;
}
}
}
}
# pragma mark - Getters And Setters
# pragma mark - Getters And Setters
- ( void ) setRoomInfo : ( RoomInfoModel * ) roomInfo {
- ( void ) setRoomInfo : ( RoomInfoModel * ) roomInfo {
_roomInfo = roomInfo ;
_roomInfo = roomInfo ;