// // YOUPAILZHomeActiveVC.m // MSYOUPAI // // Created by CY on 2022/2/28. // Copyright © 2022 MS. All rights reserved. // #import "YOUPAILZHomeActiveVC.h" #import "UIScrollView+LZRefresh.h" #import "YOUPAILZHomeActiveCell.h" #import "FSAudioStream.h" #import "YOUPAILZUserShowVC.h" #import "YOUPAILCIMSessionVC.h" @interface YOUPAILZHomeActiveVC () @property (nonatomic, strong) NSMutableArray *youpaipdataSource; @property (nonatomic, strong) NSMutableDictionary *youpaipuserDict; @property (nonatomic, assign) NSInteger page; @property (nonatomic, weak) UITableView *youpaiptableView; // 语音播放器 @property (nonatomic, strong)FSAudioStream *youpaipaudioStream; @property (nonatomic, strong)NSTimer *youpaiptimer; @property (nonatomic, strong)YOUPAILCHomeListModel *youpaipCurrentPlayModel; @end @implementation YOUPAILZHomeActiveVC - (void)viewWillDisappear:(BOOL)animated{ [super viewWillDisappear:animated]; [self youpaifstopPlay]; } - (void)viewDidLoad { [super viewDidLoad]; [self youpaifsetupUI]; [self youpaifrequestListWithStatus:kRefreshNormal]; } - (void)youpaifsetupUI{ UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped]; tableView.dataSource = self; tableView.delegate = self; tableView.rowHeight = 80.0f; tableView.estimatedRowHeight = 80.0f; tableView.estimatedSectionHeaderHeight = 0.0f; tableView.estimatedSectionFooterHeight = 0.0f; tableView.separatorStyle = UITableViewCellSeparatorStyleNone; tableView.backgroundColor = [UIColor clearColor]; [self.view addSubview:tableView]; self.youpaiptableView = tableView; [tableView mas_remakeConstraints:^(MASConstraintMaker *make) { make.top.left.bottom.right.offset(0.0f); }]; @weakify(self); [tableView setRefreshHeaderWithBlock:^{ @strongify(self); [self youpaifstopPlay]; [self youpaifrequestListWithStatus:kRefreshHeader]; }]; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return self.youpaipdataSource.count; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return 1; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ YOUPAILZHomeActiveCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(YOUPAILZHomeActiveCell.class)]; if (cell == nil) { cell = [[YOUPAILZHomeActiveCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass(YOUPAILZHomeActiveCell.class)]; } [cell youpaifreloadWithModel:self.youpaipdataSource[indexPath.section]]; @weakify(self); [cell setYoupaipAudioClickBlock:^(YOUPAILCHomeListModel * _Nonnull model, BOOL isPlay) { @strongify(self); [self youpaifplayAudioWithModel:model isPlay:isPlay]; }]; [cell setYoupaipChatClickBlock:^(YOUPAILCHomeListModel * _Nonnull model) { @strongify(self); [self youpaifChatClickWithModel:model]; }]; return cell; } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ return CGFLOAT_MIN; } - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{ return 8.0f; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ YOUPAILCHomeListModel *model = self.youpaipdataSource[indexPath.section]; YOUPAILZUserShowVC *userShow = [[YOUPAILZUserShowVC alloc] init]; userShow.youpaipuserId = model.youpaipuserid; [self.navigationController pushViewController:userShow animated:YES]; } - (void)youpaifinitAudioStream{ self.youpaipaudioStream = [[FSAudioStream alloc] init]; @weakify(self); self.youpaipaudioStream.onFailure = ^(FSAudioStreamError error,NSString *description){ @strongify(self); [self youpaifstopPlay]; }; self.youpaipaudioStream.onCompletion = ^(){ @strongify(self); [self youpaifstopPlay]; }; [self.youpaipaudioStream setVolume:0.7];//设置声音 self.youpaipaudioStream.strictContentTypeChecking = NO; self.youpaipaudioStream.defaultContentType = @"audio/mpeg"; } /// 私信 搭讪 心动 - (void)youpaifChatClickWithModel:(YOUPAILCHomeListModel *)model{ if (![LCTools isGirlsRealNameAuthentication]) { return; } if(model.youpaipis_beckon){//心动 true 已心动过,false 未心动过 @weakify(self); NIMSession *session = [NIMSession session:model.youpaipuserid type:NIMSessionTypeP2P]; if (session) { [ZCHUDHelper show]; [[[NIMSDK sharedSDK] userManager] fetchUserInfos:@[session.sessionId] completion:^(NSArray * _Nullable users, NSError * _Nullable error) { @strongify(self); [ZCHUDHelper dismiss]; YOUPAILCIMSessionVC *vc = [[YOUPAILCIMSessionVC alloc] initWithSession:session]; vc.type = @"1"; [self.navigationController pushViewController:vc animated:YES]; }]; } }else{ [UMengRecordTool umengEventCountWithId:ClickToChatHome];//友盟首页点击搭讪 NSString *str = [NSString stringWithFormat:@"[%@]",model.youpaipuserid]; @weakify(self); [LCHttpHelper requestWithURLString:HomeBeckonSend parameters:@{@"user_ids":str} needToken:YES type:(HttpRequestTypePost) success:^(id responseObject) { @strongify(self); NSDictionary* dict = (NSDictionary*)responseObject; NSInteger code = [[dict objectForKey:@"code"] integerValue]; NSDictionary* dict1 = dict[@"data"]; if (code==0) {//成功 if (dict1[@"boole"]) { model.youpaipis_beckon = YES; [UIView performWithoutAnimation:^{ [self.youpaiptableView reloadSections:[NSIndexSet indexSetWithIndex:[self.youpaipdataSource indexOfObject:model]] withRowAnimation:UITableViewRowAnimationNone]; }]; } }else{ [ZCHUDHelper showTitle:[dict objectForKey:@"message"]]; } } failure:^(NSError *error) { }]; } } /// 播放or暂停 - (void)youpaifplayAudioWithModel:(YOUPAILCHomeListModel *)model isPlay:(BOOL)isPlay{ [self youpaifstopPlay]; self.youpaipCurrentPlayModel = model; if (isPlay) { [self youpaifstartPlay]; [self youpaifstartTimer]; }else{ [self youpaifstopPlay]; } } /// 开始播放语音 - (void)youpaifstartPlay{ if (self.youpaipaudioStream == nil) { [self youpaifinitAudioStream]; } NSURL *url = [LCTools getImageUrlWithAddress:self.youpaipCurrentPlayModel.youpaipmp3]; self.youpaipaudioStream.url = url; [self.youpaipaudioStream play]; [[self youpaifLoadCurrentPlayCell] youpaifreloadCurrentPlayTime]; } /// 停止播放语音 - (void)youpaifstopPlay{ // 停止播放 if (self.youpaipaudioStream != nil || [self.youpaipaudioStream isPlaying]) { [self.youpaipaudioStream stop]; self.youpaipaudioStream = nil; } self.youpaipCurrentPlayModel.youpaipisPlay = NO; self.youpaipCurrentPlayModel.youpaipCurrentPlayTime = 0; [self youpaifstopTimer]; [[self youpaifLoadCurrentPlayCell] youpaifstopPlay]; } - (void)youpaiftimerAction{ self.youpaipCurrentPlayModel.youpaipCurrentPlayTime = self.youpaipaudioStream.currentTimePlayed.playbackTimeInSeconds; [[self youpaifLoadCurrentPlayCell] youpaifreloadCurrentPlayTime]; } // 开始计时 - (void)youpaifstartTimer{ self.youpaiptimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(youpaiftimerAction) userInfo:nil repeats:YES]; [[NSRunLoop currentRunLoop] addTimer:self.youpaiptimer forMode:NSRunLoopCommonModes]; } // 结束计时 - (void)youpaifstopTimer{ [self.youpaiptimer invalidate]; self.youpaiptimer = nil; } - (YOUPAILZHomeActiveCell *)youpaifLoadCurrentPlayCell{ if ([self.youpaipdataSource containsObject:self.youpaipCurrentPlayModel]) { NSInteger index = [self.youpaipdataSource indexOfObject:self.youpaipCurrentPlayModel]; YOUPAILZHomeActiveCell *cell = [self.youpaiptableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:index]]; return cell; } return nil; } /// 设置加载更多 - (void)youpaifrefreshFooterWithHidden:(BOOL)hidden{ @weakify(self); [self.youpaiptableView setRefreshFooter:hidden withBlock:^{ @strongify(self); [self youpaifrequestListWithStatus:kRefreshFooter]; }]; } /// 获取列表数据 - (void)youpaifrequestListWithStatus:(kRefreshStatus)status{ if (status == kRefreshFooter) { self.page ++; }else{ self.page = 1; } @weakify(self); [LCHttpHelper requestWithURLString:self.youpaiprequestUrl parameters:@{@"page":@(self.page)} needToken:YES type:(HttpRequestTypePost) success:^(id responseObject) { @strongify(self); [self.youpaiptableView endRefreshing:kRefreshAll]; NSDictionary* dict = (NSDictionary*)responseObject; NSInteger code = [[dict objectForKey:@"code"] integerValue]; if (code == 0) {//成功 if (status != kRefreshFooter) { [self.youpaipdataSource removeAllObjects]; [self.youpaipuserDict removeAllObjects]; } NSInteger totalPage = [[[[dict objectForKey:@"data"] objectForKey:@"list"]objectForKey:@"total_page"]integerValue]; NSArray* array = [[[dict objectForKey:@"data"]objectForKey:@"list"]objectForKey:@"list"]; NSArray *list = [YOUPAILCHomeListModel mj_objectArrayWithKeyValuesArray:array]; for (YOUPAILCHomeListModel *model in list) { if (![self.youpaipuserDict objectForKey:model.youpaipuserid]) { [self.youpaipdataSource addObject:model]; [self.youpaipuserDict setObject:@"1" forKey:model.youpaipuserid]; } } // [self.dataSource addObjectsFromArray:list]; NSInteger currentPage = [[[[dict objectForKey:@"data"] objectForKey:@"list"]objectForKey:@"page"]integerValue]; if(currentPage>0&¤tPage%5==0) { [[SDImageCache sharedImageCache]clearMemory]; } [self.youpaiptableView reloadData]; [self youpaifrefreshFooterWithHidden:self.page >= totalPage]; if (self.youpaipdataSource.count != 0) { [self.youpaiptableView lz_hideEmptyView]; }else{ [self.youpaiptableView lz_showEmptyViewWithImage:[UIImage imageNamed:@"vqu_images_not_home_data"] content:@"暂无查到相关数据\n看看为您推荐的热搜小姐姐吧~"]; } if (self.showUserInfoImperfectBlock != nil) { BOOL is_info = [[[[dict objectForKey:@"data"] objectForKey:@"activity"] objectForKey:@"is_info"] boolValue]; self.showUserInfoImperfectBlock(!is_info); } } } failure:^(NSError *error) { [self.youpaiptableView endRefreshing:kRefreshAll]; }]; } #pragma mark - getter and setter - (NSMutableArray *)youpaipdataSource{ if (!_youpaipdataSource) { _youpaipdataSource = [NSMutableArray array]; } return _youpaipdataSource; } - (NSMutableDictionary *)youpaipuserDict{ if (!_youpaipuserDict) { _youpaipuserDict = [NSMutableDictionary dictionary]; } return _youpaipuserDict; } #pragma mark - JXCategoryListContentViewDelegate - (UIView *)listView{ return self.view; } @end