DSHomeView.m 5.2 KB
//
//  DSHomeView.m
//  DreamSleep
//
//  Created by peter on 2022/5/11.
//

#import "DSHomeView.h"
#import "CourseMusicCell.h"
#import "GoodSleepSoundCell.h"

@interface DSHomeView () <UITableViewDelegate, UITableViewDataSource>
/// 通用数据
@property (nonatomic, strong) NSArray *courseMusicArr;
/// 舒眠课程数据
@property (nonatomic, strong) NSArray *courseListArr;
/// 助眠音乐数据
@property (nonatomic, strong) NSArray *helpListArr;
/// 类型数据
@property (nonatomic, strong) NSArray *typeArr;
@end

@implementation DSHomeView

- (instancetype)init {
    if (self = [super initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - kTopHeight(0) - kTabBarHeight) style:UITableViewStylePlain]) {
        self.dk_backgroundColorPicker = DKColorPickerWithKey(VCViewBG);
        self.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.showsVerticalScrollIndicator = NO;
        self.delegate = self;
        self.dataSource = self;
        self.tableHeaderView = self.headerView;
    }
    return self;
}

- (void)refreshBarnner:(NSArray *)bannerListData {
    [self.headerView updateBannerWithListData:bannerListData];
}

- (void)updateCourseMusicCell:(CellType)type data:(NSArray *)dataArr {
    if (dataArr && dataArr.count > 0) {
        if (type == CellTypeCourse) {
            self.courseListArr = dataArr;
        } else if (type == CellTypeMusic) {
            self.helpListArr = dataArr;
        }
        [self reloadData];
    }
}

- (void)updateNoiseAllTypeData:(NSArray *)typeDataArr {
    if (typeDataArr && typeDataArr.count > 0) {
        self.typeArr = typeDataArr;
        [self reloadData];
    }
}

#pragma mark - UITableViewDelegate && UITableViewDataSource
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    NSString *cellID = [NSString stringWithFormat:@"cmg_%zd", indexPath.row];
    if (indexPath.row == 2) {
        GoodSleepSoundCell *goodCell = [tableView dequeueReusableCellWithIdentifier:cellID];
        if (!goodCell) {
            goodCell = [[GoodSleepSoundCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        }
        [goodCell configBasicData:self.courseMusicArr[indexPath.row] cellIndex:indexPath.row];
        [goodCell updateNoiseAllTypeData:self.typeArr];
        return goodCell;
    } else {
        CourseMusicCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        if (!cell) {
            cell = [[CourseMusicCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        }
        [cell configBasicData:self.courseMusicArr[indexPath.row] cellIndex:indexPath.row];
        [cell updateCourseMusicListWithData:(indexPath.row == 0 ? self.courseListArr : self.helpListArr) cellIndex:indexPath.row];
        return cell;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.courseMusicArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat height = 0;
    if (indexPath.row == 0) {
        height = 228;
    } else if (indexPath.row == 1) {
        height = 228;
    }  else if (indexPath.row == 2) {
        height = 68 + 40 + 327 + 70;
    }
    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)];
}

- (NSString *)cellIdentiferAtIndexPath:(NSIndexPath *)indexPath {
    SafeSleepModel *item = self.courseMusicArr[indexPath.row];
    if (item.type == CellTypeGood) {
        return NSStringFromClass([GoodSleepSoundCell class]);
    } else {
        return NSStringFromClass([CourseMusicCell class]);
    }
}

#pragma mark - lazy
- (HomeHeaderView *)headerView {
    if (!_headerView) {
        // banner高度
        CGFloat bannerH = 2*(kScreenWidth - 48)/5.0;
        // 哄睡按钮宽度
        CGFloat width = kScreenWidth - 2*15 - 12;
        CGFloat height = 15 + bannerH + 24 + (170*.3*width/104.0);
        _headerView = [[HomeHeaderView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, height)];
        _headerView.dk_backgroundColorPicker = DKColorPickerWithKey(VCViewBG);
    }
    return _headerView;
}

- (NSArray *)courseMusicArr {
    if (!_courseMusicArr) {
        _courseMusicArr = [SafeSleepModel getDatas];
    }
    return _courseMusicArr;
}

- (NSArray *)courseListArr {
    if (!_courseListArr) {
        _courseListArr = [NSArray array];
    }
    return _courseListArr;
}

- (NSArray *)helpListArr {
    if (!_helpListArr) {
        _helpListArr = [NSArray array];
    }
    return _helpListArr;
}

- (NSArray *)typeArr {
    if (!_typeArr) {
        _typeArr = [NSArray array];
    }
    return _typeArr;
}

@end