MinePageVideoView.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. //
  2. // MinePageVideoView.m
  3. // KulexiuForTeacher
  4. //
  5. // Created by Kyle on 2022/3/30.
  6. //
  7. #import "MinePageVideoView.h"
  8. #import "VideoCourseModel.h"
  9. #import "StateView.h"
  10. #import "Reachability.h"
  11. #import "MinePageVideoCell.h"
  12. #import "MinePageSubmitView.h"
  13. #import "KSBaseWKWebViewController.h"
  14. #import "AuthDisplayView.h"
  15. @interface MinePageVideoView ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
  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) MinePageSubmitView *uploadView;
  24. @property (nonatomic, strong) AuthDisplayView *authView;
  25. @property (nonatomic, assign) BOOL authStatus;
  26. @end
  27. @implementation MinePageVideoView
  28. - (instancetype)initWithFrame:(CGRect)frame {
  29. self = [super initWithFrame:frame];
  30. if (self) {
  31. self.backgroundColor = HexRGB(0xf6f8f9);
  32. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  33. layout.sectionInset = UIEdgeInsetsMake(0, 14, 12, 14);
  34. self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height) collectionViewLayout:layout];
  35. self.collectionView.backgroundColor = HexRGB(0xf6f8f9);
  36. self.collectionView.delegate = self;
  37. self.collectionView.dataSource = self;
  38. self.collectionView.showsVerticalScrollIndicator = NO;
  39. self.collectionView.showsHorizontalScrollIndicator = NO;
  40. [self.collectionView registerNib:[UINib nibWithNibName:@"MinePageVideoCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"MinePageVideoCell"];
  41. [self addSubview:self.collectionView];
  42. MJWeakSelf;
  43. self.collectionView.mj_header = [KSGifRefreshHeader headerWithRefreshingBlock:^{
  44. [weakSelf resetParamenter];
  45. [weakSelf requestData];
  46. }];
  47. self.collectionView.mj_footer = [KSGifRefreshFooter footerWithRefreshingBlock:^{
  48. if (weakSelf.isLoadMore) {
  49. weakSelf.pages += 1;
  50. [weakSelf requestData];
  51. }
  52. else {
  53. [weakSelf.collectionView.mj_footer endRefreshingWithNoMoreData];
  54. }
  55. }];
  56. }
  57. return self;
  58. }
  59. - (void)endRefresh {
  60. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  61. [self.collectionView.mj_header endRefreshing];
  62. [self.collectionView.mj_footer endRefreshing];
  63. });
  64. }
  65. - (void)refreshAndRequestData {
  66. [self resetParamenter];
  67. [self requestData];
  68. }
  69. - (void)resetParamenter {
  70. self.isLoadMore = YES;
  71. self.pages = 1;
  72. self.rows = 10;
  73. self.dataArray = [NSMutableArray array];
  74. [self.collectionView.mj_footer resetNoMoreData];
  75. [self setPromptString:@"暂无课程~" imageName:@"empty_course" inView:self.collectionView];
  76. [self.collectionView reloadData];
  77. }
  78. - (void)requestData {
  79. [KSNetworkingManager videoLessonListRequest:KS_POST lessonSubject:nil auditStatus:@"PASS" pageNo:self.pages pageSize:self.rows success:^(NSDictionary * _Nonnull dic) {
  80. [self endRefresh];
  81. if ([dic integerValueForKey:@"code"] == 200 && [dic boolValueForKey:@"status"]) {
  82. NSArray *sourceArray = [[dic dictionaryValueForKey:@"data"] arrayValueForKey:@"rows"];
  83. for (NSDictionary *parm in sourceArray) {
  84. VideoCourseModel *model = [[VideoCourseModel alloc] initWithDictionary:parm];
  85. [self.dataArray addObject:model];
  86. }
  87. if (sourceArray.count < self.rows) {
  88. self.isLoadMore = NO;
  89. }
  90. }
  91. else {
  92. [self MBPShow:MESSAGEKEY];
  93. }
  94. [self.collectionView reloadData];
  95. [self changePromptLabelStateWithArray:self.dataArray];
  96. } faliure:^(NSError * _Nonnull error) {
  97. [self endRefresh];
  98. if (self.networkAvaiable == NO) {
  99. [self setPromptString:@"暂无网络" imageName:@"no_networking" inView:self.collectionView];
  100. }
  101. [self.dataArray removeAllObjects];
  102. [self.collectionView reloadData];
  103. [self changePromptLabelStateWithArray:self.dataArray];
  104. }];
  105. }
  106. - (void)beginRefreshImmediately {
  107. [self.collectionView.mj_header beginRefreshing];
  108. }
  109. - (void)selectCellAtIndexPath:(NSIndexPath *)indexPath {
  110. if (self.lastSelectedIndexPath == indexPath) {
  111. return;
  112. }
  113. if (self.lastSelectedIndexPath != nil) {
  114. UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:self.lastSelectedIndexPath];
  115. [cell setSelected:NO];
  116. }
  117. UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:indexPath];
  118. [cell setSelected:YES];
  119. self.lastSelectedIndexPath = indexPath;
  120. }
  121. - (void)layoutSubviews {
  122. [super layoutSubviews];
  123. CGFloat topHeight = 60.0f;
  124. self.collectionView.frame = CGRectMake(0, topHeight, self.bounds.size.width, self.bounds.size.height - topHeight);
  125. if (![self.subviews containsObject:self.uploadView]) {
  126. [self addSubview:self.uploadView];
  127. self.uploadView.type = MINEPAGETYPE_VIDEO;
  128. if (self.authStatus == NO) {
  129. [self showAuthView];
  130. }
  131. else {
  132. [self hideAuthView];
  133. }
  134. }
  135. }
  136. - (void)beginFirstRefresh {
  137. if (!self.isHeaderRefreshed) {
  138. [self beginRefreshImmediately];
  139. }
  140. }
  141. #pragma mark ----- collection view
  142. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
  143. return 1;
  144. }
  145. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  146. return self.dataArray.count;
  147. }
  148. - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  149. MinePageVideoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MinePageVideoCell" forIndexPath:indexPath];
  150. VideoCourseModel *model = [self.dataArray objectAtIndex:indexPath.row];
  151. [cell configSourceModel:model];
  152. return cell;
  153. }
  154. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  155. VideoCourseModel *model = [self.dataArray objectAtIndex:indexPath.row];
  156. KSBaseWKWebViewController *ctrl = [[KSBaseWKWebViewController alloc] init];
  157. ctrl.url = [NSString stringWithFormat:@"%@%@%.0f", WEBHOST, @"/#/videoDetail?groupId=",model.internalBaseClassIdentifier];
  158. [self.naviController pushViewController:ctrl animated:YES];
  159. }
  160. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  161. return CGSizeMake((kScreenWidth - 28 - 11) / 2.0f, 200);
  162. }
  163. /**
  164. 设置没有数据时的显示
  165. @param promptString 提示语
  166. @param imgName 图片名称
  167. @param view 显示在什么地方
  168. */
  169. - (void)setPromptString:(NSString *)promptString imageName:(NSString *)imgName inView:(UIView *)view {
  170. if (self.promptView != nil) {
  171. [self.promptView removeFromSuperview];
  172. }
  173. else {
  174. self.promptView = [[StateView alloc]init];
  175. self.promptView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight - 300);
  176. }
  177. _promptPlaceView = view;
  178. //当请求不到数据时 ,自定义提示view 将会出现;
  179. self.promptView.imageName = imgName;
  180. self.promptView.alpha = 0.0f;
  181. [self.promptView setText:promptString];
  182. [view addSubview:self.promptView];
  183. }
  184. // 结束刷新后调用方法
  185. - (void)changePromptLabelStateWithArray:(NSMutableArray *)array {
  186. NSInteger count;
  187. if (array.count) {
  188. count = array.count;
  189. } else {
  190. count = 0;
  191. }
  192. [UIView animateWithDuration:0.1 animations:^{
  193. [[self promptView] setAlpha:count ? 0.0f :1.0f ] ;
  194. }] ;
  195. }
  196. - (BOOL)networkAvaiable {
  197. return [self checkNetworkAvaiable];
  198. }
  199. - (BOOL)checkNetworkAvaiable {
  200. BOOL isExistenceNetwork = YES;
  201. Reachability *reach = [Reachability reachabilityWithHostName:@"www.apple.com"];
  202. switch ([reach currentReachabilityStatus]) {
  203. case NotReachable:
  204. isExistenceNetwork = NO;
  205. //NSLog(@"notReachable");
  206. break;
  207. case ReachableViaWiFi:
  208. isExistenceNetwork = YES;
  209. //NSLog(@"WIFI");
  210. break;
  211. case ReachableViaWWAN:
  212. isExistenceNetwork = YES;
  213. //NSLog(@"3G");
  214. break;
  215. }
  216. return isExistenceNetwork;
  217. }
  218. #pragma mark ----- lazying
  219. - (void)setTeaherStatus:(NSString *)teaherStatus {
  220. _teaherStatus = teaherStatus;
  221. if ([teaherStatus isEqualToString:@"PASS"]) {
  222. self.authStatus = YES;
  223. }
  224. else {
  225. self.authStatus = NO;
  226. }
  227. if (self.authStatus == NO) {
  228. [self showAuthView];
  229. }
  230. else {
  231. [self hideAuthView];
  232. }
  233. }
  234. - (NSString *)getAuthDisplayMessage {
  235. if ([self.teaherStatus isEqualToString:@"DOING"]) {
  236. return @"您已提交认证申请,请耐心等待审核结果~";
  237. }
  238. else {
  239. return @"您还没有完成老师认证,认证后才可创建视频课哦~";
  240. }
  241. }
  242. - (void)configAuthDisplay {
  243. [self.authView configDisplayMessage:[self getAuthDisplayMessage]];
  244. if ([self.teaherStatus isEqualToString:@"DOING"]) {
  245. self.authView.sureButton.userInteractionEnabled = NO;
  246. self.authView.sureButton.hidden = YES;
  247. }
  248. else {
  249. self.authView.sureButton.userInteractionEnabled = YES;
  250. self.authView.sureButton.hidden = NO;
  251. }
  252. }
  253. - (void)showAuthView {
  254. [self configAuthDisplay];
  255. if ([self.subviews containsObject:self.authView]) {
  256. [self bringSubviewToFront:self.authView];
  257. }
  258. else {
  259. [self addSubview:self.authView];
  260. [self.authView mas_makeConstraints:^(MASConstraintMaker *make) {
  261. make.left.top.bottom.right.mas_equalTo(self);
  262. }];
  263. }
  264. }
  265. - (void)hideAuthView {
  266. if ([self.subviews containsObject:self.authView]) {
  267. [self.authView removeFromSuperview];
  268. self.authView = nil;
  269. }
  270. }
  271. - (MinePageSubmitView *)uploadView {
  272. if (!_uploadView) {
  273. _uploadView = [MinePageSubmitView shareInstance];
  274. CGFloat topHeight = 60.0f;
  275. _uploadView.frame = CGRectMake(0, 0, kScreenWidth, topHeight);
  276. MJWeakSelf;
  277. [_uploadView opreationCallback:^(MINEPAGETYPE type) {
  278. [weakSelf uploadAciton:type];
  279. }];
  280. }
  281. return _uploadView;
  282. }
  283. - (AuthDisplayView *)authView {
  284. if (!_authView) {
  285. _authView = [AuthDisplayView shareInstance];
  286. [_authView.imageView setImage:[UIImage imageNamed:@"authTeacher_accompany"]];
  287. MJWeakSelf;
  288. [_authView sureCallback:^{
  289. [weakSelf authAction];
  290. }];
  291. }
  292. return _authView;
  293. }
  294. - (void)authAction {
  295. KSBaseWKWebViewController *webCtrl = [[KSBaseWKWebViewController alloc] init];
  296. webCtrl.url = [NSString stringWithFormat:@"%@%@", WEBHOST, @"/#/teacherCert"];
  297. [self.naviController pushViewController:webCtrl animated:YES];
  298. }
  299. - (void)uploadAciton:(MINEPAGETYPE)type {
  300. if (type == MINEPAGETYPE_VIDEO) {
  301. KSBaseWKWebViewController *webCtrl = [[KSBaseWKWebViewController alloc] init];
  302. webCtrl.url = [NSString stringWithFormat:@"%@%@", WEBHOST, @"/#/videoCreate"];
  303. [self.naviController pushViewController:webCtrl animated:YES];
  304. }
  305. }
  306. - (NSMutableArray *)dataArray {
  307. if (!_dataArray) {
  308. _dataArray = [NSMutableArray array];
  309. }
  310. return _dataArray;
  311. }
  312. /*
  313. // Only override drawRect: if you perform custom drawing.
  314. // An empty implementation adversely affects performance during animation.
  315. - (void)drawRect:(CGRect)rect {
  316. // Drawing code
  317. }
  318. */
  319. @end