MyLiveCourseBodyView.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. //
  2. // MyLiveCourseBodyView.m
  3. // KulexiuForTeacher
  4. //
  5. // Created by 王智 on 2022/4/6.
  6. //
  7. #import "MyLiveCourseBodyView.h"
  8. #import "MineLiveCourseGroupCell.h"
  9. #import "LiveCourseModel.h"
  10. #import "KSBaseWKWebViewController.h"
  11. #import "AuthDisplayView.h"
  12. @interface MyLiveCourseBodyView ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
  13. @property (nonatomic, strong) NSMutableArray *dataArray;
  14. @property (nonatomic, strong) KSButtonStatusView *promptView;
  15. @property (nonatomic, strong) UIView *promptPlaceView;
  16. @property (nonatomic, assign) BOOL networkAvaiable; // 网络是否可用
  17. @property (nonatomic, assign) BOOL isLoadMore;
  18. @property (nonatomic, assign) NSInteger rows;
  19. @property (nonatomic, assign) NSInteger pages;
  20. @property (nonatomic, strong) NSString *groupStatus;
  21. @property (nonatomic, strong) AuthDisplayView *authView;
  22. @property (nonatomic, assign) BOOL teacherAuthPass; // 是否通过老师审核
  23. @end
  24. @implementation MyLiveCourseBodyView
  25. - (instancetype)initWithFrame:(CGRect)frame {
  26. self = [super initWithFrame:frame];
  27. if (self) {
  28. self.backgroundColor = HexRGB(0xf8f9fc);
  29. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  30. layout.sectionInset = UIEdgeInsetsMake(12, 14, 12, 14);
  31. self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height) collectionViewLayout:layout];
  32. self.collectionView.backgroundColor = HexRGB(0xf8f9fc);
  33. self.collectionView.delegate = self;
  34. self.collectionView.dataSource = self;
  35. self.collectionView.showsVerticalScrollIndicator = NO;
  36. self.collectionView.showsHorizontalScrollIndicator = NO;
  37. [self.collectionView registerNib:[UINib nibWithNibName:@"MineLiveCourseGroupCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"MineLiveCourseGroupCell"];
  38. [self addSubview:self.collectionView];
  39. self.collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  40. MJWeakSelf;
  41. self.collectionView.mj_header = [KSGifRefreshHeader headerWithRefreshingBlock:^{
  42. [weakSelf resetParamenter];
  43. [weakSelf requestData];
  44. }];
  45. self.collectionView.mj_footer = [KSGifRefreshFooter footerWithRefreshingBlock:^{
  46. if (weakSelf.isLoadMore) {
  47. weakSelf.pages += 1;
  48. [weakSelf requestData];
  49. }
  50. else {
  51. [weakSelf.collectionView.mj_footer endRefreshingWithNoMoreData];
  52. }
  53. }];
  54. }
  55. return self;
  56. }
  57. - (void)configRequestStatus {
  58. if (self.selectIndex == 0) {
  59. self.groupStatus = @"ING";
  60. }
  61. else if (self.selectIndex == 1) {
  62. self.groupStatus = @"NOT_SALE";
  63. }
  64. else if (self.selectIndex == 2) {
  65. self.groupStatus = @"APPLY";
  66. }
  67. else if (self.selectIndex == 3) {
  68. self.groupStatus = @"COMPLETE";
  69. }
  70. else if (self.selectIndex == 4) {
  71. self.groupStatus = @"CANCEL";
  72. }
  73. else {
  74. self.groupStatus = @"OUT_SALE";
  75. }
  76. }
  77. - (void)endRefresh {
  78. @weakObj(self);
  79. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  80. @strongObj(self);
  81. [self.collectionView.mj_header endRefreshing];
  82. [self.collectionView.mj_footer endRefreshing];
  83. });
  84. }
  85. - (void)refreshAndRequestData {
  86. [self resetParamenter];
  87. [self requestData];
  88. }
  89. - (void)resetParamenter {
  90. self.isLoadMore = YES;
  91. self.pages = 1;
  92. self.rows = 10;
  93. [self configRequestStatus];
  94. self.dataArray = [NSMutableArray array];
  95. [self.collectionView.mj_footer resetNoMoreData];
  96. [self setPromptString:[self getEmptyMessage] imageName:@"authTeacher_Live" buttonTitle:@"立即创建" inView:self.collectionView];
  97. [self.collectionView reloadData];
  98. }
  99. - (NSString *)getEmptyMessage {
  100. if (self.selectIndex == 0) {
  101. return @"暂无进行中直播课程";
  102. }
  103. else if (self.selectIndex == 1) {
  104. return @"暂无未上架直播课程";
  105. }
  106. else if (self.selectIndex == 2) {
  107. return @"暂无销售中直播课程";
  108. }
  109. else if (self.selectIndex == 3) {
  110. return @"暂无已完成直播课程";
  111. }
  112. else if (self.selectIndex == 4) {
  113. return @"暂无已取消直播课程";
  114. }
  115. else {
  116. return @"暂无已下架直播课程";
  117. }
  118. }
  119. - (void)requestData {
  120. [KSNetworkingManager LiveCourseGroupRequest:KS_POST groupStatus:self.groupStatus page:self.pages rows:self.rows success:^(NSDictionary * _Nonnull dic) {
  121. [self endRefresh];
  122. if ([dic ks_integerValueForKey:@"code"] == 200 && [dic ks_boolValueForKey:@"status"]) {
  123. NSArray *sourceArray = [[dic ks_dictionaryValueForKey:@"data"] ks_arrayValueForKey:@"rows"];
  124. for (NSDictionary *parm in sourceArray) {
  125. LiveCourseModel *model = [[LiveCourseModel alloc] initWithDictionary:parm];
  126. [self.dataArray addObject:model];
  127. }
  128. if (sourceArray.count < self.rows) {
  129. self.isLoadMore = NO;
  130. }
  131. }
  132. else {
  133. [LOADING_MANAGER MBShowAUTOHidingInWindow:MESSAGEKEY];
  134. }
  135. [self.collectionView reloadData];
  136. [self changePromptLabelStateWithArray:self.dataArray];
  137. } faliure:^(NSError * _Nonnull error) {
  138. [self endRefresh];
  139. if (self.networkAvaiable == NO) {
  140. [self setPromptString:@"暂无网络" imageName:@"no_networking" buttonTitle:@"" inView:self.collectionView];
  141. }
  142. [self.dataArray removeAllObjects];
  143. [self.collectionView reloadData];
  144. [self changePromptLabelStateWithArray:self.dataArray];
  145. }];
  146. }
  147. - (void)beginRefreshImmediately {
  148. [self.collectionView.mj_header beginRefreshing];
  149. }
  150. - (void)beginFirstRefresh {
  151. if (!self.isHeaderRefreshed) {
  152. [self beginRefreshImmediately];
  153. }
  154. }
  155. - (void)selectCellAtIndexPath:(NSIndexPath *)indexPath {
  156. if (self.lastSelectedIndexPath == indexPath) {
  157. return;
  158. }
  159. if (self.lastSelectedIndexPath != nil) {
  160. UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:self.lastSelectedIndexPath];
  161. [cell setSelected:NO];
  162. }
  163. UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:indexPath];
  164. [cell setSelected:YES];
  165. self.lastSelectedIndexPath = indexPath;
  166. }
  167. - (void)layoutSubviews {
  168. [super layoutSubviews];
  169. [self.collectionView mas_remakeConstraints:^(MASConstraintMaker *make) {
  170. make.left.right.top.bottom.mas_equalTo(self);
  171. }];
  172. if (self.teacherAuthPass == NO) {
  173. [self showAuthView];
  174. }
  175. else {
  176. [self hideAuthView];
  177. }
  178. }
  179. #pragma mark ----- collection view
  180. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
  181. return 1;
  182. }
  183. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  184. return self.dataArray.count;
  185. }
  186. - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  187. MineLiveCourseGroupCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MineLiveCourseGroupCell" forIndexPath:indexPath];
  188. LiveCourseModel *model = [self.dataArray objectAtIndex:indexPath.row];
  189. COURSERSTATUS status = [self getCourseStatus:self.selectIndex];
  190. [cell configCellWithSource:model groupStatus:status hideStatusView:NO];
  191. return cell;
  192. }
  193. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  194. // 直播课程组详情
  195. LiveCourseModel *model = self.dataArray[indexPath.item];
  196. if (self.selectIndex == 5) {
  197. [self editLiveCourseWithGroupId:[NSString stringWithFormat:@"%.0f",model.courseGroupId]];
  198. }
  199. else {
  200. [self displayLiveCourseDetailWithGroupId:[NSString stringWithFormat:@"%.0f",model.courseGroupId]];
  201. }
  202. }
  203. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  204. CGFloat width = KPortraitWidth - 28;
  205. if (IS_IPAD) {
  206. width = (KPortraitWidth - 28 - 12 * 2 - 12) / 2.0f;
  207. }
  208. CGFloat height = (width - 12 * 2) / 16 * 9 + 143;
  209. return CGSizeMake(width, height);
  210. }
  211. - (COURSERSTATUS)getCourseStatus:(NSInteger)index {
  212. COURSERSTATUS status;
  213. if (index == 0) {
  214. status = COURSERSTATUS_ING;
  215. }
  216. else if (index == 1) {
  217. status = COURSERSTATUS_NOTSALE;
  218. }
  219. else if (index == 2) {
  220. status = COURSERSTATUS_APPLY;
  221. }
  222. else if (index == 3) {
  223. status = COURSERSTATUS_COMPLETE;
  224. }
  225. else if (index == 4) {
  226. status = COURSERSTATUS_CANCLE;
  227. }
  228. else {
  229. status = COURSERSTATUS_OUTSALE;
  230. }
  231. return status;
  232. }
  233. - (void)editLiveCourseWithGroupId:(NSString *)groupId {
  234. KSBaseWKWebViewController *ctrl = [[KSBaseWKWebViewController alloc] init];
  235. NSString *url = [NSString stringWithFormat:@"%@%@%@", WEBHOST, @"/#/liveCreate?groupId=",groupId];
  236. ctrl.url = url;
  237. [self.naviController pushViewController:ctrl animated:YES];
  238. }
  239. - (void)displayLiveCourseDetailWithGroupId:(NSString *)groupId {
  240. KSBaseWKWebViewController *ctrl = [[KSBaseWKWebViewController alloc] init];
  241. NSString *url = [NSString stringWithFormat:@"%@%@%@", WEBHOST, @"/#/liveDetail?groupId=",groupId];
  242. ctrl.url = url;
  243. [self.naviController pushViewController:ctrl animated:YES];
  244. }
  245. /**
  246. 设置没有数据时的显示
  247. @param promptString 提示语
  248. @param imgName 图片名称
  249. @param view 显示在什么地方
  250. */
  251. - (void)setPromptString:(NSString *)promptString imageName:(NSString *)imgName buttonTitle:(NSString *)buttonTitle inView:(UIView *)view {
  252. if (self.promptView != nil) {
  253. [self.promptView removeFromSuperview];
  254. }
  255. else {
  256. self.promptView = [[KSButtonStatusView alloc] init];
  257. }
  258. _promptPlaceView = view;
  259. //当请求不到数据时 ,自定义提示view 将会出现;
  260. self.promptView.imageName = imgName;
  261. self.promptView.alpha = 0.0f;
  262. [self.promptView setText:promptString];
  263. [self.promptView setButtonTitle:buttonTitle];
  264. MJWeakSelf;
  265. [self.promptView buttonClickCallback:^{
  266. [weakSelf emptyButtonAction];
  267. }];
  268. [view addSubview:self.promptView];
  269. [self.promptView mas_makeConstraints:^(MASConstraintMaker *make) {
  270. make.left.right.mas_equalTo(self);
  271. make.top.mas_equalTo(self.collectionView);
  272. make.height.mas_equalTo(self);
  273. }];
  274. }
  275. - (void)emptyButtonAction {
  276. KSBaseWKWebViewController *webCtrl = [[KSBaseWKWebViewController alloc] init];
  277. webCtrl.url = [NSString stringWithFormat:@"%@%@", WEBHOST, @"/#/liveCreate"];
  278. [self.naviController pushViewController:webCtrl animated:YES];
  279. }
  280. // 结束刷新后调用方法
  281. - (void)changePromptLabelStateWithArray:(NSMutableArray *)array {
  282. NSInteger count;
  283. if (array.count) {
  284. count = array.count;
  285. } else {
  286. count = 0;
  287. }
  288. [UIView animateWithDuration:0.1 animations:^{
  289. [[self promptView] setAlpha:count ? 0.0f :1.0f ] ;
  290. }] ;
  291. }
  292. - (BOOL)networkAvaiable {
  293. return [self checkNetworkAvaiable];
  294. }
  295. - (BOOL)checkNetworkAvaiable {
  296. BOOL isExistenceNetwork = YES;
  297. Reachability *reach = [Reachability reachabilityWithHostName:@"www.apple.com"];
  298. switch ([reach currentReachabilityStatus]) {
  299. case NotReachable:
  300. isExistenceNetwork = NO;
  301. //NSLog(@"notReachable");
  302. break;
  303. case ReachableViaWiFi:
  304. isExistenceNetwork = YES;
  305. //NSLog(@"WIFI");
  306. break;
  307. case ReachableViaWWAN:
  308. isExistenceNetwork = YES;
  309. //NSLog(@"3G");
  310. break;
  311. }
  312. return isExistenceNetwork;
  313. }
  314. #pragma mark ---- lazying
  315. - (NSMutableArray *)dataArray {
  316. if (!_dataArray) {
  317. _dataArray = [NSMutableArray array];
  318. }
  319. return _dataArray;
  320. }
  321. - (void)setTeaherStatus:(NSString *)teaherStatus {
  322. _teaherStatus = teaherStatus;
  323. if ([teaherStatus isEqualToString:@"UNPAALY"]) { // 未申请
  324. self.teacherAuthPass = NO;
  325. }
  326. else if ([teaherStatus isEqualToString:@"DOING"]) { // 审核中
  327. self.teacherAuthPass = NO;
  328. }
  329. else if ([teaherStatus isEqualToString:@"UNPASS"]) { // 不通过
  330. self.teacherAuthPass = NO;
  331. }
  332. else {
  333. self.teacherAuthPass = YES;
  334. }
  335. if (self.liveFlag == NO) { // 无直播权限直接改成未通过
  336. self.teacherAuthPass = NO;
  337. }
  338. if (self.teacherAuthPass == NO) {
  339. [self showAuthView];
  340. }
  341. else {
  342. [self hideAuthView];
  343. }
  344. }
  345. - (void)configAuthDisplay {
  346. [self.authView configDisplayMessage:[self getAuthDisplayMessage]];
  347. if ([self.teaherStatus isEqualToString:@"DOING"]) {
  348. self.authView.sureButton.userInteractionEnabled = NO;
  349. self.authView.sureButton.hidden = YES;
  350. }
  351. else {
  352. if ([self.teaherStatus isEqual:@"PASS"] && self.liveFlag == NO) {
  353. [self.authView.sureButton setTitle:@"立即开通" forState:UIControlStateNormal];
  354. }
  355. else {
  356. [self.authView.sureButton setTitle:@"去认证" forState:UIControlStateNormal];
  357. }
  358. self.authView.sureButton.userInteractionEnabled = YES;
  359. self.authView.sureButton.hidden = NO;
  360. }
  361. }
  362. - (void)showAuthView {
  363. [self configAuthDisplay];
  364. if ([self.subviews containsObject:self.authView]) {
  365. [self bringSubviewToFront:self.authView];
  366. }
  367. else {
  368. [self addSubview:self.authView];
  369. [self.authView mas_makeConstraints:^(MASConstraintMaker *make) {
  370. make.left.top.bottom.right.mas_equalTo(self);
  371. }];
  372. }
  373. }
  374. - (void)hideAuthView {
  375. if ([self.subviews containsObject:self.authView]) {
  376. [self.authView removeFromSuperview];
  377. self.authView = nil;
  378. }
  379. }
  380. - (AuthDisplayView *)authView {
  381. if (!_authView) {
  382. _authView = [AuthDisplayView shareInstance];
  383. [_authView.imageView setImage:[UIImage imageNamed:[self getAuthDisplayImage]]];
  384. MJWeakSelf;
  385. [_authView sureCallback:^{
  386. [weakSelf authAction];
  387. }];
  388. }
  389. return _authView;
  390. }
  391. - (NSString *)getAuthDisplayImage {
  392. return @"authTeacher_accompany";
  393. }
  394. - (NSString *)getAuthDisplayMessage {
  395. if ([self.teaherStatus isEqualToString:@"DOING"]) { // 审核中
  396. return @"您已提交认证申请,请耐心等待审核结果~";
  397. }
  398. else {
  399. if ([self.teaherStatus isEqualToString:@"PASS"] && self.liveFlag == NO) { // 如果达人认证通过
  400. return @"您尚未开通直播服务,开通后即可创建直播课程~";
  401. }
  402. return @"您还没有完成达人认证,认证后才可创建直播课哦~";
  403. }
  404. }
  405. - (void)authAction {
  406. if ([self.teaherStatus isEqual:@"PASS"] && self.liveFlag == NO) { // 开通直播
  407. KSBaseWKWebViewController *webCtrl = [[KSBaseWKWebViewController alloc] init];
  408. webCtrl.url = [NSString stringWithFormat:@"%@%@", WEBHOST, @"/#/openLive"];
  409. [self.naviController pushViewController:webCtrl animated:YES];
  410. }
  411. else {
  412. KSBaseWKWebViewController *webCtrl = [[KSBaseWKWebViewController alloc] init];
  413. webCtrl.url = [NSString stringWithFormat:@"%@%@", WEBHOST, @"/#/teacherCert"];
  414. [self.naviController pushViewController:webCtrl animated:YES];
  415. }
  416. }
  417. /*
  418. // Only override drawRect: if you perform custom drawing.
  419. // An empty implementation adversely affects performance during animation.
  420. - (void)drawRect:(CGRect)rect {
  421. // Drawing code
  422. }
  423. */
  424. @end