123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365 |
- //
- // MyLiveCourseGroupView.m
- // KulexiuForTeacher
- //
- // Created by 王智 on 2024/11/20.
- //
- #import "MyLiveCourseGroupView.h"
- #import "MyCourseSearchView.h"
- #import "MyLiveCourseGroupListCell.h"
- #import "StateView.h"
- #import "Reachability.h"
- #import "LiveLessonModel.h"
- #import "KSBaseWKWebViewController.h"
- #import "MyCourseRankSortView.h"
- @interface MyLiveCourseGroupView ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
- @property (nonatomic, strong) NSMutableArray *dataArray;
- @property (nonatomic, strong) StateView *promptView;
- @property (nonatomic, strong) UIView *promptPlaceView;
- @property (nonatomic, assign) BOOL networkAvaiable; // 网络是否可用
- @property (nonatomic, assign) BOOL isLoadMore;
- @property (nonatomic, assign) NSInteger rows;
- @property (nonatomic, assign) NSInteger pages;
- @property (nonatomic, strong) MyCourseSearchView *searchView;
- @property (nonatomic, strong) NSString *searchKey;
- @property (nonatomic, strong) NSString *subjectId;
- @property (nonatomic, strong) NSString *status;
- @property (nonatomic, strong) NSString *classDate;
- @property (nonatomic, assign) NSInteger subjectChooseIndex;
- @property (nonatomic, assign) NSInteger statusChooseIndex;
- @property (nonatomic, strong) MyCourseRankSortView *sortView;
- @property (nonatomic, strong) NSMutableArray *statusArray;
- @end
- @implementation MyLiveCourseGroupView
- - (instancetype)initWithFrame:(CGRect)frame {
- self = [super initWithFrame:frame];
- if (self) {
- self.backgroundColor = [UIColor clearColor];
- UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
- layout.sectionInset = UIEdgeInsetsMake(0, 14, 12, 14);
-
- self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height) collectionViewLayout:layout];
- self.collectionView.backgroundColor = [UIColor clearColor];
- self.collectionView.delegate = self;
- self.collectionView.dataSource = self;
- self.collectionView.showsVerticalScrollIndicator = NO;
- self.collectionView.showsHorizontalScrollIndicator = NO;
- [self.collectionView registerNib:[UINib nibWithNibName:@"MyLiveCourseGroupListCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"MyLiveCourseGroupListCell"];
- [self addSubview:self.collectionView];
- self.collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
- MJWeakSelf;
- self.collectionView.mj_header = [KSGifRefreshHeader headerWithRefreshingBlock:^{
- [weakSelf resetParamenter];
- [weakSelf requestData];
- }];
- self.collectionView.mj_footer = [KSGifRefreshFooter footerWithRefreshingBlock:^{
- if (weakSelf.isLoadMore) {
- weakSelf.pages += 1;
- [weakSelf requestData];
- }
- else {
- [weakSelf.collectionView.mj_footer endRefreshingWithNoMoreData];
- }
- }];
- }
- return self;
- }
- - (void)endRefresh {
- [self.collectionView.mj_header endRefreshing];
- [self.collectionView.mj_footer endRefreshing];
- }
- - (void)refreshAndRequestData {
- [self resetParamenter];
- [self requestData];
- }
- - (void)resetParamenter {
- self.isLoadMore = YES;
- self.pages = 1;
- self.rows = 10;
- self.dataArray = [NSMutableArray array];
- [self.collectionView.mj_footer resetNoMoreData];
- [self setPromptString:@"暂无内容" imageName:@"empty_course" inView:self.collectionView];
- [self.collectionView reloadData];
- }
- - (void)requestData {
- [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) {
- [self endRefresh];
- if ([dic ks_integerValueForKey:@"code"] == 200 && [dic ks_boolValueForKey:@"status"]) {
- NSArray *sourceArray = [[dic ks_dictionaryValueForKey:@"data"] ks_arrayValueForKey:@"rows"];
- for (NSDictionary *parm in sourceArray) {
- LiveLessonModel *model = [[LiveLessonModel alloc] initWithDictionary:parm];
- [self.dataArray addObject:model];
- }
-
- if (sourceArray.count < self.rows) {
- self.isLoadMore = NO;
- }
- }
- else {
- [LOADING_MANAGER MBShowAUTOHidingInWindow:MESSAGEKEY];
- }
- [self.collectionView reloadData];
- [self changePromptLabelStateWithArray:self.dataArray];
- } faliure:^(NSError * _Nonnull error) {
- [self endRefresh];
- if (self.networkAvaiable == NO) {
- [self setPromptString:@"暂无网络" imageName:@"no_networking" inView:self.collectionView];
- }
- [self.dataArray removeAllObjects];
- [self.collectionView reloadData];
- [self changePromptLabelStateWithArray:self.dataArray];
- }];
- }
- - (void)beginRefreshImmediately {
- [self.collectionView.mj_header beginRefreshing];
- }
- - (void)selectCellAtIndexPath:(NSIndexPath *)indexPath {
-
- if (self.lastSelectedIndexPath == indexPath) {
- return;
- }
- if (self.lastSelectedIndexPath != nil) {
- UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:self.lastSelectedIndexPath];
- [cell setSelected:NO];
- }
- UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:indexPath];
- [cell setSelected:YES];
- self.lastSelectedIndexPath = indexPath;
- }
- - (void)layoutSubviews {
- [super layoutSubviews];
- CGFloat searchViewHeight = [MyCourseSearchView getViewHeight];
- if (![self.subviews containsObject:self.searchView]) {
- [self addSubview:self.searchView];
- [self.searchView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.right.top.mas_equalTo(self);
- make.height.mas_equalTo(searchViewHeight);
- }];
- }
- [self.collectionView mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.left.right.bottom.mas_equalTo(self);
- make.top.mas_equalTo(self.searchView.mas_bottom);
- }];
- }
- - (void)beginFirstRefresh {
- if (!self.isHeaderRefreshed) {
- [self beginRefreshImmediately];
- }
- }
- #pragma mark ----- collection view
- - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
- return 1;
- }
- - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
- return self.dataArray.count;
- }
- - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
- MyLiveCourseGroupListCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyLiveCourseGroupListCell" forIndexPath:indexPath];
-
- LiveLessonModel *model = [self.dataArray objectAtIndex:indexPath.row];
- [cell configWithSource:model];
- return cell;
- }
- - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
- LiveLessonModel *model = self.dataArray[indexPath.row];
- KSBaseWKWebViewController *ctrl = [[KSBaseWKWebViewController alloc] init];
- NSString *url = [NSString stringWithFormat:@"%@/#/liveDetail?joinRoom=1&groupId=%@&classId=%@", WEBHOST, model.courseGoupId,model.courseId];
- ctrl.url = url;
- [self.naviController pushViewController:ctrl animated:YES];
- }
- - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
- CGFloat width = KPortraitWidth - 28;
- if (IS_IPAD) {
- width = (KPortraitWidth - 28 - 12) / 2.0f;
- }
- CGFloat height = (width - 12 * 2) / 16 * 9 + 143;
- return CGSizeMake(width, height);
- }
- #pragma mark ------ lazying
- - (MyCourseSearchView *)searchView {
- if (!_searchView) {
- _searchView = [MyCourseSearchView sharedInstance];
- [_searchView configPlaceholder:@"请输入课程组或老师名称"];
- MJWeakSelf;
- [_searchView searchActionCallback:^(MY_COURSE_SORT type, NSString * _Nullable searchKey) {
- [weakSelf searchAction:type searchKey:searchKey];
- }];
- }
- return _searchView;
- }
- - (void)searchAction:(MY_COURSE_SORT)type searchKey:(NSString *)searchKey {
- switch (type) {
- case MY_COURSE_SORT_RANK:
- {
- [self showSortView];
- }
- break;
- case MY_COURSE_SORT_SEARCH:
- {
- [self evaluateSource:searchKey];
- }
- break;
- default:
- break;
- }
- }
- - (void)showSortView {
- self.searchView.arrowUp = YES;
- [self.sortView refreshUI:self.statusChooseIndex subjectId:self.subjectChooseIndex];
- [self.sortView showInView:[NSObject getKeyWindow]];
- }
- - (void)evaluateSource:(NSString *)searchKey {
- self.searchKey = searchKey;
- [self refreshAndRequestData];
- }
- /**
- 设置没有数据时的显示
-
- @param promptString 提示语
- @param imgName 图片名称
- @param view 显示在什么地方
- */
- - (void)setPromptString:(NSString *)promptString imageName:(NSString *)imgName inView:(UIView *)view {
- if (self.promptView != nil) {
- [self.promptView removeFromSuperview];
- }
- else {
- self.promptView = [[StateView alloc]init];
- self.promptView.frame = CGRectMake(0, 0, KPortraitWidth, KPortraitHeight - 300);
- }
- _promptPlaceView = view;
- //当请求不到数据时 ,自定义提示view 将会出现;
- self.promptView.imageName = imgName;
- self.promptView.alpha = 0.0f;
- [self.promptView setText:promptString];
- [view addSubview:self.promptView];
- }
- // 结束刷新后调用方法
- - (void)changePromptLabelStateWithArray:(NSMutableArray *)array {
- NSInteger count;
- if (array.count) {
- count = array.count;
- } else {
- count = 0;
- }
-
- [UIView animateWithDuration:0.1 animations:^{
- [[self promptView] setAlpha:count ? 0.0f :1.0f ] ;
- }] ;
-
- }
- - (BOOL)networkAvaiable {
- return [self checkNetworkAvaiable];
- }
- - (BOOL)checkNetworkAvaiable {
- BOOL isExistenceNetwork = YES;
- Reachability *reach = [Reachability reachabilityWithHostName:@"www.apple.com"];
- switch ([reach currentReachabilityStatus]) {
- case NotReachable:
- isExistenceNetwork = NO;
- //NSLog(@"notReachable");
- break;
- case ReachableViaWiFi:
- isExistenceNetwork = YES;
- //NSLog(@"WIFI");
- break;
- case ReachableViaWWAN:
- isExistenceNetwork = YES;
- //NSLog(@"3G");
- break;
- }
- return isExistenceNetwork;
- }
- - (MyCourseRankSortView *)sortView {
- if (!_sortView) {
- _sortView = [MyCourseRankSortView sharedInstance];
- [_sortView configWithStatusArray:self.statusArray subjectArray:self.subjectList];
- MJWeakSelf;
- [_sortView sortActionCallback:^(MY_COURSE_SORTTYPE type, NSString * _Nullable status, NSString * _Nullable subjectId, NSInteger statusIndex, NSInteger subjectIndex) {
- [weakSelf sortActionWithType:type status:status subjectId:subjectId statusChooseIndex:statusIndex subjectChooseIndex:subjectIndex];
- }];
- }
- return _sortView;
- }
- - (void)sortActionWithType:(MY_COURSE_SORTTYPE)type status:(NSString *)status subjectId:(NSString *)subjectId statusChooseIndex:(NSInteger)statusChooseIndex subjectChooseIndex:(NSInteger)subjectChooseIndex {
- self.searchView.arrowUp = NO;
- if (type == MY_COURSE_SORTTYPE_SORT) {
- self.statusChooseIndex = statusChooseIndex;
- self.subjectChooseIndex = subjectChooseIndex;
- self.status = status;
- self.subjectId = subjectId;
- if ([NSString isEmptyString:status]) {
- self.status = nil;
- }
- if ([NSString isEmptyString:subjectId]) {
- self.subjectId = nil;
- }
- [self refreshAndRequestData];
- }
- }
- - (NSMutableArray *)statusArray {
- if (!_statusArray) {
- _statusArray = [NSMutableArray arrayWithArray:@[@{@"name":@"全部",@"id":@""},@{@"name":@"未开课",@"id":@"NOT_START"},@{@"name":@"已开课",@"id":@"ING"},@{@"name":@"已结课",@"id":@"COMPLETE"}]];
- }
- return _statusArray;
- }
- - (void)setSubjectList:(NSMutableArray *)subjectList {
- NSMutableArray *array = [NSMutableArray arrayWithArray:subjectList];
- [array insertObject:@{@"name":@"全部",@"id":@""} atIndex:0];
- _subjectList = array;
- }
- /*
- // Only override drawRect: if you perform custom drawing.
- // An empty implementation adversely affects performance during animation.
- - (void)drawRect:(CGRect)rect {
- // Drawing code
- }
- */
- @end
|