MyDynamicView.m 6.4 KB
//
//  MyDynamicView.m
//  DreamSleep
//
//  Created by peter on 2022/10/19.
//

#import "MyDynamicView.h"

@interface MyDynamicView () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *listView;
@property (nonatomic, strong) PersonDynViewModel *myDynVM;
@property (nonatomic, strong) UIView *deleteView;
@property (nonatomic, strong) UIButton *allSelectBtn;
@end

@implementation MyDynamicView

#pragma mark - 初始化
- (instancetype)initWithDelegate:(id<MyDynamicViewDelegate>)delegate myDynViewModel:(PersonDynViewModel *)myDynViewModel {
    if (self = [super init]) {
        self.delegate = delegate;
        self.myDynVM = myDynViewModel;
        
        self.dk_backgroundColorPicker = DKColorPickerWithKey(VCViewBG);
        [self addSubview:self.listView];
        //        [self addSubview:self.deleteView];
    }
    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)updateListView {
    [self.listView reloadData];
}

#pragma mark - private
- (void)allSelectAction {
    
}

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

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

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    ComDynModel *model = self.myDynVM.listArr[indexPath.section];
    return [model cellHeight];
}

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

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

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

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WS(weakSelf);
    ComDynamicCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ComDynamicCell class])];
    if (!cell) {
        cell = [[ComDynamicCell alloc] initWithCellType:DynModelTypeMyDyn style:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([ComDynamicCell class])];
    }
    ComDynModel *model = self.myDynVM.listArr[indexPath.section];
    cell.model = model;
    __weak ComDynamicCell * weakCell = (ComDynamicCell *)cell;
    cell.tapLikeBlock = ^{
        if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(didTapLikeItem:cell:)]) {
            [weakSelf.delegate didTapLikeItem:model cell:weakCell];
        }
    };
    cell.tapRemarkBlock = ^{
        [weakSelf tableView:tableView didSelectRowAtIndexPath:indexPath];
    };
    cell.tapSelectBlock = ^(BOOL selected) {
        [weakSelf.myDynVM updateMyDynamicModel:model selected:selected];
    };
    return cell;
}

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

#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.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        
        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 *)deleteView {
    if (!_deleteView) {
        _deleteView = [UIView new];
        _deleteView.dk_backgroundColorPicker = DKColorPickerWithKey(TabBarBG);
        
        UIButton *allSelectBtn = [UIButton btnWithTitle:@"全选" font:SysFont(15)];
        [allSelectBtn dk_setTitleColorPicker:DKColorPickerWithKey(Dk_TITLE) forState:UIControlStateNormal];
        [allSelectBtn addTarget:self action:@selector(allSelectAction) forControlEvents:UIControlEventTouchUpInside];
        [allSelectBtn dk_setImage:DKImagePickerWithNames(@"ic_person_xuanze", @"dk_ic_person_xuanze", @"ic_person_xuanze") forState:UIControlStateNormal];
        [allSelectBtn dk_setImage:DKImagePickerWithNames(@"ic_person_xuanzhong", @"dk_ic_person_xuanzhong", @"ic_person_xuanzhong") forState:UIControlStateSelected];
        allSelectBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 7);
        [_deleteView addSubview:allSelectBtn];
        self.allSelectBtn = allSelectBtn;
        
        UIButton *deleteBtn = [UIButton btnWithTitle:@"删除" font:BoldFont(18)];
        [deleteBtn dk_setTitleColorPicker:DKColorPickerWithKey(Sub_Navi_TITLE) forState:UIControlStateNormal];
        deleteBtn.dk_backgroundColorPicker = DKColorPickerWithKey(BrandBG);
        [_deleteView addSubview:deleteBtn];
        
        [allSelectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            
        }];
    }
    return _deleteView;
}

@end