MyMusicBodyView.m 14 KB

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