// // XPMomentListViewController.m // xplan-ios // // Created by XY on 2023/2/16. // #import "XPMomentListViewController.h" #import "XPMacro.h" #import #import #import "ThemeColor.h" #import "NSArray+Safe.h" #import "ClientConfig.h" /// Model #import "MonentsListInfoModel.h" /// View #import "XPMomentListCollectionViewCell.h" #import "XPMonentsEmptyCollectionViewCell.h" /// P #import "XPMonentsLatestPresenter.h" #import "XPMonentsLatestProtocol.h" #import "XPMonentsRecommendPresenter.h" #import "XPMonentsRecommendProtocol.h" #import "XPMonentsAttentionPresenter.h" #import "XPMonentsAttentionProtocol.h" /// VC #import "XPMonentsDetailViewController.h" #import "XPMonentsPublishViewController.h" @interface XPMomentListViewController () @property (nonatomic, strong) UICollectionView *collectionView; @property (nonatomic, copy) void(^scrollCallback)(UIScrollView *scrollView); ///数据源 @property (nonatomic,strong) NSMutableArray *datasource; ///当前的页数 @property (nonatomic,assign) NSInteger page; ///是否有更多的数据 @property (nonatomic,assign) BOOL hasNoMoreData; ///数据信息 @property (nonatomic,strong) MonentsListInfoModel *listInfo; @end @implementation XPMomentListViewController - (BOOL)isHiddenNavBar { return YES; } - (__kindof id)createPresenter { if (self.listType == XPMomentListRecommend) { return [[XPMonentsRecommendPresenter alloc] init]; }else if (self.listType == XPMomentListTypeLatest) { return [[XPMonentsLatestPresenter alloc] init]; }else{ return [[XPMonentsAttentionPresenter alloc] init]; } } - (void)viewDidLoad { [super viewDidLoad]; [self createUI]; [self initHeaderAndFooterRrfresh]; } - (void)createUI { self.view.backgroundColor = UIColor.whiteColor; [self.view addSubview:self.collectionView]; [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(0); make.left.right.mas_equalTo(0); make.bottom.mas_equalTo(0); }]; } #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.collectionView.mj_header = header; MJRefreshBackNormalFooter *footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefresh)]; footer.stateLabel.textColor = [ThemeColor secondTextColor]; footer.stateLabel.font = [UIFont systemFontOfSize:10.0]; footer.ignoredScrollViewContentInsetBottom = self.collectionView.contentInset.bottom; self.collectionView.mj_footer = footer; [self headerRefresh]; } #pragma mark - 刷新的fangfa - (void)headerRefresh { self.page = 1; if (self.listType == XPMomentListRecommend) { [self.presenter getMonentsRecommendList:self.page pageSize:20 state:0]; }else if (self.listType == XPMomentListTypeLatest) { [self.presenter getMonentsLatestListPageSize:20 dynamicId:@"" state:0]; }else{ [self.presenter getMonentsAttentionListPageSize:20 dynamicId:@"" state:0]; } } - (void)footerRefresh { if (self.hasNoMoreData) { [self showErrorToast:@"没有更多数据了"]; [self.collectionView.mj_footer endRefreshing]; return; } self.page++; if (self.listType == XPMomentListRecommend) { [self.presenter getMonentsRecommendList:self.page pageSize:20 state:1]; }else if (self.listType == XPMomentListTypeLatest) { [self.presenter getMonentsLatestListPageSize:20 dynamicId:self.listInfo.nextDynamicId state:1]; }else{ [self.presenter getMonentsAttentionListPageSize:20 dynamicId:self.listInfo.nextDynamicId state:1]; } } #pragma mark - XPMomentListCollectionViewCellDelegate - (void)xPMomentListCollectionViewCellDidClickLike:(MonentsInfoModel *)monentsInfo { [self.presenter likeMonent:monentsInfo.dynamicId status:!monentsInfo.isLike likedUid:monentsInfo.uid worldId:[NSString stringWithFormat:@"%ld", monentsInfo.worldId]]; } #pragma mark - XPMonentsLatestProtocol - (void)getMonentsLatestListSuccess:(MonentsListInfoModel *)listInfo state:(int)state{ self.listInfo = listInfo; if (state == 0) { [self.datasource removeAllObjects]; } if (listInfo.dynamicList.count > 0) { [self.datasource addObjectsFromArray:listInfo.dynamicList]; } if (state == 0) { self.hasNoMoreData = NO; [self.collectionView.mj_header endRefreshing]; } else { if (listInfo.dynamicList.count > 0) { self.hasNoMoreData = NO; } else { self.hasNoMoreData = YES; } [self.collectionView.mj_footer endRefreshing]; } [self.collectionView reloadData]; } - (void)getMonentsLatestListFail:(NSString *)msg state:(int)state { if (state == 0) { [self.collectionView.mj_header endRefreshing]; } else { [self.collectionView.mj_footer endRefreshing]; } } #pragma mark - XPMonentsRecommendProtocol - (void)getMonentsRecommendListSuccess:(NSArray *)array state:(int)state { if (state == 0) { [self.datasource removeAllObjects]; } if (array.count > 0) { [self.datasource addObjectsFromArray:array]; } if (state == 0) { self.hasNoMoreData = NO; [self.collectionView.mj_header endRefreshing]; } else { if (array.count > 0) { self.hasNoMoreData = NO; } else { self.hasNoMoreData = YES; } [self.collectionView.mj_footer endRefreshing]; } [self.collectionView reloadData]; } - (void)getMonentsRecommendListFail:(NSString *)msg state:(int)state { if (state == 0) { [self.collectionView.mj_header endRefreshing]; } else { [self.collectionView.mj_footer endRefreshing]; } } #pragma mark - XPMonentsRecommendProtocol - (void)getMonentsAttentionListSuccess:(MonentsListInfoModel *)listInfo state:(int)state { self.listInfo = listInfo; if (state == 0) { [self.datasource removeAllObjects]; } if (listInfo.dynamicList.count > 0) { [self.datasource addObjectsFromArray:listInfo.dynamicList]; } if (state == 0) { self.hasNoMoreData = NO; [self.collectionView.mj_header endRefreshing]; } else { if (listInfo.dynamicList.count > 0) { self.hasNoMoreData = NO; } else { self.hasNoMoreData = YES; } [self.collectionView.mj_footer endRefreshing]; } [self.collectionView reloadData]; } - (void)getMonentsAttentionListFail:(NSString *)msg state:(int)state { if (state == 0) { [self.collectionView.mj_header endRefreshing]; } else { [self.collectionView.mj_footer endRefreshing]; } } - (void)likeMonentsSuccess:(NSString *)dynamicId status:(BOOL)status { [self.datasource enumerateObjectsUsingBlock:^(MonentsInfoModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { if ([obj.dynamicId isEqualToString:dynamicId]) { if (status) { obj.isLike = YES; obj.likeCount = [NSString stringWithFormat:@"%ld",[obj.likeCount integerValue]+1]; } else { obj.isLike = NO; obj.likeCount = [NSString stringWithFormat:@"%ld",[obj.likeCount integerValue]-1]; } *stop = YES; } }]; [self.collectionView reloadData]; } #pragma mark - XPMonentsDetailViewControllerDelegate - (void)xPMonentsDetailViewController:(XPMonentsDetailViewController *)view deleteMonents:(NSString *)dynamicId { __block MonentsInfoModel * deleteInfo; [self.datasource enumerateObjectsUsingBlock:^(MonentsInfoModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { if (obj.dynamicId.integerValue == dynamicId.integerValue) { deleteInfo = obj; } }]; if (deleteInfo) { [self.datasource removeObject:deleteInfo]; [self.collectionView reloadData]; } } #pragma mark - UICollectionView - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { if (self.datasource.count > 0) { return self.datasource.count; } return 1; } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { if (self.datasource.count > 0) { CGFloat width = (KScreenWidth - 16 -13 -16)/2.0; CGFloat height = 220.0/165.0*width; return CGSizeMake(width, height); } return collectionView.frame.size; } - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section { if (self.datasource.count > 0) { return UIEdgeInsetsMake(12, 16, 12, 16); } return UIEdgeInsetsZero; } - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { if (self.datasource.count > 0) { XPMomentListCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass(XPMomentListCollectionViewCell.self) forIndexPath:indexPath]; MonentsInfoModel * monentsInfo = [self.datasource safeObjectAtIndex1:indexPath.row]; cell.monentsInfo = monentsInfo; cell.delegate = self; return cell; } XPMonentsEmptyCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([XPMonentsEmptyCollectionViewCell class]) forIndexPath:indexPath]; return cell; } - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { if (self.datasource.count > 0) { XPMonentsDetailViewController *detailVC = [[XPMonentsDetailViewController alloc] init]; MonentsInfoModel * monentsInfo = [self.datasource safeObjectAtIndex1:indexPath.row]; detailVC.monentsInfo = monentsInfo; detailVC.delegate = self; [self.navigationController pushViewController:detailVC animated:YES]; } } #pragma mark -JXCategoryListContainerViewDelegate - (UIView *)listView { return self.view; } - (UIScrollView *)listScrollView { return self.collectionView; } - (void)listViewDidScrollCallback:(void (^)(UIScrollView *))callback { self.scrollCallback = callback; } - (void)scrollViewDidScroll:(UIScrollView *)scrollView { self.scrollCallback(scrollView); } #pragma mark - 懒加载 - (UICollectionView *)collectionView { if (!_collectionView) { UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; layout.scrollDirection = UICollectionViewScrollDirectionVertical; layout.minimumLineSpacing = 12.0; layout.minimumInteritemSpacing = 13.0; _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout]; _collectionView.backgroundColor = UIColor.clearColor; _collectionView.alwaysBounceVertical = YES; [_collectionView registerClass:[XPMomentListCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass(XPMomentListCollectionViewCell.self)]; [_collectionView registerClass:[XPMonentsEmptyCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([XPMonentsEmptyCollectionViewCell class])]; _collectionView.delegate = self; _collectionView.dataSource = self; _collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; } return _collectionView; } - (NSMutableArray *)datasource { if (!_datasource) { _datasource = [NSMutableArray array]; } return _datasource; } @end