YOUPAILZLiveMusicListWindow.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. //
  2. // YOUPAILZLiveMusicListWindow.m
  3. // VQU
  4. //
  5. // Created by CY on 2021/6/25.
  6. // Copyright © 2021 leo. All rights reserved.
  7. //
  8. #import "YOUPAILZLiveMusicListWindow.h"
  9. #import "UIScrollView+LZRefresh.h"
  10. #import "YOUPAILZLiveMusicItemCell.h"
  11. #import "YOUPAILZSelectedMusicListVC.h"
  12. @interface YOUPAILZLiveMusicListWindow () <UITableViewDelegate,UITableViewDataSource>
  13. @property (nonatomic, weak) UIButton *youpaipplayBtn; // 播放暂停
  14. @property (nonatomic, weak) UIButton *youpaiplyricBtn; // 歌词
  15. @property (nonatomic, weak) UITableView *youpaiptableView;
  16. @property (nonatomic, assign) NSInteger youpaippage;
  17. @property (nonatomic, strong) NSMutableArray <YOUPAILZMusicListItemModel *>*youpaipdataSource;
  18. @end
  19. @implementation YOUPAILZLiveMusicListWindow
  20. - (void)viewWillAppear:(BOOL)animated{
  21. [super viewWillAppear:animated];
  22. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(youpaifcurrentMusicNotification:) name:@"CurrentMusicChangeNotification" object:nil];
  23. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(youpaifmusicPlayStateChangeNotification:) name:@"MusicPlayStateChangeNotification" object:nil];
  24. }
  25. - (void)viewWillDisappear:(BOOL)animated{
  26. [super viewWillDisappear:animated];
  27. [[NSNotificationCenter defaultCenter] removeObserver:self];
  28. }
  29. - (void)viewDidLoad {
  30. [super viewDidLoad];
  31. self.navigationController.navigationBarHidden = YES;
  32. [self youpaifinitUI];
  33. [self youpaifrequestMusicListWithStatus:kRefreshNormal];
  34. }
  35. - (void)youpaifinitUI{
  36. self.view.backgroundColor = [HexColorFromRGB(0x2E2B40) colorWithAlphaComponent:0.94f];
  37. UIView *playBgV = [[UIView alloc] init];
  38. [self.view addSubview:playBgV];
  39. [playBgV mas_makeConstraints:^(MASConstraintMaker *make) {
  40. make.left.top.right.offset(0.0f);
  41. make.height.offset(64.0f);
  42. }];
  43. UIButton *sheetBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  44. sheetBtn.layer.cornerRadius = 8.0f;
  45. sheetBtn.clipsToBounds = YES;
  46. sheetBtn.layer.borderColor = ZYPinkColor.CGColor;
  47. sheetBtn.layer.borderWidth = 1.0f;
  48. sheetBtn.titleLabel.font = LCFont14;
  49. [sheetBtn setTitle:@"已点" forState:UIControlStateNormal];
  50. [sheetBtn setTitleColor:ZYPinkColor forState:UIControlStateNormal];
  51. [sheetBtn addTarget:self action:@selector(youpaifsheetBtnClick) forControlEvents:UIControlEventTouchUpInside];
  52. [playBgV addSubview:sheetBtn];
  53. [sheetBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  54. make.centerY.equalTo(playBgV);
  55. make.left.offset(16.0f);
  56. make.size.mas_offset(CGSizeMake(48, 28));
  57. }];
  58. UIButton *youpaipcloseBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  59. [youpaipcloseBtn setBackgroundImage:[UIImage imageNamed:@"vqu_images_L_live_window_close"] forState:UIControlStateNormal];
  60. [youpaipcloseBtn setBackgroundImage:[UIImage imageNamed:@"vqu_images_L_live_window_close"] forState:UIControlStateHighlighted];
  61. [youpaipcloseBtn addTarget:self action:@selector(youpaifcloseBtnClick) forControlEvents:UIControlEventTouchUpInside];
  62. [playBgV addSubview:youpaipcloseBtn];
  63. [youpaipcloseBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  64. make.centerY.equalTo(playBgV);
  65. make.right.offset(-7.0f);
  66. make.size.mas_offset(CGSizeMake(28, 28));
  67. }];
  68. UIView *playV = [[UIView alloc] init];
  69. [playBgV addSubview:playV];
  70. [playV mas_makeConstraints:^(MASConstraintMaker *make) {
  71. make.centerX.equalTo(playBgV);
  72. make.top.bottom.offset(0.0f);
  73. }];
  74. UIButton *youpaipplayBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  75. [youpaipplayBtn setImage:[UIImage imageNamed:@"vqu_images_L_live_music_player_play"] forState:UIControlStateNormal];
  76. [youpaipplayBtn setImage:[UIImage imageNamed:@"vqu_images_L_live_music_player_suspend"] forState:UIControlStateSelected];
  77. // youpaipplayBtn.selected = self.youpaipmusicPlayState != AgoraAudioMixingStatePlaying;
  78. [youpaipplayBtn addTarget:self action:@selector(youpaifplayBtnClick:) forControlEvents:UIControlEventTouchUpInside];
  79. [playV addSubview:youpaipplayBtn];
  80. self.youpaipplayBtn = youpaipplayBtn;
  81. [youpaipplayBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  82. make.centerY.equalTo(playV);
  83. make.left.offset(0.0f);
  84. make.size.mas_offset(CGSizeMake(40, 40));
  85. }];
  86. UIButton *nextBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  87. [nextBtn setImage:[UIImage imageNamed:@"vqu_images_L_live_music_next"] forState:UIControlStateNormal];
  88. [nextBtn setImage:[UIImage imageNamed:@"vqu_images_L_live_music_next"] forState:UIControlStateNormal];
  89. [nextBtn addTarget:self action:@selector(youpaifnextBtnClick) forControlEvents:UIControlEventTouchUpInside];
  90. [playV addSubview:nextBtn];
  91. [nextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  92. make.centerY.equalTo(playV);
  93. make.left.equalTo(youpaipplayBtn.mas_right).offset(16.0f);
  94. make.size.mas_offset(CGSizeMake(40, 40));
  95. }];
  96. UIButton *youpaiplyricBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  97. youpaiplyricBtn.layer.cornerRadius = 20.0f;
  98. youpaiplyricBtn.clipsToBounds = YES;
  99. youpaiplyricBtn.layer.borderColor = [UIColor whiteColor].CGColor;
  100. youpaiplyricBtn.layer.borderWidth = 1.0f;
  101. youpaiplyricBtn.titleLabel.font = LCFont16;
  102. [youpaiplyricBtn setTitle:@"词" forState:UIControlStateNormal];
  103. [youpaiplyricBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  104. [youpaiplyricBtn setTitleColor:ZYPinkColor forState:UIControlStateSelected];
  105. youpaiplyricBtn.selected = self.showLyric;
  106. youpaiplyricBtn.layer.borderColor = youpaiplyricBtn.selected ? ZYPinkColor.CGColor :[UIColor whiteColor].CGColor;
  107. [youpaiplyricBtn addTarget:self action:@selector(youpaiflyricBtnClick:) forControlEvents:UIControlEventTouchUpInside];
  108. [playV addSubview:youpaiplyricBtn];
  109. self.youpaiplyricBtn = youpaiplyricBtn;
  110. [youpaiplyricBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  111. make.centerY.equalTo(playV);
  112. make.left.equalTo(nextBtn.mas_right).offset(16.0f);
  113. make.right.offset(0.0f);
  114. make.size.mas_offset(CGSizeMake(40, 40));
  115. }];
  116. UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
  117. tableView.delegate = self;
  118. tableView.dataSource = self;
  119. tableView.rowHeight = 56.0f;
  120. tableView.estimatedRowHeight = 56.0f;
  121. tableView.estimatedSectionHeaderHeight = 0.0f;
  122. tableView.estimatedSectionFooterHeight = 0.0f;
  123. tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  124. tableView.backgroundColor = [UIColor clearColor];
  125. tableView.showsVerticalScrollIndicator = NO;
  126. [self.view addSubview:tableView];
  127. self.youpaiptableView = tableView;
  128. __weak typeof(self) weakSelf = self;
  129. [tableView setRefreshHeaderWithBlock:^{
  130. [weakSelf youpaifrequestMusicListWithStatus:kRefreshHeader];
  131. }];
  132. [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
  133. make.left.bottom.right.offset(0.0f);
  134. make.top.equalTo(playBgV.mas_bottom).offset(0.0f);
  135. }];
  136. }
  137. /// 当前播放歌曲发生改变
  138. - (void)youpaifcurrentMusicNotification:(NSNotification *)notification{
  139. self.youpaipcurrentMusic = notification.object;
  140. [self youpaifsetupCurrentMusic];
  141. }
  142. - (void)youpaifsetupCurrentMusic{
  143. if (self.youpaipcurrentMusic == nil) {
  144. return;
  145. }
  146. for (YOUPAILZMusicListItemModel *itemModel in self.youpaipdataSource) {
  147. if ([itemModel.youpaipsongCode isEqual:self.youpaipcurrentMusic.youpaipsongCode]) {
  148. itemModel.youpaipisPlay = YES;
  149. }else{
  150. itemModel.youpaipisPlay = NO;
  151. }
  152. }
  153. [self.youpaiptableView reloadData];
  154. }
  155. /// 播放状态发生改变
  156. - (void)youpaifmusicPlayStateChangeNotification:(NSNotification *)notification{
  157. // self.youpaipmusicPlayState = [notification.object integerValue];
  158. // self.youpaipplayBtn.selected = self.youpaipmusicPlayState != AgoraAudioMixingStatePlaying;
  159. }
  160. #pragma mark - UITableViewDataSource
  161. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  162. return self.youpaipdataSource.count;
  163. }
  164. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  165. NSString *cellID = @"YOUPAILZLiveMusicItemCell";
  166. YOUPAILZLiveMusicItemCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
  167. if (cell == nil) {
  168. cell = [[YOUPAILZLiveMusicItemCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
  169. }
  170. [cell youpaifreloadWithModel:self.youpaipdataSource[indexPath.row]];
  171. WeakSelf;
  172. [cell setYoupaipaddMusicBlock:^(YOUPAILZMusicListItemModel * _Nonnull model) {
  173. BOOL youpaipisContains = NO;
  174. for (YOUPAILZMusicListItemModel *itemModel in weakSelf.youpaipmusicList) {
  175. if ([itemModel.youpaipsongCode isEqual:model.youpaipsongCode]) {
  176. youpaipisContains = YES;
  177. break;
  178. }
  179. }
  180. if (!youpaipisContains) {
  181. [weakSelf.youpaipmusicList insertObject:model atIndex:0];
  182. }
  183. }];
  184. return cell;
  185. }
  186. #pragma mark - UITableViewDelegate
  187. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
  188. return CGFLOAT_MIN;
  189. }
  190. - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
  191. return CGFLOAT_MIN;
  192. }
  193. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  194. YOUPAILZMusicListItemModel *model = self.youpaipdataSource[indexPath.row];
  195. BOOL youpaipisContains = NO;
  196. for (YOUPAILZMusicListItemModel *itemModel in self.youpaipmusicList) {
  197. if ([itemModel.youpaipsongCode isEqual:model.youpaipsongCode]) {
  198. youpaipisContains = YES;
  199. break;
  200. }
  201. }
  202. if (!youpaipisContains) {
  203. [self.youpaipmusicList insertObject:model atIndex:0];
  204. }
  205. if (self.youpaipplayMusicBlock) {
  206. self.youpaipplayMusicBlock(model);
  207. }
  208. [self.youpaiptableView reloadData];
  209. }
  210. // 已点歌曲
  211. - (void)youpaifsheetBtnClick{
  212. YOUPAILZSelectedMusicListVC *vc = [[YOUPAILZSelectedMusicListVC alloc] init];
  213. vc.youpaipmusicList = self.youpaipmusicList;
  214. vc.youpaipcurrentMusic = self.youpaipcurrentMusic;
  215. WeakSelf;
  216. [vc setYoupaipplayMusicBlock:^(YOUPAILZMusicListItemModel * _Nonnull model) {
  217. if (weakSelf.youpaipplayMusicBlock != nil) {
  218. weakSelf.youpaipplayMusicBlock(model);
  219. }
  220. }];
  221. [self.navigationController pushViewController:vc animated:YES];
  222. }
  223. // 播放暂停
  224. - (void)youpaifplayBtnClick:(UIButton *)sender{
  225. if (self.youpaipsuspendMusicBlock) {
  226. self.youpaipsuspendMusicBlock();
  227. }
  228. }
  229. // 下一曲
  230. - (void)youpaifnextBtnClick{
  231. if (self.youpaipnextMusicBlock) {
  232. self.youpaipnextMusicBlock();
  233. }
  234. }
  235. // 歌词
  236. - (void)youpaiflyricBtnClick:(UIButton *)sender{
  237. sender.selected = !sender.selected;
  238. self.youpaiplyricBtn.layer.borderColor = sender.selected ? ZYPinkColor.CGColor :[UIColor whiteColor].CGColor;
  239. if (self.showLyricBlock != nil) {
  240. self.showLyricBlock(sender.selected);
  241. }
  242. }
  243. // 退出
  244. - (void)youpaifcloseBtnClick{
  245. if (self.youpaipcloseBlock != nil) {
  246. self.youpaipcloseBlock();
  247. }
  248. [self dismissViewControllerAnimated:NO completion:nil];
  249. }
  250. /// 设置加载更多
  251. - (void)youpaifrefreshFooterWithHidden:(BOOL)hidden{
  252. __weak typeof(self) weakSelf = self;
  253. [self.youpaiptableView setRefreshFooter:hidden withBlock:^{
  254. [weakSelf youpaifrequestMusicListWithStatus:kRefreshFooter];
  255. }];
  256. }
  257. /// 获取列表数据
  258. - (void)youpaifrequestMusicListWithStatus:(kRefreshStatus)status{
  259. if (status == kRefreshFooter) {
  260. self.youpaippage ++;
  261. }else{
  262. self.youpaippage = 1;
  263. }
  264. WeakSelf;
  265. [LCHttpHelper requestWithURLString:MusicBaseHot parameters:@{@"page":@(self.youpaippage),@"pageSize":@(10)} needToken:YES type:(HttpRequestTypePost) success:^(id responseObject) {
  266. [weakSelf.youpaiptableView endRefreshing:kRefreshAll];
  267. NSDictionary* dict = (NSDictionary*)responseObject;
  268. NSInteger code = [[dict objectForKey:@"code"] integerValue];
  269. if (code == 0) {//成功
  270. if (status != kRefreshFooter) {
  271. [weakSelf.youpaipdataSource removeAllObjects];
  272. }
  273. NSDictionary *data = [dict objectForKey:@"data"];
  274. NSArray <YOUPAILZMusicListItemModel *>*list = [YOUPAILZMusicListItemModel mj_objectArrayWithKeyValuesArray:[data objectForKey:@"record"]];
  275. [weakSelf.youpaipdataSource addObjectsFromArray:list];
  276. [weakSelf.youpaiptableView reloadData];
  277. [weakSelf youpaifsetupCurrentMusic];
  278. NSInteger totalCount = [[[data objectForKey:@"meta"] objectForKey:@"totalCount"] integerValue];
  279. [weakSelf youpaifrefreshFooterWithHidden:weakSelf.youpaippage * 10 >= totalCount];
  280. }
  281. } failure:^(NSError *error) {
  282. [weakSelf.youpaiptableView endRefreshing:kRefreshAll];
  283. }];
  284. }
  285. - (NSMutableArray<YOUPAILZMusicListItemModel *> *)youpaipdataSource{
  286. if (!_youpaipdataSource) {
  287. _youpaipdataSource = [NSMutableArray array];
  288. }
  289. return _youpaipdataSource;
  290. }
  291. @end