MyGroupCourseGroupView.m 13 KB

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