// // TFDiscoverNovelViewController.m // TFReader // // Created by 谢腾飞 on 2020/12/16. // Copyright © 2020 xtfei_2011@126.com. All rights reserved. // #import "TFDiscoverNovelViewController.h" #import "TFLimitFreeViewController.h" #import "TFBookStoreComicBasicViewCell.h" #import "TFBookStoreNovelStyleOneCell.h" #import "TFBookStoreNovelStyleTwoCell.h" #import "TFBookStoreNovelStyleThreeCell.h" #import "TFBookStoreNovelStyleFourCell.h" #import "TFPublicAdvertisementViewCell.h" #import "TFDiscoverHeaderView.h" #import "TFBannerActionManager.h" @interface TFDiscoverNovelViewController () @property (nonatomic ,strong) TFDiscoverHeaderView *headerView; @property (nonatomic ,strong) TFBookStoreModel *bookDiscoverModel; @property (nonatomic ,assign) BOOL needRefresh; @end @implementation TFDiscoverNovelViewController - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self setStatusBarDefaultStyle]; } - (void)viewDidLoad { [super viewDidLoad]; [self initialize]; [self createSubviews]; [self netRequest]; } - (void)initialize { self.needRefresh = YES; [self hiddenNavigationBar:YES]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(netRequest) name:Notification_EndOfTimeLimit object:nil]; } - (void)createSubviews { self.mainTableViewGroup.delegate = self; self.mainTableViewGroup.dataSource = self; [self.view addSubview:self.mainTableViewGroup]; [self.mainTableViewGroup mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(0); make.top.mas_equalTo(PUB_NAVBAR_HEIGHT); make.width.mas_equalTo(self.view.mas_width); make.height.mas_equalTo(self.view.mas_height).with.offset(- PUB_TABBAR_HEIGHT - PUB_NAVBAR_HEIGHT); }]; WS(weakSelf) self.mainTableViewGroup.mj_header = [TFRefreshHeader headerWithRefreshingBlock:^{ [weakSelf netRequest]; }]; self.headerView = [[TFDiscoverHeaderView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, CGFLOAT_MIN)]; self.headerView.bannerrImageClickBlock = ^(TFBannerModel * _Nonnull bannerModel) { if ([TFBannerActionManager getBannerActionWithBannerModel:bannerModel productionType:TFProductionTypeNovel]) { [weakSelf.navigationController pushViewController:[TFBannerActionManager getBannerActionWithBannerModel:bannerModel productionType:TFProductionTypeNovel] animated:YES]; } }; [self.mainTableViewGroup setTableHeaderView:self.headerView]; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return self.bookDiscoverModel.label.count; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 1; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { TFBookStoreLabelModel *labelModel = [self.bookDiscoverModel.label objectOrNilAtIndex:indexPath.section]; if (labelModel.ad_type == 0) { switch (labelModel.style) { case 1: return [self createSingleStyleCellWithTableView:tableView indexPath:indexPath labelModel:labelModel]; break; case 2: return [self createDoubleStyleCellWithTableView:tableView indexPath:indexPath labelModel:labelModel]; break; case 3: return [self createMixtureStyleCellWithTableView:tableView indexPath:indexPath labelModel:labelModel]; break; case 4: return [self createMixtureMoreStyleCellWithTableView:tableView indexPath:indexPath labelModel:labelModel]; break; default: return [self createSingleStyleCellWithTableView:tableView indexPath:indexPath labelModel:labelModel]; break; } } else { return [self createAdStyleCellWithTableView:tableView indexPath:indexPath adModel:labelModel]; } } - (UITableViewCell *)createSingleStyleCellWithTableView:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath labelModel:(TFBookStoreLabelModel *)labelModel { WS(weakSelf) static NSString *cellName = @"TFBookStoreNovelStyleOneCell"; TFBookStoreNovelStyleOneCell *cell = [tableView dequeueReusableCellWithIdentifier:cellName]; if (!cell) { cell = [[TFBookStoreNovelStyleOneCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellName]; } cell.labelModel = labelModel; cell.cellDidSelectItemBlock = ^(NSInteger production_id) { TFNovelDetailViewController *vc = [[TFNovelDetailViewController alloc] init]; vc.book_id = production_id; [weakSelf.navigationController pushViewController:vc animated:YES]; }; cell.selectionStyle = UITableViewCellSelectionStyleNone; return cell; } - (UITableViewCell *)createDoubleStyleCellWithTableView:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath labelModel:(TFBookStoreLabelModel *)labelModel { WS(weakSelf) static NSString *cellName = @"TFBookStoreNovelStyleTwoCell"; TFBookStoreNovelStyleTwoCell *cell = [tableView dequeueReusableCellWithIdentifier:cellName]; if (!cell) { cell = [[TFBookStoreNovelStyleTwoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellName]; } cell.labelModel = labelModel; cell.cellDidSelectItemBlock = ^(NSInteger production_id) { TFNovelDetailViewController *vc = [[TFNovelDetailViewController alloc] init]; vc.book_id = production_id; [weakSelf.navigationController pushViewController:vc animated:YES]; }; cell.selectionStyle = UITableViewCellSelectionStyleNone; return cell; } - (UITableViewCell *)createMixtureStyleCellWithTableView:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath labelModel:(TFBookStoreLabelModel *)labelModel { WS(weakSelf) static NSString *cellName = @"TFBookStoreNovelStyleThreeCell"; TFBookStoreNovelStyleThreeCell *cell = [tableView dequeueReusableCellWithIdentifier:cellName]; if (!cell) { cell = [[TFBookStoreNovelStyleThreeCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellName]; } cell.labelModel = labelModel; cell.cellDidSelectItemBlock = ^(NSInteger production_id) { TFNovelDetailViewController *vc = [[TFNovelDetailViewController alloc] init]; vc.book_id = production_id; [weakSelf.navigationController pushViewController:vc animated:YES]; }; cell.selectionStyle = UITableViewCellSelectionStyleNone; return cell; } - (UITableViewCell *)createMixtureMoreStyleCellWithTableView:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath labelModel:(TFBookStoreLabelModel *)labelModel { WS(weakSelf) static NSString *cellName = @"TFBookStoreNovelStyleFourCell"; TFBookStoreNovelStyleFourCell *cell = [tableView dequeueReusableCellWithIdentifier:cellName]; if (!cell) { cell = [[TFBookStoreNovelStyleFourCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellName]; } cell.labelModel = labelModel; cell.cellDidSelectItemBlock = ^(NSInteger production_id) { TFNovelDetailViewController *vc = [[TFNovelDetailViewController alloc] init]; vc.book_id = production_id; [weakSelf.navigationController pushViewController:vc animated:YES]; }; cell.selectionStyle = UITableViewCellSelectionStyleNone; return cell; } - (UITableViewCell *)createAdStyleCellWithTableView:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath adModel:(TFBookStoreLabelModel *)labelModel { static NSString *cellName = @"TFPublicAdvertisementViewCell"; TFPublicAdvertisementViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellName]; if (!cell) { cell = [[TFPublicAdvertisementViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellName]; } [cell setAdModel:labelModel refresh:self.needRefresh]; cell.mainTableView = tableView; cell.selectionStyle = UITableViewCellSelectionStyleNone; return cell; } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return CGFLOAT_MIN; } - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, CGFLOAT_MIN)]; view.backgroundColor = [UIColor clearColor]; return view; } - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { TFBookStoreLabelModel *labelModel = [self.bookDiscoverModel.label objectOrNilAtIndex:section]; if (labelModel.can_more || labelModel.can_refresh) { return 50; } return CGFLOAT_MIN; } - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section { UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50)]; view.backgroundColor = [UIColor whiteColor]; CGFloat buttonX = SCREEN_WIDTH / 4; CGFloat buttonY = 10; CGFloat buttonW = SCREEN_WIDTH / 2; CGFloat buttonH = 30; TFBookStoreLabelModel *labelModel = [self.bookDiscoverModel.label objectOrNilAtIndex:section]; if (!labelModel.can_more && !labelModel.can_refresh) { return view; } TFButton *moreButton = [TFButton initWithFrame:CGRectMake(buttonX, buttonY, buttonW, buttonH) btnTitle:TFLocalizedString(@"更多") btnImageName:@"comic_mall_more" backgroundColor:kGrayViewColor btnTintColor:kGrayTextLightColor btnStyle:TFButtonIndicatorTitleLeft cornerRadius:15 target:self action:@selector(moreButtonClick:)]; moreButton.graphicDistance = 5; moreButton.tag = section; [moreButton addTarget:self action:@selector(moreButtonClick:) forControlEvents:UIControlEventTouchUpInside]; TFButton *changeButton = [TFButton initWithFrame:CGRectMake(buttonX, 0, buttonW, buttonH) btnTitle:TFLocalizedString(@"换一换") btnImageName:@"comic_mall_refresh" backgroundColor:kGrayViewColor btnTintColor:kGrayTextLightColor btnStyle:TFButtonIndicatorTitleLeft cornerRadius:15 target:self action:@selector(changeButtonClick:)]; changeButton.graphicDistance = 5; changeButton.buttonImageScale = 0.5; changeButton.tag = section; [changeButton addTarget:self action:@selector(changeButtonClick:) forControlEvents:UIControlEventTouchUpInside]; if (labelModel.can_more && !labelModel.can_refresh) { [view addSubview:moreButton]; } if (!labelModel.can_more && labelModel.can_refresh) { [view addSubview:changeButton]; } if (labelModel.can_more && labelModel.can_refresh) { moreButton.frame = CGRectMake(kMargin, buttonY, (SCREEN_WIDTH - 3 * kMargin) / 2, buttonH); changeButton.frame = CGRectMake(2 * kMargin + (SCREEN_WIDTH - 3 * kMargin) / 2, buttonY, (SCREEN_WIDTH - 3 * kMargin) / 2, buttonH); [view addSubview:moreButton]; [view addSubview:changeButton]; } return view; } - (void)moreButtonClick:(UIButton *)sender { TFBookStoreLabelModel *t_labelModel = [self.bookDiscoverModel.label objectOrNilAtIndex:sender.tag]; if (t_labelModel.expire_time > 0) { TFLimitFreeViewController *vc = [[TFLimitFreeViewController alloc] init]; vc.productionType = self.productionType; [self.navigationController pushViewController:vc animated:YES]; } else { TFBookStoreMoreViewController *vc = [[TFBookStoreMoreViewController alloc] init]; vc.productionType = self.productionType; vc.recommend_id = [TFUtilsHelper formatStringWithInteger:t_labelModel.recommend_id]; [self.navigationController pushViewController:vc animated:YES]; } } - (void)changeButtonClick:(UIButton *)sender { TFButton *button = (TFButton *)sender; [button startSpin]; [self changeRecommendRequestAtIndex:sender.tag complete:^{ [button stopSpin]; }]; } - (void)changeRecommendRequestAtIndex:(NSInteger)index complete:(void(^)(void))complete { TFBookStoreLabelModel *t_labelModel = [self.bookDiscoverModel.label objectOrNilAtIndex:index]; if (t_labelModel.recommend_id <= 0) { return; } WS(weakSelf) [TFNetworkTools POST:Book_Refresh parameters:@{@"recommend_id":[TFUtilsHelper formatStringWithInteger:t_labelModel.recommend_id]} model:TFBookStoreLabelModel.class success:^(BOOL isSuccess, TFBookStoreLabelModel * _Nullable t_model, TFNetworkRequestModel * _Nonnull requestModel) { if (isSuccess) { if (t_model.list.count > 0) { [weakSelf.bookDiscoverModel.label objectAtIndex:index].list = t_model.list; t_model.style = t_labelModel.style; TFBookStoreComicBasicViewCell *cell = [weakSelf.mainTableViewGroup cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:index]]; cell.labelModel = t_model; !complete ?: complete(); } } } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { !complete ?: complete(); }]; } - (void)netRequest { WS(weakSelf) [TFNetworkTools POSTQuick:Book_Discover parameters:nil model:TFBookStoreModel.class success:^(BOOL isSuccess, TFBookStoreModel * _Nullable t_model, BOOL isCache, TFNetworkRequestModel * _Nonnull requestModel) { if (isSuccess) { weakSelf.headerView.banner = t_model.banner; if (t_model.banner.count > 0) { weakSelf.headerView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH / 4); } else { weakSelf.headerView.frame = CGRectMake(0, 0, SCREEN_WIDTH, CGFLOAT_MIN); } weakSelf.bookDiscoverModel = t_model; } [weakSelf.mainTableViewGroup endRefreshing]; weakSelf.needRefresh = YES; [weakSelf.mainTableViewGroup reloadData]; dispatch_async(dispatch_get_main_queue(), ^{ weakSelf.needRefresh = NO; }); } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { [weakSelf.mainTableViewGroup endRefreshing]; }]; } - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; } @end