MyVideoCourseBodyView.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. //
  2. // MyVideoCourseBodyView.m
  3. // KulexiuForStudent
  4. //
  5. // Created by 王智 on 2022/4/20.
  6. //
  7. #import "MyVideoCourseBodyView.h"
  8. #import "VideoCourseCell.h"
  9. #import "VideoCourseModel.h"
  10. #import "KSBaseWKWebViewController.h"
  11. #import "KSChoosePicker.h"
  12. #import "MyVideoSearchView.h"
  13. #import "KSChoosePicker.h"
  14. @interface MyVideoCourseBodyView ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
  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 *searchKey;
  23. @property (nonatomic, copy) MyVideoSearchView *sortView;
  24. @end
  25. @implementation MyVideoCourseBodyView
  26. - (instancetype)initWithFrame:(CGRect)frame {
  27. self = [super initWithFrame:frame];
  28. if (self) {
  29. self.backgroundColor = HexRGB(0xf6f8f9);
  30. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  31. layout.sectionInset = UIEdgeInsetsMake(0, 14, 12, 14);
  32. self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height) collectionViewLayout:layout];
  33. self.collectionView.backgroundColor = HexRGB(0xf6f8f9);
  34. self.collectionView.delegate = self;
  35. self.collectionView.dataSource = self;
  36. self.collectionView.showsVerticalScrollIndicator = NO;
  37. self.collectionView.showsHorizontalScrollIndicator = NO;
  38. [self.collectionView registerNib:[UINib nibWithNibName:@"VideoCourseCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"VideoCourseCell"];
  39. [self addSubview:self.collectionView];
  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)endRefresh {
  58. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  59. [self.collectionView.mj_header endRefreshing];
  60. [self.collectionView.mj_footer endRefreshing];
  61. });
  62. }
  63. - (void)refreshAndRequestData {
  64. [self resetParamenter];
  65. [self requestData];
  66. }
  67. - (void)resetParamenter {
  68. self.isLoadMore = YES;
  69. self.pages = 1;
  70. self.rows = 10;
  71. self.dataArray = [NSMutableArray array];
  72. [self.collectionView.mj_footer resetNoMoreData];
  73. [self setPromptString:@"暂无课程" imageName:@"empty_course" inView:self.collectionView];
  74. [self.collectionView reloadData];
  75. }
  76. - (void)requestData {
  77. [KSNetworkingManager videoLessonGroupRequest:KS_POST lessonSubject:self.searchKey page:self.pages rows:self.rows success:^(NSDictionary * _Nonnull dic) {
  78. [self endRefresh];
  79. if ([dic integerValueForKey:@"code"] == 200 && [dic boolValueForKey:@"status"]) {
  80. NSArray *sourceArray = [[dic dictionaryValueForKey:@"data"] arrayValueForKey:@"rows"];
  81. for (NSDictionary *parm in sourceArray) {
  82. VideoCourseModel *model = [[VideoCourseModel alloc] initWithDictionary:parm];
  83. [self.dataArray addObject:model];
  84. }
  85. if (sourceArray.count < self.rows) {
  86. self.isLoadMore = NO;
  87. }
  88. }
  89. else {
  90. [self MBPShow:MESSAGEKEY];
  91. }
  92. [self.collectionView reloadData];
  93. [self changePromptLabelStateWithArray:self.dataArray];
  94. } faliure:^(NSError * _Nonnull error) {
  95. [self endRefresh];
  96. if (self.networkAvaiable == NO) {
  97. [self setPromptString:@"暂无网络" imageName:@"no_networking" inView:self.collectionView];
  98. }
  99. [self.dataArray removeAllObjects];
  100. [self.collectionView reloadData];
  101. [self changePromptLabelStateWithArray:self.dataArray];
  102. }];
  103. }
  104. - (void)beginRefreshImmediately {
  105. [self.collectionView.mj_header beginRefreshing];
  106. }
  107. - (void)selectCellAtIndexPath:(NSIndexPath *)indexPath {
  108. if (self.lastSelectedIndexPath == indexPath) {
  109. return;
  110. }
  111. if (self.lastSelectedIndexPath != nil) {
  112. UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:self.lastSelectedIndexPath];
  113. [cell setSelected:NO];
  114. }
  115. UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:indexPath];
  116. [cell setSelected:YES];
  117. self.lastSelectedIndexPath = indexPath;
  118. }
  119. - (void)layoutSubviews {
  120. [super layoutSubviews];
  121. CGFloat topHeight = 50.0f;
  122. self.collectionView.frame = CGRectMake(0, topHeight, self.bounds.size.width, self.bounds.size.height - topHeight);
  123. if (!_sortView) {
  124. _sortView = [MyVideoSearchView shareInstance];
  125. _sortView.frame = CGRectMake(0, 0, kScreenWidth, 50);
  126. MJWeakSelf;
  127. [_sortView sortAction:^{
  128. [weakSelf showSortView];
  129. }];
  130. [self addSubview:self.sortView];
  131. }
  132. }
  133. - (void)showSortView {
  134. if (self.subjectList.count) {
  135. NSMutableArray *nameArray = [NSMutableArray array];
  136. [nameArray addObject:@"全部"];
  137. for (NSDictionary *parm in self.subjectList) {
  138. [nameArray addObject:[parm stringValueForKey:@"subjectName"]];
  139. }
  140. MJWeakSelf;
  141. KSChoosePicker *picker = [[KSChoosePicker alloc] initWithTitle:@"声部筛选" sourceData:nameArray chooseReturnWithBlock:^(NSString * _Nonnull returnValue, NSInteger chooseIndex) {
  142. if (chooseIndex == 0) {
  143. weakSelf.sortView.sortTitleLabel.text = @"全部声部";
  144. weakSelf.searchKey = nil;
  145. }
  146. else {
  147. weakSelf.sortView.sortTitleLabel.text = returnValue;
  148. NSDictionary *parm = self.subjectList[chooseIndex-1];
  149. weakSelf.searchKey = [parm stringValueForKey:@"subjectId"];
  150. }
  151. self.sortView.arrowUp = NO;
  152. [weakSelf refreshAndRequestData];
  153. } cancel:^{
  154. self.sortView.arrowUp = NO;
  155. }];
  156. [picker showPicker];
  157. }
  158. else {
  159. [self MBPShow:@"无声部信息"];
  160. self.sortView.arrowUp = NO;
  161. }
  162. }
  163. - (void)beginFirstRefresh {
  164. if (!self.isHeaderRefreshed) {
  165. [self beginRefreshImmediately];
  166. }
  167. }
  168. #pragma mark ----- collection view
  169. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
  170. return 1;
  171. }
  172. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  173. return self.dataArray.count;
  174. }
  175. - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  176. VideoCourseCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"VideoCourseCell" forIndexPath:indexPath];
  177. VideoCourseModel *model = [self.dataArray objectAtIndex:indexPath.row];
  178. BOOL isCheck = self.selectIndex != 0;
  179. [cell configSourceModel:model isInCheck:isCheck];
  180. return cell;
  181. }
  182. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  183. VideoCourseModel *model = [self.dataArray objectAtIndex:indexPath.row];
  184. NSString *url = [NSString stringWithFormat:@"%@%@%.0f", WEBHOST, @"/#/videoDetail?groupId=",model.internalBaseClassIdentifier];
  185. KSBaseWKWebViewController *ctrl = [[KSBaseWKWebViewController alloc] init];
  186. ctrl.url = url;
  187. [self.naviController pushViewController:ctrl animated:YES];
  188. }
  189. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  190. return CGSizeMake((kScreenWidth - 28 - 11) / 2.0f, 200);
  191. }
  192. /**
  193. 设置没有数据时的显示
  194. @param promptString 提示语
  195. @param imgName 图片名称
  196. @param view 显示在什么地方
  197. */
  198. - (void)setPromptString:(NSString *)promptString imageName:(NSString *)imgName inView:(UIView *)view {
  199. if (self.promptView != nil) {
  200. [self.promptView removeFromSuperview];
  201. }
  202. else {
  203. self.promptView = [[StateView alloc]init];
  204. self.promptView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight - 300);
  205. }
  206. _promptPlaceView = view;
  207. //当请求不到数据时 ,自定义提示view 将会出现;
  208. self.promptView.imageName = imgName;
  209. self.promptView.alpha = 0.0f;
  210. [self.promptView setText:promptString];
  211. [view addSubview:self.promptView];
  212. }
  213. // 结束刷新后调用方法
  214. - (void)changePromptLabelStateWithArray:(NSMutableArray *)array {
  215. NSInteger count;
  216. if (array.count) {
  217. count = array.count;
  218. } else {
  219. count = 0;
  220. }
  221. [UIView animateWithDuration:0.1 animations:^{
  222. [[self promptView] setAlpha:count ? 0.0f :1.0f ] ;
  223. }] ;
  224. }
  225. - (BOOL)networkAvaiable {
  226. return [self checkNetworkAvaiable];
  227. }
  228. - (BOOL)checkNetworkAvaiable {
  229. BOOL isExistenceNetwork = YES;
  230. Reachability *reach = [Reachability reachabilityWithHostName:@"www.apple.com"];
  231. switch ([reach currentReachabilityStatus]) {
  232. case NotReachable:
  233. isExistenceNetwork = NO;
  234. //NSLog(@"notReachable");
  235. break;
  236. case ReachableViaWiFi:
  237. isExistenceNetwork = YES;
  238. //NSLog(@"WIFI");
  239. break;
  240. case ReachableViaWWAN:
  241. isExistenceNetwork = YES;
  242. //NSLog(@"3G");
  243. break;
  244. }
  245. return isExistenceNetwork;
  246. }
  247. - (NSMutableArray *)dataArray {
  248. if (!_dataArray) {
  249. _dataArray = [NSMutableArray array];
  250. }
  251. return _dataArray;
  252. }
  253. /*
  254. // Only override drawRect: if you perform custom drawing.
  255. // An empty implementation adversely affects performance during animation.
  256. - (void)drawRect:(CGRect)rect {
  257. // Drawing code
  258. }
  259. */
  260. @end