Files
peko-ios/YuMi/Modules/YMRoom/View/RoomHeaderView/RoomHeaderView.m

565 lines
22 KiB
Objective-C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// XPRoomInfoView.m
// xplan-ios
//
// Created by 冯硕 on 2021/10/11.
//
#import "RoomHeaderView.h"
///Third
#import <NIMSDK/NIMSDK.h>
#import <Masonry/Masonry.h>
#import "TTPopup.h"
#import <FFPopup/FFPopup.h>
#import "HttpRequestHelper.h"
///Tool
#import "UIImage+Utils.h"
#import "UIButton+EnlargeTouchArea.h"
#import "ThemeColor+Room.h"
#import "TTPopup.h"
#import "AccountInfoStorage.h"
#import "HttpRequestHelper.h"
#import "Api+Room.h"
#import "XCCurrentVCStackManager.h"
///Model
#import "RoomInfoModel.h"
#import "UserInfoModel.h"
#import "AttachMentModel.h"
#import "MicroQueueModel.h"
#import "XPRoomRecommendModel.h"
///View
#import "XPRoomTopicAlertView.h"
#import "XPRoomRecommendView.h"
///VC
#import "XPWebViewcontroller.h"
#import "XPRoomOnLineViewController.h"
#import "XPRoomViewController.h"
#import "XPRoomTypeSelectionViewController.h"
#import "XPRoomTopicViewController.h"
#import "ShareHelder.h"
#import "XPRoomPresenter.h"
#import "TurboModeStateManager.h"
#import "XPTurboModeConstants.h"
@interface RoomHeaderView ()<XPRoomRecommendViewDelegate>
///容器
@property (nonatomic,strong) UIStackView *nickStackView;
///
@property (nonatomic,strong) UIStackView *titleStackView;
@property(nonatomic,strong) NetImageView *avatarView;
///房间名字
@property (nonatomic,strong) MarqueeLabel *titleLabel;
@property(nonatomic, strong) NetImageView *levelImageView;
///礼物特效
@property (nonatomic,strong) UIImageView *noGiftEffectImageView;
///锁房标识
@property (nonatomic,strong) UIImageView *lockRoomImageView;
///
@property (nonatomic,strong) UIStackView *onlineStackView;
///id和在线人数
@property (nonatomic,strong) YYLabel *idLabel;
///分享
@property (nonatomic,strong) UIButton *shareButton;
///收藏
@property (nonatomic,strong) UIButton *collectButton;
///设置
@property (nonatomic,strong) UIButton *settingButton;
///话题设置
@property (nonatomic, strong) UIButton *topicButton;
///代理
@property (nonatomic, weak) id<RoomHostDelegate> hostDelegate;
///右侧推荐房间
@property (nonatomic, strong) XPRoomRecommendView *recommendRoomView;
///返回按钮,最小化
@property (nonatomic,strong) UIButton *backBtn;
///是否已发送过收藏房间公屏消息
@property (nonatomic, assign) BOOL hadShowCollectInScreen;
@end
@implementation RoomHeaderView
- (instancetype)initWithDelegate:(id<RoomHostDelegate>)delegate {
self = [super init];
if (self) {
_hostDelegate = delegate;
[self initSubViews];
[self initSubViewConstraints];
[self setupTurboModeNotifications];
}
return self;
}
#pragma mark - Event Response
- (void)backButtonAction:(UIButton *)sender {
[self.hostDelegate miniRoom];
}
- (void)settingButtonAction:(UIButton *)sender {
self.recommendRoomView.frame = CGRectMake(KScreenWidth, 0, 250, KScreenHeight);
FFPopup *popup = [FFPopup popupWithContentView:self.recommendRoomView];
popup.showType = isMSRTL() ? FFPopupShowType_SlideInFromLeft:FFPopupShowType_SlideInFromRight;
popup.dismissType = isMSRTL() ? FFPopupDismissType_SlideOutToLeft : FFPopupDismissType_SlideOutToRight;
popup.maskType = FFPopupMaskType_Dimmed;
popup.dimmedMaskAlpha = 0.0;
popup.shouldDismissOnBackgroundTouch = YES;
FFPopupHorizontalLayout horizontalLayout = isMSRTL() ? FFPopupHorizontalLayout_Left : FFPopupHorizontalLayout_Right;
FFPopupVerticalLayout verticalLayout = FFPopupVerticalLayout_Center;
[popup showWithLayout:FFPopupLayoutMake(horizontalLayout, verticalLayout) duration:0.0];
@kWeakify(self);
popup.didFinishDismissingBlock = ^{
@kStrongify(self);
[self.recommendRoomView removeFromSuperview];
};
popup.didFinishShowingBlock = ^{
};
[Api reqeustRecommendRoom:^(BaseModel * _Nullable data, NSInteger code, NSString * _Nullable msg) {
@kStrongify(self);
if (code == 200) {
NSArray *array = [XPRoomRecommendModel modelsWithArray:data.data];
self.recommendRoomView.roomList = [NSMutableArray arrayWithArray:array];
}
} roomId:[NSString stringWithFormat:@"%ld", self.hostDelegate.getRoomInfo.roomId]];
}
- (void)topicButtonAction:(UIButton *)sender {
NIMChatroomMembersByIdsRequest *request = [[NIMChatroomMembersByIdsRequest alloc]init];
request.roomId = [NSString stringWithFormat:@"%ld", self.hostDelegate.getRoomInfo.roomId];
request.userIds = @[[AccountInfoStorage instance].getUid];
[[NIMSDK sharedSDK].chatroomManager fetchChatroomMembersByIds:request
completion:^(NSError * _Nullable error, NSArray<NIMChatroomMember *> * _Nullable members) {
if (error== nil) {
NIMChatroomMember* member = members.firstObject;
RoomInfoModel * roomInfo= self.hostDelegate.getRoomInfo;
if (member.type == NIMChatroomMemberTypeCreator || member.type == NIMChatroomMemberTypeManager) {
XPRoomTopicViewController * editTopicVC = [[XPRoomTopicViewController alloc] init];
editTopicVC.roomInfo = roomInfo;
[self.hostDelegate.getCurrentNav pushViewController:editTopicVC animated:YES];
} else {
TTPopupService * config = [[TTPopupService alloc] init];
XPRoomTopicAlertView * alertView = [[XPRoomTopicAlertView alloc] init];
alertView.title = roomInfo.roomDesc;
alertView.message = roomInfo.introduction;
config.contentView = alertView;
[TTPopup popupWithConfig:config];
}
}
}];
}
- (void)shareButtonAction:(UIButton *)sender {
// @kWeakify(self);
XPRoomPresenter *presenter = [[XPRoomPresenter alloc] init];
[presenter getShareLink:@(self.hostDelegate.getRoomInfo.uid).stringValue
success:^(NSString * _Nonnull link) {
// @kStrongify(self);
if (![NSString isEmpty:link]) {
[ShareHelder shareImage:kImage(@"share_icon") //self.hostDelegate.getRoomInfo.avatar
url:link
fromController:[XCCurrentVCStackManager shareManager].getCurrentVC];
}
} failure:^(NSError * _Nonnull error) {
}];
}
#pragma mark - Private Method
- (void)initSubViews {
[self addSubview:self.avatarView];
[self addSubview:self.nickStackView];
[self addSubview:self.collectButton];
[self addSubview:self.settingButton];
[self addSubview:self.topicButton];
[self addSubview:self.shareButton];
[self addSubview:self.backBtn];
[self.nickStackView addArrangedSubview:self.titleStackView];
[self.nickStackView addArrangedSubview:self.onlineStackView];
[self.onlineStackView addArrangedSubview:self.idLabel];
[self.titleStackView addArrangedSubview:self.titleLabel];
[self.titleStackView addArrangedSubview:self.noGiftEffectImageView];
[self.titleStackView addArrangedSubview:self.lockRoomImageView];
[self.titleStackView addArrangedSubview:self.levelImageView];
}
- (void)initSubViewConstraints {
// 设置
[self.settingButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.trailing.mas_equalTo(-15);
make.bottom.mas_equalTo(-11);
make.width.height.mas_equalTo(22);
}];
[self.topicButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.trailing.mas_equalTo(-45);
make.bottom.mas_equalTo(-11);
make.width.height.mas_equalTo(22);
}];
[self.collectButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(self.settingButton);
make.trailing.mas_equalTo(-75);
// make.trailing.equalTo(self.topicButton.mas_leading).mas_offset(-5);
make.size.mas_equalTo(CGSizeMake(20, 20));
}];
[self.shareButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(self.collectButton);
make.trailing.mas_equalTo(self.collectButton.mas_leading).offset(-6);
make.size.mas_equalTo(CGSizeMake(24, 24));
}];
[self.avatarView mas_makeConstraints:^(MASConstraintMaker *make) {
make.leading.mas_equalTo(self).offset(35);
make.width.height.mas_equalTo(31);
make.centerY.mas_equalTo(self.settingButton);
}];
[self.nickStackView mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.mas_equalTo(self.settingButton);
make.leading.equalTo(self.avatarView.mas_trailing).mas_offset(6);
make.trailing.mas_equalTo(self.shareButton.mas_leading);
}];
[self.titleStackView mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.mas_greaterThanOrEqualTo(50);
}];
[self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.mas_lessThanOrEqualTo(150);
// make.height.mas_equalTo(12);
}];
[self.idLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(7);
}];
[self.noGiftEffectImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(20,20));
}];
[self.lockRoomImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(20,20));
}];
[self.levelImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(40,16));
}];
[self.backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.leading.mas_equalTo(15);
make.height.width.mas_equalTo(20);
make.centerY.mas_equalTo(self.settingButton);
}];
}
- (void)updateLevel:(NSString *)levelIcon {
self.levelImageView.imageUrl = levelIcon;
}
#pragma mark - RoomGuestDelegate
- (void)onRoomEntered {
[self handleRoomInfoChange:NO];
}
- (void)onRoomUpdate {
[self handleRoomInfoChange:YES];
}
- (void)handleRoomInfoChange:(BOOL)isUpdate {
RoomInfoModel* roomInfo = self.hostDelegate.getRoomInfo;
if (roomInfo) {
self.idLabel.text = [NSString stringWithFormat:@"ID%ld",(long)roomInfo.erbanNo];
self.avatarView.imageUrl = roomInfo.avatar;
self.titleLabel.text = roomInfo.title;
[self updateGiftEffectImageViewVisibility];
if (roomInfo.uid == [AccountInfoStorage instance].getUid.integerValue) {
self.collectButton.hidden = YES;
} else {
self.collectButton.hidden = NO;
}
if (!isUpdate) {
self.collectButton.selected = roomInfo.isRoomFans; // 收藏后,房间数据没有及时更新到 room info此时不需读 room info 状态
}
self.lockRoomImageView.hidden = roomInfo.roomPwd.length <= 0;
self.levelImageView.hidden = [NSString isEmpty:roomInfo.roomLevelIcon];
self.levelImageView.imageUrl = roomInfo.roomLevelIcon;
}
}
#pragma mark - Event Response
- (void)collectButtonAction:(UIButton *)sender {
NSString * type = self.collectButton.selected ? @"2" : @"1";
NSString * uid = [AccountInfoStorage instance].getUid;
NSString * roomUid = [NSString stringWithFormat:@"%ld", self.hostDelegate.getRoomInfo.uid];
@kWeakify(self);
[Api collectRoom:^(BaseModel * _Nullable data, NSInteger code, NSString * _Nullable msg) {
@kStrongify(self);
if (code == 200) {
self.collectButton.selected = !self.collectButton.selected;
if ([type isEqualToString:@"1"]) {///收藏的话
[XNDJTDDLoadingTool showSuccessWithMessage:YMLocalizedString(@"RoomHeaderView6")];
if (self.hadShowCollectInScreen) {
return;
}
self.hadShowCollectInScreen = YES;
NSMutableDictionary * dic = [NSMutableDictionary dictionary];
[dic setValue:[AccountInfoStorage instance].getUid forKey:@"uid"];
[dic setValue:self.hostDelegate.getUserInfo.nick forKey:@"nick"];
[dic setValue:[NSString stringWithFormat:@"%ld", self.hostDelegate.getRoomInfo.uid] forKey:@"targetUid"];
[dic setValue:self.hostDelegate.getRoomInfo.nick forKey:@"targetNick"];
NSDictionary * data = @{@"data":dic};
AttachmentModel *attachment = [[AttachmentModel alloc]init];
attachment.first = CustomMessageType_Collection_Room;
attachment.second = Custom_Message_Sub_Collect_Room_Tips;
attachment.data = data;
NIMMessage *message = [[NIMMessage alloc]init];
NIMCustomObject *object = [[NIMCustomObject alloc] init];
object.attachment = attachment;
message.messageObject = object;
NSString *sessionId = [NSString stringWithFormat:@"%ld",self.hostDelegate.getRoomInfo.roomId];
//构造会话
NIMSession *session = [NIMSession session:sessionId type:NIMSessionTypeChatroom];
[[NIMSDK sharedSDK].chatManager sendMessage:message toSession:session error:nil];
}
} else {
[XNDJTDDLoadingTool showErrorWithMessage:msg];
}
} roomUid:roomUid uid:uid type:type roomUids:@""];
}
#pragma mark - XPRoomRecommendViewDelegate
- (void)xPRoomRecommendViewReport {
TTActionSheetConfig *action = [TTActionSheetConfig normalTitle:YMLocalizedString(@"RoomHeaderView10") clickAction:^{
[FFPopup dismissPopupForView:self.recommendRoomView animated:NO];
[self.hostDelegate requesstShieldingAction];
}];
TTActionSheetConfig *action1 = [TTActionSheetConfig normalTitle:YMLocalizedString(@"RoomHeaderView12") clickAction:^{
[FFPopup dismissPopupForView:self.recommendRoomView animated:NO];
XPWebViewController * webVC = [[XPWebViewController alloc] initWithRoomUID:@(self.hostDelegate.getRoomInfo.uid).stringValue];
NSString *urlstr = [NSString stringWithFormat:@"%@?reportUid=%ld&source=ROOM", URLWithType(kReportRoomURL),self.hostDelegate.getRoomInfo.uid];
webVC.url = urlstr;
[[self.hostDelegate getCurrentNav] pushViewController:webVC animated:YES];
}];
[TTPopup actionSheetWithItems:@[action,action1]];
}
- (void)xPRoomRecommendViewMiniRoom {
[FFPopup dismissPopupForView:self.recommendRoomView animated:NO];
[self.hostDelegate miniRoom];
}
- (void)xPRoomRecommendViewExitRoom {
[FFPopup dismissPopupForView:self.recommendRoomView animated:NO];
[self.hostDelegate exitRoom];
}
- (void)xPRoomRecommendViewJumpToRoom:(NSString *)roomUid {
[FFPopup dismissPopupForView:self.recommendRoomView animated:NO];
[self.hostDelegate exitRoom];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[XPRoomViewController openRoom:roomUid viewController:[XCCurrentVCStackManager shareManager].getCurrentVC];
});
}
#pragma mark - Getters And Setters
- (UIButton *)settingButton {
if (!_settingButton) {
_settingButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_settingButton setImage:kImage(@"room_info_setting") forState:UIControlStateNormal];
[_settingButton addTarget:self action:@selector(settingButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[_settingButton setEnlargeEdgeWithTop:8 right:8 bottom:8 left:8];
}
return _settingButton;
}
- (UIButton *)shareButton {
if (!_shareButton) {
_shareButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_shareButton setImage:[UIImage imageNamed:@"room_share"] forState:UIControlStateNormal];
[_shareButton addTarget:self action:@selector(shareButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[_shareButton setEnlargeEdgeWithTop:8 right:8 bottom:8 left:8];
}
return _shareButton;
}
- (UIButton *)topicButton {
if (!_topicButton) {
_topicButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_topicButton setImage:[UIImage imageNamed:@"room_header_topic_icon"] forState:UIControlStateNormal];
[_topicButton addTarget:self action:@selector(topicButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[_topicButton setEnlargeEdgeWithTop:8 right:8 bottom:8 left:8];
}
return _topicButton;
}
- (UIButton *)collectButton {
if (!_collectButton) {
_collectButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_collectButton setImage:kImage(@"ms_room_top_no_collect") forState:UIControlStateNormal];
[_collectButton setImage:kImage(@"ms_room_top_collect") forState:UIControlStateSelected];
_collectButton.hidden = YES;
[_collectButton setEnlargeEdgeWithTop:10 right:10 bottom:10 left:10];
[_collectButton addTarget:self action:@selector(collectButtonAction:) forControlEvents:UIControlEventTouchUpInside];
}
return _collectButton;
}
- (UIStackView *)nickStackView {
if (!_nickStackView) {
_nickStackView = [[UIStackView alloc] init];
_nickStackView.axis = UILayoutConstraintAxisVertical;
_nickStackView.distribution = UIStackViewDistributionFill;
_nickStackView.alignment = UIStackViewAlignmentLeading;
_nickStackView.spacing = 4;
}
return _nickStackView;
}
- (UIStackView *)titleStackView {
if (!_titleStackView) {
_titleStackView = [[UIStackView alloc] init];
_titleStackView.axis = UILayoutConstraintAxisHorizontal;
_titleStackView.distribution = UIStackViewDistributionFill;
_titleStackView.alignment = UIStackViewAlignmentCenter;
_titleStackView.spacing = 5;
}
return _titleStackView;
}
- (MarqueeLabel *)titleLabel{
if (!_titleLabel) {
_titleLabel = [[MarqueeLabel alloc] init];
_titleLabel.scrollDuration = 8.0;
_titleLabel.fadeLength = 6.0f;
_titleLabel.font = [UIFont systemFontOfSize:12 weight:UIFontWeightBold];
_titleLabel.textColor = UIColorFromRGB(0xEFEBF3);
_titleLabel.userInteractionEnabled = YES;
}
return _titleLabel;
}
- (UIImageView *)noGiftEffectImageView {
if (!_noGiftEffectImageView) {
_noGiftEffectImageView = [[UIImageView alloc] init];
_noGiftEffectImageView.userInteractionEnabled = YES;
_noGiftEffectImageView.image = [UIImage imageNamed:@"room_info_disable_gift_effect"];
_noGiftEffectImageView.hidden = YES;
}
return _noGiftEffectImageView;
}
- (UIImageView *)lockRoomImageView {
if (!_lockRoomImageView) {
_lockRoomImageView = [[UIImageView alloc] init];
_lockRoomImageView.userInteractionEnabled = YES;
_lockRoomImageView.image = [UIImage imageNamed:@"room_info_lock_room"];
_lockRoomImageView.hidden = YES;
}
return _lockRoomImageView;
}
- (UIStackView *)onlineStackView {
if (!_onlineStackView) {
_onlineStackView = [[UIStackView alloc] init];
_onlineStackView.axis = UILayoutConstraintAxisHorizontal;
_onlineStackView.distribution = UIStackViewDistributionFill;
_onlineStackView.alignment = UIStackViewAlignmentCenter;
_onlineStackView.spacing = 2;
}
return _onlineStackView;
}
- (YYLabel *)idLabel {
if (!_idLabel) {
_idLabel = [[YYLabel alloc] init];
_idLabel.textColor = [UIColor whiteColor];
_idLabel.font = [UIFont systemFontOfSize:9 weight:UIFontWeightBold];
}
return _idLabel;
}
- (XPRoomRecommendView *)recommendRoomView {
if (!_recommendRoomView) {
_recommendRoomView = [[XPRoomRecommendView alloc] init];
self.recommendRoomView.delegate = self;
}
return _recommendRoomView;
}
- (UIButton *)backBtn{
if (!_backBtn){
_backBtn = [UIButton new];
[_backBtn setBackgroundImage:[[UIImage imageNamed:@"common_nav_back_white"] ms_SetImageForRTL] forState:UIControlStateNormal];
[_backBtn addTarget:self action:@selector(backButtonAction:) forControlEvents:UIControlEventTouchUpInside];
// [_backBtn setCornerWithLeftTopCorner:10 rightTopCorner:10 bottomLeftCorner:10 bottomRightCorner:10 size:CGSizeMake(20,20)];
[_backBtn setEnlargeEdgeWithTop:5 right:5 bottom:5 left:5];
}
return _backBtn;
}
- (NetImageView *)avatarView{
if(!_avatarView){
NetImageConfig *config = [NetImageConfig new];
config.placeHolder = [UIImageConstant defaultAvatarPlaceholder];
_avatarView = [[NetImageView alloc]initWithConfig:config];
_avatarView.layer.cornerRadius = 31/2;
_avatarView.layer.masksToBounds = YES;
_avatarView.layer.borderWidth = 1;
_avatarView.layer.borderColor = [UIColor whiteColor].CGColor;
}
return _avatarView;
}
- (NetImageView *)levelImageView{
if(!_levelImageView){
_levelImageView = [[NetImageView alloc]init];
}
return _levelImageView;
}
#pragma mark - Turbo Mode Methods
- (void)setupTurboModeNotifications {
// 监听 Turbo Mode 礼物特效开关状态变化
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleTurboGiftEffectsStateChanged:)
name:kTurboGiftEffectsEnabledChanged
object:nil];
}
- (void)updateGiftEffectImageViewVisibility {
RoomInfoModel* roomInfo = self.hostDelegate.getRoomInfo;
if (!roomInfo) return;
NSString *roomId = @(roomInfo.roomId).stringValue;
BOOL turboGiftEffectsEnabled = [[TurboModeStateManager sharedManager] isGiftEffectsEnabledForRoom:roomId];
// TODO: 1. 直接使用 isGiftEffectsEnabledForRoom: 的状态NO = hidden2. 当 TurboModeStateManager isGiftEffectsEnabledForRoom 值有改变时,需要通知同步
BOOL shouldShow = turboGiftEffectsEnabled && !roomInfo.hasAnimationEffect;
self.noGiftEffectImageView.hidden = !shouldShow;
NSLog(@"🎮 RoomHeaderView: giftEffectImageView 显示状态更新 - 房间ID: %@, turboGiftEffects: %@, hasAnimationEffect: %@, 显示: %@",
roomId, turboGiftEffectsEnabled ? @"开启" : @"关闭", roomInfo.hasAnimationEffect ? @"" : @"", shouldShow ? @"" : @"");
}
- (void)handleTurboGiftEffectsStateChanged:(NSNotification *)notification {
NSLog(@"🎮 RoomHeaderView: 收到 Turbo Mode 礼物特效状态变化通知");
[self updateGiftEffectImageViewVisibility];
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end