Files
peko-ios/YuMi/Modules/YMRoom/View/Setting/View/XPRoomSettingViewController.m
liyuhua d22d0aa9fe 1.UI修改
2.修复bug
2024-05-17 11:27:36 +08:00

533 lines
21 KiB
Objective-C

//
// YMRoomSettingViewController.m
// YUMI
//
// Created by YUMI on 2021/12/27.
//
#import "XPRoomSettingViewController.h"
///Third
#import <Masonry/Masonry.h>
///Tool
#import "ThemeColor+Room.h"
#import "YUMIMacroUitls.h"
#import "TTPopup.h"
#import "DESEncrypt.h"
#import "YUMIConstant.h"
#import "AccountInfoStorage.h"
#import "NSArray+Safe.h"
///Model
#import "RoomInfoModel.h"
#import "XPRoomSettingItemModel.h"
#import "AttachMentModel.h"
#import "MicroQueueModel.h"
#import "UserInfoModel.h"
#import "XPKickUserModel.h"
#import "GuildSuperAdminInfoModel.h"
///View
#import "XPRoomSettingTableViewCell.h"
#import "XPRoomSettingInputView.h"
#import "XPRoomTagListView.h"
///P
#import "XPRoomSettingPresenter.h"
#import "XPRoomSettingProtocol.h"
///VC
#import "XPRoomTagListViewController.h"
#import "XPRoomRoleViewController.h"
@interface XPRoomSettingViewController ()<UITableViewDelegate, UITableViewDataSource, XPRoomSettingProtocol, XPRoomSettingTableViewCellDelegate, XPRoomSettingInputViewDelegate>
///房间信息
@property (nonatomic,strong) RoomInfoModel *roomInfo;
///列表
@property (nonatomic,strong) UITableView *tableView;
///数据源
@property (nonatomic,strong) NSArray<NSArray *> *datasource;
///代理
@property (nonatomic,weak) id<RoomHostDelegate> hostDelegate;
///是否是公会超管
@property (nonatomic,assign) BOOL meIsSuperAdmin;
@end
@implementation XPRoomSettingViewController
- (instancetype)initWithDelegate:(id<RoomHostDelegate>)delegate {
if (self = [super init]) {
self.hostDelegate = delegate;
self.roomInfo = self.hostDelegate.getRoomInfo;
}
return self;
}
- (XPRoomSettingPresenter *)createPresenter {
return [[XPRoomSettingPresenter alloc] init];
}
- (void)viewDidLoad {
[super viewDidLoad];
NSString * roomUid = [NSString stringWithFormat:@"%ld", self.roomInfo.uid];
[self.presenter getRoomInfo:roomUid];
///自己是公会超管
BOOL meIsSuperAdmin = NO;
for (GuildSuperAdminInfoModel *managerInfo in self.hostDelegate.getRoomSuperAdminList) {
if ([managerInfo.uid isEqualToString:[AccountInfoStorage instance].getUid]) {
meIsSuperAdmin = YES;
break;
}
}
self.meIsSuperAdmin = meIsSuperAdmin;
[self.presenter getRoomSettingList:self.roomInfo isSuperAdmin:meIsSuperAdmin];
[self initSubViews];
[self initSubViewConstraints];
}
#pragma mark - Private Method
- (void)initSubViews {
self.title = YMLocalizedString(@"XPRoomSettingViewController0");
[self.view addSubview:self.tableView];
}
- (void)initSubViewConstraints {
[self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.mas_equalTo(self.view);
}];
}
- (void)handleRoomLeaveMode {
[self showLoading];
NSString * ownerUid = [NSString stringWithFormat:@"%ld", self.roomInfo.uid];
NSArray *chatRoomMicSequences = [self.hostDelegate.getMicroQueue allValues];
///房主是否在坑位上
BOOL ownerIsOnMic = NO;
NSString * ownerPosition;
if (chatRoomMicSequences != nil && chatRoomMicSequences.count > 0) {
for (int i = 0; i < chatRoomMicSequences.count; i ++) {
MicroQueueModel *micqueueModel = chatRoomMicSequences[i];
if (micqueueModel.userInfo && micqueueModel.userInfo.uid > 0) {
if (micqueueModel.userInfo.uid == ownerUid.integerValue) {
ownerIsOnMic = YES;
ownerPosition = [NSString stringWithFormat:@"%d", micqueueModel.microState.position];
break;
}
}
}
}
NSString * roomId = [NSString stringWithFormat:@"%ld", self.roomInfo.roomId];
///能设置离线模式的只有房主
if (ownerIsOnMic) {
NIMChatroomQueueRemoveRequest *request = [[NIMChatroomQueueRemoveRequest alloc]init];
request.key = ownerPosition;
request.roomId = roomId;
[[NIMSDK sharedSDK].chatroomManager removeChatroomQueueObject:request completion:^(NSError * _Nullable error, NSDictionary<NSString *,NSString *> * _Nullable element) {
}];
}
NSString *position = @"-1"; // 房主位
UserInfoModel *onMicInfo = [self.hostDelegate.getMicroQueue objectForKey:position].userInfo;
if (onMicInfo) {
NIMChatroomQueueRemoveRequest *request = [[NIMChatroomQueueRemoveRequest alloc]init];
request.key = @"-1";
request.roomId =roomId;
[[NIMSDK sharedSDK].chatroomManager removeChatroomQueueObject:request completion:^(NSError * _Nullable error, NSDictionary<NSString *,NSString *> * _Nullable element) {
if (error == nil) {
XPKickUserModel *notifyModel = [[XPKickUserModel alloc] init];
notifyModel.handleNick = onMicInfo.nick;
notifyModel.handleUid = [AccountInfoStorage instance].getUid.integerValue;
notifyModel.targetUid = onMicInfo.uid;
notifyModel.targetNick = onMicInfo.nick;
notifyModel.uid = onMicInfo.uid;
AttachmentModel *attachment = [[AttachmentModel alloc] init];
attachment.first = CustomMessageType_Queue;
attachment.second = Custom_Message_Sub_Queue_Kick;
attachment.data = [notifyModel model2dictionary];
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];
}
}];
}
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.6 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSString * roomUid = [NSString stringWithFormat:@"%ld", self.roomInfo.uid];
[self hideHUD];
[self.presenter openRoomLeaveMode:roomUid];
});
}
#pragma mark - UITableViewDelegate And UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return self.datasource.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.datasource[section].count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
XPRoomSettingTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([XPRoomSettingTableViewCell class])];
if (cell == nil) {
cell = [[XPRoomSettingTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([XPRoomSettingTableViewCell class])];
}
cell.delegate = self;
cell.itemModel = [[self.datasource safeObjectAtIndex1:indexPath.section] safeObjectAtIndex1:indexPath.row];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
XPRoomSettingItemModel * itemModel = [[self.datasource safeObjectAtIndex1:indexPath.section] safeObjectAtIndex1:indexPath.row];
if (itemModel.type == RoomSettingItemType_Leave_Model && itemModel.switchState) {
return 60;
}
return 50;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
UIView * view = [[UIView alloc] init];
view.frame = CGRectMake(0, 0, KScreenWidth, 10);
view.backgroundColor = [UIColor clearColor];
return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 10;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
XPRoomSettingItemModel * itemModel = [[self.datasource safeObjectAtIndex1:indexPath.section] safeObjectAtIndex1:indexPath.row];
NSString * roomUid = [NSString stringWithFormat:@"%ld", self.roomInfo.uid];
NSString * roomId = [NSString stringWithFormat:@"%ld", self.roomInfo.roomId];
NSString * roomTag = [NSString stringWithFormat:@"%ld", self.roomInfo.tagId];
NSString *roomClassifyId = self.roomInfo.singleRoomSortId;
switch (itemModel.type) {
case RoomSettingItemType_Class:
{
XPRoomTagListView *view = [[XPRoomTagListView alloc] init];
view.tagType = RoomSettingSelectTagTypeClassify;
view.tagId = self.roomInfo.singleRoomSortId;
view.completion = ^(NSString * _Nonnull tag) {
NSString * mgId = [NSString stringWithFormat:@"%lld", self.roomInfo.mgId];
[self.presenter updateRoomInfo:self.roomInfo.title roomPwd:self.roomInfo.roomPwd tagId:roomTag classifyId:tag hasAnimationEffect:self.roomInfo.hasAnimationEffect roomUid:roomUid roomId:roomId type:self.roomInfo.type itemType:itemModel.type mgId:mgId];
};
[TTPopup popupView:view style:TTPopupStyleAlert];
}
break;
case RoomSettingItemType_Tag:
{
XPRoomTagListView *view = [[XPRoomTagListView alloc] init];
view.tagType = RoomSettingSelectTagTypeTag;
view.tagId = [NSString stringWithFormat:@"%ld", self.roomInfo.tagId];
view.completion = ^(NSString * _Nonnull tag) {
NSString * mgId = [NSString stringWithFormat:@"%lld", self.roomInfo.mgId];
[self.presenter updateRoomInfo:self.roomInfo.title roomPwd:self.roomInfo.roomPwd tagId:tag classifyId:roomClassifyId hasAnimationEffect:self.roomInfo.hasAnimationEffect roomUid:roomUid roomId:roomId type:self.roomInfo.type itemType:itemModel.type mgId:mgId];
};
[TTPopup popupView:view style:TTPopupStyleAlert];
}
break;
case RoomSettingItemType_Title:
{
XPRoomSettingInputView * titleView = [[XPRoomSettingInputView alloc] init];
titleView.maxCount = 15;
titleView.delegate = self;
titleView.type = RoomSettingInputType_Title;
titleView.placeHolder = self.roomInfo.title;
[TTPopup popupView:titleView style:TTPopupStyleAlert];
}
break;
case RoomSettingItemType_Pwd:
{
XPRoomSettingInputView * titleView = [[XPRoomSettingInputView alloc] init];
titleView.maxCount = 8;
titleView.delegate = self;
titleView.type = RoomSettingInputType_Pwd;
NSString * roomPwd = [DESEncrypt decryptUseDES:self.roomInfo.roomPwd key:KeyWithType(KeyType_PasswordEncode)];
titleView.placeHolder = roomPwd;
[TTPopup popupView:titleView style:TTPopupStyleAlert];
}
break;
case RoomSettingItemType_Manager_List:
{
XPRoomRoleViewController * managerVC = [[XPRoomRoleViewController alloc] init];
managerVC.superAdmminList = self.hostDelegate.getRoomSuperAdminList;
managerVC.roomId = roomId;
managerVC.type = RoomRoleListType_Manager;
[self.navigationController pushViewController:managerVC animated:YES];
}
break;
case RoomSettingItemType_Black_List:
{
XPRoomRoleViewController * managerVC = [[XPRoomRoleViewController alloc] init];
managerVC.roomId = roomId;
managerVC.type = RoomRoleListType_Black;
[self.navigationController pushViewController:managerVC animated:YES];
}
break;
default:
break;
}
}
#pragma mark - XPRoomSettingInputViewDelegate
- (void)xPRoomSettingInputView:(XPRoomSettingInputView *)view didClickConfirm:(NSString *)text type:(RoomSettingInputType)type {
NSString * roomUid = [NSString stringWithFormat:@"%ld", self.roomInfo.uid];
NSString * roomId = [NSString stringWithFormat:@"%ld", self.roomInfo.roomId];
NSString * roomTag = [NSString stringWithFormat:@"%ld", self.roomInfo.tagId];
NSString * mgId = [NSString stringWithFormat:@"%lld", self.roomInfo.mgId];
NSString * roomClassifyId = self.roomInfo.singleRoomSortId;
if (type == RoomSettingInputType_Pwd) {
NSString *pwdDes = [DESEncrypt encryptUseDES:text key:KeyWithType(KeyType_PasswordEncode)];
[self.presenter updateRoomInfo:self.roomInfo.title roomPwd:pwdDes tagId:roomTag classifyId:roomClassifyId hasAnimationEffect:self.roomInfo.hasAnimationEffect roomUid:roomUid roomId:roomId type:self.roomInfo.type itemType:RoomSettingItemType_Pwd mgId:mgId];
} else if(type == RoomSettingInputType_Title) {
[self.presenter updateRoomInfo:text roomPwd:self.roomInfo.roomPwd tagId:roomTag classifyId:roomClassifyId hasAnimationEffect:self.roomInfo.hasAnimationEffect roomUid:roomUid roomId:roomId type:self.roomInfo.type itemType:RoomSettingItemType_Title mgId:mgId];
}
}
#pragma mark - XPRoomSettingTableViewCellDelegate
- (void)didChangeSwitch:(UISwitch *)switchView itemModel:(XPRoomSettingItemModel *)itemModel {
BOOL isOn = switchView.on;
NSString * roomUid = [NSString stringWithFormat:@"%ld", self.roomInfo.uid];
NSString * roomId = [NSString stringWithFormat:@"%ld", self.roomInfo.roomId];
NSString * roomTag = [NSString stringWithFormat:@"%ld", self.roomInfo.tagId];
NSString * roomClassifyId = self.roomInfo.singleRoomSortId;
switch (itemModel.type) {
case RoomSettingItemType_Arrange_Mic:
{
if (self.roomInfo.roomModeType == RoomModeType_Open_Blind) return;
NSString *msg;
if (isOn) {
msg = YMLocalizedString(@"XPRoomSettingViewController1");
} else {
msg = YMLocalizedString(@"XPRoomSettingViewController2");
}
TTAlertConfig *config = [[TTAlertConfig alloc] init];
config.title = YMLocalizedString(@"XPRoomSettingViewController3");
config.message = msg;
config.shouldDismissOnBackgroundTouch = NO;
[TTPopup alertWithConfig:config confirmHandler:^{
if (isOn) {
[self.presenter openRoomArrangeMic:roomUid];
}else {
[self.presenter closeRoomArrangeMic:roomUid];
}
} cancelHandler:^{
[switchView setOn:!switchView.isOn];
}];
}
break;
case RoomSettingItemType_Leave_Model:
{
if (!isOn) {
[self.presenter closeRoomLeaveMode:roomUid];
return;
}
TTAlertConfig *config = [[TTAlertConfig alloc] init];
config.message = YMLocalizedString(@"XPRoomSettingViewController4" );
config.shouldDismissOnBackgroundTouch = NO;
[TTPopup alertWithConfig:config confirmHandler:^{
[self handleRoomLeaveMode];
} cancelHandler:^{
[switchView setOn:!switchView.isOn];
}];
}
break;
case RoomSettingItemType_Lock:
{
if (isOn) {
XPRoomSettingInputView * titleView = [[XPRoomSettingInputView alloc] init];
titleView.maxCount = 8;
titleView.delegate = self;
titleView.type = RoomSettingInputType_Pwd;
[TTPopup popupView:titleView style:TTPopupStyleAlert];
} else {
NSString * mgId = [NSString stringWithFormat:@"%lld", self.roomInfo.mgId];
[self.presenter updateRoomInfo:self.roomInfo.title roomPwd:@"" tagId:roomTag classifyId:roomClassifyId hasAnimationEffect:self.roomInfo.hasAnimationEffect roomUid:roomUid roomId:roomId type:self.roomInfo.type itemType:itemModel.type mgId:mgId];
}
}
break;
case RoomSettingItemType_Gift_Effect:
{
if (isOn) {
NSString * mgId = [NSString stringWithFormat:@"%lld", self.roomInfo.mgId];
[self.presenter updateRoomInfo:self.roomInfo.title roomPwd:self.roomInfo.roomPwd tagId:roomTag classifyId:roomClassifyId hasAnimationEffect:YES roomUid:roomUid roomId:roomId type:self.roomInfo.type itemType:itemModel.type mgId:mgId];
return;
}
TTAlertConfig *config = [[TTAlertConfig alloc] init];
config.title = YMLocalizedString(@"XPRoomSettingViewController5");
config.message = YMLocalizedString(@"XPRoomSettingViewController6");
config.shouldDismissOnBackgroundTouch = NO;
[TTPopup alertWithConfig:config confirmHandler:^{
NSString * mgId = [NSString stringWithFormat:@"%lld", self.roomInfo.mgId];
[self.presenter updateRoomInfo:self.roomInfo.title roomPwd:self.roomInfo.roomPwd tagId:roomTag classifyId:roomClassifyId hasAnimationEffect:NO roomUid:roomUid roomId:roomId type:self.roomInfo.type itemType:itemModel.type mgId:mgId];
} cancelHandler:^{
[switchView setOn:!switchView.isOn];
}];
}
break;
case RoomSettingItemType_Message_Screen:
{
if (isOn) {
[self.presenter updateRoomMessageScreenState:NO roomId:roomId];
return;
}
TTAlertConfig *config = [[TTAlertConfig alloc] init];
config.title = YMLocalizedString(@"XPRoomSettingViewController7");
config.message = YMLocalizedString(@"XPRoomSettingViewController8");
config.shouldDismissOnBackgroundTouch = NO;
[TTPopup alertWithConfig:config confirmHandler:^{
[self.presenter updateRoomMessageScreenState:YES roomId:roomId];
} cancelHandler:^{
[switchView setOn:!switchView.isOn];
}];
}
break;
case RoomSettingItemType_Gift_Value_Model:
{
if (self.roomInfo.roomModeType == RoomModeType_Open_Blind) {
[self showErrorToast:YMLocalizedString(@"XPRoomMoreMenuViewController2")];
return;
}
if(isOn){
[self.presenter openRoomGiftValue:roomUid itemModel:itemModel];
return;
}
TTAlertConfig * config = [[TTAlertConfig alloc] init];
config.title = @"";
config.message = YMLocalizedString(@"XPRoomMoreMenuViewController3");
[TTPopup alertWithConfig:config confirmHandler:^{
[self.presenter closeRoomGiftValue:roomUid itemModel:itemModel];
} cancelHandler:^{
[switchView setOn:!switchView.isOn];
}];
break;
}
default:
break;
}
}
#pragma mark - XPRoomSettingProtocol
-(void)closeRoomGiftValueSuccessWithItemModel:(XPRoomSettingItemModel *)itemModel{
itemModel.switchState = !itemModel.switchState;
[self.tableView reloadData];
[self showSuccessToast:YMLocalizedString(@"XPRoomMoreMenuViewController1")];
}
-(void)closeRoomGiftValueFailWithItemModel:(XPRoomSettingItemModel *)itemModel{
[self.tableView reloadData];
}
-(void)openRoomGiftValueSuccessWithItemModel:(XPRoomSettingItemModel *)itemModel{
itemModel.switchState = !itemModel.switchState;
[self.tableView reloadData];
[self showSuccessToast:YMLocalizedString(@"XPRoomMoreMenuViewController0")];
}
-(void)openRoomGiftValueFailWithItemModel:(XPRoomSettingItemModel *)itemModel{
[self.tableView reloadData];
}
- (void)getRoomSettingListSuccess:(NSArray *)list {
self.datasource = list;
[self.tableView reloadData];
}
- (void)getRoomInfoSuccess:(RoomInfoModel *)roomInfo {
self.roomInfo = roomInfo;
[self.presenter getRoomSettingList:roomInfo isSuperAdmin:self.meIsSuperAdmin];
}
- (void)openRoomArrangeMicSuccess {
NSString * roomUid = [NSString stringWithFormat:@"%ld", self.roomInfo.uid];
[self.presenter getRoomInfo:roomUid];
}
- (void)closeRoomArrangeMicSuccess {
NSString * roomUid = [NSString stringWithFormat:@"%ld", self.roomInfo.uid];
[self.presenter getRoomInfo:roomUid];
}
- (void)updateRoomMessageScreenStateSuccess:(RoomInfoModel *)roomInfo {
NSString * roomUid = [NSString stringWithFormat:@"%ld", self.roomInfo.uid];
[self.presenter getRoomInfo:roomUid];
///发送公屏关闭开启的消息
NSString *sessionID = [NSString stringWithFormat:@"%ld", roomInfo.roomId];
AttachmentModel *attachement = [[AttachmentModel alloc]init];
attachement.first = CustomMessageType_Update_RoomInfo;
attachement.second = Custom_Message_Sub_Update_RoomInfo_MessageState;
attachement.data = [roomInfo model2dictionary];
NIMMessage *message = [[NIMMessage alloc]init];
NIMCustomObject *object = [[NIMCustomObject alloc] init];
object.attachment = attachement;
message.messageObject = object;
//构造会话
NIMSession *session = [NIMSession session:sessionID type:NIMSessionTypeChatroom];
[[NIMSDK sharedSDK].chatManager sendMessage:message toSession:session error:nil];
}
- (void)updateRoomInfoSuccess:(RoomInfoModel *)roomInfo itemType:(RoomSettingItemType)itemType {
NSString * roomUid = [NSString stringWithFormat:@"%ld", self.roomInfo.uid];
[self.presenter getRoomInfo:roomUid];
if (itemType == RoomSettingItemType_Gift_Effect) {
NSString *sessionID = [NSString stringWithFormat:@"%ld", roomInfo.roomId];
AttachmentModel *attachement = [[AttachmentModel alloc]init];
attachement.first = CustomMessageType_Update_RoomInfo;
attachement.second = Custom_Message_Sub_Update_RoomInfo_AnimateEffect;
attachement.data = [roomInfo model2dictionary];
NIMMessage *message = [[NIMMessage alloc]init];
NIMCustomObject *object = [[NIMCustomObject alloc] init];
object.attachment = attachement;
message.messageObject = object;
//构造会话
NIMSession *session = [NIMSession session:sessionID type:NIMSessionTypeChatroom];
[[NIMSDK sharedSDK].chatManager sendMessage:message toSession:session error:nil];
}
}
- (void)updateRoomInfoFail:(NSString *)message {
[self showErrorToast:message];
}
- (void)openRoomLeaveModeSuccess {
NSString * roomUid = [NSString stringWithFormat:@"%ld", self.roomInfo.uid];
[self.presenter getRoomInfo:roomUid];
}
- (void)closeRoomLeaveModeSuccess {
NSString * roomUid = [NSString stringWithFormat:@"%ld", self.roomInfo.uid];
[self.presenter getRoomInfo:roomUid];
}
#pragma mark - Getters And Setters
- (UITableView *)tableView {
if (!_tableView) {
_tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.tableFooterView = [UIView new];
_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
_tableView.backgroundColor = [UIColor clearColor];
if (@available(iOS 11.0, *)) {
_tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
}
[_tableView registerClass:[XPRoomSettingTableViewCell class] forCellReuseIdentifier:NSStringFromClass([XPRoomSettingTableViewCell class])];
}
return _tableView;
}
@end