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

#import "CommunityView.h"
#import "OfficialMessageCell.h"
#import "InformCancelAlertView.h"

@interface CommunityView () <UITableViewDelegate, UITableViewDataSource, InformCancelAlertViewDelegate>
@property (nonatomic, strong) UIView *headView;
@property (nonatomic, strong) UITableView *listView;
@property (nonatomic, strong) ComListViewModel *comListVM;
@property (nonatomic, strong) InformCancelAlertView *informCancelView;
@end

@implementation CommunityView

#pragma mark - 初始化
- (instancetype)initWithDelegate:(id<CommunityViewDelegate>)delegate comListViewModel:(ComListViewModel *)comListVM {
    if (self = [super init]) {
        self.delegate = delegate;
        self.comListVM = comListVM;
        
        self.dk_backgroundColorPicker = DKColorPickerWithKey(VCViewBG);
        [self addSubview:self.listView];
        
        UIButton *publishBtn = [UIButton new];
        [publishBtn setImage:[UIImage imageNamed:@"ic_fabu"] forState:UIControlStateNormal];
        publishBtn.imageView.dk_alphaPicker = DKAlphaPickerWithAlphas(1.0, .5, 1);
        [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 {
    [self endRefreshing:loadMore];
    [self.listView reloadData];
}

- (void)endRefreshing:(BOOL)loadMore {
    if (loadMore) {
        [self.listView.mj_footer endRefreshing];
    } else {
        [self.listView.mj_header endRefreshing];
        [self.listView.mj_footer resetNoMoreData];
    }
}

- (void)endRefreshingWithNoMoreData {
    [self.listView.mj_footer endRefreshingWithNoMoreData];
}

- (void)showInformCancelView {
    [self.informCancelView display];
}

- (void)updateListView {
    [self.listView reloadData];
}

- (void)notiRefresh {
    [self.listView.mj_header beginRefreshing];
}

#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.comListVM.listArr.count;
}

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

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.comListVM.isOfficial && indexPath.section == 0) {
        OfficialMessageModel *model = self.comListVM.listArr[0];
        return [model cellHeight];
    } else {
        ComDynModel *model = self.comListVM.listArr[indexPath.section];
        return [model cellHeight];
    }
}

- (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.comListVM.listArr.count - 1 ? (24 + Bottom_SafeArea_Height) : 12;;
}

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WS(weakSelf);
    if (self.comListVM.isOfficial && indexPath.section == 0) {
        OfficialMessageCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([OfficialMessageCell class]) forIndexPath:indexPath];
        OfficialMessageModel *model = self.comListVM.listArr[0];
        cell.model = model;
        cell.showAllContentBlock = ^{
            [weakSelf.comListVM showOfficialAllContent];
            [weakSelf.listView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];
            NSIndexPath *scrollIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
            [weakSelf.listView scrollToRowAtIndexPath:scrollIndexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
        };
        return cell;
    } else {
        ComDynamicCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ComDynamicCell class])];
        if (!cell) {
            cell = [[ComDynamicCell alloc] initWithCellType:DynModelTypeCom style:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([ComDynamicCell class])];
        }
        ComDynModel *model = self.comListVM.listArr[indexPath.section];
        cell.model = model;
        __weak ComDynamicCell * weakCell = (ComDynamicCell *)cell;
        cell.tapInformBlock = ^{
            if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(didTapInformItem:)]) {
                [weakSelf.delegate didTapInformItem:model.dynamicID];
            }
        };
        cell.tapLikeBlock = ^{
            if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(didTapLikeItem:cell:)]) {
                [weakSelf.delegate didTapLikeItem:model cell:weakCell];
            }
        };
        cell.tapRemarkBlock = ^{
            [weakSelf tableView:tableView didSelectRowAtIndexPath:indexPath];
        };
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.comListVM.isOfficial && indexPath.section == 0) { return; }
    ComDynModel *model = self.comListVM.listArr[indexPath.section];
    if (self.delegate && [self.delegate respondsToSelector:@selector(didSelectItemWithModel:)]) {
        [self.delegate didSelectItemWithModel:model];
    }
}

#pragma mark - InformCancelAlertViewDelegate
- (void)tapInformBtn {
    if (self.delegate && [self.delegate respondsToSelector:@selector(jumpToReportPage)]) {
        [self.delegate jumpToReportPage];
    }
}

#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.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        [_listView registerClass:[OfficialMessageCell class] forCellReuseIdentifier:NSStringFromClass([OfficialMessageCell class])];
        _listView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, CGFLOAT_MIN)];;

        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" nightImgName:@"dk_btn_shequ_shuimianwenzhang" tag:1];
        [self plateWithFrame:CGRectMake(articleW + 12, articleIV.y, evaluationW, articleH) title:@"#人气测评" desc:@"玩转趣味测试" imgName:@"btn_shequ_ceping" nightImgName:@"dk_btn_shequ_ceping" tag:2];
    }
    return _headView;
}

- (InformCancelAlertView *)informCancelView {
    if (!_informCancelView) {
        _informCancelView = [[InformCancelAlertView alloc] initWithDelegate:self];
    }
    return _informCancelView;
}

- (UIImageView *)plateWithFrame:(CGRect)frame title:(NSString *)title desc:(NSString *)desc imgName:(NSString *)imgName nightImgName:(NSString *)nightImgName tag:(NSInteger)tag {
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:frame];
    imgView.dk_imagePicker = DKImagePickerWithNames(imgName, nightImgName, 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