// // XPRoomFunctionContainerView.m // xplan-ios // // Created by 冯硕 on 2021/12/15. // #import "XPRoomFunctionContainerView.h" ///Third #import #import #import ///Tool #import "XPMacro.h" #import "AccountInfoStorage.h" #import "UIImage+Utils.h" #import "ThemeColor.h" #import "TTPopup.h" #import "Api+Room.h" #import "XCHudTool.h" #import "XPHtmlUrl.h" #import "Api+Mine.h" ///Model #import "RoomInfoModel.h" #import "MicroQueueModel.h" #import "UserInfoModel.h" #import "AttachmentModel.h" #import "GiftValueInfoModel.h" ///View #import "XPRoomHalfWebView.h" #import "XPAnchorAudienceUpMicView.h" #import "XPRoomAnchorInfoCardView.h" #import "AnchorGiftValueView.h" #import "XPRoomTopicViewController.h" @interface XPRoomFunctionContainerView () ///host 代理 @property (nonatomic,weak) iddelegate; ///房间榜 @property (nonatomic,strong) UIButton *contributionButton; ///相亲阶段的按钮 @property (nonatomic,strong) UIButton *datingProgresButton; ///关注个播房主倒计时 @property (nonatomic, strong) dispatch_source_t followAnchorTimer; ///个播房房主魅力值 @property (nonatomic, strong) AnchorGiftValueView *anchorGiftValueView; ///话题的容器 @property (nonatomic,strong) UIStackView *topicStackView; ///话题 @property (nonatomic,strong) UILabel *topicLabel; ///编辑 @property (nonatomic,strong) UIButton *editButton; @end @implementation XPRoomFunctionContainerView - (void)dealloc { if (self.followAnchorTimer != nil) { dispatch_source_cancel(self.followAnchorTimer); } } - (instancetype)initWithdelegate:(id)delegate { self = [super init]; if (self) { self.delegate = delegate; [self initSubViews]; [self initSubViewConstraints]; } return self; } #pragma mark - Private Method - (void)initSubViews { [self addSubview:self.contributionButton]; [self addSubview:self.anchorGiftValueView]; [self addSubview:self.topicStackView]; [self.topicStackView addArrangedSubview:self.topicLabel]; [self.topicStackView addArrangedSubview:self.editButton]; } - (void)initSubViewConstraints { [self.contributionButton mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(-12); make.top.mas_equalTo(statusbarHeight+57); make.width.mas_equalTo(90); make.height.mas_equalTo(26); }]; [self.anchorGiftValueView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(self.contributionButton.mas_right).mas_offset(8); make.centerY.mas_equalTo(self.contributionButton); make.height.mas_equalTo(26); }]; [self.topicStackView mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.mas_equalTo(self); make.height.mas_equalTo(15); make.top.mas_equalTo(58 + 5 + 6 + 12 + 3 + kNavigationHeight); }]; } #pragma mark - RoomGuestDelegate - (void)onRoomUpdate { RoomInfoModel * roomInfo = self.delegate.getRoomInfo; MicroQueueModel * model = [self.delegate.getMicroQueue objectForKey:@"-1"]; if (roomInfo.roomModeType == RoomModeType_Open_Blind && model.userInfo && model.userInfo.uid == [AccountInfoStorage instance].getUid.integerValue) { if (!self.datingProgresButton.superview) { [self addSubview:self.datingProgresButton]; [self.datingProgresButton mas_makeConstraints:^(MASConstraintMaker *make) { make.size.mas_equalTo(CGSizeMake(70, 30)); make.centerX.mas_equalTo(self); make.top.mas_equalTo(self).offset(354 + kSafeAreaTopHeight); }]; } switch (roomInfo.blindDateState) { case RoomPlayDateingType_Talk: [self.datingProgresButton setTitle:@"开始选择>" forState:UIControlStateNormal]; break; case RoomPlayDateingType_Pick: [self.datingProgresButton setTitle:@"公布心动>" forState:UIControlStateNormal]; break; case RoomPlayDateingType_Result: [self.datingProgresButton setTitle:@"结束本轮>" forState:UIControlStateNormal]; break; case RoomPlayDateingType_Finish: [self.datingProgresButton setTitle:@"嘉宾交流>" forState:UIControlStateNormal]; break; default: break; } } else { if (self.datingProgresButton.superview) { [self.datingProgresButton removeFromSuperview]; } } if (roomInfo.type == RoomType_Anchor) { [self.contributionButton setTitle:@"主播榜" forState:UIControlStateNormal]; self.anchorGiftValueView.hidden = !roomInfo.showGiftValue; if (!roomInfo.showGiftValue) { self.anchorGiftValueView.giftValue = 0; } [self.contributionButton mas_updateConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(statusbarHeight+49); }]; } else { [self.contributionButton setTitle:@"房间榜" forState:UIControlStateNormal]; self.anchorGiftValueView.hidden = YES; [self.contributionButton mas_updateConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(statusbarHeight+57); }]; } [self updateRoomTopic]; } - (void)onRoomEntered { RoomInfoModel * roomInfo = self.delegate.getRoomInfo; NSString * roomUid = [NSString stringWithFormat:@"%ld", roomInfo.uid]; if (roomInfo.type == RoomType_Anchor) {//个播房 if (![[AccountInfoStorage instance].getUid isEqualToString:roomUid]) {//非房主 NSString * uid = [[AccountInfoStorage instance] getUid]; [Api attentionStatusCompletion:^(BaseModel * _Nullable data, NSInteger code, NSString * _Nullable msg) {//是否关注了个播房主 BOOL isLike = ((NSNumber *)data.data).boolValue; if (!isLike) { [self setFollowAnchorTimer]; } } uid:uid isLikeUid:roomUid]; } } [self updateRoomTopic]; } - (void)onMicroGiftValueUpdate:(NSDictionary *)data { [self handleAnchorGiftValue:data]; } - (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_AnchorRoom_AudienceUpMic) { [self showAskForUpMic:attachment.data]; } else if(attachment.first == CustomMessageType_Room_GiftValue && attachment.second == Custom_Message_Sub_Room_GiftValue_Sync) { [self handleAnchorGiftValue:attachment.data]; } else if(attachment.first == CustomMessageType_Gift && (attachment.second == Custom_Message_Sub_Gift_Send || attachment.second == Custom_Message_Sub_Gift_LuckySend || attachment.second == Custom_Message_Sub_Gift_ChannelNotify)) { [self handleAnchorGiftValue:attachment.data]; }else if(attachment.first == CustomMessageType_AllMicroSend && (attachment.second == Custom_Message_Sub_AllMicroSend || attachment.second == Custom_Message_Sub_AllBatchSend || attachment.second == Custom_Message_Sub_AllMicroLuckySend || attachment.second == Custom_Message_Sub_AllBatchMicroLuckySend)) { [self handleAnchorGiftValue:attachment.data]; } } } #pragma mark - Event Response - (void)contributionButtonAction:(UIButton *)sender { NSString * roomUid = [NSString stringWithFormat:@"%ld", self.delegate.getRoomInfo.uid]; XPRoomHalfWebView * webView = [[XPRoomHalfWebView alloc] init]; webView.url = [NSString stringWithFormat:@"%@?roomUid=%@", URLWithType(kRoomRankURL), roomUid]; [TTPopup popupView:webView style:TTPopupStyleActionSheet]; } - (void)datingProgresButtonAction:(UIButton *)sender { [TTPopup dismiss]; RoomInfoModel * roomInfo = self.delegate.getRoomInfo; NSString * message = @""; if (roomInfo.blindDateState == RoomPlayDateingType_Talk) { message = @"进入心动选人环节?"; } else if(roomInfo.blindDateState == RoomPlayDateingType_Pick) { message = @"进入心动公布环节?"; } else if(roomInfo.blindDateState == RoomPlayDateingType_Result) { message = @"结束本轮,同时清空魅力值?"; } else if (roomInfo.blindDateState == RoomPlayDateingType_Finish) { message = @"进入嘉宾交流环节?"; } TTAlertConfig * config = [[TTAlertConfig alloc] init]; config.message = message; [TTPopup alertWithConfig:config confirmHandler:^{ NSString * roomUid = [NSString stringWithFormat:@"%ld", self.delegate.getRoomInfo.uid]; NSString * roundId = [NSString stringWithFormat:@"%ld", self.delegate.getRoomInfo.blindDateState]; [Api changeRoomDatingState:^(BaseModel * _Nullable data, NSInteger code, NSString * _Nullable msg) { if (code == 200) { } else { [XCHUDTool showErrorWithMessage:msg]; } } roomUserId:roomUid roundId:roundId]; } cancelHandler:^{ }];} - (void)tapGiftValueRecognizer { if (self.anchorGiftValueView.hidden) return; if (self.delegate.getUserInfo && self.delegate.getUserInfo.uid > 0) { XPRoomHalfWebView * webView = [[XPRoomHalfWebView alloc] init]; webView.roomUid = [NSString stringWithFormat:@"%ld", self.delegate.getRoomInfo.uid]; webView.url = [NSString stringWithFormat:@"%@?uid=%ld", URLWithType(kRoomCharmRankURL), self.delegate.getRoomInfo.uid]; [TTPopup popupView:webView style:TTPopupStyleActionSheet]; } } #pragma mark - 房间话题 - (void)updateRoomTopicViewConstraint { RoomInfoModel * roomInfo = self.delegate.getRoomInfo; CGFloat offsetY = kNavigationHeight; if (roomInfo.datingState == RoomDatingStateChangeType_Open) { offsetY += ((58 + 5) * kScreenScale + 6 + 12 + 3); } else if (roomInfo.type == RoomType_Anchor) { offsetY += (120 + 5 + 6 + 20 + 3); } else if(roomInfo.hadChangeRoomType) { offsetY += (58 + 5 + 6 + 12 + 3 ); } else { return; } [self.topicStackView mas_updateConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(offsetY); }]; } - (void)updateRoomTopic { [self updateRoomTopicViewConstraint]; NIMChatroomMembersByIdsRequest *request = [[NIMChatroomMembersByIdsRequest alloc]init]; request.roomId = [NSString stringWithFormat:@"%ld", self.delegate.getRoomInfo.roomId]; request.userIds = @[[AccountInfoStorage instance].getUid]; [[NIMSDK sharedSDK].chatroomManager fetchChatroomMembersByIds:request completion:^(NSError * _Nullable error, NSArray * _Nullable members) { if (error== nil) { NIMChatroomMember* member = members.firstObject; if (member.type == NIMChatroomMemberTypeCreator || member.type == NIMChatroomMemberTypeManager) { self.editButton.hidden = NO; } else { self.editButton.hidden = YES; } } }]; self.topicLabel.text = self.delegate.getRoomInfo.roomDesc.length > 0 ? self.delegate.getRoomInfo.roomDesc : @"暂未设置话题"; } - (void)topicGestureRecognizer:(UITapGestureRecognizer *)tap { NIMChatroomMembersByIdsRequest *request = [[NIMChatroomMembersByIdsRequest alloc]init]; request.roomId = [NSString stringWithFormat:@"%ld", self.delegate.getRoomInfo.roomId]; request.userIds = @[[AccountInfoStorage instance].getUid]; [[NIMSDK sharedSDK].chatroomManager fetchChatroomMembersByIds:request completion:^(NSError * _Nullable error, NSArray * _Nullable members) { if (error== nil) { NIMChatroomMember* member = members.firstObject; RoomInfoModel * roomInfo= self.delegate.getRoomInfo; if (member.type == NIMChatroomMemberTypeCreator || member.type == NIMChatroomMemberTypeManager) { XPRoomTopicViewController * editTopicVC = [[XPRoomTopicViewController alloc] init]; editTopicVC.roomInfo = roomInfo; [self.delegate.getCurrentNav pushViewController:editTopicVC animated:YES]; } else { NSString * title = roomInfo.roomDesc.length > 0 ? roomInfo.roomDesc : @"暂未设置话题"; TTAlertConfig * config = [[TTAlertConfig alloc] init]; config.title = title; config.message = roomInfo.introduction.length > 0 ? roomInfo.introduction : @"暂未设置房间话题"; config.actionStyle = TTAlertActionCancelStyle; config.cancelButtonConfig.title = @"关闭"; [TTPopup alertWithConfig:config confirmHandler:^{ } cancelHandler:^{ }]; } } }]; } #pragma mark - 个播模式弹窗关注主播 - (void)setFollowAnchorTimer { if (self.followAnchorTimer != nil) { dispatch_source_cancel(self.followAnchorTimer); } #ifdef DEBUG NSInteger totalTime = 5; #else NSInteger totalTime = 420; #endif dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); self.followAnchorTimer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue); dispatch_source_set_timer(self.followAnchorTimer,dispatch_walltime(NULL, totalTime*NSEC_PER_SEC), totalTime*NSEC_PER_SEC, 0); //每秒执行 @weakify(self); dispatch_source_set_event_handler(self.followAnchorTimer, ^{ dispatch_async(dispatch_get_main_queue(), ^{ @strongify(self); if (self.followAnchorTimer != nil) { dispatch_source_cancel(self.followAnchorTimer); self.followAnchorTimer = nil; } RoomInfoModel* roomInfo = self.delegate.getRoomInfo; NSString *roomUid = [NSString stringWithFormat:@"%zd", roomInfo.uid]; NSString * uid = [[AccountInfoStorage instance] getUid]; [Api attentionStatusCompletion:^(BaseModel * _Nullable data, NSInteger code, NSString * _Nullable msg) { BOOL isLike = ((NSNumber *)data.data).boolValue; if (!isLike) { // 还没有关注 if (self.window) { [self showFollowAnchorView]; } } } uid:uid isLikeUid:roomUid]; }); }); dispatch_resume(self.followAnchorTimer); } //弹出关注主播的窗口 - (void)showFollowAnchorView { RoomInfoModel* roomInfo = self.delegate.getRoomInfo; NSString *roomUid = [NSString stringWithFormat:@"%zd", roomInfo.uid]; [Api getUserInfo:^(BaseModel * _Nullable data, NSInteger code, NSString * _Nullable msg) { UserInfoModel *userInfo = [UserInfoModel modelWithDictionary:data.data]; XPRoomAnchorInfoCardView *view = [[XPRoomAnchorInfoCardView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 300)]; view.userInfo = self.delegate.getUserInfo; view.targetUserInfo = userInfo; view.roomId =roomInfo.roomId; [TTPopup popupView:view style:TTPopupStyleActionSheet]; } uid:roomUid]; } ///个播房用户请求上麦弹窗 - (void)showAskForUpMic:(NSDictionary *)dict { NSString *roomUid = [NSString stringWithFormat:@"%zd", self.delegate.getRoomInfo.uid]; if (![roomUid isEqualToString:[AccountInfoStorage instance].getUid]) { return; } UserInfoModel *model = [UserInfoModel modelWithJSON:dict]; XPAnchorAudienceUpMicView *upMicView = [[XPAnchorAudienceUpMicView alloc] initWithFrame:CGRectMake(0, 0, 300, 226) delegate:self.delegate]; upMicView.info = model; TTPopupConfig *config = [[TTPopupConfig alloc] init]; config.filterIdentifier = @"audienceRequestUpMic"; config.shouldFilterPopup = YES; config.contentView = upMicView; config.style = TTPopupStyleAlert; [TTPopup popupWithConfig:config]; } - (void)handleAnchorGiftValue:(NSDictionary *)dict { if (self.delegate.getRoomInfo.type != RoomType_Anchor) {//个播房房主位置魅力值刷新 return; } GiftValueInfoModel * model = [GiftValueInfoModel modelWithDictionary:dict]; for (int i = 0; i < model.giftValueVos.count; i++) { GiftValueDetailModel * giftValueModel = [model.giftValueVos objectAtIndex:i]; if (![giftValueModel.uid isEqualToString:[NSString stringWithFormat:@"%zd", self.delegate.getRoomInfo.uid]]) { continue; } self.anchorGiftValueView.giftValue = giftValueModel.giftValue; } } #pragma mark - Getters And Setters - (UIButton *)contributionButton { if (!_contributionButton) { _contributionButton = [[UIButton alloc]init]; [_contributionButton addTarget:self action:@selector(contributionButtonAction:) forControlEvents:UIControlEventTouchUpInside]; _contributionButton.backgroundColor = [UIColor colorWithWhite:1 alpha:0.2]; [_contributionButton setTitle:@"房间榜" forState:UIControlStateNormal]; [_contributionButton setImage:[UIImage imageNamed:@"room_rank_enter_icon"] forState:UIControlStateNormal]; [_contributionButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; _contributionButton.titleLabel.font = [UIFont systemFontOfSize:12]; _contributionButton.layer.cornerRadius = 13; _contributionButton.layer.masksToBounds = YES; _contributionButton.imageEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0); _contributionButton.titleEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0); } return _contributionButton; } - (UIButton *)datingProgresButton { if (!_datingProgresButton) { _datingProgresButton = [UIButton buttonWithType:UIButtonTypeCustom]; [_datingProgresButton setBackgroundImage:[UIImage gradientColorImageFromColors:@[UIColorFromRGB(0xFA7186), UIColorFromRGB(0xFA4972)] gradientType:GradientTypeLeftToRight imgSize:CGSizeMake(65, 30)] forState:UIControlStateNormal]; [_datingProgresButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; _datingProgresButton.titleLabel.font = [UIFont systemFontOfSize:13]; [_datingProgresButton setTitle:@"开始选择>" forState:UIControlStateNormal]; [_datingProgresButton addTarget:self action:@selector(datingProgresButtonAction:) forControlEvents:UIControlEventTouchUpInside]; _datingProgresButton.layer.masksToBounds = YES; _datingProgresButton.layer.cornerRadius = 15; } return _datingProgresButton; } - (AnchorGiftValueView *)anchorGiftValueView { if (!_anchorGiftValueView) { _anchorGiftValueView = [[AnchorGiftValueView alloc] init]; _anchorGiftValueView.hidden = YES; _anchorGiftValueView.layer.cornerRadius = 13; _anchorGiftValueView.layer.masksToBounds = YES; UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGiftValueRecognizer)]; [_anchorGiftValueView addGestureRecognizer:tap]; } return _anchorGiftValueView; } - (UIStackView *)topicStackView { if (!_topicStackView) { _topicStackView = [[UIStackView alloc] init]; _topicStackView.axis = UILayoutConstraintAxisHorizontal; _topicStackView.distribution = UIStackViewDistributionFill; _topicStackView.alignment = UIStackViewAlignmentFill; _topicStackView.spacing = 3; UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(topicGestureRecognizer:)]; [_topicStackView addGestureRecognizer:tap]; } return _topicStackView; } - (UILabel *)topicLabel { if (!_topicLabel) { _topicLabel = [[UILabel alloc] init]; _topicLabel.font = [UIFont systemFontOfSize:12]; _topicLabel.textColor = [UIColor whiteColor]; } return _topicLabel; } - (UIButton *)editButton { if (!_editButton) { _editButton = [UIButton buttonWithType:UIButtonTypeCustom]; [_editButton setImage:[UIImage imageNamed:@"room_postion_topic_edit"] forState:UIControlStateNormal]; [_editButton setImage:[UIImage imageNamed:@"room_postion_topic_edit"] forState:UIControlStateSelected]; _editButton.hidden = YES; } return _editButton; } @end