Files
yinmeng-ios/xplan-ios/Main/Mine/View/XPMineVisitorViewController.m
2022-03-11 18:01:36 +08:00

198 lines
7.0 KiB
Objective-C

//
// XPMineVisitorViewController.m
// xplan-ios
//
// Created by GreenLand on 2022/1/26.
//
#import "XPMineVisitorViewController.h"
///Third
#import <Masonry/Masonry.h>
#import <MJRefresh/MJRefresh.h>
///Tool
#import "ThemeColor.h"
///Model
#import "XPMineVisitorItemModel.h"
///View
#import "XPMineVisitorTableViewCell.h"
#import "XPMineVisitorEmptyTableViewCell.h"
///P
#import "XPMineVisitorPresenter.h"
#import "XPMineVisitorProtocol.h"
///VC
#import "XPMineUserInfoViewController.h"
#import "SessionViewController.h"
@interface XPMineVisitorViewController ()<UITableViewDelegate,UITableViewDataSource, XPMineVisitorProtocol, XPMineVisitorTableViewCellDelegate>
///列表
@property (nonatomic,strong) UITableView *tableView;
///数据源
@property (nonatomic,strong) NSMutableArray *datasource;
///当前页数
@property (nonatomic,assign) int page;
///更多数据
@property (nonatomic,assign) BOOL hasNoMoreData;
@end
@implementation XPMineVisitorViewController
- (XPMineVisitorPresenter *)createPresenter {
return [[XPMineVisitorPresenter alloc] init];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self initHeaderAndFooterRrfresh];
[self initSubViews];
[self initSubViewConstraints];
}
#pragma mark - 下拉刷新
- (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];
}
#pragma mark - 刷新的fangfa
- (void)headerRefresh {
self.page = 1;
[self.presenter getVisitorList:self.page pageSize:20 state:0];
}
- (void)footerRefresh {
if (self.hasNoMoreData) {
[self showErrorToast:@"没有更多访客记录了"];
return;
}
self.page++;
[self.presenter getVisitorList: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 - XPMineVisitorProtocol
- (void)onGetVisitorListSuccess:(NSArray *)array state:(int)state {
if (state == 0) {
[self.datasource removeAllObjects];
[self.tableView.mj_header endRefreshing];
}
[self.tableView.mj_footer endRefreshing];
if (array.count > 0) {
self.hasNoMoreData = NO;
[self.datasource addObjectsFromArray:array];
} else {
self.hasNoMoreData = YES;
[self.tableView.mj_footer endRefreshingWithNoMoreData];
}
[self.tableView reloadData];
}
- (void)getVisitorListFail:(int)state {
if (state == 0) {
[self.tableView.mj_header endRefreshing];
} else {
[self.tableView.mj_footer endRefreshing];
}
}
#pragma mark - UITableViewDelegate And UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.datasource.count > 0 ? self.datasource.count : 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (self.datasource.count > 0) {
XPMineVisitorTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([XPMineVisitorTableViewCell class])];
if (cell == nil) {
cell = [[XPMineVisitorTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([XPMineVisitorTableViewCell class])];
}
cell.isFirstItem = indexPath.row == 0;
cell.isLastItem = indexPath.row == self.datasource.count - 1;
cell.delegate = self;
cell.item = [self.datasource objectAtIndex:indexPath.row];
return cell;
}
XPMineVisitorEmptyTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([XPMineVisitorEmptyTableViewCell class])];
if (cell == nil) {
cell = [[XPMineVisitorEmptyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([XPMineVisitorEmptyTableViewCell class])];
}
cell.emptyTitle = @"暂无访客记录";
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) {
return 85;
} else {
return 70;
}
}
#pragma mark - TTVisitorRecordCellDelegate
- (void)onAvatarClick:(XPMineVisitorTableViewCell *)cell {
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
XPMineVisitorItemModel *item = [self.datasource objectAtIndex:indexPath.row];
XPMineUserInfoViewController * userInfoVC = [[XPMineUserInfoViewController alloc] init];
userInfoVC.uid = item.uid;
[self.navigationController pushViewController:userInfoVC animated:YES];
}
- (void)onChatClick:(XPMineVisitorTableViewCell *)cell {
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
XPMineVisitorItemModel *item = [self.datasource objectAtIndex:indexPath.row];
NSString * sessionId = [NSString stringWithFormat:@"%lld",item.uid];
NIMSession * session = [NIMSession session:sessionId type:NIMSessionTypeP2P];
SessionViewController * sessionVC = [[SessionViewController alloc] initWithSession:session];
[self.navigationController pushViewController:sessionVC animated:YES];
}
#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];
_tableView.rowHeight = 65;
if (@available(iOS 11.0, *)) {
_tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
}
[_tableView registerClass:[XPMineVisitorEmptyTableViewCell class] forCellReuseIdentifier:NSStringFromClass([XPMineVisitorEmptyTableViewCell class])];
[_tableView registerClass:[XPMineVisitorTableViewCell class] forCellReuseIdentifier:NSStringFromClass([XPMineVisitorTableViewCell class])];
}
return _tableView;
}
- (NSMutableArray *)datasource {
if (!_datasource) {
_datasource = [NSMutableArray array];
}
return _datasource;
}
@end