// // YMHomePageListViewController.m // MSYOUPAI // // Created by YoMi on 2024/2/11. // Copyright © 2024 MS. All rights reserved. // #import "YMHomePageListViewController.h" #import "YMHomePageListViewModel.h" #import "YMChatNotificationViewModel.h" #import "YMHomePageListCell.h" #import "CHTCollectionViewWaterfallLayout.h" @interface YMHomePageListViewController () /// 滚动视图回调 @property (nonatomic, copy) void(^scrollCallback)(UIScrollView *scrollView); /// 列表VM @property (nonatomic, strong) YMHomePageListViewModel *viewModel; /// 内容排版 @property (nonatomic, strong) CHTCollectionViewWaterfallLayout *contentLayout; /// 容器列表 @property (nonatomic, strong) UICollectionView *contentCollectionView; @end @implementation YMHomePageListViewController @dynamic viewModel; - (void)viewDidLoad { [super viewDidLoad]; [self setNavHidden:YES]; [OCNotificationCenter addObserver:self selector:@selector(refreshHomeData:) name:REFRESH_HOME_NOTIFICATION object:nil]; YMEmptyView *empty = [YMEmptyView emptyActionViewWithImage:ImageByName(@"ym_common_no_data_icon") titleStr:@"开启“个性化推荐”后可启用推荐列表" detailStr:@"" btnTitleStr:@"立即开启" btnClickBlock:^{ YMChatNotificationViewModel *chatNotificationVM = [[YMChatNotificationViewModel alloc]initWithParams:@{}]; [YMRouter openURL:stringFormat(@"%@%@", YM_ROUTER_URL_PREFIX, YM_ROUTER_CHAT_NOTIFICATION) withUserInfo:@{ RouterViewModel:chatNotificationVM } completion:nil]; }]; empty.contentViewOffset = adapt(-80); empty.imageSize = kEmptyViewSize; empty.titleLabFont = LCFont(13); empty.titleLabTextColor = HexColorFromRGB(0x9b9b9b); empty.actionBtnCornerRadius = adapt(10); empty.actionBtnTitleColor = HexColorFromRGB(0xFFFFFF); empty.actionBtnBackGroundColor = HexColorFromRGB(0xfd7bc5); self.view.ym_emptyView = empty; self.view.ym_emptyView.autoShowEmptyView = NO; } - (void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; [self homePageIsHidePersonalizedRecommendationTips]; } - (void)ym_setupViews { [self.view addSubview:self.contentCollectionView]; [self.view setNeedsUpdateConstraints]; [self.view updateConstraintsIfNeeded]; } - (void)updateViewConstraints { [self.contentCollectionView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.view); make.left.equalTo(self.view); make.right.equalTo(self.view); make.bottom.equalTo(self.view).offset(-TabbarHeight); }]; [super updateViewConstraints]; } - (void)ym_bindViewModel { [self headerRefreshing]; @weakify(self) [[self.viewModel.refreshUISubject takeUntil:self.rac_willDeallocSignal] subscribeNext:^(id result) { @strongify(self) if (self.refreshBlock) { self.refreshBlock(); } [self.contentCollectionView reloadData]; switch ([result intValue]) { case YMHeaderRefresh_HasMoreData: case YMHeaderRefresh_HasNoMoreData: { [self.contentCollectionView.mj_footer endRefreshing]; } break; case YMFooterRefresh_HasMoreData: { [self.contentCollectionView.mj_footer endRefreshing]; if (self.contentCollectionView.mj_footer == self.noLoadMoreFooter) { self.contentCollectionView.mj_footer = self.loadMoreFooter; } } break; case YMFooterRefresh_HasNoMoreData: { [self.contentCollectionView.mj_footer endRefreshing]; if (self.contentCollectionView.mj_footer == self.loadMoreFooter) { self.contentCollectionView.mj_footer = self.noLoadMoreFooter; } } break; case YMRefreshError: { [self.contentCollectionView.mj_footer endRefreshing]; } break; default: break; } [self.contentCollectionView ym_endLoading]; }]; } #pragma mark - UICollectionViewDataSource - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 1; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return self.viewModel.listDataArray.count; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { YMHomePageListCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([YMHomePageListCell class]) forIndexPath:indexPath]; YMHomePageListCellViewModel *cellVM = self.viewModel.listDataArray[indexPath.item]; cellVM.jxCategoryPageIndex = self.pageIndex; cellVM.jxPageTitle = self.jxPageTitle; [cellVM updateWithPageIndex]; @weakify(self) cellVM.changeAccostStatusBlock = ^{ @strongify(self) [UIView performWithoutAnimation:^{ [self.contentCollectionView reloadItemsAtIndexPaths:@[indexPath]]; }]; }; [cell ym_bindViewModel:cellVM]; return cell; } - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{ UICollectionReusableView *reusableView = nil; reusableView.backgroundColor = [UIColor clearColor]; return reusableView; } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{ return CGSizeZero; } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{ return CGSizeZero; } #pragma mark - UICollectionViewDelegate - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{ if (self.viewModel.listDataArray[indexPath.item].userId != 0) { [self.viewModel.gotoPersonalPageSubject sendNext:@(self.viewModel.listDataArray[indexPath.item].userId)]; } } #pragma mark - CHTCollectionViewDelegateWaterfallLayout - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { return CGSizeMake(adapt(170), adapt(220)); } ///首页是否隐藏个性化推荐提示 - (void)homePageIsHidePersonalizedRecommendationTips{ if ([[OCUserDefaults objectForKey:kIS_HIDE_PERSONALIZED_RECOMMENDATION_TIPS] boolValue]) { self.contentCollectionView.hidden = NO; [self.view ym_hideEmptyView]; } else { self.contentCollectionView.hidden = YES; [self.view ym_showEmptyView]; } } - (void)refreshHomeData:(NSNotification *)notice{ [self headerRefreshing]; } - (void)headerRefreshing{ [self.contentCollectionView ym_startLoading]; self.viewModel.currentPage = 1; [self.viewModel getHomePageListData]; } - (void)footerRefreshing{ [self.contentCollectionView ym_startLoading]; self.viewModel.currentPage++; [self.viewModel getHomePageListData]; } - (void)scrollViewDidScroll:(UIScrollView *)scrollView { !self.scrollCallback ?: self.scrollCallback(scrollView); } #pragma mark - JXPagingViewListViewDelegate - (UIView *)listView { return self.view; } - (UIScrollView *)listScrollView { return self.contentCollectionView; } - (void)listViewDidScrollCallback:(void (^)(UIScrollView *))callback { self.scrollCallback = callback; } - (void)listWillAppear { // NSLog(@"%@:%@", self.title, NSStringFromSelector(_cmd)); } - (void)listDidAppear { // NSLog(@"%@:%@", self.title, NSStringFromSelector(_cmd)); } - (void)listWillDisappear { // NSLog(@"%@:%@", self.title, NSStringFromSelector(_cmd)); } - (void)listDidDisappear { // NSLog(@"%@:%@", self.title, NSStringFromSelector(_cmd)); } - (CHTCollectionViewWaterfallLayout *)contentLayout{ if (!_contentLayout) { _contentLayout = [[CHTCollectionViewWaterfallLayout alloc] init]; _contentLayout.columnCount = 2; _contentLayout.sectionInset = UIEdgeInsetsMake(adapt(5), adapt(5), adapt(5), adapt(5)); } return _contentLayout; } - (UICollectionView *)contentCollectionView{ if (!_contentCollectionView) { _contentCollectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:self.contentLayout]; _contentCollectionView.delegate = self; _contentCollectionView.dataSource = self; _contentCollectionView.showsVerticalScrollIndicator = NO; _contentCollectionView.showsHorizontalScrollIndicator = NO; _contentCollectionView.backgroundColor = UIColor.whiteColor; [_contentCollectionView registerClass:[YMHomePageListCell class] forCellWithReuseIdentifier:NSStringFromClass([YMHomePageListCell class])]; _contentCollectionView.mj_footer = self.noLoadMoreFooter; YMEmptyView *empty = [YMEmptyView emptyViewWithImageStr:@"ym_common_no_data_icon" titleStr:@"暂无数据" detailStr:@""]; empty.imageSize = kEmptyViewSize; _contentCollectionView.ym_emptyView = empty; _contentCollectionView.ym_emptyView.autoShowEmptyView = NO; } return _contentCollectionView; } @end