// // audioPlayListView.m // Private-X // // Created by xd h on 2024/7/15. // #import "audioPlayListView.h" #import "audioPlayListViewCell.h" @interface audioPlayListView () @property(nonatomic,strong) NSMutableArray*curDataArr; @property(nonatomic,strong) UILabel*leftTitleLab; @property(nonatomic,strong) UIView *whiteBgView; @property (nonatomic, strong) UITableView *tableView; @end @implementation audioPlayListView - (id)initWithFrame:(CGRect)frame withIndex:(NSInteger)index{ self = [super initWithFrame:frame]; self.backgroundColor = [UIColor hwColor:@"#000000" alpha:0.6]; _playingIndex = index; [self drawAnyView]; return self; } -(void)drawAnyView { //大按钮响应 UIButton *bigRightButton = [[UIButton alloc] init]; bigRightButton.tag = 1; [bigRightButton addTarget:self action:@selector(didClickButtonFun:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:bigRightButton]; //bigRightButton.backgroundColor= [UIColor greenColor]; [bigRightButton mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(0); make.left.mas_equalTo(0); make.right.mas_equalTo(0); make.bottom.mas_equalTo(0); }]; CGFloat topViewH = 52.0; CGFloat bottomViewH = 20.0 + safeArea; CGFloat tableViewCellH = 70.0; _curDataArr = [audioPlayListManager shareManager].audioPlayListArr; NSInteger count = _curDataArr.count; if(count >5){ count = 5; } else if(count == 0){ count = 1; } _whiteBgView = [[UIView alloc] init]; _whiteBgView.backgroundColor = [UIColor whiteColor]; [self addSubview:_whiteBgView]; _whiteBgView.layer.cornerRadius = 12; _whiteBgView.layer.masksToBounds = YES; [_whiteBgView mas_makeConstraints:^(MASConstraintMaker *make) { make.bottom.mas_equalTo(20); make.right.mas_equalTo(0); make.left.mas_equalTo(0); make.height.mas_equalTo(topViewH + bottomViewH +tableViewCellH*count); //make.height.mas_equalTo(150); }]; //当前播放列表 _leftTitleLab = [[UILabel alloc] init]; _leftTitleLab.textColor = [UIColor hwColor:@"#0A132B"]; _leftTitleLab.font = [UIFont systemFontOfSize:14.0]; [_whiteBgView addSubview:_leftTitleLab]; [_leftTitleLab mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(15); make.width.mas_equalTo(200); make.height.mas_equalTo(52); make.top.mas_equalTo(0); }]; //添加播放列表 UIButton *addAudioBut = [[UIButton alloc] init]; [addAudioBut setTitle:NSLocalizedString(@"NAS_add_audio_to_play_list",nil) forState:UIControlStateNormal]; [addAudioBut setTitleColor:[UIColor hwColor:@"#51555C"] forState:UIControlStateNormal]; addAudioBut.titleLabel.font = [UIFont systemFontOfSize:10.0]; [addAudioBut setImage:[UIImage imageNamed:@"nas_audio_add_icon"] forState:UIControlStateNormal]; [addAudioBut addTarget:self action:@selector(didClickAddAudioFun) forControlEvents:UIControlEventTouchUpInside]; addAudioBut.backgroundColor = [UIColor hwColor:@"#F3F3F3"]; addAudioBut.layer.cornerRadius = 8; addAudioBut.layer.masksToBounds = YES; [_whiteBgView addSubview:addAudioBut]; [addAudioBut mas_makeConstraints:^(MASConstraintMaker *make) { make.right.mas_equalTo(-20); make.width.mas_equalTo(100); make.height.mas_equalTo(26); make.centerY.equalTo(_leftTitleLab.mas_centerY).offset(0); }]; [_whiteBgView addSubview:self.tableView]; [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(0); make.right.mas_equalTo(0); make.bottom.mas_equalTo(0); make.top.mas_equalTo(topViewH); }]; [self setTitleStrFun]; } #pragma mark 点击了添加播放列表 - (void)didClickAddAudioFun { [self removeFun]; if(_didClickButtonFun){ _didClickButtonFun(); } } #pragma mark - 懒加载 - (UITableView *)tableView{ if (!_tableView) { _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, SCREEN_H - TABBARHEIGHT) style:UITableViewStylePlain]; _tableView.delegate = self; _tableView.dataSource = self; _tableView.showsVerticalScrollIndicator = NO; _tableView.showsHorizontalScrollIndicator = NO; // _tableView.contentInset = UIEdgeInsetsMake(-H_STATE_BAR, 0, 0, 0); [_tableView setSeparatorStyle:(UITableViewCellSeparatorStyleNone)]; [_tableView setSeparatorColor:[UIColor clearColor]]; [_tableView setBackgroundColor:[UIColor clearColor]]; [_tableView setTableFooterView:[UIView new]]; [_tableView setBounces:YES]; if (@available(iOS 15.0, *)) { _tableView.sectionHeaderTopPadding = 0; } //空数据引入第三方开源处理 // _tableView.emptyDataSetSource = self; // _tableView.emptyDataSetDelegate = self; } return _tableView; } #pragma mark - 列表委托 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return _curDataArr.count; } - (audioPlayListViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ __block NSInteger row = indexPath.row; static NSString *identifier = @"audioPlayListViewCell"; audioPlayListViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier]; cell.selectionStyle = UITableViewCellSelectionStyleNone; if (!cell){ cell = [[audioPlayListViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier]; [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; [cell setBackgroundColor:[UIColor clearColor]]; [cell setAccessoryType:(UITableViewCellAccessoryNone)]; } if(row < _curDataArr.count){ lastFileModel* dataModel = _curDataArr[row]; cell.curLastFileModel = dataModel; if(row == _playingIndex){ cell.isPlayingType = YES; } else{ cell.isPlayingType = NO; } KWeakSelf cell.didClickSwitch = ^(BOOL SwitchOn) { [weakSelf didClickAudioInPlayListBy:row]; }; } return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSInteger row = indexPath.row; if(_playingIndex == row){ return; } if(row < _curDataArr.count){ if(_didClickAudioFun){ _didClickAudioFun(row); } } } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 70; } #pragma mark 设置标题 - (void)setTitleStrFun { NSString *leftStr = NSLocalizedString(@"NAS_audio_cur_play_list",nil); _curDataArr = [audioPlayListManager shareManager].audioPlayListArr; NSString *rightStr = [[NSString alloc] initWithFormat:@"(%ld)",_curDataArr.count]; NSString *totalStr = [[NSString alloc] initWithFormat:@"%@ %@",leftStr,rightStr]; NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:totalStr]; NSRange redRange = NSMakeRange([totalStr rangeOfString:rightStr].location, [totalStr rangeOfString:rightStr].length); UIColor *noteColor =[UIColor hwColor:@"#959799" alpha:1.0]; [attrStr addAttribute:NSForegroundColorAttributeName value:noteColor range:redRange]; [attrStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12.0] range:redRange]; _leftTitleLab.attributedText = attrStr; NSInteger count = _curDataArr.count; if(count >5){ count = 5; } else if(count == 0){ count = 1; } CGFloat topViewH = 52.0; CGFloat bottomViewH = 20.0 + safeArea; CGFloat tableViewCellH = 70.0; [_whiteBgView mas_remakeConstraints:^(MASConstraintMaker *make) { make.bottom.mas_equalTo(20); make.right.mas_equalTo(0); make.left.mas_equalTo(0); make.height.mas_equalTo(topViewH + bottomViewH +tableViewCellH*count); }]; [self.tableView reloadData]; } - (void)setPlayingIndex:(NSInteger)playingIndex { _playingIndex = playingIndex; [self.tableView reloadData]; } #pragma mark 按钮事件 - (void)didClickButtonFun:(UIButton*)but { NSInteger tag = but.tag; HLog(@"%ld",tag); [self removeFun]; } - (void)removeFun { [self removeFromSuperview]; } #pragma mark 重新获取数据 - (void)reGetDataFun { [self setTitleStrFun]; HLog(@"%@",_curDataArr); } #pragma mark 点击删除音频 - (void)didClickAudioInPlayListBy:(NSInteger)row { if(row < _curDataArr.count){ lastFileModel* dataModel = _curDataArr[row]; if(_didClickDeleteFun){ if(row < _playingIndex){ _playingIndex --; } _didClickDeleteFun(dataModel); } } } @end