MyLiveCourseGroupView.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. //
  2. // MyLiveCourseGroupView.m
  3. // KulexiuForTeacher
  4. //
  5. // Created by 王智 on 2024/11/20.
  6. //
  7. #import "MyLiveCourseGroupView.h"
  8. #import "MyCourseSearchView.h"
  9. #import "MyLiveCourseGroupListCell.h"
  10. #import "StateView.h"
  11. #import "Reachability.h"
  12. #import "LiveLessonModel.h"
  13. #import "KSBaseWKWebViewController.h"
  14. #import "MyCourseRankSortView.h"
  15. @interface MyLiveCourseGroupView ()<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) MyCourseSearchView *searchView;
  24. @property (nonatomic, strong) NSString *searchKey;
  25. @property (nonatomic, strong) NSString *subjectId;
  26. @property (nonatomic, strong) NSString *status;
  27. @property (nonatomic, strong) NSString *classDate;
  28. @property (nonatomic, assign) NSInteger subjectChooseIndex;
  29. @property (nonatomic, assign) NSInteger statusChooseIndex;
  30. @property (nonatomic, strong) MyCourseRankSortView *sortView;
  31. @property (nonatomic, strong) NSMutableArray *statusArray;
  32. @end
  33. @implementation MyLiveCourseGroupView
  34. - (instancetype)initWithFrame:(CGRect)frame {
  35. self = [super initWithFrame:frame];
  36. if (self) {
  37. self.backgroundColor = [UIColor clearColor];
  38. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  39. layout.sectionInset = UIEdgeInsetsMake(0, 14, 12, 14);
  40. self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height) collectionViewLayout:layout];
  41. self.collectionView.backgroundColor = [UIColor clearColor];
  42. self.collectionView.delegate = self;
  43. self.collectionView.dataSource = self;
  44. self.collectionView.showsVerticalScrollIndicator = NO;
  45. self.collectionView.showsHorizontalScrollIndicator = NO;
  46. [self.collectionView registerNib:[UINib nibWithNibName:@"MyLiveCourseGroupListCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"MyLiveCourseGroupListCell"];
  47. [self addSubview:self.collectionView];
  48. self.collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  49. MJWeakSelf;
  50. self.collectionView.mj_header = [KSGifRefreshHeader headerWithRefreshingBlock:^{
  51. [weakSelf resetParamenter];
  52. [weakSelf requestData];
  53. }];
  54. self.collectionView.mj_footer = [KSGifRefreshFooter footerWithRefreshingBlock:^{
  55. if (weakSelf.isLoadMore) {
  56. weakSelf.pages += 1;
  57. [weakSelf requestData];
  58. }
  59. else {
  60. [weakSelf.collectionView.mj_footer endRefreshingWithNoMoreData];
  61. }
  62. }];
  63. }
  64. return self;
  65. }
  66. - (void)endRefresh {
  67. [self.collectionView.mj_header endRefreshing];
  68. [self.collectionView.mj_footer endRefreshing];
  69. }
  70. - (void)refreshAndRequestData {
  71. [self resetParamenter];
  72. [self requestData];
  73. }
  74. - (void)resetParamenter {
  75. self.isLoadMore = YES;
  76. self.pages = 1;
  77. self.rows = 10;
  78. self.dataArray = [NSMutableArray array];
  79. [self.collectionView.mj_footer resetNoMoreData];
  80. [self setPromptString:@"暂无内容" imageName:@"empty_course" inView:self.collectionView];
  81. [self.collectionView reloadData];
  82. }
  83. - (void)requestData {
  84. [KSNetworkingManager queryMyLiveCourse:KS_POST courseType:@"LIVE" classDate:self.classDate status:self.status subjectId:self.subjectId search:self.searchKey page:self.pages rows:self.rows success:^(NSDictionary * _Nonnull dic) {
  85. [self endRefresh];
  86. if ([dic ks_integerValueForKey:@"code"] == 200 && [dic ks_boolValueForKey:@"status"]) {
  87. NSArray *sourceArray = [[dic ks_dictionaryValueForKey:@"data"] ks_arrayValueForKey:@"rows"];
  88. for (NSDictionary *parm in sourceArray) {
  89. LiveLessonModel *model = [[LiveLessonModel alloc] initWithDictionary:parm];
  90. [self.dataArray addObject:model];
  91. }
  92. if (sourceArray.count < self.rows) {
  93. self.isLoadMore = NO;
  94. }
  95. }
  96. else {
  97. [LOADING_MANAGER MBShowAUTOHidingInWindow:MESSAGEKEY];
  98. }
  99. [self.collectionView reloadData];
  100. [self changePromptLabelStateWithArray:self.dataArray];
  101. } faliure:^(NSError * _Nonnull error) {
  102. [self endRefresh];
  103. if (self.networkAvaiable == NO) {
  104. [self setPromptString:@"暂无网络" imageName:@"no_networking" inView:self.collectionView];
  105. }
  106. [self.dataArray removeAllObjects];
  107. [self.collectionView reloadData];
  108. [self changePromptLabelStateWithArray:self.dataArray];
  109. }];
  110. }
  111. - (void)beginRefreshImmediately {
  112. [self.collectionView.mj_header beginRefreshing];
  113. }
  114. - (void)selectCellAtIndexPath:(NSIndexPath *)indexPath {
  115. if (self.lastSelectedIndexPath == indexPath) {
  116. return;
  117. }
  118. if (self.lastSelectedIndexPath != nil) {
  119. UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:self.lastSelectedIndexPath];
  120. [cell setSelected:NO];
  121. }
  122. UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:indexPath];
  123. [cell setSelected:YES];
  124. self.lastSelectedIndexPath = indexPath;
  125. }
  126. - (void)layoutSubviews {
  127. [super layoutSubviews];
  128. CGFloat searchViewHeight = [MyCourseSearchView getViewHeight];
  129. if (![self.subviews containsObject:self.searchView]) {
  130. [self addSubview:self.searchView];
  131. [self.searchView mas_makeConstraints:^(MASConstraintMaker *make) {
  132. make.left.right.top.mas_equalTo(self);
  133. make.height.mas_equalTo(searchViewHeight);
  134. }];
  135. }
  136. [self.collectionView mas_remakeConstraints:^(MASConstraintMaker *make) {
  137. make.left.right.bottom.mas_equalTo(self);
  138. make.top.mas_equalTo(self.searchView.mas_bottom);
  139. }];
  140. }
  141. - (void)beginFirstRefresh {
  142. if (!self.isHeaderRefreshed) {
  143. [self beginRefreshImmediately];
  144. }
  145. }
  146. #pragma mark ----- collection view
  147. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
  148. return 1;
  149. }
  150. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  151. return self.dataArray.count;
  152. }
  153. - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  154. MyLiveCourseGroupListCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyLiveCourseGroupListCell" forIndexPath:indexPath];
  155. LiveLessonModel *model = [self.dataArray objectAtIndex:indexPath.row];
  156. [cell configWithSource:model];
  157. return cell;
  158. }
  159. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  160. LiveLessonModel *model = self.dataArray[indexPath.row];
  161. KSBaseWKWebViewController *ctrl = [[KSBaseWKWebViewController alloc] init];
  162. NSString *url = [NSString stringWithFormat:@"%@/#/liveDetail?joinRoom=1&groupId=%@&classId=%@", WEBHOST, model.courseGoupId,model.courseId];
  163. ctrl.url = url;
  164. [self.naviController pushViewController:ctrl animated:YES];
  165. }
  166. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  167. CGFloat width = KPortraitWidth - 28;
  168. if (IS_IPAD) {
  169. width = (KPortraitWidth - 28 - 12) / 2.0f;
  170. }
  171. CGFloat height = (width - 12 * 2) / 16 * 9 + 143;
  172. return CGSizeMake(width, height);
  173. }
  174. #pragma mark ------ lazying
  175. - (MyCourseSearchView *)searchView {
  176. if (!_searchView) {
  177. _searchView = [MyCourseSearchView sharedInstance];
  178. [_searchView configPlaceholder:@"请输入课程组或老师名称"];
  179. MJWeakSelf;
  180. [_searchView searchActionCallback:^(MY_COURSE_SORT type, NSString * _Nullable searchKey) {
  181. [weakSelf searchAction:type searchKey:searchKey];
  182. }];
  183. }
  184. return _searchView;
  185. }
  186. - (void)searchAction:(MY_COURSE_SORT)type searchKey:(NSString *)searchKey {
  187. switch (type) {
  188. case MY_COURSE_SORT_RANK:
  189. {
  190. [self showSortView];
  191. }
  192. break;
  193. case MY_COURSE_SORT_SEARCH:
  194. {
  195. [self evaluateSource:searchKey];
  196. }
  197. break;
  198. default:
  199. break;
  200. }
  201. }
  202. - (void)showSortView {
  203. self.searchView.arrowUp = YES;
  204. [self.sortView refreshUI:self.statusChooseIndex subjectId:self.subjectChooseIndex];
  205. [self.sortView showInView:[NSObject getKeyWindow]];
  206. }
  207. - (void)evaluateSource:(NSString *)searchKey {
  208. self.searchKey = searchKey;
  209. [self refreshAndRequestData];
  210. }
  211. /**
  212. 设置没有数据时的显示
  213. @param promptString 提示语
  214. @param imgName 图片名称
  215. @param view 显示在什么地方
  216. */
  217. - (void)setPromptString:(NSString *)promptString imageName:(NSString *)imgName inView:(UIView *)view {
  218. if (self.promptView != nil) {
  219. [self.promptView removeFromSuperview];
  220. }
  221. else {
  222. self.promptView = [[StateView alloc]init];
  223. self.promptView.frame = CGRectMake(0, 0, KPortraitWidth, KPortraitHeight - 300);
  224. }
  225. _promptPlaceView = view;
  226. //当请求不到数据时 ,自定义提示view 将会出现;
  227. self.promptView.imageName = imgName;
  228. self.promptView.alpha = 0.0f;
  229. [self.promptView setText:promptString];
  230. [view addSubview:self.promptView];
  231. }
  232. // 结束刷新后调用方法
  233. - (void)changePromptLabelStateWithArray:(NSMutableArray *)array {
  234. NSInteger count;
  235. if (array.count) {
  236. count = array.count;
  237. } else {
  238. count = 0;
  239. }
  240. [UIView animateWithDuration:0.1 animations:^{
  241. [[self promptView] setAlpha:count ? 0.0f :1.0f ] ;
  242. }] ;
  243. }
  244. - (BOOL)networkAvaiable {
  245. return [self checkNetworkAvaiable];
  246. }
  247. - (BOOL)checkNetworkAvaiable {
  248. BOOL isExistenceNetwork = YES;
  249. Reachability *reach = [Reachability reachabilityWithHostName:@"www.apple.com"];
  250. switch ([reach currentReachabilityStatus]) {
  251. case NotReachable:
  252. isExistenceNetwork = NO;
  253. //NSLog(@"notReachable");
  254. break;
  255. case ReachableViaWiFi:
  256. isExistenceNetwork = YES;
  257. //NSLog(@"WIFI");
  258. break;
  259. case ReachableViaWWAN:
  260. isExistenceNetwork = YES;
  261. //NSLog(@"3G");
  262. break;
  263. }
  264. return isExistenceNetwork;
  265. }
  266. - (MyCourseRankSortView *)sortView {
  267. if (!_sortView) {
  268. _sortView = [MyCourseRankSortView sharedInstance];
  269. [_sortView configWithStatusArray:self.statusArray subjectArray:self.subjectList];
  270. MJWeakSelf;
  271. [_sortView sortActionCallback:^(MY_COURSE_SORTTYPE type, NSString * _Nullable status, NSString * _Nullable subjectId, NSInteger statusIndex, NSInteger subjectIndex) {
  272. [weakSelf sortActionWithType:type status:status subjectId:subjectId statusChooseIndex:statusIndex subjectChooseIndex:subjectIndex];
  273. }];
  274. }
  275. return _sortView;
  276. }
  277. - (void)sortActionWithType:(MY_COURSE_SORTTYPE)type status:(NSString *)status subjectId:(NSString *)subjectId statusChooseIndex:(NSInteger)statusChooseIndex subjectChooseIndex:(NSInteger)subjectChooseIndex {
  278. self.searchView.arrowUp = NO;
  279. if (type == MY_COURSE_SORTTYPE_SORT) {
  280. self.statusChooseIndex = statusChooseIndex;
  281. self.subjectChooseIndex = subjectChooseIndex;
  282. self.status = status;
  283. self.subjectId = subjectId;
  284. if ([NSString isEmptyString:status]) {
  285. self.status = nil;
  286. }
  287. if ([NSString isEmptyString:subjectId]) {
  288. self.subjectId = nil;
  289. }
  290. [self refreshAndRequestData];
  291. }
  292. }
  293. - (NSMutableArray *)statusArray {
  294. if (!_statusArray) {
  295. _statusArray = [NSMutableArray arrayWithArray:@[@{@"name":@"全部",@"id":@""},@{@"name":@"未开课",@"id":@"NOT_START"},@{@"name":@"已开课",@"id":@"ING"},@{@"name":@"已结课",@"id":@"COMPLETE"}]];
  296. }
  297. return _statusArray;
  298. }
  299. - (void)setSubjectList:(NSMutableArray *)subjectList {
  300. NSMutableArray *array = [NSMutableArray arrayWithArray:subjectList];
  301. [array insertObject:@{@"name":@"全部",@"id":@""} atIndex:0];
  302. _subjectList = array;
  303. }
  304. /*
  305. // Only override drawRect: if you perform custom drawing.
  306. // An empty implementation adversely affects performance during animation.
  307. - (void)drawRect:(CGRect)rect {
  308. // Drawing code
  309. }
  310. */
  311. @end