MyMusicBodyView.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. //
  2. // MyMusicBodyView.m
  3. // KulexiuForTeacher
  4. //
  5. // Created by Kyle on 2022/3/29.
  6. //
  7. #import "MyMusicBodyView.h"
  8. #import "MusicMessageModel.h"
  9. #import "MusicMessageCell.h"
  10. #import "MusicUploadView.h"
  11. #import "KSBaseWKWebViewController.h"
  12. #import "AuthDisplayView.h"
  13. #import "KSAccompanyWebViewController.h"
  14. #import "KSPublicAlertView.h"
  15. @interface MyMusicBodyView ()<UITableViewDelegate,UITableViewDataSource>
  16. @property (nonatomic, strong) NSMutableArray *dataArray;
  17. @property (nonatomic, strong) StateView *promptView;
  18. @property (nonatomic, strong) UIView *promptPlaceView;
  19. @property (nonatomic, assign) BOOL networkAvaiable; // 网络是否可用
  20. @property (nonatomic, assign) BOOL isLoadMore;
  21. @property (nonatomic, assign) NSInteger rows;
  22. @property (nonatomic, assign) NSInteger pages;
  23. @property (nonatomic, strong) NSString *audioStatus; // 审核状态(0:待审核;1:通过;2:未通过)
  24. @property (nonatomic, strong) MusicUploadView *uploadView;
  25. @property (nonatomic, assign) BOOL authStatus;
  26. @property (nonatomic, strong) AuthDisplayView *authView;
  27. @property (nonatomic, strong) KSPublicAlertView *alertView;
  28. @end
  29. @implementation MyMusicBodyView
  30. - (instancetype)initWithFrame:(CGRect)frame {
  31. self = [super initWithFrame:frame];
  32. if (self) {
  33. self.backgroundColor = HexRGB(0xf6f8f9);
  34. self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height) style:UITableViewStylePlain];
  35. self.tableView.backgroundColor = HexRGB(0xf6f8f9);
  36. self.tableView.showsVerticalScrollIndicator = NO;
  37. self.tableView.dataSource = self;
  38. self.tableView.delegate = self;
  39. self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  40. self.tableView.rowHeight = 110;
  41. [self addSubview:self.tableView];
  42. UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 10)];
  43. bottomView.backgroundColor = HexRGB(0xf6f8f9);
  44. self.tableView.tableFooterView = bottomView;
  45. [self.tableView registerNib:[UINib nibWithNibName:@"MusicMessageCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"MusicMessageCell"];
  46. MJWeakSelf;
  47. self.tableView.mj_header = [KSGifRefreshHeader headerWithRefreshingBlock:^{
  48. [weakSelf resetParamenter];
  49. [weakSelf requestData];
  50. }];
  51. self.tableView.mj_footer = [KSGifRefreshFooter footerWithRefreshingBlock:^{
  52. if (weakSelf.isLoadMore) {
  53. weakSelf.pages += 1;
  54. [weakSelf requestData];
  55. }
  56. else {
  57. [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
  58. }
  59. }];
  60. }
  61. return self;
  62. }
  63. - (void)endRefresh {
  64. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  65. [self.tableView.mj_header endRefreshing];
  66. [self.tableView.mj_footer endRefreshing];
  67. });
  68. }
  69. - (void)refreshAndRequestData {
  70. [self resetParamenter];
  71. [self requestData];
  72. }
  73. - (void)resetParamenter {
  74. self.isLoadMore = YES;
  75. self.pages = 1;
  76. self.rows = 10;
  77. if (self.selectIndex == 1) { // 已上架
  78. self.audioStatus = @"PASS";
  79. }
  80. else if (self.selectIndex == 2) { // 审核中
  81. self.audioStatus = @"DOING";
  82. }
  83. else if (self.selectIndex == 3) { // 审核失败
  84. self.audioStatus = @"UNPASS";
  85. }
  86. else if (self.selectIndex == 4) { // 已下架
  87. self.audioStatus = @"OUT_SALE";
  88. }
  89. self.dataArray = [NSMutableArray array];
  90. [self.tableView.mj_footer resetNoMoreData];
  91. [self setPromptString:@"暂无内容" imageName:@"wd_img_zwsj" inView:self.tableView];
  92. [self.tableView reloadData];
  93. }
  94. - (void)requestData {
  95. if (self.selectIndex == 0) {
  96. [KSNetworkingManager myMusicListRequest:KS_POST page:self.pages rows:self.rows search:self.searchKey subjectIds:self.subjectIds success:^(NSDictionary * _Nonnull dic) {
  97. [self endRefresh];
  98. if ([dic integerValueForKey:@"code"] == 200 && [dic boolValueForKey:@"status"]) {
  99. NSArray *sourceArray = [[dic dictionaryValueForKey:@"data"] arrayValueForKey:@"rows"];
  100. for (NSDictionary *parm in sourceArray) {
  101. MusicMessageModel *model = [[MusicMessageModel alloc] initWithDictionary:parm];
  102. [self.dataArray addObject:model];
  103. }
  104. if (sourceArray.count < self.rows) {
  105. self.isLoadMore = NO;
  106. }
  107. }
  108. else {
  109. [self MBPShow:MESSAGEKEY];
  110. }
  111. [self.tableView reloadData];
  112. [self changePromptLabelStateWithArray:self.dataArray];
  113. } faliure:^(NSError * _Nonnull error) {
  114. [self endRefresh];
  115. if (self.networkAvaiable == NO) {
  116. [self setPromptString:@"暂无网络" imageName:@"no_networking" inView:self.tableView];
  117. }
  118. [self.dataArray removeAllObjects];
  119. [self.tableView reloadData];
  120. [self changePromptLabelStateWithArray:self.dataArray];
  121. }];
  122. }
  123. else {
  124. [KSNetworkingManager musicListRequest:KS_POST auditStatus:self.audioStatus page:self.pages rows:self.rows search:self.searchKey subjectIds:self.subjectIds success:^(NSDictionary * _Nonnull dic) {
  125. [self endRefresh];
  126. if ([dic integerValueForKey:@"code"] == 200 && [dic boolValueForKey:@"status"]) {
  127. NSArray *sourceArray = [[dic dictionaryValueForKey:@"data"] arrayValueForKey:@"rows"];
  128. for (NSDictionary *parm in sourceArray) {
  129. MusicMessageModel *model = [[MusicMessageModel alloc] initWithDictionary:parm];
  130. [self.dataArray addObject:model];
  131. }
  132. if (sourceArray.count < self.rows) {
  133. self.isLoadMore = NO;
  134. }
  135. }
  136. else {
  137. [self MBPShow:MESSAGEKEY];
  138. }
  139. [self.tableView reloadData];
  140. [self changePromptLabelStateWithArray:self.dataArray];
  141. } faliure:^(NSError * _Nonnull error) {
  142. [self endRefresh];
  143. if (self.networkAvaiable == NO) {
  144. [self setPromptString:@"暂无网络" imageName:@"no_networking" inView:self.tableView];
  145. }
  146. [self.dataArray removeAllObjects];
  147. [self.tableView reloadData];
  148. [self changePromptLabelStateWithArray:self.dataArray];
  149. }];
  150. }
  151. }
  152. - (void)beginRefreshImmediately {
  153. [self.tableView.mj_header beginRefreshing];
  154. }
  155. - (void)selectCellAtIndexPath:(NSIndexPath *)indexPath {
  156. if (self.lastSelectedIndexPath == indexPath) {
  157. return;
  158. }
  159. if (self.lastSelectedIndexPath != nil) {
  160. UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:self.lastSelectedIndexPath];
  161. [cell setSelected:NO animated:NO];
  162. }
  163. UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
  164. [cell setSelected:YES animated:NO];
  165. self.lastSelectedIndexPath = indexPath;
  166. }
  167. - (void)layoutSubviews {
  168. [super layoutSubviews];
  169. CGFloat topHeight = self.selectIndex == 0 ? 10 : 60.0f;
  170. self.tableView.frame = CGRectMake(0, topHeight, self.bounds.size.width, self.bounds.size.height - topHeight);
  171. if (!_uploadView && self.selectIndex != 0) {
  172. self.uploadView = [MusicUploadView shareInstance];
  173. self.uploadView.frame = CGRectMake(0, 0, kScreenWidth, topHeight);
  174. [self addSubview:self.uploadView];
  175. MJWeakSelf;
  176. [self.uploadView uploadAction:^(BOOL hasAuth) {
  177. [weakSelf uploadSong];
  178. }];
  179. if (self.authStatus == NO) {
  180. [self showAuthView];
  181. }
  182. else {
  183. [self hideAuthView];
  184. }
  185. }
  186. }
  187. - (void)uploadSong {
  188. NSString *url = [NSString stringWithFormat:@"%@%@", WEBHOST, @"/#/music-upload"];
  189. KSBaseWKWebViewController *webCtrl = [[KSBaseWKWebViewController alloc] init];
  190. webCtrl.url = url;
  191. [self.naviController pushViewController:webCtrl animated:YES];
  192. }
  193. - (void)beginFirstRefresh {
  194. if (!self.isHeaderRefreshed) {
  195. [self beginRefreshImmediately];
  196. }
  197. }
  198. #pragma mark - UITableViewDataSource
  199. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  200. return self.dataArray.count;
  201. }
  202. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  203. MusicMessageModel *model = self.dataArray[indexPath.row];
  204. MusicMessageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MusicMessageCell"];
  205. BOOL needOffButton = self.selectIndex == 1 ? YES : NO;
  206. MJWeakSelf;
  207. [cell configWithMessage:model needOffButton:needOffButton callback:^(MusicMessageModel * _Nonnull songMessage) {
  208. [weakSelf showAlert:songMessage];
  209. }];
  210. return cell;
  211. }
  212. - (void)showAlert:(MusicMessageModel *)songMessage {
  213. MJWeakSelf;
  214. self.alertView = [KSPublicAlertView shareInstanceWithTitle:@"提示" descMessage:@"确认下架该曲谱吗?" leftTitle:@"取消" rightTitle:@"确定" cancelAction:^{
  215. } sureAction:^{
  216. [weakSelf musicOffAction:songMessage];
  217. }];
  218. }
  219. - (void)musicOffAction:(MusicMessageModel *)songMessage {
  220. [KSNetworkingManager musicOffRequest:KS_POST musicId:[NSString stringWithFormat:@"%.0f",songMessage.internalBaseClassIdentifier] success:^(NSDictionary * _Nonnull dic) {
  221. if ([dic integerValueForKey:@"code"] == 200 && [dic boolValueForKey:@"status"]) {
  222. MJWeakSelf;
  223. [self KSShowMsg:@"下架成功" promptCompletion:^{
  224. [weakSelf refreshAndRequestData];
  225. }];
  226. }
  227. else {
  228. [self MBPShow:MESSAGEKEY];
  229. }
  230. } faliure:^(NSError * _Nonnull error) {
  231. }];
  232. }
  233. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  234. if (self.selectIndex == 3 || self.selectIndex == 4) {
  235. MusicMessageModel *model = self.dataArray[indexPath.row];
  236. KSBaseWKWebViewController *webCtrl = [[KSBaseWKWebViewController alloc] init];
  237. webCtrl.url = [NSString stringWithFormat:@"%@/#/music-upload/%.0f/edit", WEBHOST, model.internalBaseClassIdentifier];
  238. [self.naviController pushViewController:webCtrl animated:YES];
  239. }
  240. else { // 播放曲谱
  241. MusicMessageModel *model = self.dataArray[indexPath.row];
  242. KSAccompanyWebViewController *detailCtrl = [[KSAccompanyWebViewController alloc] init];
  243. detailCtrl.url = [NSString stringWithFormat:@"%@/accompany?id=%.0f&client=teacher",hostURL, model.internalBaseClassIdentifier];
  244. detailCtrl.hiddenNavBar = YES;
  245. detailCtrl.parmDic = @{@"isOpenLight" : @(YES), @"orientation" : @(0),@"isHideTitle" : @(YES)};
  246. [self.naviController pushViewController:detailCtrl animated:YES];
  247. }
  248. }
  249. /**
  250. 设置没有数据时的显示
  251. @param promptString 提示语
  252. @param imgName 图片名称
  253. @param view 显示在什么地方
  254. */
  255. - (void)setPromptString:(NSString *)promptString imageName:(NSString *)imgName inView:(UIView *)view {
  256. if (self.promptView != nil) {
  257. [self.promptView removeFromSuperview];
  258. }
  259. else {
  260. self.promptView = [[StateView alloc]init];
  261. self.promptView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight - 300);
  262. }
  263. _promptPlaceView = view;
  264. //当请求不到数据时 ,自定义提示view 将会出现;
  265. self.promptView.imageName = imgName;
  266. self.promptView.alpha = 0.0f;
  267. [self.promptView setText:promptString];
  268. [view addSubview:self.promptView];
  269. }
  270. // 结束刷新后调用方法
  271. - (void)changePromptLabelStateWithArray:(NSMutableArray *)array {
  272. NSInteger count;
  273. if (array.count) {
  274. count = array.count;
  275. } else {
  276. count = 0;
  277. }
  278. [UIView animateWithDuration:0.1 animations:^{
  279. [[self promptView] setAlpha:count ? 0.0f :1.0f ] ;
  280. }] ;
  281. }
  282. - (BOOL)networkAvaiable {
  283. return [self checkNetworkAvaiable];
  284. }
  285. - (BOOL)checkNetworkAvaiable {
  286. BOOL isExistenceNetwork = YES;
  287. Reachability *reach = [Reachability reachabilityWithHostName:@"www.apple.com"];
  288. switch ([reach currentReachabilityStatus]) {
  289. case NotReachable:
  290. isExistenceNetwork = NO;
  291. //NSLog(@"notReachable");
  292. break;
  293. case ReachableViaWiFi:
  294. isExistenceNetwork = YES;
  295. //NSLog(@"WIFI");
  296. break;
  297. case ReachableViaWWAN:
  298. isExistenceNetwork = YES;
  299. //NSLog(@"3G");
  300. break;
  301. }
  302. return isExistenceNetwork;
  303. }
  304. #pragma mark ---- lazying
  305. - (NSMutableArray *)dataArray {
  306. if (!_dataArray) {
  307. _dataArray = [NSMutableArray array];
  308. }
  309. return _dataArray;
  310. }
  311. - (void)setTeaherStatus:(NSString *)teaherStatus {
  312. _teaherStatus = teaherStatus;
  313. if ([teaherStatus isEqualToString:@"PASS"]) {
  314. self.authStatus = YES;
  315. }
  316. else {
  317. self.authStatus = NO;
  318. }
  319. if (self.authStatus == NO && self.selectIndex != 0) {
  320. [self showAuthView];
  321. }
  322. else {
  323. [self hideAuthView];
  324. }
  325. }
  326. - (NSString *)getAuthDisplayMessage {
  327. if ([self.teaherStatus isEqualToString:@"DOING"]) { // 审核中
  328. return @"您已提交认证申请,请耐心等待审核结果~";
  329. }
  330. else {
  331. return @"您还没有完成达人认证,认证后才可上传曲谱哦~";
  332. }
  333. }
  334. - (void)showAuthView {
  335. [self configAuthDisplay];
  336. if ([self.subviews containsObject:self.authView]) {
  337. [self bringSubviewToFront:self.authView];
  338. }
  339. else {
  340. [self addSubview:self.authView];
  341. [self.authView mas_makeConstraints:^(MASConstraintMaker *make) {
  342. make.left.top.bottom.right.mas_equalTo(self);
  343. }];
  344. }
  345. }
  346. - (void)hideAuthView {
  347. if ([self.subviews containsObject:self.authView]) {
  348. [self.authView removeFromSuperview];
  349. self.authView = nil;
  350. }
  351. }
  352. - (void)configAuthDisplay {
  353. [self.authView configDisplayMessage:[self getAuthDisplayMessage]];
  354. if ([self.teaherStatus isEqualToString:@"DOING"]) {
  355. self.authView.sureButton.userInteractionEnabled = NO;
  356. self.authView.sureButton.hidden = YES;
  357. }
  358. else {
  359. self.authView.sureButton.userInteractionEnabled = YES;
  360. self.authView.sureButton.hidden = NO;
  361. }
  362. }
  363. - (void)authAction {
  364. KSBaseWKWebViewController *webCtrl = [[KSBaseWKWebViewController alloc] init];
  365. webCtrl.url = [NSString stringWithFormat:@"%@%@", WEBHOST, @"/#/teacherCert"];
  366. [self.naviController pushViewController:webCtrl animated:YES];
  367. }
  368. - (AuthDisplayView *)authView {
  369. if (!_authView) {
  370. _authView = [AuthDisplayView shareInstance];
  371. [_authView.imageView setImage:[UIImage imageNamed:[self getAuthDisplayImage]]];
  372. MJWeakSelf;
  373. [_authView sureCallback:^{
  374. [weakSelf authAction];
  375. }];
  376. }
  377. return _authView;
  378. }
  379. - (NSString *)getAuthDisplayImage {
  380. return @"authMuscian";
  381. }
  382. /*
  383. // Only override drawRect: if you perform custom drawing.
  384. // An empty implementation adversely affects performance during animation.
  385. - (void)drawRect:(CGRect)rect {
  386. // Drawing code
  387. }
  388. */
  389. @end