HomeTableView.m 3.9 KB
//
//  HomeTableView.m
//  DreamSleep
//
//  Created by peter on 2022/4/8.
//

#import "HomeTableView.h"
#import "DSGifHeader.h"
#import "HomeHeaderView.h"
#import "SafeSleepCell.h"
#import "HomeRequestModel.h"
#import "SafeSleepRequestModel.h"

@interface HomeTableView () <UITableViewDelegate>
@property (nonatomic, strong) HomeHeaderView *headerView;
@property (nonatomic, strong) DSDataSource *homeDataSource;
@end

@implementation HomeTableView

- (instancetype)initDemo {
    if (self = [super initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) style:UITableViewStylePlain]) {
        self.dk_backgroundColorPicker = DKColorPickerWithKey(VCViewBG);
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.showsVerticalScrollIndicator = NO;
        // iOS 15适配UITableView内容自动下移22像素BUG
        if (@available(iOS 15.0, *)) {
            self.sectionHeaderTopPadding = 0;
        }
        [self.homeDataSource addDataArray:[SafeSleepModel getDatas]];
        
        self.mj_header = [DSGifHeader headerWithRefreshingBlock:^{
            [HomeRequestModel queryBannerListWithCompletion:^(HomeRequestModel * _Nonnull requestModel) {
                [self.mj_header endRefreshing];
                if (requestModel.resCode == DSResCodeSuccess) {
                    [self.headerView updateBannerWithListData:requestModel.bannerListData];
                }
            }];
//            [SafeSleepRequestModel getCourseListDataWithSubID:6 completion:^(SafeSleepRequestModel * _Nonnull requestModel) {
//                [self.mj_header endRefreshing];
//            }];
//            [SafeSleepRequestModel getCourseListDataWithSubID:18 completion:^(SafeSleepRequestModel * _Nonnull requestModel) {
//                [self.mj_header endRefreshing];
//            }];
        }];
        [self.mj_header beginRefreshing];
    }
    return self;
}

- (DSDataSource *)homeDataSource {
    if (!_homeDataSource) {
        CellConfigureBlock cellBlock = ^(SafeSleepCell * cell, SafeSleepModel * model, NSIndexPath * indexPath) {
            [cell configureCellWithModel:model indexPath:indexPath];
        };
        NSString * const safeSleepCellID = @"safeSleepCellID";
        _homeDataSource = [[DSDataSource alloc] initWithIdentifier:safeSleepCellID datas:@[] isSection:NO configureBlock:cellBlock];
        self.dataSource = _homeDataSource;
        self.delegate = self;
        [self registerClass:[SafeSleepCell class] forCellReuseIdentifier:safeSleepCellID];
        self.tableHeaderView = self.headerView;
    }
    return _homeDataSource;
}

- (HomeHeaderView *)headerView {
    if (!_headerView) {
        // banner高度
        CGFloat bannerH = 2*(kScreenWidth - 48)/5.0;
        // 三分钟即刻入睡按钮宽度
        CGFloat width = (kScreenWidth - 2*15 - 14)/2;
        CGFloat height = 15 + bannerH + 24 + 133*width/165.0;
        _headerView = [[HomeHeaderView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, height)];
        _headerView.dk_backgroundColorPicker = DKColorPickerWithKey(VCViewBG);
    }
    return _headerView;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat height = 300;
    if (indexPath.row == 0) {
        height = 206;
    } else if (indexPath.row == 1) {
        height = 196;
    }
    return height;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.001;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 0.001)];
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 0.001)];
}

@end