// // XPHomeRecommendViewController.m // YuMi // // Created by YuMi on 2022/2/21. // #import "XPHomeRecommendViewController.h" ///Third #import #import #import #import ///Tool #import "YUMIMacroUitls.h" #import "DJDKMIMOMColor.h" #import "XPWeakTimer.h" #import "ClientConfig.h" #import "UIImage+Utils.h" #import "Api+Room.h" #import "Api+RoomSetting.h" #import "AccountInfoStorage.h" #import "TTPopup.h" #import "NSArray+Safe.h" #import "CountDownHelper.h" #import "XPSkillCardPlayerManager.h" #import "UploadFile.h" ///View #import "XPNewHomePlayTableViewCell.h" #import "XPNewHomeRecommendTableViewCell.h" #import "XPNewHomePlayEmptyTableViewCell.h" #import "XPHomeBannerTableViewCell.h" #import "XPNewHomePartyTableViewCell.h" #import "XPRoomViewController.h" #import "XPWebViewController.h" #import "XPMineUserInfoViewController.h" #import "SessionViewController.h" ///Model #import "HomePlayRoomModel.h" #import "HomeBannerInfoModel.h" #import "HomeRecommendRoomModel.h" #import "ClanDetailInfoModel.h" ///P #import "XPNewHomeRecommendPresenter.h" #import "XPNewHomeRecommendProtocol.h" #import "XPHomeGameView.h" UIKIT_EXTERN NSString * const kShieldingNotification; @interface XPHomeRecommendViewController () ///列表 @property (nonatomic,strong) UITableView *tableView; ///组队开黑 @property (nonatomic,copy) NSArray *playTeamList; ///个人房列表数据 @property (nonatomic,strong) NSMutableArray *personalRoomList; ///房间信息 @property (nonatomic,strong) RoomInfoModel * roomInfo; ///是否正在请示数据,防止过多请求 @property (nonatomic,assign) BOOL isRequestData; ///倒计时 @property (nonatomic,strong) CountDownHelper *countDownHelper; ///正在播放声音的cell @property(nonatomic,strong)XPNewHomePartyTableViewCell *cell; ///正在播放声音的cell在UITableView的Y坐标 @property(nonatomic,assign) CGFloat cellY; ///游戏列表 @property (nonatomic, strong) NSMutableArray *littleGameArray; @end @implementation XPHomeRecommendViewController - (XPNewHomeRecommendPresenter *)createPresenter { return [[XPNewHomeRecommendPresenter alloc] init]; } - (BOOL)isHiddenNavBar { return YES; } - (void)viewDidLoad { [super viewDidLoad]; [self initSubViews]; } -(void)viewDidAppear:(BOOL)animated{ [super viewDidAppear:animated]; self.countDownHelper.delegate = self; } -(void)viewDidDisappear:(BOOL)animated{ [super viewDidAppear:animated]; [self.countDownHelper stopCountDown]; self.countDownHelper.delegate = nil; [[XPSkillCardPlayerManager shareInstance] stopMusic]; if(self.cell != nil){ [self.cell setPlaySoundStatus:NO]; self.cell = nil; } } -(void)dealloc{ [[NSNotificationCenter defaultCenter]removeObserver:self]; } #pragma mark - InitHttp -(void)requestData{ if(self.cell != nil){ if(self.refreshComplete){ self.refreshComplete(); } return; }///正在播放声音,不能刷新数据 ///加锁,防止测试发现偶尔会出现闪退bug @synchronized (self.presenter) { if(self.isRequestData == YES){ if(self.refreshComplete){ self.refreshComplete(); } return; } self.isRequestData = YES; ///网络请求异步加载 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, ^{ [self.presenter getPlayGameWithTeam:1 withGroup:group]; }); dispatch_group_enter(group); dispatch_group_async(group, queue, ^{ [self.presenter getFriendListWithGroup:group]; }); dispatch_group_enter(group); dispatch_group_async(group, queue, ^{ [self.presenter getLittleGameListWithGroup:group]; }); dispatch_group_notify(group,dispatch_get_main_queue(), ^{ self.isRequestData = NO; if(self.refreshComplete){ self.refreshComplete(); } if(self.cell == nil){///播放音频时不能刷新数据 [self.tableView reloadData]; } }); } } - (void)headerRefresh { self.isRequestData = NO; [self requestData]; } #pragma mark - Private Method - (void)initSubViews { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(shieldingNotification) name:kShieldingNotification object:nil]; self.view.backgroundColor = [UIColor clearColor]; [self.view addSubview:self.tableView]; [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.mas_equalTo(self.view); }]; } -(void)shieldingNotification{ [self requestData]; } #pragma mark - UITableViewDelegate And UITableViewDataSource - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 2; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if(section == 0) return 1; return self.personalRoomList.count > 0 ? self.personalRoomList.count : 1; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section == 0) { return self.playTeamList.count > 0 ? kGetScaleWidth(102) : kGetScaleWidth(75); } return self.personalRoomList.count > 0 ? kGetScaleWidth(104) : 300; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if(indexPath.section == 0) { if(self.playTeamList.count > 0){ XPNewHomePlayTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([XPNewHomePlayTableViewCell class])]; if (cell == nil) { cell = [[XPNewHomePlayTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([XPNewHomePlayTableViewCell class])]; } cell.playRoomList = self.playTeamList; cell.delegate = self; return cell; } XPNewHomePlayEmptyTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([XPNewHomePlayEmptyTableViewCell class])]; if (cell == nil) { cell = [[XPNewHomePlayEmptyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([XPNewHomePlayEmptyTableViewCell class])]; } cell.isClan = YES; cell.delegate = self; return cell; } XPNewHomePartyTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([XPNewHomePartyTableViewCell class])]; if (cell == nil) { cell = [[XPNewHomePartyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([XPNewHomePartyTableViewCell class])]; } cell.roomInfo = [self.personalRoomList safeObjectAtIndex1:indexPath.row]; cell.delegate = self; return cell; } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 0.01; } - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { return 0.01; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:YES]; if(indexPath.section == 3) { if (self.personalRoomList.count > 0) { HomePlayRoomModel * roomInfo1 = [self.personalRoomList safeObjectAtIndex1:indexPath.row]; if (roomInfo1.uid.length > 0) { [XPRoomViewController openRoom:roomInfo1.uid viewController:self]; } } } } -(void)scrollViewDidScroll:(UIScrollView *)scrollView{ if(self.cell != nil){ if(scrollView.contentOffset.y > self.cellY){ [self.countDownHelper stopCountDown]; self.countDownHelper .delegate = nil; [[XPSkillCardPlayerManager shareInstance] stopMusic]; [self.cell setPlaySoundStatus:NO]; self.cell = nil; } } if(self.scrollCallback){ self.scrollCallback(scrollView); } } #pragma mark - XPNewHomeRecommendTableViewCellDelegate - (void)xPNewHomeRecommendTableViewCell:(XPNewHomeRecommendTableViewCell *)view didSelectItem:(HomeRecommendRoomModel *)info { if (info.roomUid.length > 0) { [XPRoomViewController openRoom:info.roomUid viewController:self]; } } #pragma mark - XPNewHomePlayTableViewCellDelegate - (void)xPNewHomePlayTableViewCell:(XPNewHomePlayTableViewCell *)cell didSelectItem:(HomePlayRoomModel *)info { if (info.uid.length > 0) { [XPRoomViewController openRoom:info.uid viewController:self]; } } - (void)chooseGameAction{ // XPHomeGameView *gameView = [XPHomeGameView new]; // gameView.frame = CGRectMake(0, 0, KScreenWidth, KScreenHeight); // gameView.playGameList = self.littleGameArray; // gameView.delegate = self; // [kWindow addSubview:gameView]; } #pragma mark - XPNewHomePlayEmptyTableViewCellDelegate - (void)emptyCellChooseGameAction{ // XPHomeGameView *gameView = [XPHomeGameView new]; // gameView.frame = CGRectMake(0, 0, KScreenWidth, KScreenHeight); // gameView.playGameList = self.littleGameArray; // gameView.delegate = self; // [kWindow addSubview:gameView]; if(self.delegate && [self.delegate respondsToSelector:@selector(openRoom)]){ [self.delegate openRoom]; } } #pragma mark - XPHomeGameViewDelegate - (void)xpHomeGameViewChooseGameWithGameModel:(LittleGameInfoModel *)gameModel{ NSString * uid = [AccountInfoStorage instance].getUid; [self showLoading]; [Api getRoomInfo:^(BaseModel * _Nullable data, NSInteger code, NSString * _Nullable msg) { if (code == 200) { RoomInfoModel * roomInfo = [RoomInfoModel modelWithJSON:data.data]; if(roomInfo.isReselect){ [XPRoomViewController openRoom:[AccountInfoStorage instance].getUid mgId:gameModel.mgId viewController:self]; }else{ [self updateRoomInformation:gameModel roomInfo:roomInfo]; } } else { [self hideHUD]; [self showErrorToast:msg]; } } uid:uid intoUid:uid]; } ///更新为游戏房 -(void)updateRoomInformation:(LittleGameInfoModel *)gameModel roomInfo:(RoomInfoModel *)roomInfo{ NSString * uid = [AccountInfoStorage instance].getUid; NSMutableDictionary * params = [NSMutableDictionary dictionary]; NSString * ticket = [AccountInfoStorage instance].getTicket; [params setObject:ticket forKey:@"ticket"]; [params setObject:uid forKey:@"uid"]; [params setObject:[NSString stringWithFormat:@"%ld", roomInfo.uid] forKey:@"roomUid"]; if (roomInfo.title.length > 0) { [params setObject:roomInfo.title forKey:@"title"]; } if (roomInfo.roomPwd.length > 0) { [params setObject:roomInfo.roomPwd forKey:@"roomPwd"]; } else{ [params setObject:@"" forKey:@"roomPwd"]; } if (roomInfo.tagId > 0) { [params setObject:[NSString stringWithFormat:@"%ld", roomInfo.tagId] forKey:@"tagId"]; } [params setObject:@(RoomType_MiniGame) forKey:@"type"]; [params setObject:gameModel.mgId forKey:@"mgId"]; [params setObject:@(roomInfo.hasAnimationEffect) forKey:@"hasAnimationEffect"]; @weakify(self) [Api ownerUpdateRoomInfo:^(BaseModel * _Nullable data, NSInteger code, NSString * _Nullable msg) { @strongify(self); [self hideHUD]; if (code == 200) { [XPRoomViewController openRoom:[AccountInfoStorage instance].getUid mgId:gameModel.mgId viewController:self]; } else { [self showErrorToast:msg]; } } params:params]; } #pragma mark - XPHomeBannerTableViewCell - (void)xPHomeBannerTableViewCell:(XPHomeBannerTableViewCell *)view didClickBanner:(HomeBannerInfoModel *)info { switch (info.skipType) { case HomeBannerInfoSkipType_Room: { if (info.skipUri.length > 0) { [XPRoomViewController openRoom:info.skipUri viewController:self]; } } break; case HomeBannerInfoSkipType_Web: { XPWebViewController *vc = [[XPWebViewController alloc]init]; vc.url = info.skipUri; [self.navigationController pushViewController:vc animated:YES]; } break; default: break; } } #pragma mark - XPHomeRecommendProtocol ///扩列交友 - (void)getPlayGameWithTeamSuccess:(NSArray *)list withGroup:(nonnull dispatch_group_t)group{ self.playTeamList = [NSMutableArray arrayWithArray:list]; dispatch_group_leave(group); } - (void)getHomeRecommendDataFailWithGroup:(dispatch_group_t)group{ dispatch_group_leave(group); } - (void)getFriendListSuccess:(NSArray *)list withGroup:(dispatch_group_t)group{ for (int i = 0;i < list.count;i++) { HomePlayRoomModel *model = list[i]; NSMutableArray *labels = [NSMutableArray array]; [labels addObject:[NSString calculateConstellationWithMonth:model.birth] ?: @""]; [labels addObjectsFromArray:model.labels]; model.labels = labels; NSMutableArray *widthList = [NSMutableArray array]; for (NSString *text in model.labels) { CGFloat width = [UILabel getWidthWithText:text height:kGetScaleWidth(18) font:kFontRegular(10)] + kGetScaleWidth(16) + 1; [widthList addObject:@(width)]; } model.labelsWidthList = widthList; } self.personalRoomList = [NSMutableArray arrayWithArray:list]; dispatch_group_leave(group); } - (void)onGetLittleGameListSuccess:(NSArray *)items withGroup:(nonnull dispatch_group_t)group{ self.littleGameArray = [[NSMutableArray alloc]initWithArray:items]; dispatch_group_leave(group); } - (void)getFriendListFailWithGroup:(dispatch_group_t)group{ dispatch_group_leave(group); } #pragma mark - JXPagingViewListViewDelegate - (UIScrollView *)listScrollView { return self.tableView; } - (void)listViewDidScrollCallback:(void (^)(UIScrollView *))callback { self.scrollCallback = callback; } - (UIView *)listView { return self.view; } #pragma mark- XPNewHomePartyTableViewCellDelegate ///点击头像 -(void)xPNewHomePartyTableViewCell:(XPNewHomePartyTableViewCell *_Nullable)cell didSelectItem:(HomePlayRoomModel *_Nullable)roomModel{ XPMineUserInfoViewController *infoVC = [[XPMineUserInfoViewController alloc]init]; infoVC.uid = [roomModel.uid integerValue]; [self.navigationController pushViewController:infoVC animated:YES]; } ///点击聊天 -(void)xPNewHomePartyTableViewCell:(XPNewHomePartyTableViewCell *_Nullable)cell didSelectChat:(HomePlayRoomModel *_Nonnull)roomModel{ if(roomModel.inMic == YES){ [XPRoomViewController openRoom:roomModel.inRoomUid fromNick:roomModel.nick fromType:UserEnterRoomFromType_Follow_User fromUid:roomModel.uid viewController:self]; return; } NIMSession * session = [NIMSession session:roomModel.uid type:NIMSessionTypeP2P]; SessionViewController * sessionVC = [[SessionViewController alloc] initWithSession:session]; [self.navigationController pushViewController:sessionVC animated:YES]; } ///播放音乐 -(void)xPNewHomePartyTableViewCell:(XPNewHomePartyTableViewCell *_Nullable)cell didPlayVoice:(HomePlayRoomModel *_Nonnull)roomModel didClickPlaySound:(BOOL)isPlay{ if(isPlay == YES){ if(self.cell != nil){ [self.countDownHelper stopCountDown]; [self.cell setPlaySoundStatus:NO]; [[XPSkillCardPlayerManager shareInstance] stopMusic]; self.cell = nil; } self.cell = cell; CGRect cellRect = [self.tableView rectForRowAtIndexPath:[self.tableView indexPathForCell:self.cell]]; self.cellY = cellRect.origin.y + cellRect.size.height; NSString *fileName = [[roomModel.userVoice componentsSeparatedByString:@"/"] lastObject]; NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) safeObjectAtIndex1:0] stringByAppendingPathComponent:@"kMineSoundCard"]; NSString *fullPath = [filePath stringByAppendingPathComponent:fileName]; if ([[NSFileManager defaultManager] fileExistsAtPath:fullPath]) { [self playAudioWithUrl:fullPath isDelay:[XPSkillCardPlayerManager shareInstance].isMineInMic]; }else{ if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]) { NSFileManager *fileMgr = [[NSFileManager alloc] init]; [fileMgr createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil]; } [UploadFile downloadAudioWithFileName:fileName musicUrl:roomModel.userVoice mainFileName:@"kMineSoundCard" completion:^(BOOL isSuccess, NSString *editAudioPath) { if(isSuccess){ [self playAudioWithUrl:editAudioPath isDelay:[XPSkillCardPlayerManager shareInstance].isMineInMic]; }else{ [self.cell setPlaySoundStatus:NO]; [[XPSkillCardPlayerManager shareInstance] stopMusic]; self.cell = nil; } }]; } }else{ [self.countDownHelper stopCountDown]; [self.cell setPlaySoundStatus:NO]; [[XPSkillCardPlayerManager shareInstance] stopMusic]; self.cell = nil; } } /// 播放录音 /// - Parameters: /// - url: 录音链接 /// - isDelay: 是否延时0.5秒播放录音,因为如果在麦上的话,需要先关麦再播放,所以要延时0.5秒来关麦后再播放,不然会没声音 -(void)playAudioWithUrl:(NSString*)url isDelay:(BOOL)isDelay{ if(isDelay == YES){ [[XPSkillCardPlayerManager shareInstance] playerNewVoiceWithPath:url completionBlock:^{ [[XPSkillCardPlayerManager shareInstance] stopMusic]; }]; dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)); dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ NSInteger getCurrentTime = ceil([self.cell.roomInfo.voiceDura integerValue]); [self.countDownHelper openCountdownWithTime:(int)getCurrentTime]; }); return; } NSInteger getCurrentTime = ceil([self.cell.roomInfo.voiceDura integerValue]); [self.countDownHelper openCountdownWithTime:(int)getCurrentTime]; [[XPSkillCardPlayerManager shareInstance] playerVoiceWithPath:url completionBlock:^{ [[XPSkillCardPlayerManager shareInstance] stopMusic]; }]; } ///倒计时结束 - (void)onCountdownFinish{ if(self.cell != nil){ [self.cell setPlaySoundTime:0]; dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)); dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ [self.cell setPlaySoundStatus:NO]; self.cell = nil; }); } } ///倒计时进行 - (void)onCountdownOpen:(int)time{ if(self.cell != nil){ [self.cell setPlaySoundTime:(int)time + 1]; } } - (UITableView *)tableView { if (!_tableView) { _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain]; _tableView.delegate = self; _tableView.dataSource = self; _tableView.separatorStyle = UITableViewCellSeparatorStyleNone; _tableView.backgroundColor = [UIColor clearColor]; if (@available(iOS 11.0, *)) { _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; } [_tableView registerClass:[XPNewHomePlayTableViewCell class] forCellReuseIdentifier:NSStringFromClass([XPNewHomePlayTableViewCell class])]; [_tableView registerClass:[XPNewHomePlayEmptyTableViewCell class] forCellReuseIdentifier:NSStringFromClass([XPNewHomePlayEmptyTableViewCell class])]; [_tableView registerClass:[XPNewHomePartyTableViewCell class] forCellReuseIdentifier:NSStringFromClass([XPNewHomePartyTableViewCell class])]; } return _tableView; } - (CountDownHelper *)countDownHelper{ if (!_countDownHelper){ _countDownHelper = [[CountDownHelper alloc]init]; } return _countDownHelper; } @end