Files
peko-ios/YuMi/Modules/YMMonents/View/SubViews/XPMoentsTopicListView.m
2024-07-17 17:49:33 +08:00

264 lines
8.3 KiB
Objective-C

//
// YMMoentsTopicListView.m
// YUMI
//
// Created by YUMI on 2022/8/16.
//
#import "XPMoentsTopicListView.h"
///Third
#import <Masonry/Masonry.h>
#import <MJRefresh/MJRefresh.h>
///Tool
#import "DJDKMIMOMColor.h"
#import "Api+Moments.h"
#import "NetImageView.h"
#import "XNDJTDDLoadingTool.h"
#import "AccountInfoStorage.h"
#import "MomentsTopicModel.h"
#import "YUMIMacroUitls.h"
#import "NSArray+Safe.h"
@interface XPMoentsTopicListTableViewCell : UITableViewCell
///头像
@property (nonatomic,strong) NetImageView *avatarImageView;
///标题
@property (nonatomic,strong) UILabel *nameLabel;
///描述
@property (nonatomic,strong) UILabel *desLabel;
@property (nonatomic,strong) MomentsTopicModel *topicInfo;
@end
@implementation XPMoentsTopicListTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
[self initSubViews];
[self initSubViewConstraints];
}
return self;
}
#pragma mark - Private Method
- (void)initSubViews {
self.backgroundColor = UIColor.clearColor;
self.selectionStyle = UITableViewCellSelectionStyleNone;
[self.contentView addSubview:self.avatarImageView];
[self.contentView addSubview:self.nameLabel];
[self.contentView addSubview:self.desLabel];
}
- (void)initSubViewConstraints {
[self.avatarImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.leading.mas_equalTo(10);
make.centerY.mas_equalTo(self.contentView);
make.width.mas_equalTo(100);
make.height.mas_equalTo(60);
}];
[self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.mas_equalTo(self.avatarImageView.mas_centerY).offset(-2);
make.leading.mas_equalTo(self.avatarImageView.mas_trailing).offset(10);
make.trailing.mas_equalTo(-20);
}];
[self.desLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.avatarImageView.mas_centerY).offset(2);
make.leading.trailing.mas_equalTo(self.nameLabel);
}];
}
#pragma mark - Getters And Setters
- (void)setTopicInfo:(MomentsTopicModel *)topicInfo {
_topicInfo= topicInfo;
if (_topicInfo) {
self.avatarImageView.imageUrl = _topicInfo.icon;
self.nameLabel.text = _topicInfo.worldName;
self.desLabel.text = _topicInfo.desc;
}
}
- (NetImageView *)avatarImageView {
if (!_avatarImageView) {
NetImageConfig * config = [[NetImageConfig alloc]init];
config.placeHolder = [UIImageConstant defaultAvatarPlaceholder];
_avatarImageView = [[NetImageView alloc] initWithConfig:config];
_avatarImageView.contentMode = UIViewContentModeScaleAspectFill;
_avatarImageView.layer.masksToBounds = YES;
_avatarImageView.layer.cornerRadius = 12;
}
return _avatarImageView;
}
- (UILabel *)nameLabel {
if (_nameLabel == nil) {
_nameLabel = [[UILabel alloc] init];
_nameLabel.font = [UIFont systemFontOfSize:15];
_nameLabel.textColor = DJDKMIMOMColor.mainTextColor;
}
return _nameLabel;
}
- (UILabel *)desLabel {
if (_desLabel == nil) {
_desLabel = [[UILabel alloc] init];
_desLabel.font = [UIFont systemFontOfSize:12];
_desLabel.textColor = DJDKMIMOMColor.secondTextColor;
_desLabel.numberOfLines = 2;
}
return _desLabel;
}
@end
@interface XPMoentsTopicListView ()<UITableViewDelegate, UITableViewDataSource>
///列表
@property (nonatomic,strong) UITableView *tableView;
///数据源
@property (nonatomic,strong) NSMutableArray *datasource;
@property (nonatomic,assign) int page;
@end
@implementation XPMoentsTopicListView
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self initHeaderAndFooterRrfresh];
[self initSubViews];
[self initSubViewConstraints];
}
return self;
}
- (void)initHeaderAndFooterRrfresh {
MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRefresh)];
header.stateLabel.font = [UIFont systemFontOfSize:10.0];
header.lastUpdatedTimeLabel.font = [UIFont systemFontOfSize:10.0];
header.stateLabel.textColor = [DJDKMIMOMColor secondTextColor];
header.lastUpdatedTimeLabel.textColor = [DJDKMIMOMColor secondTextColor];
self.tableView.mj_header = header;
MJRefreshBackNormalFooter *footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefresh)];
footer.stateLabel.textColor = [DJDKMIMOMColor secondTextColor];
footer.stateLabel.font = [UIFont systemFontOfSize:10.0];
self.tableView.mj_footer = footer;
[self headerRefresh];
}
- (void)headerRefresh {
self.page = 1;
NSString * pageStr = [NSString stringWithFormat:@"%d", self.page];
NSString * uid = [AccountInfoStorage instance].getUid;
[Api momentsTopicList:^(BaseModel * _Nullable data, NSInteger code, NSString * _Nullable msg) {
[self.tableView.mj_header endRefreshing];
if (code == 200) {
[self.datasource removeAllObjects];
NSArray * array = [MomentsTopicModel modelsWithArray:data.data];
[self.datasource addObjectsFromArray:array];
[self.tableView reloadData];
} else {
[XNDJTDDLoadingTool showErrorWithMessage:msg];
}
} uid:uid type:@"0" page:pageStr];
}
- (void)footerRefresh {
self.page++;
NSString * pageStr = [NSString stringWithFormat:@"%d", self.page];
NSString * uid = [AccountInfoStorage instance].getUid;
[Api momentsTopicList:^(BaseModel * _Nullable data, NSInteger code, NSString * _Nullable msg) {
[self.tableView.mj_footer endRefreshing];
if (code == 200) {
NSArray * array = [MomentsTopicModel modelsWithArray:data.data];
if (array.count > 0) {
[self.datasource addObjectsFromArray:array];
[self.tableView reloadData];
} else {
[XNDJTDDLoadingTool showErrorWithMessage:YMLocalizedString(@"XPMoentsTopicListView0")];
}
} else {
[XNDJTDDLoadingTool showErrorWithMessage:msg];
}
} uid:uid type:@"0" page:pageStr];
}
#pragma mark - Private Method
- (void)initSubViews {
self.backgroundColor = [UIColor whiteColor];
self.layer.masksToBounds = YES;
self.layer.cornerRadius = 10;
[self addSubview:self.tableView];
}
- (void)initSubViewConstraints {
[self mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(KScreenWidth, 450));
}];
[self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
make.leading.trailing.bottom.mas_equalTo(self);
make.top.mas_equalTo(self).offset(10);
}];
}
#pragma mark - UITableViewDelegate And UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.datasource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
XPMoentsTopicListTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([XPMoentsTopicListTableViewCell class])];
if (cell == nil) {
cell = [[XPMoentsTopicListTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([XPMoentsTopicListTableViewCell class])];
}
cell.topicInfo = [self.datasource xpSafeObjectAtIndex:indexPath.row];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 80;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if (self.datasource.count > 0) {
MomentsTopicModel * infor = [self.datasource xpSafeObjectAtIndex:indexPath.row];
if (self.delegate && [self.delegate respondsToSelector:@selector(xPMoentsTopicListView:didSelectItem:)]) {
[self.delegate xPMoentsTopicListView:self didSelectItem:infor];
}
}
}
#pragma mark - Getters And Setters
- (UITableView *)tableView {
if (!_tableView) {
_tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.tableFooterView = [UIView new];
_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
_tableView.backgroundColor = [UIColor clearColor];
if (@available(iOS 11.0, *)) {
_tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
}
[_tableView registerClass:[XPMoentsTopicListTableViewCell class] forCellReuseIdentifier:NSStringFromClass([XPMoentsTopicListTableViewCell class])];
}
return _tableView;
}
- (NSMutableArray *)datasource {
if (!_datasource) {
_datasource = [NSMutableArray array];
}
return _datasource;
}
@end