SafeSleepListController.m 4.0 KB
//
//  SafeSleepListController.m
//  DreamSleep
//
//  Created by peter on 2022/5/5.
//

#import "SafeSleepListController.h"
#import "SafeSleepRequestModel.h"
#import "CourseCollectionCell.h"
#import "CourseDetailController.h"

@interface SafeSleepListController () <UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, strong) UICollectionView *courseListView;
@property (nonatomic, strong) NSArray *listArr;
@property (nonatomic, strong) ExceptionDefaultView *exceptionView;
@end

@implementation SafeSleepListController {
    CourseType _type;
}

- (instancetype)initWithCourseType:(CourseType)type {
    if (self = [super init]) {
        _type = type;
        _listArr = @[];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.dk_backgroundColorPicker = DKColorPickerWithKey(VCViewBG);
    [self.view addSubview:self.courseListView];
    
    [self getCourseDataRequest];
}

- (void)getCourseDataRequest {
    [SafeSleepRequestModel getCourseListDataWithSubID:(_type == CourseTypeSafe ? 6 : 18) isHome:NO completion:^(SafeSleepRequestModel * _Nonnull requestModel) {
        if (requestModel.resCode == DSResCodeSuccess) {
            self.exceptionView.hidden = YES;
            self.listArr = requestModel.courseListData;
            [self.courseListView reloadData];
        } else {
            self.exceptionView.hidden = NO;
            [self.exceptionView showServerErrInfo:requestModel.errMessage];
        }
    }];
}

#pragma mark - UICollectionViewDataSource && UICollectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.listArr.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CourseCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CourseCollectionCellID" forIndexPath:indexPath];
    CourseModel *model = [self.listArr objectAtIndex:indexPath.row];
    if (indexPath.row < self.listArr.count) {
        cell.model = model;
    }
    cell.courseType = _type;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (self.listArr.count > indexPath.row) {
        CourseModel *model = [self.listArr objectAtIndex:indexPath.row];
        CourseDetailController *coureDetailVC = [[CourseDetailController alloc] init];
        coureDetailVC.courseModel = model;
        coureDetailVC.courseType = _type;
        coureDetailVC.entrance = EntranceCourseList;
        [self.navigationController pushViewController:coureDetailVC animated:YES];
    }
}

#pragma mark - lazy
- (UICollectionView *)courseListView {
    if (!_courseListView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.sectionFootersPinToVisibleBounds = YES;
        layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        layout.itemSize = [CourseModel itemSize];
        layout.sectionInset = [CourseModel sectionEdgeInset];
        layout.minimumLineSpacing = [CourseModel minimumLineSpacing];
        _courseListView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, self.view.height - Bottom_SafeArea_Height - kTopHeight(0) - 40) collectionViewLayout:layout];
        _courseListView.dk_backgroundColorPicker = DKColorPickerWithKey(VCViewBG);
        _courseListView.showsVerticalScrollIndicator = NO;
        _courseListView.delegate = self;
        _courseListView.dataSource = self;
        [_courseListView registerClass:[CourseCollectionCell class] forCellWithReuseIdentifier:@"CourseCollectionCellID"];
    }
    return _courseListView;
}

- (ExceptionDefaultView *)exceptionView {
    if (!_exceptionView) {
        WS(weakSelf);
        _exceptionView = [[ExceptionDefaultView alloc] initWithType:ExceptionTypeNet block:^{
            weakSelf.exceptionView.hidden = YES;
            [weakSelf getCourseDataRequest];
        } superView:self.view];
    }
    return _exceptionView;
}

@end