audioPlayListView.m 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. //
  2. // audioPlayListView.m
  3. // Private-X
  4. //
  5. // Created by xd h on 2024/7/15.
  6. //
  7. #import "audioPlayListView.h"
  8. #import "audioPlayListViewCell.h"
  9. @interface audioPlayListView ()<UITableViewDelegate,UITableViewDataSource>
  10. @property(nonatomic,strong) NSMutableArray*curDataArr;
  11. @property(nonatomic,strong) UILabel*leftTitleLab;
  12. @property(nonatomic,strong) UIView *whiteBgView;
  13. @property (nonatomic, strong) UITableView *tableView;
  14. @end
  15. @implementation audioPlayListView
  16. - (id)initWithFrame:(CGRect)frame withIndex:(NSInteger)index{
  17. self = [super initWithFrame:frame];
  18. self.backgroundColor = [UIColor hwColor:@"#000000" alpha:0.6];
  19. _playingIndex = index;
  20. [self drawAnyView];
  21. return self;
  22. }
  23. -(void)drawAnyView
  24. {
  25. //大按钮响应
  26. UIButton *bigRightButton = [[UIButton alloc] init];
  27. bigRightButton.tag = 1;
  28. [bigRightButton addTarget:self action:@selector(didClickButtonFun:) forControlEvents:UIControlEventTouchUpInside];
  29. [self addSubview:bigRightButton];
  30. //bigRightButton.backgroundColor= [UIColor greenColor];
  31. [bigRightButton mas_makeConstraints:^(MASConstraintMaker *make) {
  32. make.top.mas_equalTo(0);
  33. make.left.mas_equalTo(0);
  34. make.right.mas_equalTo(0);
  35. make.bottom.mas_equalTo(0);
  36. }];
  37. CGFloat topViewH = 52.0;
  38. CGFloat bottomViewH = 20.0 + safeArea;
  39. CGFloat tableViewCellH = 70.0;
  40. _curDataArr = [audioPlayListManager shareManager].audioPlayListArr;
  41. NSInteger count = _curDataArr.count;
  42. if(count >5){
  43. count = 5;
  44. }
  45. else if(count == 0){
  46. count = 1;
  47. }
  48. _whiteBgView = [[UIView alloc] init];
  49. _whiteBgView.backgroundColor = [UIColor whiteColor];
  50. [self addSubview:_whiteBgView];
  51. _whiteBgView.layer.cornerRadius = 12;
  52. _whiteBgView.layer.masksToBounds = YES;
  53. [_whiteBgView mas_makeConstraints:^(MASConstraintMaker *make) {
  54. make.bottom.mas_equalTo(20);
  55. make.right.mas_equalTo(0);
  56. make.left.mas_equalTo(0);
  57. make.height.mas_equalTo(topViewH + bottomViewH +tableViewCellH*count);
  58. //make.height.mas_equalTo(150);
  59. }];
  60. //当前播放列表
  61. _leftTitleLab = [[UILabel alloc] init];
  62. _leftTitleLab.textColor = [UIColor hwColor:@"#0A132B"];
  63. _leftTitleLab.font = [UIFont systemFontOfSize:14.0];
  64. [_whiteBgView addSubview:_leftTitleLab];
  65. [_leftTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
  66. make.left.mas_equalTo(15);
  67. make.width.mas_equalTo(200);
  68. make.height.mas_equalTo(52);
  69. make.top.mas_equalTo(0);
  70. }];
  71. //添加播放列表
  72. UIButton *addAudioBut = [[UIButton alloc] init];
  73. [addAudioBut setTitle:NSLocalizedString(@"NAS_add_audio_to_play_list",nil) forState:UIControlStateNormal];
  74. [addAudioBut setTitleColor:[UIColor hwColor:@"#51555C"] forState:UIControlStateNormal];
  75. addAudioBut.titleLabel.font = [UIFont systemFontOfSize:10.0];
  76. [addAudioBut setImage:[UIImage imageNamed:@"nas_audio_add_icon"] forState:UIControlStateNormal];
  77. [addAudioBut addTarget:self action:@selector(didClickAddAudioFun) forControlEvents:UIControlEventTouchUpInside];
  78. addAudioBut.backgroundColor = [UIColor hwColor:@"#F3F3F3"];
  79. addAudioBut.layer.cornerRadius = 8;
  80. addAudioBut.layer.masksToBounds = YES;
  81. [_whiteBgView addSubview:addAudioBut];
  82. [addAudioBut mas_makeConstraints:^(MASConstraintMaker *make) {
  83. make.right.mas_equalTo(-20);
  84. make.width.mas_equalTo(100);
  85. make.height.mas_equalTo(26);
  86. make.centerY.equalTo(_leftTitleLab.mas_centerY).offset(0);
  87. }];
  88. [_whiteBgView addSubview:self.tableView];
  89. [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
  90. make.left.mas_equalTo(0);
  91. make.right.mas_equalTo(0);
  92. make.bottom.mas_equalTo(0);
  93. make.top.mas_equalTo(topViewH);
  94. }];
  95. [self setTitleStrFun];
  96. }
  97. #pragma mark 点击了添加播放列表
  98. - (void)didClickAddAudioFun
  99. {
  100. [self removeFun];
  101. if(_didClickButtonFun){
  102. _didClickButtonFun();
  103. }
  104. }
  105. #pragma mark - 懒加载
  106. - (UITableView *)tableView{
  107. if (!_tableView) {
  108. _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, SCREEN_H - TABBARHEIGHT) style:UITableViewStylePlain];
  109. _tableView.delegate = self;
  110. _tableView.dataSource = self;
  111. _tableView.showsVerticalScrollIndicator = NO;
  112. _tableView.showsHorizontalScrollIndicator = NO;
  113. // _tableView.contentInset = UIEdgeInsetsMake(-H_STATE_BAR, 0, 0, 0);
  114. [_tableView setSeparatorStyle:(UITableViewCellSeparatorStyleNone)];
  115. [_tableView setSeparatorColor:[UIColor clearColor]];
  116. [_tableView setBackgroundColor:[UIColor clearColor]];
  117. [_tableView setTableFooterView:[UIView new]];
  118. [_tableView setBounces:YES];
  119. if (@available(iOS 15.0, *)) {
  120. _tableView.sectionHeaderTopPadding = 0;
  121. }
  122. //空数据引入第三方开源处理
  123. // _tableView.emptyDataSetSource = self;
  124. // _tableView.emptyDataSetDelegate = self;
  125. }
  126. return _tableView;
  127. }
  128. #pragma mark - 列表委托
  129. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
  130. return 1;
  131. }
  132. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  133. return _curDataArr.count;
  134. }
  135. - (audioPlayListViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  136. __block NSInteger row = indexPath.row;
  137. static NSString *identifier = @"audioPlayListViewCell";
  138. audioPlayListViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
  139. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  140. if (!cell){
  141. cell = [[audioPlayListViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
  142. [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
  143. [cell setBackgroundColor:[UIColor clearColor]];
  144. [cell setAccessoryType:(UITableViewCellAccessoryNone)];
  145. }
  146. if(row < _curDataArr.count){
  147. lastFileModel* dataModel = _curDataArr[row];
  148. cell.curLastFileModel = dataModel;
  149. if(row == _playingIndex){
  150. cell.isPlayingType = YES;
  151. }
  152. else{
  153. cell.isPlayingType = NO;
  154. }
  155. KWeakSelf
  156. cell.didClickSwitch = ^(BOOL SwitchOn) {
  157. [weakSelf didClickAudioInPlayListBy:row];
  158. };
  159. }
  160. return cell;
  161. }
  162. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  163. {
  164. NSInteger row = indexPath.row;
  165. if(_playingIndex == row){
  166. return;
  167. }
  168. if(row < _curDataArr.count){
  169. if(_didClickAudioFun){
  170. _didClickAudioFun(row);
  171. }
  172. }
  173. }
  174. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  175. return 70;
  176. }
  177. #pragma mark 设置标题
  178. - (void)setTitleStrFun
  179. {
  180. NSString *leftStr = NSLocalizedString(@"NAS_audio_cur_play_list",nil);
  181. _curDataArr = [audioPlayListManager shareManager].audioPlayListArr;
  182. NSString *rightStr = [[NSString alloc] initWithFormat:@"(%ld)",_curDataArr.count];
  183. NSString *totalStr = [[NSString alloc] initWithFormat:@"%@ %@",leftStr,rightStr];
  184. NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:totalStr];
  185. NSRange redRange = NSMakeRange([totalStr rangeOfString:rightStr].location, [totalStr rangeOfString:rightStr].length);
  186. UIColor *noteColor =[UIColor hwColor:@"#959799" alpha:1.0];
  187. [attrStr addAttribute:NSForegroundColorAttributeName value:noteColor range:redRange];
  188. [attrStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12.0] range:redRange];
  189. _leftTitleLab.attributedText = attrStr;
  190. NSInteger count = _curDataArr.count;
  191. if(count >5){
  192. count = 5;
  193. }
  194. else if(count == 0){
  195. count = 1;
  196. }
  197. CGFloat topViewH = 52.0;
  198. CGFloat bottomViewH = 20.0 + safeArea;
  199. CGFloat tableViewCellH = 70.0;
  200. [_whiteBgView mas_remakeConstraints:^(MASConstraintMaker *make) {
  201. make.bottom.mas_equalTo(20);
  202. make.right.mas_equalTo(0);
  203. make.left.mas_equalTo(0);
  204. make.height.mas_equalTo(topViewH + bottomViewH +tableViewCellH*count);
  205. }];
  206. [self.tableView reloadData];
  207. }
  208. - (void)setPlayingIndex:(NSInteger)playingIndex
  209. {
  210. _playingIndex = playingIndex;
  211. [self.tableView reloadData];
  212. }
  213. #pragma mark 按钮事件
  214. - (void)didClickButtonFun:(UIButton*)but
  215. {
  216. NSInteger tag = but.tag;
  217. HLog(@"%ld",tag);
  218. [self removeFun];
  219. }
  220. - (void)removeFun
  221. {
  222. [self removeFromSuperview];
  223. }
  224. #pragma mark 重新获取数据
  225. - (void)reGetDataFun
  226. {
  227. [self setTitleStrFun];
  228. HLog(@"%@",_curDataArr);
  229. }
  230. #pragma mark 点击删除音频
  231. - (void)didClickAudioInPlayListBy:(NSInteger)row
  232. {
  233. if(row < _curDataArr.count){
  234. lastFileModel* dataModel = _curDataArr[row];
  235. if(_didClickDeleteFun){
  236. if(row < _playingIndex){
  237. _playingIndex --;
  238. }
  239. _didClickDeleteFun(dataModel);
  240. }
  241. }
  242. }
  243. @end