MyLessonBodyView.m 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. //
  2. // MyLessonBodyView.m
  3. // KulexiuForTeacher
  4. //
  5. // Created by 王智 on 2022/4/12.
  6. //
  7. #import "MyLessonBodyView.h"
  8. #import "KSBaseWKWebViewController.h"
  9. #import "MyLiveCourseCell.h"
  10. #import "AccompanyCourseCell.h"
  11. #import "LiveLessonModel.h"
  12. #import "AccompanyLessonModel.h"
  13. #import "KSChatConversationViewController.h"
  14. #import "MyLessonSearchView.h"
  15. #import "NewClassPopView.h"
  16. #import "KSFullDatePicker.h"
  17. #import "AccompanyDetailViewController.h"
  18. #import "KSChoosePicker.h"
  19. @interface MyLessonBodyView ()<UITableViewDelegate,UITableViewDataSource>
  20. @property (nonatomic, strong) NSDateFormatter *dateFormatter;
  21. @property (nonatomic, copy) MyLessonSearchView *sortView;
  22. @property (nonatomic, strong) NewClassPopView *popView;
  23. @property (nonatomic, strong) NSMutableArray *dataArray;
  24. @property (nonatomic, strong) StateView *promptView;
  25. @property (nonatomic, strong) UIView *promptPlaceView;
  26. @property (nonatomic, assign) BOOL networkAvaiable; // 网络是否可用
  27. @property (nonatomic, assign) BOOL isLoadMore;
  28. @property (nonatomic, assign) NSInteger rows;
  29. @property (nonatomic, assign) NSInteger pages;
  30. @property (nonatomic, assign) NSInteger secondChooseIndex;
  31. @property (nonatomic, assign) NSInteger thirdChooseIndex;
  32. @property (nonatomic, strong) NSString *classDate;
  33. @property (nonatomic, strong) NSString *status;
  34. @property (nonatomic, strong) NSString *subjectId;
  35. @property (nonatomic, strong) NSMutableArray *subjectMessageArray;
  36. @end
  37. @implementation MyLessonBodyView
  38. - (instancetype)initWithFrame:(CGRect)frame {
  39. self = [super initWithFrame:frame];
  40. if (self) {
  41. self.backgroundColor = HexRGB(0xf6f8f9);
  42. self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height) style:UITableViewStylePlain];
  43. self.tableView.backgroundColor = HexRGB(0xf6f8f9);
  44. self.tableView.showsVerticalScrollIndicator = NO;
  45. self.tableView.dataSource = self;
  46. self.tableView.delegate = self;
  47. self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  48. [self addSubview:self.tableView];
  49. UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 10)];
  50. headView.backgroundColor = HexRGB(0xf6f8f9);
  51. self.tableView.tableHeaderView = headView;
  52. UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 10)];
  53. bottomView.backgroundColor = HexRGB(0xf6f8f9);
  54. self.tableView.tableFooterView = bottomView;
  55. [self.tableView registerNib:[UINib nibWithNibName:@"AccompanyCourseCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"AccompanyCourseCell"];
  56. [self.tableView registerNib:[UINib nibWithNibName:@"MyLiveCourseCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"MyLiveCourseCell"];
  57. [self.dateFormatter setDateFormat:@"yyyy-MM"];
  58. NSDate *currentDate = [NSDate date];
  59. self.classDate = [self.dateFormatter stringFromDate:currentDate];
  60. MJWeakSelf;
  61. self.tableView.mj_header = [KSGifRefreshHeader headerWithRefreshingBlock:^{
  62. [weakSelf resetParamenter];
  63. [weakSelf requestData];
  64. }];
  65. self.tableView.mj_footer = [KSGifRefreshFooter footerWithRefreshingBlock:^{
  66. if (weakSelf.isLoadMore) {
  67. weakSelf.pages += 1;
  68. [weakSelf requestData];
  69. }
  70. else {
  71. [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
  72. }
  73. }];
  74. }
  75. return self;
  76. }
  77. - (void)endRefresh {
  78. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  79. [self.tableView.mj_header endRefreshing];
  80. [self.tableView.mj_footer endRefreshing];
  81. });
  82. }
  83. - (void)refreshAndRequestData {
  84. [self resetParamenter];
  85. [self requestData];
  86. }
  87. - (void)resetParamenter {
  88. self.isLoadMore = YES;
  89. self.pages = 1;
  90. self.rows = 10;
  91. self.dataArray = [NSMutableArray array];
  92. [self.tableView.mj_footer resetNoMoreData];
  93. [self setPromptString:@"暂无内容" imageName:@"wd_img_zwsj" inView:self.tableView];
  94. [self.tableView reloadData];
  95. }
  96. - (void)requestData {
  97. if (self.selectIndex == 0) { // 陪练课
  98. [KSNetworkingManager queryStudentPracticeCourse:KS_POST classMonth:self.classDate status:self.status subjectId:self.subjectId page:self.pages rows:self.rows success:^(NSDictionary * _Nonnull dic) {
  99. [self endRefresh];
  100. if ([dic integerValueForKey:@"code"] == 200 && [dic boolValueForKey:@"status"]) {
  101. NSArray *sourceArray = [[dic dictionaryValueForKey:@"data"] arrayValueForKey:@"rows"];
  102. for (NSDictionary *parm in sourceArray) {
  103. AccompanyLessonModel *model = [[AccompanyLessonModel alloc] initWithDictionary:parm];
  104. [self.dataArray addObject:model];
  105. }
  106. if (sourceArray.count < self.rows) {
  107. self.isLoadMore = NO;
  108. }
  109. }
  110. else {
  111. [self MBPShow:MESSAGEKEY];
  112. }
  113. [self.tableView reloadData];
  114. [self changePromptLabelStateWithArray:self.dataArray];
  115. } faliure:^(NSError * _Nonnull error) {
  116. [self endRefresh];
  117. if (self.networkAvaiable == NO) {
  118. [self setPromptString:@"暂无网络" imageName:@"no_networking" inView:self.tableView];
  119. }
  120. [self.dataArray removeAllObjects];
  121. [self.tableView reloadData];
  122. [self changePromptLabelStateWithArray:self.dataArray];
  123. }];
  124. }
  125. else { // 直播课
  126. [KSNetworkingManager queryMyLiveCourse:KS_POST classDate:self.classDate status:self.status subjectId:self.subjectId page:self.pages rows:self.rows success:^(NSDictionary * _Nonnull dic) {
  127. [self endRefresh];
  128. if ([dic integerValueForKey:@"code"] == 200 && [dic boolValueForKey:@"status"]) {
  129. NSArray *sourceArray = [[dic dictionaryValueForKey:@"data"] arrayValueForKey:@"rows"];
  130. for (NSDictionary *parm in sourceArray) {
  131. LiveLessonModel *model = [[LiveLessonModel alloc] initWithDictionary:parm];
  132. [self.dataArray addObject:model];
  133. }
  134. if (sourceArray.count < self.rows) {
  135. self.isLoadMore = NO;
  136. }
  137. }
  138. else {
  139. [self MBPShow:MESSAGEKEY];
  140. }
  141. [self.tableView reloadData];
  142. [self changePromptLabelStateWithArray:self.dataArray];
  143. } faliure:^(NSError * _Nonnull error) {
  144. [self endRefresh];
  145. if (self.networkAvaiable == NO) {
  146. [self setPromptString:@"暂无网络" imageName:@"no_networking" inView:self.tableView];
  147. }
  148. [self.dataArray removeAllObjects];
  149. [self.tableView reloadData];
  150. [self changePromptLabelStateWithArray:self.dataArray];
  151. }];
  152. }
  153. }
  154. - (void)beginRefreshImmediately {
  155. [self.tableView.mj_header beginRefreshing];
  156. }
  157. - (void)beginFirstRefresh {
  158. if (!self.isHeaderRefreshed) {
  159. [self beginRefreshImmediately];
  160. }
  161. }
  162. - (void)selectCellAtIndexPath:(NSIndexPath *)indexPath {
  163. if (self.lastSelectedIndexPath == indexPath) {
  164. return;
  165. }
  166. if (self.lastSelectedIndexPath != nil) {
  167. UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:self.lastSelectedIndexPath];
  168. [cell setSelected:NO animated:NO];
  169. }
  170. UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
  171. [cell setSelected:YES animated:NO];
  172. self.lastSelectedIndexPath = indexPath;
  173. }
  174. - (void)layoutSubviews {
  175. [super layoutSubviews];
  176. self.tableView.frame = self.bounds;
  177. CGFloat topHeight = 55.0f;
  178. self.tableView.frame = CGRectMake(0, topHeight, self.bounds.size.width, self.bounds.size.height - topHeight);
  179. if (!_sortView) {
  180. _sortView = [MyLessonSearchView shareInstance];
  181. _sortView.frame = CGRectMake(0, 0, kScreenWidth, 55);
  182. MJWeakSelf;
  183. [_sortView sortAction:^(SORT_TYPE type) {
  184. [weakSelf sortWithType:type];
  185. }];
  186. [self.dateFormatter setDateFormat:@"yyyy年MM月"];
  187. NSDate *currentDate = [NSDate date];
  188. [self.sortView.firstButton setTitle:[self.dateFormatter stringFromDate:currentDate] forState:UIControlStateNormal];
  189. [self addSubview:self.sortView];
  190. }
  191. }
  192. - (void)sortWithType:(SORT_TYPE)type {
  193. if (type == SORT_TYPE_TIME) { // time
  194. [self showPickerView];
  195. }
  196. else if (type == SORT_TYPE_STATUS) { // 状态
  197. [self.popView refreshWithSourceArray:@[@"全部",@"未开始",@"进行中",@"已结束"] preChooseIndex:_secondChooseIndex inView:self actionItem:1];
  198. }
  199. else if (type == SORT_TYPE_SUBJECT) {
  200. if (self.subjectList.count) {
  201. NSMutableArray *nameArray = [NSMutableArray array];
  202. [nameArray addObject:@"全部"];
  203. for (NSDictionary *parm in self.subjectList) {
  204. [nameArray addObject:[parm stringValueForKey:@"subjectName"]];
  205. }
  206. MJWeakSelf;
  207. KSChoosePicker *picker = [[KSChoosePicker alloc] initWithTitle:@"声部筛选" sourceData:nameArray chooseReturnWithBlock:^(NSString * _Nonnull returnValue, NSInteger chooseIndex) {
  208. if (chooseIndex == 0) {
  209. [weakSelf.sortView.thirdButton setTitle:@"全部声部" forState:UIControlStateNormal];
  210. weakSelf.subjectId = nil;
  211. }
  212. else {
  213. [weakSelf.sortView.thirdButton setTitle:returnValue forState:UIControlStateNormal];
  214. NSDictionary *parm = self.subjectList[chooseIndex-1];
  215. weakSelf.subjectId = [parm stringValueForKey:@"subjectId"];
  216. }
  217. self.sortView.thirdArrowUp = NO;
  218. [weakSelf refreshAndRequestData];
  219. } cancel:^{
  220. self.sortView.thirdArrowUp = NO;
  221. }];
  222. [picker showPicker];
  223. }
  224. else {
  225. [self MBPShow:@"无声部信息"];
  226. self.sortView.thirdArrowUp = NO;
  227. }
  228. }
  229. else {
  230. [self hiddenPopView];
  231. }
  232. }
  233. - (void)showPickerView {
  234. KSFullDatePicker *picker = [[KSFullDatePicker alloc] initWithTitle:@"" date:[NSDate date] pickMode:KSDATEPICKER_MODE_YEAR_MONTH selectDateBlock:^(NSString *date) {
  235. self.classDate = date;
  236. NSString *displayTime = [self getTimeDisplay:date];
  237. [self.sortView.firstButton setTitle:displayTime forState:UIControlStateNormal];
  238. [self resetPickerStatus];
  239. // 请求数据
  240. [self refreshAndRequestData];
  241. } cancleBlock:^{
  242. [self resetPickerStatus];
  243. }];
  244. [picker show];
  245. }
  246. - (NSString *)getTimeDisplay:(NSString *)chooseMonth {
  247. [self.dateFormatter setDateFormat:@"yyyy-MM"];
  248. NSDate *chooseDate = [self.dateFormatter dateFromString:chooseMonth];
  249. [self.dateFormatter setDateFormat:@"yyyy年MM月"];
  250. NSString *displayTime = [self.dateFormatter stringFromDate:chooseDate];
  251. return displayTime;
  252. }
  253. - (void)resetPickerStatus {
  254. self.sortView.firstArrowUp = NO;
  255. }
  256. - (NewClassPopView *)popView {
  257. if (!_popView) {
  258. MJWeakSelf;
  259. _popView = [[NewClassPopView alloc] initWithFrame:CGRectMake(0, 55, kScreenWidth, kScreenHeight - 55 - kNaviBarHeight - iPhoneXSafeBottomMargin) chooseCallback:^(NSString * _Nonnull sortStr, NSInteger chooseIndex, NSInteger item) {
  260. if (chooseIndex != 0) {
  261. // 回调
  262. [weakSelf sortWithChooseIndex:chooseIndex item:item title:sortStr];
  263. }
  264. [weakSelf hiddenPopView];
  265. }];
  266. }
  267. return _popView;
  268. }
  269. - (void)sortWithChooseIndex:(NSInteger)chooseIndex item:(NSInteger)item title:(NSString *)title {
  270. if (item == 0) { //
  271. }
  272. else if (item == 1) { // 课程状态
  273. self.secondChooseIndex = chooseIndex;
  274. self.thirdChooseIndex = 0;
  275. [self.sortView.secondButton setTitle:title forState:UIControlStateNormal];
  276. switch (chooseIndex) {
  277. case 1:
  278. {
  279. self.status = nil;
  280. [self.sortView.secondButton setTitle:@"课程状态" forState:UIControlStateNormal];
  281. }
  282. break;
  283. case 2:
  284. {
  285. self.status = @"NOT_START";
  286. }
  287. break;
  288. case 3:
  289. {
  290. self.status = @"ING";
  291. }
  292. break;
  293. case 4:
  294. {
  295. self.status = @"COMPLETE";
  296. }
  297. break;
  298. default:
  299. break;
  300. }
  301. }
  302. else if (item == 2) { // 声部筛选
  303. self.thirdChooseIndex = chooseIndex;
  304. [self.sortView.thirdButton setTitle:title forState:UIControlStateNormal];
  305. if (chooseIndex == 1) {
  306. self.subjectId = nil;
  307. [self.sortView.thirdButton setTitle:@"全部声部" forState:UIControlStateNormal];
  308. }
  309. else {
  310. NSDictionary *parm = self.subjectMessageArray[chooseIndex-1];
  311. self.subjectId = [parm stringValueForKey:@"subjectId"];
  312. }
  313. }
  314. [self resetParamenter];
  315. [self requestData];
  316. }
  317. - (void)hiddenPopView {
  318. self.sortView.firstArrowUp = NO;
  319. self.sortView.secondArrowUp = NO;
  320. self.sortView.thirdArrowUp = NO;
  321. [self.popView hiddenView];
  322. }
  323. #pragma mark - UITableViewDataSource
  324. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  325. return self.dataArray.count;
  326. }
  327. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  328. if (self.selectIndex == 0) {
  329. return 127.0f;
  330. }
  331. else {
  332. return 164.0f;
  333. }
  334. }
  335. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  336. if (self.selectIndex == 0) {
  337. AccompanyLessonModel *model = self.dataArray[indexPath.row];
  338. AccompanyCourseCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AccompanyCourseCell"];
  339. MJWeakSelf;
  340. [cell configWithSource:model actionCallback:^(ACCOMPANY_TYPE type, AccompanyLessonModel * _Nonnull source) {
  341. [weakSelf courseOperation:type sourceModel:source];
  342. }];
  343. return cell;
  344. }
  345. else {
  346. LiveLessonModel *model = self.dataArray[indexPath.row];
  347. MyLiveCourseCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyLiveCourseCell"];
  348. MJWeakSelf;
  349. [cell configCellWithSource:model callback:^(NSString *targetId) {
  350. [weakSelf chatAction:targetId];
  351. }];
  352. return cell;
  353. }
  354. }
  355. - (void)courseOperation:(ACCOMPANY_TYPE)type sourceModel:(AccompanyLessonModel *)model {
  356. switch (type) {
  357. case ACCOMPANY_TYPE_CHAT: // 聊天
  358. {
  359. KSChatConversationViewController *conversationVC = [[KSChatConversationViewController alloc] init];
  360. conversationVC.targetId = model.userId;
  361. conversationVC.conversationType = ConversationType_PRIVATE;
  362. [self.naviController pushViewController:conversationVC animated:YES];
  363. }
  364. break;
  365. case ACCOMPANY_DETAIL: // 陪练课详情
  366. {
  367. [self showAccompanyDetail:model];
  368. }
  369. break;
  370. default:
  371. break;
  372. }
  373. }
  374. - (void)showAccompanyDetail:(AccompanyLessonModel *)model {
  375. AccompanyDetailViewController *detailVC = [[AccompanyDetailViewController alloc] init];
  376. detailVC.courseId = model.courseId;
  377. detailVC.courseGroupId = model.courseGoupId;
  378. detailVC.teacherId = model.userId;
  379. [self.naviController pushViewController:detailVC animated:YES];
  380. }
  381. - (void)chatAction:(NSString *)targetId {
  382. KSChatConversationViewController *conversationVC = [[KSChatConversationViewController alloc] init];
  383. conversationVC.targetId = targetId;
  384. conversationVC.conversationType = ConversationType_GROUP;
  385. [self.naviController pushViewController:conversationVC animated:YES];
  386. }
  387. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  388. if (self.selectIndex == 0) { // 进入陪练课详情
  389. AccompanyLessonModel *model = self.dataArray[indexPath.row];
  390. [self showAccompanyDetail:model];
  391. }
  392. else { // 直播课程组详情
  393. LiveLessonModel *model = self.dataArray[indexPath.row];
  394. KSBaseWKWebViewController *ctrl = [[KSBaseWKWebViewController alloc] init];
  395. NSString *url = [NSString stringWithFormat:@"%@/#/liveDetail?groupId=%@&classId=%@", WEBHOST, model.courseGoupId,model.courseId];
  396. ctrl.url = url;
  397. [self.naviController pushViewController:ctrl animated:YES];
  398. }
  399. }
  400. /**
  401. 设置没有数据时的显示
  402. @param promptString 提示语
  403. @param imgName 图片名称
  404. @param view 显示在什么地方
  405. */
  406. - (void)setPromptString:(NSString *)promptString imageName:(NSString *)imgName inView:(UIView *)view {
  407. if (self.promptView != nil) {
  408. [self.promptView removeFromSuperview];
  409. }
  410. else {
  411. self.promptView = [[StateView alloc]init];
  412. self.promptView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight - 300);
  413. }
  414. _promptPlaceView = view;
  415. //当请求不到数据时 ,自定义提示view 将会出现;
  416. self.promptView.imageName = imgName;
  417. self.promptView.alpha = 0.0f;
  418. [self.promptView setText:promptString];
  419. [view addSubview:self.promptView];
  420. }
  421. // 结束刷新后调用方法
  422. - (void)changePromptLabelStateWithArray:(NSMutableArray *)array {
  423. NSInteger count;
  424. if (array.count) {
  425. count = array.count;
  426. } else {
  427. count = 0;
  428. }
  429. [UIView animateWithDuration:0.1 animations:^{
  430. [[self promptView] setAlpha:count ? 0.0f :1.0f ] ;
  431. }] ;
  432. }
  433. - (BOOL)networkAvaiable {
  434. return [self checkNetworkAvaiable];
  435. }
  436. - (BOOL)checkNetworkAvaiable {
  437. BOOL isExistenceNetwork = YES;
  438. Reachability *reach = [Reachability reachabilityWithHostName:@"www.apple.com"];
  439. switch ([reach currentReachabilityStatus]) {
  440. case NotReachable:
  441. isExistenceNetwork = NO;
  442. //NSLog(@"notReachable");
  443. break;
  444. case ReachableViaWiFi:
  445. isExistenceNetwork = YES;
  446. //NSLog(@"WIFI");
  447. break;
  448. case ReachableViaWWAN:
  449. isExistenceNetwork = YES;
  450. //NSLog(@"3G");
  451. break;
  452. }
  453. return isExistenceNetwork;
  454. }
  455. #pragma mark ----- lazying
  456. - (NSDateFormatter *)dateFormatter {
  457. if (!_dateFormatter) {
  458. _dateFormatter = [NSObject getDateformatter];
  459. }
  460. return _dateFormatter;
  461. }
  462. - (void)setSubjectList:(NSMutableArray *)subjectList {
  463. _subjectList = subjectList;
  464. self.thirdChooseIndex = 0;
  465. }
  466. /*
  467. // Only override drawRect: if you perform custom drawing.
  468. // An empty implementation adversely affects performance during animation.
  469. - (void)drawRect:(CGRect)rect {
  470. // Drawing code
  471. }
  472. */
  473. @end