SleepStoryView.m 3.1 KB
//
//  SleepStoryView.m
//  DreamSleep
//
//  Created by peter on 2022/10/21.
//

#import "SleepStoryView.h"

@interface SleepStoryView () <UITableViewDelegate>
@property (nonatomic, strong) UITableView *storyListView;
@end

@implementation SleepStoryView

- (instancetype)initWithDelegate:(id<SleepStoryViewDelegate>)delegate {
    if (self = [super init]) {
        self.delegate = delegate;
        [self.dataSource addDataArray:@[]];
        self.dk_backgroundColorPicker = DKColorPickerWithKey(VCViewBG);
    }
    return self;
}

#pragma mark - public
- (void)updateList:(NSArray *)dataList {
    if (dataList && dataList.count) {
        [self.dataSource addDataArray:dataList];
        [self.storyListView reloadData];
    }
    [self.storyListView.mj_header endRefreshing];
}

- (void)endRefresh {
    [self.storyListView.mj_header endRefreshing];
}

- (void)reRefresh {
    [self.storyListView.mj_header beginRefreshing];
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 105;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    SubAudioModel *model = self.dataSource.dataArray[indexPath.row];
    if (self.delegate && [self.delegate respondsToSelector:@selector(didSelectStoryItem:)]) {
        [self.delegate didSelectStoryItem:model];
    }
}

#pragma mark - lazy
- (DSDataSource *)dataSource {
    if (!_dataSource) {
        CellConfigureBlock cellBlock = ^(SleepStoryCell * cell, SubAudioModel * model, NSIndexPath * indexPath) {
            cell.storyModel = model;
        };
        _dataSource = [[DSDataSource alloc] initWithIdentifier:NSStringFromClass([SleepStoryCell class]) datas:@[] isSection:NO configureBlock:cellBlock];
        
        UITableView *storyListView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        storyListView.separatorStyle = UITableViewCellSeparatorStyleNone;
        storyListView.delegate = self;
        storyListView.dataSource = _dataSource;
        storyListView.showsVerticalScrollIndicator = NO;
        storyListView.backgroundColor = DSClearColor;
        storyListView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        [storyListView registerClass:[SleepStoryCell class] forCellReuseIdentifier:NSStringFromClass([SleepStoryCell class])];
        storyListView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 24 + Bottom_SafeArea_Height)];
        [self addSubview:storyListView];
        self.storyListView = storyListView;
        
        [self.storyListView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
        
        WS(weakSelf);
        storyListView.mj_header = [DSGifHeader headerWithRefreshingBlock:^{
            if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(getSleepStoryListData)]) {
                [weakSelf.delegate getSleepStoryListData];
            }
        }];
        [storyListView.mj_header beginRefreshing];
    }
    return _dataSource;
}

@end