MyLessonBodyView.m 17 KB

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