// // QXRoomSongListSubView.m // QXLive // // Created by 启星 on 2025/6/9. // #import "QXRoomSongListSubView.h" #import "QXRoomSongListCell.h" #import "QXAgoraEngine.h" #import "QXMineNetwork.h" @interface QXRoomSongListSubView() @property (nonatomic,strong)UIView *searchBgView; @property (nonatomic,strong)UIImageView *searchIcon; @property (nonatomic,strong)UITextField *textField; @property (nonatomic,strong)NSMutableArray *dataArray; @property (nonatomic,assign)NSInteger page; @end @implementation QXRoomSongListSubView -(UIView *)listView{ return self; } -(void)listWillAppear{ if (!self.isSearch) { [self getSongList]; } } - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { [self initSubviews]; } return self; } -(void)initSubviews{ self.page = 1; self.searchBgView = [[UIView alloc] initWithFrame:CGRectMake(16, 6, self.width-32, 35)]; self.searchBgView.backgroundColor = RGB16(0xEFF2F8); [self.searchBgView addRoundedCornersWithRadius:17.5]; [self addSubview:self.searchBgView]; self.searchIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"room_song_search"]]; self.searchIcon.frame = CGRectMake(10, 10, 15, 15); [self.searchBgView addSubview:self.searchIcon]; self.textField = [[UITextField alloc] initWithFrame:CGRectMake(self.searchIcon.right+2, 0, self.width-self.searchIcon.right-2-10, 35)]; self.textField.textColor = RGB16(0x333333); self.textField.placeholder = QXText(@"请输入歌名搜索"); self.textField.font = [UIFont systemFontOfSize:13]; self.textField.returnKeyType = UIReturnKeyDone; [self.textField addTarget:self action:@selector(textDidChange:) forControlEvents:UIControlEventEditingChanged]; self.textField.delegate = self; [self.searchBgView addSubview:self.textField]; self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.searchBgView.bottom+10, self.width, self.height-self.searchBgView.bottom-10) style:(UITableViewStylePlain)]; self.tableView.delegate = self; self.tableView.dataSource = self; self.tableView.backgroundColor = [UIColor clearColor]; self.tableView.rowHeight = 72; self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; MJWeakSelf self.tableView.mj_footer = [MJRefreshBackStateFooter footerWithRefreshingBlock:^{ weakSelf.page++; [weakSelf getSongList]; }]; self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{ weakSelf.page = 1; [weakSelf getSongList]; }]; [self addSubview:self.tableView]; // [self getSongList]; } -(void)setIsBgMusic:(BOOL)isBgMusic{ _isBgMusic = isBgMusic; [self.tableView reloadData]; } #pragma mark - UITextFieldDelegate -(BOOL)textFieldShouldReturn:(UITextField *)textField{ [textField resignFirstResponder]; return YES; } -(void)textFieldDidBeginEditing:(UITextField *)textField{ if (self.beiginEditBlock) { self.beiginEditBlock(); } } -(void)textFieldDidEndEditing:(UITextField *)textField{ if (self.endEditBlock) { self.endEditBlock(); } } -(void)setIsSearch:(BOOL)isSearch{ _isSearch = isSearch; self.searchBgView.hidden = !isSearch; self.tableView.frame = CGRectMake(0, isSearch?self.searchBgView.bottom+10:6, self.width, isSearch?(self.height-self.searchBgView.bottom-10):self.height - 6); } -(void)setRoomId:(NSString *)roomId{ _roomId = roomId; [self getSongList]; } -(void)getSongList{ MJWeakSelf if (self.isSearch) { if (self.textField.text.length == 0) { return; } [[QXAgoraEngine sharedEngine] searchSongWithKeyword:self.textField.text page:self.page resultBlock:^(NSArray * _Nonnull songList, BOOL isReload) { dispatch_async(dispatch_get_main_queue(), ^{ if (isReload) { [weakSelf.dataArray removeAllObjects]; } for (AgoraMusic*music in songList) { if ([music.singer isEqualToString:@"彭丽媛"]) { continue; } QXSongListModel *model = [[QXSongListModel alloc] init]; model.song_code = music.songCode; model.song_name = music.name; model.poster = music.poster; model.singer = music.singer; model.duration = music.durationS; [weakSelf.dataArray addObject:model]; } [weakSelf.tableView reloadData]; if (songList.count == 0) { weakSelf.tableView.mj_footer.state = MJRefreshStateNoMoreData; }else{ [weakSelf.tableView.mj_footer endRefreshing]; } [weakSelf.tableView.mj_header endRefreshing]; }); }]; }else{ if (self.isBgMusic) { self.dataArray = [NSMutableArray arrayWithArray:[QXAgoraEngine sharedEngine].bgMusicArray]; [self.tableView reloadData]; [self.tableView.mj_header endRefreshing]; self.tableView.mj_footer.state = MJRefreshStateNoMoreData; }else{ [QXMineNetwork roomSongListWithRoomId:self.roomId successBlock:^(NSArray * _Nonnull list) { [weakSelf.dataArray removeAllObjects]; [weakSelf.dataArray addObjectsFromArray:list]; [weakSelf.tableView reloadData]; weakSelf.tableView.mj_footer.state = MJRefreshStateNoMoreData; [weakSelf.tableView.mj_header endRefreshing]; } failBlock:^(NSError * _Nonnull error, NSString * _Nonnull msg) { [weakSelf.tableView.mj_header endRefreshing]; }]; } } } -(void)textDidChange:(UITextField*)textField{ [self getSongList]; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return self.dataArray.count; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ MJWeakSelf QXRoomSongListCell *cell = [QXRoomSongListCell cellWithTableView:tableView]; cell.isBgMusic = self.isBgMusic; cell.isCompere = self.isCompere; cell.roomId = self.roomId; cell.isSearch = self.isSearch; cell.model = [self.dataArray objectAtIndex:indexPath.row]; cell.moveSuccessBlock = ^{ [weakSelf getSongList]; }; return cell; } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ // QXSongListModel *model = self.dataArray[indexPath.row]; // [[QXAgoraEngine sharedEngine] ktv_EndSing]; // [[QXAgoraEngine sharedEngine] ktv_StartSing:YES withSong:model]; } -(NSMutableArray *)dataArray{ if (!_dataArray) { _dataArray = [NSMutableArray array]; } return _dataArray; } @end