Files
yinmeng-ios/xplan-ios/Main/Room/View/ActivityContainerView/XPRoomActivityContainerView.m
2022-11-11 17:46:37 +08:00

521 lines
21 KiB
Objective-C

//
// XPRoomActivityView.m
// xplan-ios
//
// Created by 冯硕 on 2021/10/12.
//
#import "XPRoomActivityContainerView.h"
///Third
#import <Masonry/Masonry.h>
#import <SDCycleScrollView/SDCycleScrollView.h>
///Tool
#import "AccountInfoStorage.h"
#import "Api+Room.h"
#import "ClientConfig.h"
#import "NetImageView.h"
#import "TTPopup.h"
#import "XCHUDTool.h"
#import "XPMacro.h"
#import "StatisticsServiceHelper.h"
#import "NSArray+Safe.h"
///Model
#import "UserInfoModel.h"
#import "RoomInfoModel.h"
#import "ActivityInfoModel.h"
#import "AttachmentModel.h"
#import "FirstRechargeModel.h"
#import "XPRoomActivityPlayModel.h"
///View
#import "XPRoomHalfWebView.h"
#import "XPCandyTreeViewController.h"
#import "XPWebViewController.h"
#import "XPRoomViewController.h"
#import "XPFirstRechargeViewController.h"
#import "XPFirstRechargeSuccessView.h"
#import "XPArrangeMicViewController.h"
#import "XPSailingViewController.h"
#import "XPRoomActivityPlayView.h"
@interface XPRoomActivityContainerView ()<SDCycleScrollViewDelegate>
///容器
@property (nonatomic,strong) UIStackView *stackView;
///轮播图
@property (nonatomic,strong) SDCycleScrollView *cycleScrollView;
///
@property (nonatomic,strong) UIView * placeHolderView;
///首充的
@property (nonatomic,strong) UIImageView *firstRechargeImageView;
///参加相亲
@property (nonatomic,strong) UIImageView *joinDatingView;
///host 代理
@property (nonatomic,weak) id<RoomHostDelegate>hostDelegate;
///房间活动的列表
@property (nonatomic,copy) NSMutableArray<ActivityInfoModel *> *activityList;
///活动的地址
@property (nonatomic,strong) NSMutableArray<NSString *> *imageUrlList;
///是否加载了活动
@property (nonatomic,assign) BOOL isLoadActivity;
///糖果树
@property (nonatomic,strong) NetImageView *candyTreeImageView;
@end
@implementation XPRoomActivityContainerView
- (instancetype)initWithdelegate:(id<RoomHostDelegate>)delegate {
self = [super init];
if (self) {
self.hostDelegate = delegate;
[self initSubViews];
[self initSubViewConstraints];
[self requestActivityList];
[self configCandyTree];
}
return self;
}
#pragma mark - Private Method
- (void)initSubViews {
[self addSubview:self.stackView];
[self.stackView addArrangedSubview:self.cycleScrollView];
[self.stackView addArrangedSubview:self.placeHolderView];
[self.stackView addArrangedSubview:self.firstRechargeImageView];
[self.stackView addArrangedSubview:self.joinDatingView];
[self.stackView addArrangedSubview:self.candyTreeImageView];
}
- (void)initSubViewConstraints {
[self.stackView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.top.right.mas_equalTo(self);
make.bottom.mas_equalTo(self);
}];
CGFloat itemWidth = KScreenHeight > 667 ? 65 : 55 * kScreenHeightScale;
[self.cycleScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(itemWidth , itemWidth));
}];
[self.firstRechargeImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(itemWidth, itemWidth));
}];
[self.candyTreeImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(itemWidth, itemWidth));
}];
[self.joinDatingView mas_makeConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(35* kScreenHeightScale);
}];
}
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
for (NSInteger i = (self.subviews.count - 1) ; i >= 0 ; i--) {
UIView * subView = [self.subviews safeObjectAtIndex1:i];
CGPoint convertPoint = [subView convertPoint:point fromView:self];
if (CGRectContainsPoint(subView.bounds, convertPoint)) {
return [subView hitTest:convertPoint withEvent:event];
}
}
return nil;
}
- (void)requestActivityList {
NSString * roomId = [NSString stringWithFormat:@"%ld", self.hostDelegate.getRoomInfo.roomId];
[Api roomActivityList:^(BaseModel * _Nullable data, NSInteger code, NSString * _Nullable msg) {
self.isLoadActivity = YES;
if (code == 200) {
NSArray <ActivityInfoModel *>* array = [ActivityInfoModel modelsWithArray:data.data];
if (array.count <=0) return;
self.cycleScrollView.hidden = NO;
[self.activityList addObjectsFromArray:array];
NSMutableArray * picList = [NSMutableArray array];
[array enumerateObjectsUsingBlock:^(ActivityInfoModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if (obj.bannerPic.length > 0) {
[picList addObject:obj.bannerPic];
}
}];
self.imageUrlList = picList;
if (self.hostDelegate.getRoomInfo.type == RoomType_MiniGame) {
[self configLittleGameActivity];
} else {
self.cycleScrollView.imageURLStringsGroup = picList;
if (array.count > 1) {
[self.cycleScrollView setAutoScroll:YES];
self.cycleScrollView.autoScrollTimeInterval = 3;
} else {
[self.cycleScrollView setAutoScroll:NO];
}
}
}
} roomId:roomId type:@"2"];
}
- (void)configCandyTree {
UserInfoModel * userInfo = self.hostDelegate.getUserInfo;
RoomInfoModel * roomInfo = self.hostDelegate.getRoomInfo;
if (userInfo.userLevelVo.experLevelSeq >= [ClientConfig shareConfig].openCandyTreeLimitLevel && [ClientConfig shareConfig].openCandyTree && roomInfo.boxSwitchVo.openBoxSwitch) {
self.candyTreeImageView.hidden = NO;
if (roomInfo.boxSwitchVo.openBoxIcon.length > 0) {
self.candyTreeImageView.imageUrl = roomInfo.boxSwitchVo.openBoxIcon;
} else {
self.candyTreeImageView.image = [UIImage imageNamed:@"openBoxIcon"];
}
} else {
self.candyTreeImageView.hidden = YES;
}
}
- (void)configLittleGameActivity {
RoomInfoModel * roomInfo = self.hostDelegate.getRoomInfo;
UserInfoModel * userInfo = self.hostDelegate.getUserInfo;
BOOL hadContainCandy = NO;
for (ActivityInfoModel *activityInfo in self.activityList) {
if ([activityInfo.bannerName isEqualToString:@"糖果树"]) {
hadContainCandy = YES;
}
}
///根据条件加入糖果树
if (userInfo.userLevelVo.experLevelSeq >= [ClientConfig shareConfig].openCandyTreeLimitLevel && [ClientConfig shareConfig].openCandyTree && roomInfo.boxSwitchVo.openBoxSwitch && !hadContainCandy) {
ActivityInfoModel * activityInfo = [[ActivityInfoModel alloc] init];
activityInfo.bannerPic = roomInfo.boxSwitchVo.openBoxIcon.length > 0 ? roomInfo.boxSwitchVo.openBoxIcon : @"openBoxIcon";
activityInfo.bannerName = @"糖果树";
[self.activityList insertObject:activityInfo atIndex:0];
[self.imageUrlList insertObject:activityInfo.bannerPic atIndex:0];
}
///是否包含首充
BOOL hadContainFirstRecharge = NO;
for (ActivityInfoModel *activityInfo in self.activityList) {
if ([activityInfo.bannerName isEqualToString:@"首充有礼"]) {
hadContainFirstRecharge = YES;
}
}
///是否包含糖果树
BOOL hadContainCandy1 = NO;
for (ActivityInfoModel *activityInfo in self.activityList) {
if ([activityInfo.bannerName isEqualToString:@"糖果树"]) {
hadContainCandy1 = YES;
}
}
///根据条件加入首充
if (userInfo.isFirstCharge && !hadContainFirstRecharge) {
ActivityInfoModel * activityInfo = [[ActivityInfoModel alloc] init];
activityInfo.bannerPic = @"room_first_recharge_enter";
activityInfo.bannerName = @"首充有礼";
if (hadContainCandy1) {
[self.activityList insertObject:activityInfo atIndex:0];
[self.imageUrlList insertObject:activityInfo.bannerPic atIndex:0];
} else {
[self.activityList insertObject:activityInfo atIndex:1];
[self.imageUrlList insertObject:activityInfo.bannerPic atIndex:1];
}
}
///根据条件加入航海
BOOL hadSailing = NO;
for (ActivityInfoModel *activityInfo in self.activityList) {
if ([activityInfo.bannerName isEqualToString:@"航海"]) {
hadSailing = YES;
}
}
if ([ClientConfig shareConfig].configInfo.linearlyPoolOpenLevel <= self.hostDelegate.getUserInfo.userLevelVo.experLevelSeq && !hadSailing) {
ActivityInfoModel * activityInfo = [[ActivityInfoModel alloc] init];
activityInfo.bannerPic = @"room_sailiing_enter";
activityInfo.bannerName = @"航海";
[self.activityList insertObject:activityInfo atIndex:0];
[self.imageUrlList insertObject:activityInfo.bannerPic atIndex:0];
}
if (self.imageUrlList.count > 0) {
self.cycleScrollView.imageURLStringsGroup = self.imageUrlList;
[self.cycleScrollView setAutoScroll:YES];
self.cycleScrollView.autoScrollTimeInterval = 3;
}
}
- (void)dealRoomInfoChangeActivity {
__block ActivityInfoModel * firstRechargeActivity;
__block ActivityInfoModel * candyActivity;
__block ActivityInfoModel * sailingActivity;
[self.activityList enumerateObjectsUsingBlock:^(ActivityInfoModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if ([obj.bannerName isEqualToString:@"首充有礼"]) {
firstRechargeActivity = obj;
} else if([obj.bannerName isEqualToString:@"糖果树"]) {
candyActivity = obj;
} else if ([obj.bannerName isEqualToString:@"航海"]) {
sailingActivity = obj;
}
}];
if (firstRechargeActivity && [self.activityList containsObject:firstRechargeActivity]) {
NSInteger index = [self.activityList indexOfObject:firstRechargeActivity];
[self.imageUrlList removeObjectAtIndex:index];
[self.activityList removeObject:firstRechargeActivity];
}
if (candyActivity && [self.activityList containsObject:candyActivity]) {
NSInteger index = [self.activityList indexOfObject:candyActivity];
[self.imageUrlList removeObjectAtIndex:index];
[self.activityList removeObject:candyActivity];
}
if (sailingActivity && [self.activityList containsObject:sailingActivity]) {
NSInteger index = [self.activityList indexOfObject:sailingActivity];
[self.imageUrlList removeObjectAtIndex:index];
[self.activityList removeObject:sailingActivity];
}
}
#pragma mark - RoomGuestDelegat
- (void)onRoomUpdate {
RoomInfoModel * roomInfo = self.hostDelegate.getRoomInfo;
UserInfoModel * userInfo = self.hostDelegate.getUserInfo;
if (roomInfo.type == RoomType_MiniGame) {
self.firstRechargeImageView.hidden = YES;
if (self.isLoadActivity) {
[self configLittleGameActivity];
} else {
[self requestActivityList];
}
} else {
[self dealRoomInfoChangeActivity];
[self configCandyTree];
self.firstRechargeImageView.hidden = !userInfo.isFirstCharge;
if (roomInfo.roomModeType == RoomModeType_Open_Blind || roomInfo.roomModeType == RoomModeType_Open_PK_Mode) {
if (roomInfo.roomModeType == RoomModeType_Open_PK_Mode) {
self.joinDatingView.image = [UIImage imageNamed:@"room_pk_normal_member_enter"];
} else {
self.joinDatingView.image = [UIImage imageNamed:@"room_mode_dating_enter"];
}
NIMChatroomMembersByIdsRequest *request = [[NIMChatroomMembersByIdsRequest alloc]init];
request.roomId = [NSString stringWithFormat:@"%ld", roomInfo.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;
if (member.type == NIMTeamMemberTypeOwner || member.type == NIMTeamMemberTypeManager) {
self.joinDatingView.hidden = YES;
return;
}
}
self.joinDatingView.hidden = NO;
}];
} else {
self.joinDatingView.hidden = YES;
}
}
}
- (void)onRoomEntered {
[self onRoomUpdate];
}
- (void)handleNIMCustomMessage:(NIMMessage *)message {
NIMCustomObject *obj = (NIMCustomObject *)message.messageObject;
if (obj.attachment != nil && [obj.attachment isKindOfClass:[AttachmentModel class]]) {
AttachmentModel *attachment = (AttachmentModel *)obj.attachment;
if (attachment.first == CustomMessageType_First_Recharge_Reward && attachment.second == Custom_Message_Sub_Room_First_Recharge_Reward) {
FirstRechargeModel *model = [FirstRechargeModel modelWithJSON:attachment.data];
XPFirstRechargeSuccessView * firstRechargeView= [[XPFirstRechargeSuccessView alloc] init];
firstRechargeView.rechargeInfo = model;
[TTPopup popupView:firstRechargeView style:TTPopupStyleAlert];
}
}
}
- (void)handleNIMNotificationMessage:(NIMMessage *)message {
NIMNotificationObject *notiMsg = (NIMNotificationObject *)message.messageObject;
NIMChatroomNotificationContent *content = (NIMChatroomNotificationContent *)notiMsg.content;
switch (content.eventType) {
case NIMChatroomEventTypeAddManager:
{
if (self.hostDelegate.getRoomInfo.roomModeType == RoomModeType_Open_Blind || self.hostDelegate.getRoomInfo.roomModeType == RoomModeType_Open_PK_Mode) {
for (NIMChatroomNotificationMember * member in content.targets) {
if (member.userId.intValue == [AccountInfoStorage instance].getUid.integerValue) {
self.joinDatingView.hidden = YES;
break;
}
}
}
}
break;
case NIMChatroomEventTypeRemoveManager:
{
if (self.hostDelegate.getRoomInfo.roomModeType == RoomModeType_Open_Blind || self.hostDelegate.getRoomInfo.roomModeType == RoomModeType_Open_PK_Mode) {
for (NIMChatroomNotificationMember * member in content.targets) {
if (member.userId.intValue == [AccountInfoStorage instance].getUid.integerValue) {
self.joinDatingView.hidden = NO;
break;
}
}
}
}
break;
default:
break;
}
}
#pragma mark - SDCycleScrollViewDelegate
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index {
if (self.activityList.count > index) {
ActivityInfoModel * info = [self.activityList safeObjectAtIndex1:index];
[StatisticsServiceHelper trackEventWithKey:StatisticsServiceEventroom_banner_click eventAttributes:@{@"bannerName": info.bannerName}];
if([info.bannerName isEqualToString:@"首充有礼"]) {
[self firstRechargeTapRecognizer];
} else if([info.bannerName isEqualToString:@"糖果树"]) {
[self candyTreeTapRecognizer];
} else {
if (info.skipType == ActivitySkipType_Room) {
[self.hostDelegate exitRoom];
[XPRoomViewController openRoom:info.skipUri viewController:self.hostDelegate.getCurrentNav];
} else if(info.skipType == ActivitySkipType_Web) {
if (info.showType == ActivityShowType_Half) {
XPRoomHalfWebView * webView = [[XPRoomHalfWebView alloc] init];
webView.roomUid = [NSString stringWithFormat:@"%ld", self.hostDelegate.getRoomInfo.uid];
webView.url = info.skipUri;
[TTPopup popupView:webView style:TTPopupStyleActionSheet];
return;
}
XPWebViewController * webVC = [[XPWebViewController alloc] init];
webVC.roomUid = [NSString stringWithFormat:@"%ld", self.hostDelegate.getRoomInfo.uid];
webVC.url = info.skipUri;
[self.hostDelegate.getCurrentNav pushViewController:webVC animated:YES];
}
}
}
}
/** 图片滚动回调 */
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didScrollToIndex:(NSInteger)index {
if (self.activityList.count > index) {
ActivityInfoModel * info = [self.activityList safeObjectAtIndex1:index];
[StatisticsServiceHelper trackEventWithKey:StatisticsServiceEventroom_banner_show eventAttributes:@{@"bannerName": info.bannerName}];
}
}
#pragma mark - Event Response
- (void)firstRechargeTapRecognizer {
[StatisticsServiceHelper trackEventWithKey:StatisticsServiceEventroom_pop_click eventAttributes:@{@"bannerName": @"首充有礼"}];
XPFirstRechargeViewController * firstRechargeVC = [[XPFirstRechargeViewController alloc] initWithNavigation:self.hostDelegate.getCurrentNav];
[self.hostDelegate.getCurrentNav presentViewController:firstRechargeVC animated:YES completion:nil];
}
- (void)didTapJoinDatingRecognizer {
if (self.hostDelegate.getRoomInfo.roomModeType == RoomModeType_Open_PK_Mode && self.hostDelegate.isRoomPKPlaying) {
[XCHUDTool showErrorWithMessage:@"PK已开始暂时无法排麦"];
return;
}
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) {
NIMChatroomMember * member;
if (error == nil) {
member = members.firstObject;
}
RoomInfoModel * roomInfo = self.hostDelegate.getRoomInfo;
XPArrangeMicInfoModel * info = [[XPArrangeMicInfoModel alloc] init];
info.roomUid = [NSString stringWithFormat:@"%ld", roomInfo.uid];
info.roomId = [NSString stringWithFormat:@"%ld", roomInfo.roomId];
info.nick = roomInfo.nick;
info.roomAvatar = roomInfo.avatar;
info.roomTitle = roomInfo.title;
info.micQueue = [self.hostDelegate getMicroQueue];
info.isManager = (member.type == NIMChatroomMemberTypeCreator || member.type == NIMChatroomMemberTypeManager);
info.type = roomInfo.roomModeType == RoomModeType_Open_Blind ? ArrangeMicType_Dating : roomInfo.roomModeType == RoomModeType_Open_PK_Mode ? ArrangeMicType_Room_PK : ArrangeMicType_Normal;
XPArrangeMicViewController * arrangeMicVC = [[XPArrangeMicViewController alloc] initWithInfo:info];
[self.hostDelegate.getCurrentNav presentViewController:arrangeMicVC animated:YES completion:nil];
}];
}
- (void)candyTreeTapRecognizer {
XPCandyTreeViewController * candyTreeVC = [[XPCandyTreeViewController alloc] initWithDelegate:self.hostDelegate];
candyTreeVC.modalPresentationStyle = UIModalPresentationOverFullScreen;
[self.hostDelegate.getCurrentNav presentViewController:candyTreeVC animated:YES completion:nil];
}
#pragma mark - Getters And Setters
- (SDCycleScrollView *)cycleScrollView {
if (!_cycleScrollView) {
_cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectZero delegate:self placeholderImage:nil];
_cycleScrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
_cycleScrollView.currentPageDotColor = [UIColor whiteColor];
_cycleScrollView.pageDotColor = [UIColor colorWithWhite:1 alpha:0.15];
_cycleScrollView.currentPageDotImage = [UIImage imageNamed:@"room_activity_banner_select"];
_cycleScrollView.pageDotImage = [UIImage imageNamed:@"room_activity_banner_normal"];
_cycleScrollView.backgroundColor = [UIColor colorWithWhite:1 alpha:0.00];
_cycleScrollView.bannerImageViewContentMode = UIViewContentModeScaleAspectFit;
_cycleScrollView.pageControlBottomOffset = -10;
_cycleScrollView.hidden = YES;
}
return _cycleScrollView;
}
- (UIView *)placeHolderView {
if (!_placeHolderView) {
_placeHolderView = [[UIView alloc] init];
_placeHolderView.backgroundColor = [UIColor clearColor];
[_placeHolderView setContentHuggingPriority:UILayoutPriorityDragThatCanResizeScene forAxis:UILayoutConstraintAxisHorizontal];
[_placeHolderView setContentCompressionResistancePriority:UILayoutPriorityFittingSizeLevel forAxis:UILayoutConstraintAxisHorizontal];
}
return _placeHolderView;
}
- (UIStackView *)stackView {
if (!_stackView) {
_stackView = [[UIStackView alloc] init];
_stackView.axis = UILayoutConstraintAxisVertical;
_stackView.distribution = UIStackViewDistributionFill;
_stackView.alignment = UIStackViewAlignmentCenter;
_stackView.spacing = 5;
}
return _stackView;
}
- (UIImageView *)firstRechargeImageView {
if (!_firstRechargeImageView) {
_firstRechargeImageView = [[UIImageView alloc] init];
_firstRechargeImageView.image = [UIImage imageNamed:@"room_first_recharge_enter"];
_firstRechargeImageView.userInteractionEnabled = YES;
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(firstRechargeTapRecognizer)];
[_firstRechargeImageView addGestureRecognizer:tap];
}
return _firstRechargeImageView;
}
- (UIImageView *)joinDatingView {
if (!_joinDatingView) {
_joinDatingView = [[UIImageView alloc] init];
_joinDatingView.image = [UIImage imageNamed:@"room_mode_dating_enter"];
_joinDatingView.userInteractionEnabled = YES;
_joinDatingView.hidden = YES;
_joinDatingView.contentMode = UIViewContentModeScaleAspectFit;
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapJoinDatingRecognizer)];
[_joinDatingView addGestureRecognizer:tap];
}
return _joinDatingView;
}
- (NSMutableArray<ActivityInfoModel *> *)activityList {
if (!_activityList) {
_activityList = [NSMutableArray array];
}
return _activityList;
}
- (UIImageView *)candyTreeImageView {
if (!_candyTreeImageView) {
NetImageConfig * config = [[NetImageConfig alloc] init];
config.placeHolder = [UIImage imageNamed:@"room_candy_tree_enter"];
config.imageType = ImageTypeUserIcon;
_candyTreeImageView = [[NetImageView alloc] initWithConfig:config];
_candyTreeImageView.userInteractionEnabled = YES;
_candyTreeImageView.hidden = YES;
_candyTreeImageView.image = [UIImage imageNamed:@"room_candy_tree_enter"];
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(candyTreeTapRecognizer)];
[_candyTreeImageView addGestureRecognizer:tap];
}
return _candyTreeImageView;
}
@end