choseMuiscVC.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. //
  2. // choseMuiscVC.m
  3. // BuguLive
  4. //
  5. // Created by zzl on 16/6/14.
  6. // Copyright © 2016年 xfg. All rights reserved.
  7. //
  8. #import "choseMuiscVC.h"
  9. #import "musicCell.h"
  10. #import "dataModel.h"
  11. #import "MJRefresh.h"
  12. @interface choseMuiscVC ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate,musicDownloadDelegate,UIScrollViewDelegate>
  13. @end
  14. @implementation choseMuiscVC
  15. {
  16. NSMutableArray* _savedmusic;
  17. NSMutableArray* _searchmusic;
  18. NSMutableArray* _history;
  19. int _pagesearch;
  20. int _pagesaved;
  21. int _datatype;// 0 下载歌曲,1 搜索数据,2历史记录
  22. NSString* _keywords;
  23. musiceModel* _selectback;
  24. }
  25. - (void)viewDidLoad
  26. {
  27. self.mHidNarBar = YES;
  28. [super viewDidLoad];
  29. self.view.layer.cornerRadius = 10;
  30. self.mPageName = ASLocalizedString(@"点歌");
  31. self.msearchbar.placeholder = ASLocalizedString(@"请输音乐名称");
  32. _savedmusic = NSMutableArray.new;
  33. _searchmusic = NSMutableArray.new;
  34. _history = NSMutableArray.new;
  35. self.msearchbar.delegate = self;
  36. _datatype = 0;
  37. UIView *leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
  38. UIImageView *leftImageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 20, 20)];
  39. [leftImageView setImage:[UIImage imageNamed:@"搜索 (10)"]];
  40. [leftView addSubview:leftImageView];
  41. self.msearchbar.leftView = leftView;
  42. self.msearchbar.leftViewMode = UITextFieldViewModeAlways;
  43. self.msearchbar.clearButtonMode = UITextFieldViewModeAlways;
  44. UINib* nib = [UINib nibWithNibName:@"musicCell" bundle:nil];
  45. [self.mtableview registerNib:nib forCellReuseIdentifier:@"cell"];
  46. self.mtableview.delegate = self;
  47. self.mtableview.dataSource = self;
  48. self.mtableview.tableFooterView = UIView.new;
  49. self.mTableView = self.mtableview;
  50. self.mHaveFooter = YES;
  51. self.mHaveHeader = YES;
  52. self.mtableview.backgroundColor = [UIColor colorWithRed:240/255.0f green:247/255.0f blue:246/255.0f alpha:1];
  53. [BGMJRefreshManager refresh:self.mtableview target:self headerRereshAction:@selector(headerStartRefresh) shouldHeaderBeginRefresh:NO footerRereshAction:@selector(footerStartRefresh)];
  54. self.titleTopConstraint.constant = MG_BOTTOM_MARGIN + 20;
  55. //搜索使用的
  56. nib = [UINib nibWithNibName:@"musicCell" bundle:nil];
  57. [self.msearchtableview registerNib:nib forCellReuseIdentifier:@"cell"];
  58. self.msearchtableview.delegate = self;
  59. self.msearchtableview.dataSource = self;
  60. self.msearchtableview.tableFooterView = UIView.new;
  61. [BGMJRefreshManager refresh:self.msearchtableview target:self headerRereshAction:@selector(headerStartRefresh) shouldHeaderBeginRefresh:NO footerRereshAction:@selector(footerStartRefresh)];
  62. self.msearchtableview.backgroundColor = [UIColor colorWithRed:240/255.0f green:247/255.0f blue:246/255.0f alpha:1];
  63. self.mhistorytableview.delegate = self;
  64. self.mhistorytableview.dataSource = self;
  65. self.mhistorytableview.tableFooterView = UIView.new;
  66. [BGMJRefreshManager refresh:self.mhistorytableview target:self headerRereshAction:@selector(headerStartRefresh) shouldHeaderBeginRefresh:NO footerRereshAction:@selector(footerStartRefresh)];
  67. self.mhistorytableview.backgroundColor = [UIColor colorWithRed:240/255.0f green:247/255.0f blue:246/255.0f alpha:1];
  68. [self forcesHeaderReFresh];
  69. self.playerView.hidden = NO;
  70. [self.view addSubview:self.playerView];
  71. [self.playerView mas_makeConstraints:^(MASConstraintMaker *make) {
  72. make.bottom.equalTo(self.view).offset(-20);
  73. make.left.equalTo(self.view).offset(1);
  74. make.right.equalTo(self.view).offset(-1);
  75. make.height.equalTo(@74);
  76. }];
  77. [self.searchBtn addTarget:self action:@selector(handleSearchEvent) forControlEvents:UIControlEventTouchUpInside];
  78. }
  79. - (IBAction)backClick:(id)sender {
  80. _keywords = @"";
  81. self.backBtn.hidden = YES;
  82. self.msearchbar.hidden = YES;
  83. self.searchBtn.hidden = NO;
  84. self.closeBtn.hidden = NO;
  85. self.msearchbar.text = @"";
  86. [self headerStartRefresh];
  87. }
  88. - (void)handleSearchEvent {
  89. self.backBtn.hidden = NO;
  90. self.msearchbar.hidden = NO;
  91. self.searchBtn.hidden = YES;
  92. self.closeBtn.hidden = YES;
  93. }
  94. - (void)viewWillAppear:(BOOL)animated
  95. {
  96. [super viewWillAppear:animated];
  97. [self.navigationController setNavigationBarHidden:YES animated:NO];
  98. [IQKeyboardManager sharedManager].shouldResignOnTouchOutside = YES;
  99. [IQKeyboardManager sharedManager].enable = YES;
  100. [IQKeyboardManager sharedManager].enableAutoToolbar = YES;
  101. }
  102. - (void)viewWillDisappear:(BOOL)animated
  103. {
  104. [super viewWillDisappear:animated];
  105. [IQKeyboardManager sharedManager].shouldResignOnTouchOutside = NO;
  106. [IQKeyboardManager sharedManager].enable = NO;
  107. [IQKeyboardManager sharedManager].enableAutoToolbar = NO;
  108. }
  109. - (IBAction)backBtClicked:(UIButton *)sender
  110. {
  111. [super backBtClicked:sender];
  112. if(self.mitblock)
  113. {
  114. self.mitblock(_selectback);
  115. }
  116. _selectback = nil;
  117. [_savedmusic removeAllObjects];
  118. [_searchmusic removeAllObjects];
  119. [_history removeAllObjects];
  120. }
  121. - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField // return NO to not become first responder
  122. {
  123. // if( textField.text.length == 0 )
  124. // {
  125. // [self showPage:YES];
  126. // [self chageDataType:1 bforceload:NO];//刚刚上去 如果没有输入东西 就是 历史记录
  127. // }
  128. // else
  129. // {
  130. // [self showPage:YES];
  131. // [self chageDataType:1 bforceload:NO];//如果有文字,就是搜索歌曲
  132. // }
  133. return YES;
  134. }
  135. - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar // called when cancel button pressed
  136. {
  137. // [self showPage:NO];
  138. // [self chageDataType:0 bforceload:NO];//不搜索了,就是 本地歌曲
  139. // searchBar.text = nil;
  140. // //[_searchmusic removeAllObjects];
  141. // //[_history removeAllObjects];
  142. // [searchBar resignFirstResponder];
  143. // //刷新一下
  144. // [self headerStartRefresh];
  145. }
  146. - (BOOL)textFieldShouldReturn:(UITextField *)textField
  147. {
  148. _keywords = textField.text;
  149. [self headerStartRefresh];//n 35
  150. [textField resignFirstResponder];
  151. return YES;
  152. }
  153. - (void)textField:(UITextField *)textField textDidChange:(NSString *)searchText // called when text changes (including clear)
  154. {
  155. _keywords = searchText;
  156. if( _keywords.length == 0 )
  157. {
  158. [self chageDataType:2 bforceload:YES];//如果搜索关键字删除完了, 就展示历史搜索记录
  159. }
  160. else
  161. {
  162. [self chageDataType:1 bforceload:YES];//搜索开始
  163. [self headerStartRefresh];//n 35
  164. }
  165. }
  166. - (void)chageDataType:(int)type bforceload:(BOOL)bforceload
  167. {
  168. // _datatype = type;
  169. // UITableView* whattab = nil;
  170. //
  171. // BOOL brefrush =YES;
  172. // if(_datatype == 0)
  173. // { // 有数据就不管
  174. // self.msearchtableview.hidden = YES;
  175. // self.mhistorytableview.hidden = YES;
  176. // self.mtableview.hidden = NO;
  177. // if( _savedmusic.count && !bforceload ) brefrush= NO;
  178. //
  179. // whattab = self.mtableview;
  180. //
  181. // }
  182. // else if(_datatype == 1)
  183. // { // 如果搜索有数据,
  184. // self.msearchtableview.hidden = NO;
  185. // self.mhistorytableview.hidden = YES;
  186. // self.mtableview.hidden = YES;
  187. // if( _searchmusic.count && !bforceload ) brefrush= NO;
  188. //
  189. // whattab = self.msearchtableview;
  190. // }
  191. // else if(_datatype == 2)
  192. // {
  193. // self.msearchtableview.hidden = YES;
  194. // self.mhistorytableview.hidden = NO;
  195. // self.mtableview.hidden = YES;
  196. // if( _history.count && !bforceload ) brefrush= NO;
  197. //
  198. // whattab = self.mhistorytableview;
  199. // }
  200. //
  201. // if(brefrush)
  202. // [whattab.mj_header beginRefreshing];
  203. // else
  204. // [whattab reloadData];
  205. }
  206. - (void)showPage:(BOOL)bshowsearch
  207. {
  208. // [UIView animateWithDuration:0.3f animations:^{
  209. //
  210. // self.mtopconsth.constant = bshowsearch ? 75:125;
  211. // [self.view layoutIfNeeded];
  212. //
  213. // }];
  214. }
  215. - (void)headerStartRefresh
  216. {
  217. _pagesaved = 1;
  218. [musiceModel getMyMusicList:_pagesaved search:_keywords block:^(SResBase *resb, NSArray *all) {
  219. [self headerEndRefresh];
  220. [_savedmusic removeAllObjects];
  221. if( resb.msuccess )
  222. {
  223. [_savedmusic addObjectsFromArray: all];
  224. }
  225. else
  226. {
  227. [SVProgressHUD showErrorWithStatus:resb.mmsg];
  228. }
  229. [self.mtableview reloadData];
  230. }];
  231. }
  232. - (void)footerStartRefresh
  233. {
  234. if( _datatype == 0 )
  235. {
  236. _pagesaved ++;
  237. [musiceModel getMyMusicList:_pagesaved search:_keywords block:^(SResBase *resb, NSArray *all) {
  238. [self footerEndRefresh];
  239. if( resb.msuccess )
  240. {
  241. if (all)
  242. {
  243. if ([all count])
  244. {
  245. [_savedmusic addObjectsFromArray:all];
  246. }
  247. else
  248. {
  249. _pagesaved --;
  250. }
  251. }
  252. else
  253. {
  254. _pagesaved --;
  255. }
  256. }
  257. else
  258. {
  259. [SVProgressHUD showErrorWithStatus:resb.mmsg];
  260. }
  261. [self.mtableview reloadData];
  262. }];
  263. }
  264. }
  265. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  266. {
  267. if( tableView == self.msearchtableview )
  268. return _searchmusic.count;
  269. else if( tableView == self.mtableview )
  270. return _savedmusic.count;
  271. else
  272. return _history.count;
  273. }
  274. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  275. {
  276. return 80;
  277. }
  278. #pragma mark 删除单元格
  279. - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
  280. {
  281. if(tableView == self.mtableview || tableView == self.mhistorytableview)
  282. return UITableViewCellEditingStyleDelete;
  283. return UITableViewCellEditingStyleNone;
  284. }
  285. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
  286. {
  287. if(tableView == self.mtableview || tableView == self.mhistorytableview)
  288. return YES;
  289. return NO;
  290. }
  291. - (NSString*)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
  292. {
  293. if( tableView == self.mtableview || tableView == self.mhistorytableview)
  294. return ASLocalizedString(@"删除");
  295. return nil;
  296. }
  297. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  298. {
  299. if( tableView == self.mtableview )
  300. {
  301. if( editingStyle == UITableViewCellEditingStyleDelete )
  302. {
  303. if (indexPath.row < _savedmusic.count)
  304. {
  305. musiceModel* oneobj = _savedmusic[ indexPath.row ];
  306. [SVProgressHUD showWithStatus:ASLocalizedString(@"正在删除...")];
  307. [oneobj delThis:^(SResBase *resb) {
  308. if( resb.msuccess )
  309. {
  310. [SVProgressHUD dismiss];
  311. [_savedmusic removeObject:oneobj];
  312. [self.mtableview beginUpdates];
  313. [self.mtableview deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
  314. [self.mtableview endUpdates];
  315. }
  316. else
  317. {
  318. [SVProgressHUD showErrorWithStatus:resb.mmsg];
  319. }
  320. }];
  321. }
  322. }
  323. }
  324. if ( tableView == self.mhistorytableview)
  325. {
  326. if ( editingStyle == UITableViewCellEditingStyleDelete)
  327. {
  328. if (indexPath.row < _history.count)
  329. {
  330. [musiceModel deleteHistory:indexPath.row];
  331. [_history removeObjectAtIndex:indexPath.row];
  332. [self.mhistorytableview deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
  333. }
  334. }
  335. }
  336. }
  337. - (void)setPlayMusicId:(NSString *)playMusicId
  338. {
  339. _playMusicId = playMusicId;
  340. [self.mTableView reloadData];
  341. }
  342. - (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  343. {
  344. musiceModel * oneobj = nil;
  345. if( tableView == self.mtableview )
  346. {
  347. if (indexPath.row < _savedmusic.count)
  348. {
  349. oneobj = _savedmusic[ indexPath.row ];
  350. }
  351. }
  352. musicCell* cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
  353. cell.model = oneobj;
  354. if([self.playMusicId isEqualToString:oneobj.id])
  355. {
  356. cell.playBtn.selected = YES;
  357. }
  358. else
  359. {
  360. cell.playBtn.selected = NO;
  361. }
  362. cell.clickPlay = ^(BOOL isPlay, musiceModel *model) {
  363. self.playerView.songTitleLabel.text = model.title;
  364. self.playerView.playPauseButton.selected = isPlay;
  365. self.playerView.chosemusic = model;
  366. if(isPlay) {
  367. //播放中先调一下停止播放
  368. if (self.playMusicId.intValue > 0) {
  369. [[NSNotificationCenter defaultCenter] postNotificationName:@"stopMusic" object:nil];
  370. }
  371. self.playMusicId = model.id;
  372. // if([self.delegate respondsToSelector:@selector(playMusicClicked:)])
  373. // {
  374. // [self.delegate playMusicClicked:model];
  375. // }
  376. //发送一个通知
  377. [[NSNotificationCenter defaultCenter] postNotificationName:@"playMusic" object:model];
  378. }
  379. else
  380. {
  381. // if([self.delegate respondsToSelector:@selector(stopMusic)])
  382. // {
  383. // [self.delegate stopMusic];
  384. // }
  385. //发送一个通知
  386. [[NSNotificationCenter defaultCenter] postNotificationName:@"stopMusic" object:nil];
  387. }
  388. };
  389. oneobj.mmUIRef = cell;
  390. return cell;
  391. }
  392. -(void)clickDelete:(UITapGestureRecognizer *)sender{
  393. [musiceModel deleteHistory:sender.view.tag];
  394. [_history removeObjectAtIndex:sender.view.tag];
  395. [_mhistorytableview reloadData];
  396. // removeObject:sender.view];
  397. }
  398. #pragma mark 点击单元格
  399. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  400. {
  401. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  402. // musiceModel * oneobj = nil;
  403. // if( tableView == self.msearchtableview )
  404. // {
  405. // if (indexPath.row < _searchmusic.count)
  406. // {
  407. // oneobj = _searchmusic[ indexPath.row];
  408. // }
  409. // }
  410. // else if( tableView == self.mtableview )
  411. // {
  412. // if (indexPath.row < _savedmusic.count)
  413. // {
  414. // oneobj = _savedmusic[ indexPath.row ];
  415. // }
  416. // }
  417. // else
  418. // {
  419. // if (indexPath.row < _history.count)
  420. // {
  421. // oneobj = _history[ indexPath.row ];
  422. // }
  423. // }
  424. //
  425. // if( [oneobj isKindOfClass:[NSString class]] )
  426. // {
  427. // self.msearchbar.text = (NSString*)oneobj;
  428. // [self textField:self.msearchbar textDidChange:(NSString*)oneobj];
  429. // }
  430. // else
  431. // {
  432. // if( oneobj.mmFileStatus == 1 )
  433. // {//选择了
  434. // [oneobj addThisToMyList:nil];//如果本地有,也添加到服务器,
  435. // _selectback = oneobj;
  436. // [self backBtClicked:nil];
  437. // }
  438. // else if( oneobj.mmFileStatus == 0 )
  439. // {//下载
  440. // oneobj.mmDelegate = self;
  441. // [oneobj startDonwLoad:tableView];
  442. // }
  443. // else
  444. // {//下载中,不处理
  445. //
  446. // }
  447. // }
  448. }
  449. #pragma mark 滑动键盘下落
  450. - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
  451. {
  452. [self.msearchbar resignFirstResponder];
  453. }
  454. #pragma mark --------------------------------musicModel代理方法---------------------------------------
  455. - (void)musicDownloading:(musiceModel *)obj context:(id)context needstop:(BOOL *)needstop
  456. {
  457. NSInteger index;
  458. UITableView* tagtableview = (UITableView*)context;
  459. if( tagtableview == nil || tagtableview.isDragging || tagtableview.tracking || tagtableview.decelerating )
  460. return;
  461. if( 0 == obj.mmFileStatus )
  462. {
  463. [SVProgressHUD showErrorWithStatus:obj.mmDownloadInfo];
  464. }
  465. if( context == self.msearchtableview )
  466. index = [_searchmusic indexOfObject:obj];
  467. else if(context == self.mtableview)
  468. index = [_savedmusic indexOfObject:obj];
  469. if( index != NSNotFound /*&& !tagtableview.hidden*/ )
  470. {
  471. if( obj.mmUIRef )
  472. {
  473. musicCell* cell = (musicCell*) obj.mmUIRef;
  474. NSIndexPath* row = [tagtableview indexPathForCell:cell];
  475. if( row && row.row == index )
  476. {
  477. [tagtableview reloadData];
  478. //[tagtableview reloadRowsAtIndexPaths:@[ row ] withRowAnimation:UITableViewRowAnimationNone];
  479. }
  480. }
  481. }
  482. }
  483. - (void)didReceiveMemoryWarning
  484. {
  485. [super didReceiveMemoryWarning];
  486. }
  487. -(CustomPlayerView *)playerView
  488. {
  489. __weak __typeof(self)weakSelf = self;
  490. if (!_playerView)
  491. {
  492. _playerView = [[CustomPlayerView alloc] init];
  493. _playerView.play = ^(musiceModel * _Nonnull chosemusic) {
  494. weakSelf.playMusicId = chosemusic.id;
  495. //发送一个通知
  496. [[NSNotificationCenter defaultCenter] postNotificationName:@"playMusic" object:chosemusic];
  497. };
  498. }
  499. return _playerView;
  500. }
  501. @end