Files
yinmeng-ios/xplan-ios/Main/Monents/View/XPMoentsTopicListViewController.m
2022-08-26 21:45:18 +08:00

166 lines
5.0 KiB
Objective-C

//
// XPMoentsTopicListViewController.m
// xplan-ios
//
// Created by 冯硕 on 2022/8/18.
//
#import "XPMoentsTopicListViewController.h"
///Third
#import <Masonry/Masonry.h>
#import <MJRefresh/MJRefresh.h>
///Tool
#import "ThemeColor.h"
///Model
#import "MonentsTopicModel.h"
///View
#import "XPMonentTopicContainerViewController.h"
///P
#import "XPMonentsTopicListPresenter.h"
#import "XPMonentsTopicListProtocol.h"
@interface XPMoentsTopicListViewController ()<UITableViewDelegate, UITableViewDataSource, XPMonentsTopicListProtocol>
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *datasource;
@property (nonatomic,assign) int page;
@end
@implementation XPMoentsTopicListViewController
- (__kindof id)createPresenter {
return [[XPMonentsTopicListPresenter alloc] init];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self initHeaderAndFooterRrfresh];
[self initSubViews];
[self initSubViewConstraints];
}
- (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 = [ThemeColor secondTextColor];
header.lastUpdatedTimeLabel.textColor = [ThemeColor secondTextColor];
self.tableView.mj_header = header;
MJRefreshBackNormalFooter *footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefresh)];
footer.stateLabel.textColor = [ThemeColor secondTextColor];
footer.stateLabel.font = [UIFont systemFontOfSize:10.0];
self.tableView.mj_footer = footer;
[self headerRefresh];
}
- (void)headerRefresh {
self.page = 1;
[self.presenter getMoentsTopicList:self.page pageSize:20 state:0];
}
- (void)footerRefresh {
self.page++;
[self.presenter getMoentsTopicList:self.page pageSize:20 state:1];
}
#pragma mark - Private Method
- (void)initSubViews {
self.title = @"更多话题";
[self.view addSubview:self.tableView];
}
- (void)initSubViewConstraints {
[self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.mas_equalTo(self.view);
}];
}
#pragma mark - UITableViewDelegate And UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.datasource.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 50;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class])];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([UITableViewCell class])];
}
MonentsTopicModel * topicModel = [self.datasource objectAtIndex:indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:@"#%@", topicModel.name];
cell.textLabel.textColor = [ThemeColor mainTextColor];
cell.textLabel.font = [UIFont systemFontOfSize:13];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if (self.datasource.count > 0) {
MonentsTopicModel * topicModel = [self.datasource objectAtIndex:indexPath.row];
XPMonentTopicContainerViewController * topicVC = [[XPMonentTopicContainerViewController alloc] init];
topicVC.worldId = topicModel.tId;
[self.navigationController pushViewController:topicVC animated:YES];
}
}
#pragma mark - XPMonentsTopicListProtocol
- (void)getMonentsTopicListSuccess:(NSArray *)list state:(int)state {
if (state == 0) {
[self.tableView.mj_header endRefreshing];
[self.datasource removeAllObjects];
} else {
if (list.count <= 0) {
[self showErrorToast:@"没有更多数据了"];
}
[self.tableView.mj_footer endRefreshing];
}
if (list.count > 0) {
[self.datasource addObjectsFromArray:list];
}
[self.tableView reloadData];
}
- (void)getMoentsTopicFail:(NSString *)message state:(int)state {
if (state == 0) {
[self.tableView.mj_header endRefreshing];
} else {
[self.tableView.mj_footer endRefreshing];
}
}
#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 = UITableViewCellSeparatorStyleSingleLine;
_tableView.backgroundColor = [UIColor clearColor];
if (@available(iOS 11.0, *)) {
_tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
}
[_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:NSStringFromClass([UITableViewCell class])];
}
return _tableView;
}
- (NSMutableArray *)datasource {
if (!_datasource) {
_datasource = [NSMutableArray array];
}
return _datasource;
}
@end