Files
peko-ios/YuMi/Modules/YMRoom/View/ActivityContainerView/XPRoomActivityContainerView.m
2023-07-21 14:07:04 +08:00

476 lines
18 KiB
Objective-C

//
// XPRoomActivityView.m
// YuMi
//
// Created by YuMi 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 "XNDJTDDLoadingTool.h"
#import "YUMIMacroUitls.h"
///Model
#import "UserInfoModel.h"
#import "RoomInfoModel.h"
#import "ActivityInfoModel.h"
#import "AttachmentModel.h"
#import "FirstRechargeModel.h"
///View
#import "XPRoomHalfWebView.h"
#import "XPCandyTreeViewController.h"
#import "XPWebViewController.h"
#import "XPRoomViewController.h"
#import "XPFirstRechargeSuccessView.h"
#import "XPArrangeMicViewController.h"
#import "XPSailingViewController.h"
#import "XCCurrentVCStackManager.h"
UIKIT_EXTERN NSString *kShowFirstRechargeView;
@interface XPRoomActivityContainerView ()<SDCycleScrollViewDelegate>
///容器
@property (nonatomic,strong) UIStackView *stackView;
///轮播图
@property (nonatomic,strong) SDCycleScrollView *cycleScrollView;
///
@property (nonatomic,strong) UIView * placeHolderView;
///糖果树
@property (nonatomic,strong) NetImageView *lookLoveImageView;
///首充的
@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,assign) BOOL isLoadActivity;
///夺宝精灵
@property (nonatomic,strong) ActivityInfoModel *fairyActivityModel;
///首充
@property (nonatomic,strong) ActivityInfoModel * firstRechargeModel;
///寻爱之旅
@property (nonatomic,strong) ActivityInfoModel * lookLoveModel;
@end
@implementation XPRoomActivityContainerView
- (instancetype)initWithdelegate:(id<RoomHostDelegate>)delegate {
self = [super init];
if (self) {
self.hostDelegate = delegate;
[self initSubViews];
[self initSubViewConstraints];
[self requestActivityList];
[self configLookLove];
}
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.lookLoveImageView];
[self.stackView addArrangedSubview:self.joinDatingView];
}
- (void)initSubViewConstraints {
[self.stackView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.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.lookLoveImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(itemWidth, itemWidth));
}];
[self.joinDatingView mas_makeConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(35* kScreenHeightScale);
}];
}
- (void)requestActivityList {
///网络请求异步加载
dispatch_group_t group =dispatch_group_create();
// 并行队列
dispatch_queue_t queue =dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
dispatch_group_enter(group);
dispatch_group_async(group, queue, ^{
NSString * roomId = [NSString stringWithFormat:@"%ld", self.hostDelegate.getRoomInfo.roomId];
[Api roomActivityList:^(BaseModel * _Nullable data, NSInteger code, NSString * _Nullable msg) {
if (code == 200) {
NSArray <ActivityInfoModel *>* array = [ActivityInfoModel modelsWithArray:data.data];
[self.activityList addObjectsFromArray:array];
}
dispatch_group_leave(group);
} roomId:roomId type:@"2"];
});
dispatch_group_notify(group,dispatch_get_main_queue(), ^{
self.isLoadActivity = YES;
self.cycleScrollView.hidden = NO;
///是否包含首充
[self configfirstRecharge];
NSMutableArray *picArray = [NSMutableArray array];
for (ActivityInfoModel *model in self.activityList) {
[picArray addObject:model.bannerPic];
}
self.cycleScrollView.imageURLStringsGroup = picArray;
if (self.activityList.count > 1) {
[self.cycleScrollView setAutoScroll:YES];
self.cycleScrollView.autoScrollTimeInterval = 3;
} else {
[self.cycleScrollView setAutoScroll:NO];
}
});
}
///首充
-(void)configfirstRecharge{
UserInfoModel * userInfo = self.hostDelegate.getUserInfo;
if (userInfo.isFirstCharge == YES) {
if(![self.activityList containsObject:self.firstRechargeModel]){
[self.activityList insertObject:self.firstRechargeModel atIndex:0];
}
}else{
if([self.activityList containsObject:self.firstRechargeModel]){
[self.activityList removeObject:self.firstRechargeModel];
}
}
}
///寻爱之旅
- (void)configLookLove {
UserInfoModel * userInfo = self.hostDelegate.getUserInfo;
RoomInfoModel * roomInfo = self.hostDelegate.getRoomInfo;
if (userInfo.userLevelVo.experLevelSeq >= roomInfo.findLoveDrawSwitchVo.openLevel && roomInfo.findLoveDrawSwitchVo.open == YES) {
self.lookLoveImageView.hidden = NO;
self.lookLoveImageView.image = [UIImage imageNamed:@"room_candy_tree_enter"];
} else {
self.lookLoveImageView.hidden = YES;
}
}
#pragma mark - RoomGuestDelegat
- (void)onRoomUpdate {
RoomInfoModel * roomInfo = self.hostDelegate.getRoomInfo;
if (roomInfo.type == RoomType_MiniGame) {
self.firstRechargeImageView.hidden = YES;
self.lookLoveImageView.hidden = YES;
[self requestActivityList];
} else {
if([self.activityList containsObject:self.lookLoveModel]){
[self.activityList removeObject:self.lookLoveModel];
}
[self configLookLove];
self.firstRechargeImageView.hidden = YES;
[self configfirstRecharge];
NSMutableArray *picArray = [NSMutableArray array];
for (ActivityInfoModel *model in self.activityList) {
[picArray addObject:model.bannerPic];
}
self.cycleScrollView.imageURLStringsGroup = picArray;
if (self.activityList.count > 1) {
[self.cycleScrollView setAutoScroll:YES];
self.cycleScrollView.autoScrollTimeInterval = 3;
} else {
[self.cycleScrollView setAutoScroll:NO];
}
if (roomInfo.roomModeType == RoomModeType_Open_Blind || roomInfo.roomModeType == RoomModeType_Open_PK_Mode || roomInfo.roomModeType == RoomModeType_Open_Micro_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 {
NSArray *imageUrlList = cycleScrollView.imageURLStringsGroup;
if (imageUrlList.count > index) {
NSString *pic = imageUrlList[index];
ActivityInfoModel * info;
for (ActivityInfoModel * getInfo in self.activityList) {
if([getInfo.bannerPic isEqualToString:pic]){
info = getInfo;
break;;
}
}
if(info == nil)return;
if (info.activityType == ActivityType_Love) {
[self lookLoveTapRecognizer];
} else if(info.activityType == ActivityType_First) {
[self firstRechargeTapRecognizer];
}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;
webView.isPush = YES;
[TTPopup popupView:webView style:TTPopupStyleActionSheet];
return;
}
XPWebViewController * webVC = [[XPWebViewController alloc] init];
webVC.roomUid = [NSString stringWithFormat:@"%ld", self.hostDelegate.getRoomInfo.uid];
webVC.url = info.skipUri;
webVC.isPush = YES;
[self.hostDelegate.getCurrentNav pushViewController:webVC animated:YES];
}
}
}
}
#pragma mark - Event Response
- (void)lookLoveTapRecognizer {
XPCandyTreeViewController * candyTreeVC = [[XPCandyTreeViewController alloc] initWithDelegate:self.hostDelegate];
candyTreeVC.view.frame = CGRectMake(0, 0, KScreenWidth, KScreenHeight);
[[XCCurrentVCStackManager shareManager].getCurrentVC addChildViewController:candyTreeVC];
[candyTreeVC.navigationController setNavigationBarHidden:YES animated:NO];
[[XCCurrentVCStackManager shareManager].getCurrentVC.view addSubview:candyTreeVC.view];
}
- (void)firstRechargeTapRecognizer {
[[NSNotificationCenter defaultCenter]postNotificationName:kShowFirstRechargeView object:nil];
}
- (void)didTapJoinDatingRecognizer {
if (self.hostDelegate.getRoomInfo.roomModeType == RoomModeType_Open_PK_Mode && self.hostDelegate.isRoomPKPlaying) {
[XNDJTDDLoadingTool showErrorWithMessage:YMLocalizedString(@"XPRoomActivityContainerView2")];
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)sailTapRecognizer {
NSString * roomUid = [NSString stringWithFormat:@"%ld", self.hostDelegate.getRoomInfo.uid];
XPSailingViewController * sailingVC = [[XPSailingViewController alloc] initWithRoomUid:roomUid];
[self.hostDelegate.getCurrentNav presentViewController:sailingVC 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.2];
_cycleScrollView.pageControlDotSize = CGSizeMake(5, 2);
_cycleScrollView.pageControlStyle = SDCycleScrollViewPageContolStyleClassic;
_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 *)lookLoveImageView {
if (!_lookLoveImageView) {
NetImageConfig * config = [[NetImageConfig alloc] init];
config.placeHolder = [UIImage imageNamed:@"room_candy_tree_enter"];
config.imageType = ImageTypeUserIcon;
_lookLoveImageView = [[NetImageView alloc] initWithConfig:config];
_lookLoveImageView.userInteractionEnabled = YES;
_lookLoveImageView.hidden = YES;
_lookLoveImageView.image = [UIImage imageNamed:@"room_candy_tree_enter"];
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(lookLoveTapRecognizer)];
[_lookLoveImageView addGestureRecognizer:tap];
}
return _lookLoveImageView;
}
- (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;
}
- (ActivityInfoModel *)firstRechargeModel{
if (!_firstRechargeModel){
ActivityInfoModel * activityInfo = [[ActivityInfoModel alloc] init];
activityInfo.bannerPic = @"room_first_recharge_enter";
activityInfo.bannerName = YMLocalizedString(@"XPRoomActivityContainerView0");
_firstRechargeModel = activityInfo;
_firstRechargeModel.activityType = ActivityType_First;
}
return _firstRechargeModel;
}
-(ActivityInfoModel *)lookLoveModel{
if (!_lookLoveModel){
ActivityInfoModel * activityInfo = [[ActivityInfoModel alloc] init];
activityInfo.bannerName = YMLocalizedString(@"XPRoomActivityContainerView1");
_lookLoveModel = activityInfo;
_lookLoveModel.activityType = ActivityType_Love;
}
return _lookLoveModel;
}
@end