CommunityView.m 7.0 KB
//
//  CommunityView.m
//  DreamSleep
//
//  Created by peter on 2022/9/20.
//

#import "CommunityView.h"

@interface CommunityView () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UIView *headView;
@property (nonatomic, strong) UITableView *listView;
@property (nonatomic, strong) NSArray *listArr;
@end

@implementation CommunityView

#pragma mark - 初始化
- (instancetype)initWithDelegate:(id<CommunityViewDelegate>)delegate {
    if (self = [super init]) {
        self.delegate = delegate;
        self.listArr = @[@"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", @"10"];
        self.dk_backgroundColorPicker = DKColorPickerWithKey(VCViewBG);
        [self addSubview:self.listView];
        
        UIButton *publishBtn = [UIButton new];
        [publishBtn setImage:[UIImage imageNamed:@"ic_fabu"] forState:UIControlStateNormal];
        [publishBtn addTarget:self action:@selector(publishAction) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:publishBtn];
        [publishBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self).offset(-15);
            make.bottom.equalTo(self).offset(-45);
        }];
    }
    return self;
}

- (void)layoutSubviews {
    self.listView.frame = CGRectMake(15, 0, self.width - 30, self.height);
}

#pragma mark - public
- (void)updateCommunityMoments:(BOOL)loadMore {
    if (loadMore) {
        self.listArr = @[@"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", @"10", @"8", @"9", @"10"];
        [self.listView.mj_footer endRefreshing];
    } else {
        [self.listView.mj_header endRefreshing];
    }
    [self.listView reloadData];
}

#pragma mark - Action
- (void)tapAction:(UITapGestureRecognizer *)tapGR {
    if (self.delegate && [self.delegate respondsToSelector:@selector(tapCommunityHeaderModule:)]) {
        [self.delegate tapCommunityHeaderModule:tapGR.view.tag];
    }
}

- (void)publishAction {
    if (self.delegate && [self.delegate respondsToSelector:@selector(publishLogicDeal)]) {
        [self.delegate publishLogicDeal];
    }
}

#pragma mark - UITableViewDelegate, UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.listArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 150;
}

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

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return section == self.listArr.count - 1 ? 6.5 : 12;;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class])];
    [cell cornerRadius:24.0];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.dk_backgroundColorPicker = DKColorPickerWithColors(DSWhite, CornerViewDarkColor, DSWhite);
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.delegate && [self.delegate respondsToSelector:@selector(didSelectItem:)]) {
        [self.delegate didSelectItem:indexPath];
    }
}

#pragma mark - lazy
- (UITableView *)listView {
    if (!_listView) {
        _listView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _listView.delegate = self;
        _listView.dataSource = self;
        _listView.backgroundColor = DSClearColor;
        _listView.showsVerticalScrollIndicator = NO;
        _listView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _listView.tableHeaderView = self.headView;
        [_listView registerClass:[UITableViewCell class] forCellReuseIdentifier:NSStringFromClass([UITableViewCell class])];
        
        WS(weakSelf);
        _listView.mj_header = [DSGifHeader headerWithRefreshingBlock:^{
            if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(getDynamicListRequest:)]) {
                [weakSelf.delegate getDynamicListRequest:NO];
            }
        }];
        [_listView.mj_header beginRefreshing];
        
        _listView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(getDynamicListRequest:)]) {
                [weakSelf.delegate getDynamicListRequest:YES];
            }
        }];
    }
    return _listView;
}

- (UIView *)headView {
    if (!_headView) {
        CGFloat aroundMargin = 15;
        CGFloat middleSpace = 12;
        CGFloat articleScale = 146.0 / (kBaseW - 2*aroundMargin - middleSpace);
        CGFloat totalW = kScreenWidth - 2*aroundMargin - middleSpace;
        CGFloat articleW = articleScale * totalW;
        CGFloat evaluationW = totalW * (1 - articleScale);
        CGFloat articleH = 80 * articleW / 146;
        
        _headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.listView.width, articleH + 2*aroundMargin)];
        _headView.backgroundColor = DSClearColor;
        
        UIImageView *articleIV = [self plateWithFrame:CGRectMake(0, 15, articleW, articleH) title:@"#睡眠文章" desc:@"精选海量好文" imgName:@"btn_shequ_shuimianwenzhang" tag:1];
        [self plateWithFrame:CGRectMake(articleW + 12, articleIV.y, evaluationW, articleH) title:@"#人气测评" desc:@"玩转趣味测试" imgName:@"btn_shequ_ceping" tag:2];
    }
    return _headView;
}

- (UIImageView *)plateWithFrame:(CGRect)frame title:(NSString *)title desc:(NSString *)desc imgName:(NSString *)imgName tag:(NSInteger)tag {
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:frame];
    [imgView dk_setAlphaPicker:DKAlphaPickerWithAlphas(1.0, .5, .5)];
    imgView.image = [UIImage imageNamed:imgName];
    imgView.tag = tag;
    imgView.userInteractionEnabled = YES;
    [self.headView addSubview:imgView];
    
    UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [imgView addGestureRecognizer:tapGR];
    
    UILabel *titleLab = [UILabel labWithText:title textColor:DSWhite font:BoldFont(16)];
    [imgView addSubview:titleLab];
    UILabel *descLab = [UILabel labWithText:desc textColor:ColorFromHexA(0xFFFFFF, .5) font:SysFont(14)];
    [imgView addSubview:descLab];
    
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(imgView).offset(15);
        make.height.equalTo(@22);
    }];
    [descLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(titleLab);
        make.bottom.equalTo(imgView).offset(-15);
    }];
    return imgView;
}

@end