最小化房间执行一些下麦上麦的操作

This commit is contained in:
fengshuo
2021-12-08 14:36:35 +08:00
parent a55c6a268c
commit 36415cca74
4 changed files with 198 additions and 13 deletions

View File

@@ -137,6 +137,8 @@
sequence.userInfo = userInfo;
}
[self microQueueUpdated];
//
[self.hostDelegate onMicroQueueUpdate:self.micQueue];
}];
}

View File

@@ -8,7 +8,7 @@
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@class RoomInfoModel;
@class RoomInfoModel, UserInfoModel;
@interface XPMiniRoomView : UIView
/*
@@ -23,7 +23,7 @@ NS_ASSUME_NONNULL_BEGIN
///房间的id
@property (nonatomic,strong, readonly) NSString *currentRoomId;
///房间最小化 初始化
- (void)configRoomMiniView:(RoomInfoModel *)roomInfo;
- (void)configRoomMiniView:(RoomInfoModel *)roomInfo userInfo:(UserInfoModel *)userInfo micQueue:(NSMutableDictionary *)micQueue;
///隐藏最小化的view
- (void)hiddenRoomMiniView;
@end

View File

@@ -15,13 +15,19 @@
#import "NetImageView.h"
#import "RtcManager.h"
#import "UIButton+EnlargeTouchArea.h"
#import "XPNoteView.h"
#import "TTPopup.h"
#import "XCHudTool.h"
///Model
#import "RoomInfoModel.h"
#import "MicroQueueModel.h"
#import "MicroStateModel.h"
#import "UserInfoModel.h"
#import "AttachmentModel.h"
///View
#import "XPRoomViewController.h"
#import "XPNoteView.h"
@interface XPMiniRoomView ()
@interface XPMiniRoomView ()<NIMChatroomManagerDelegate, NIMChatManagerDelegate>
///
@property (nonatomic,strong) UIImageView *backImageView;
///
@@ -36,10 +42,14 @@
@property (nonatomic,strong) XPNoteView *noteView;
///
@property (nonatomic,strong) RoomInfoModel *roomInfo;
///
@property (nonatomic,strong) UserInfoModel *userInfo;
///uid nil
@property (nonatomic,strong) NSString *currentRoomUid;
///id
@property (nonatomic,strong) NSString *currentRoomId;
///
@property (nonatomic, strong) NSMutableDictionary<NSString *, MicroQueueModel *> *micQueue;
@end
@@ -57,6 +67,8 @@
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[[NIMSDK sharedSDK].chatroomManager addDelegate:self];
[[NIMSDK sharedSDK].chatManager addDelegate:self];
[self initEvents];
[self initSubViews];
[self initSubViewConstraints];
@@ -64,11 +76,13 @@
return self;
}
#pragma mark - Public Method
- (void)configRoomMiniView:(RoomInfoModel *)roomInfo {
- (void)configRoomMiniView:(RoomInfoModel *)roomInfo userInfo:(UserInfoModel *)userInfo micQueue:(NSMutableDictionary *)micQueue {
self.hidden = NO;
self.currentRoomUid = [NSString stringWithFormat:@"%ld", roomInfo.uid];
self.currentRoomId = [NSString stringWithFormat:@"%ld", roomInfo.roomId];
self.roomInfo = roomInfo;
self.userInfo = userInfo;
self.micQueue = micQueue;
[self startAvatarAnimation];
[self.noteView startAnimation];
}
@@ -78,6 +92,9 @@
[self.noteView stopAnimation];
self.currentRoomUid = nil;
self.currentRoomId = nil;
self.roomInfo = nil;
self.userInfo = nil;
self.micQueue = nil;
self.hidden = YES;
}
@@ -145,6 +162,177 @@
rotationAnimation.removedOnCompletion = NO;
[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
@@ -157,7 +345,6 @@
- (void)enterRomRecognizer:(UITapGestureRecognizer *)tap {
if (self.currentRoomUid > 0 && self.controller) {
[XPRoomViewController openRoom:self.currentRoomUid viewController:self.controller];
[self hiddenRoomMiniView];
}
}
@@ -209,12 +396,8 @@
self.center = newCenter;
}
}];
} else {
NSLog(@"pan state : %zd", p.state);
}
}
#pragma mark - Getters And Setters
- (void)setRoomInfo:(RoomInfoModel *)roomInfo {
_roomInfo = roomInfo;

View File

@@ -207,7 +207,7 @@
[self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - NIMChatManagerDelegate
#pragma mark - NIMChatroomManagerDelegate
- (void)chatroomBeKicked:(NIMChatroomBeKickedResult *)result {
if (result.reason == 2) {
[self showErrorToast:@"您被管理员踢出直播间"];
@@ -215,7 +215,7 @@
[self showErrorToast:@"您已被管理员拉黑"];
}
[[RtcManager instance] exitRoom];
[self enterRoomFail];
[self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - NIMChatManagerDelegate
@@ -280,7 +280,7 @@
}
- (void)miniRoom {
[[XPMiniRoomView shareMiniRoomView] configRoomMiniView:self.roomInfo];
[[XPMiniRoomView shareMiniRoomView] configRoomMiniView:self.roomInfo userInfo:self.userInfo micQueue:[self getMicroQueue]];
[self.view endEditing:YES];
[self dismissViewControllerAnimated:YES completion:nil];
}