// // XPFindNewFriendViewController.m // xplan-ios // // Created by XY on 2023/3/6. // #import "XPFindNewFriendViewController.h" ///Third #import #import ///Tool #import "NSArray+Safe.h" #import "ThemeColor.h" ///View #import "XPFindNewFriendTableViewCell.h" #import "XPHomeListEmptyTableViewCell.h" #import "XPHomeBannerTableViewCell.h" ///VC #import "XPWebViewController.h" #import "XPRoomViewController.h" #import "SessionViewController.h" ///Model #import "HomeRecommendRoomModel.h" ///P #import "XPHomePresenter.h" #import "XPHomeProtocol.h" @interface XPFindNewFriendViewController () ///列表 @property (nonatomic,strong) UITableView *tableView; @property (nonatomic, copy) void(^scrollCallback)(UIScrollView *scrollView); /// 数据源 @property (nonatomic, strong) NSMutableArray *dataSource; ///当前的页数 @property (nonatomic,assign) int page; /// 没有新的数据了 @property (nonatomic,assign) BOOL hasNoMoreData; @property (nonatomic,assign) NSString *tabId; @end @implementation XPFindNewFriendViewController - (XPHomePresenter *)createPresenter { return [[XPHomePresenter alloc] init]; } - (BOOL)isHiddenNavBar { return YES; } - (void)viewDidLoad { [super viewDidLoad]; [self initSubViews]; [self initSubViewConstraints]; self.tabId = @"3"; [self initHeaderAndFooterRrfresh]; } /// 刷新数据,可见时才调用 - (void)refreshData { if (self.isViewLoaded && self.view.window) { [self headerRefresh]; } } - (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 getRecommendRoomList:self.tabId page:self.page pageSize:20 state:0]; } - (void)footerRefresh { if (self.hasNoMoreData) { [self showErrorToast:@"没有更多房间了"]; [self.tableView.mj_footer endRefreshing]; return; } self.page++; [self.presenter getRecommendRoomList:self.tabId page:self.page pageSize:20 state:1]; } #pragma mark - Private Method - (void)initSubViews { self.view.backgroundColor = [UIColor clearColor]; [self.view addSubview:self.tableView]; } - (void)initSubViewConstraints { [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.mas_equalTo(self.view); }]; } #pragma mark - XPFindNewFriendTableViewCellDelegate #pragma mark - XPHomeProtocol - (void)getHomeRecommendRoomListSuccess:(NSArray *)list state:(BOOL)state { if (state == 0) { self.hasNoMoreData = NO; [self.dataSource removeAllObjects]; [self.tableView.mj_header endRefreshing]; } else { [self.tableView.mj_footer endRefreshing]; } if (list.count > 0) { self.hasNoMoreData = NO; [self.dataSource addObjectsFromArray:list]; } else { self.hasNoMoreData = YES; } [self.tableView reloadData]; } - (void)getHomeRecommendRoomListFail:(NSString *)message state:(BOOL)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; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { if (self.dataSource.count > 0) { return 105; } return 300; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if (self.dataSource.count > 0) { HomeRecommendRoomModel * model = [self.dataSource safeObjectAtIndex1:indexPath.row]; XPFindNewFriendTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([XPFindNewFriendTableViewCell class])]; cell.roomInfo = model; cell.delegate = self; return cell; } XPHomeListEmptyTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([XPHomeListEmptyTableViewCell class])]; if (cell == nil) { cell = [[XPHomeListEmptyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([XPHomeListEmptyTableViewCell class])]; } return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:YES]; if (self.dataSource.count > 0) { HomeRecommendRoomModel * model = [self.dataSource safeObjectAtIndex1:indexPath.row]; if (model.roomUid.integerValue > 0) { [XPRoomViewController openRoom:model.uid fromNick:model.title fromType:UserEnterRoomFromType_Home_Recommend fromUid:nil viewController:self]; } /// 跳聊天 // NSString * sessionId = [NSString stringWithFormat:@"%@",model.uid]; // NIMSession * session = [NIMSession session:sessionId type:NIMSessionTypeP2P]; // SessionViewController * sessionVC = [[SessionViewController alloc] initWithSession:session]; // [self.navigationController pushViewController:sessionVC animated:YES]; } } #pragma mark - JXPagingViewListViewDelegate - (UIView *)listView { return self.view; } - (UIScrollView *)listScrollView { return self.tableView; } - (void)listViewDidScrollCallback:(void (^)(UIScrollView *))callback { self.scrollCallback = callback; } - (void)scrollViewDidScroll:(UIScrollView *)scrollView { self.scrollCallback(scrollView); } #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:[XPFindNewFriendTableViewCell class] forCellReuseIdentifier:NSStringFromClass([XPFindNewFriendTableViewCell class])]; [_tableView registerClass:[XPHomeListEmptyTableViewCell class] forCellReuseIdentifier:NSStringFromClass([XPHomeListEmptyTableViewCell class])]; } return _tableView; } - (NSMutableArray *)dataSource { if (!_dataSource) { _dataSource = [NSMutableArray array]; } return _dataSource; } @end