// // TFBookStoreNovelController.m // TFReader // // Created by 谢腾飞 on 2020/12/16. // Copyright © 2020 xtfei_2011@126.com. All rights reserved. // #import "TFBookStoreNovelController.h" #import "TFLimitFreeViewController.h" #import "TFBookStoreNovelStyleOneCell.h" #import "TFBookStoreNovelStyleTwoCell.h" #import "TFBookStoreNovelStyleThreeCell.h" #import "TFBookStoreNovelStyleFourCell.h" #import "TFPublicAdvertisementViewCell.h" #import "TFBookStoreMenuView.h" #import "TFBookStoreLabelModel.h" #import "TFBannerActionManager.h" // 分类 #import "TFSortViewController.h" // 排行榜 #import "TFPopularityViewController.h" // 包月 #import "TFMemberViewController.h" // 完结 #import "TFCompleteViewController.h" // 限免 #import "TFFreeViewController.h" #import "TFWebViewController.h" @interface TFBookStoreNovelController () @property (nonatomic ,strong) TFBookStoreModel *bookStoreNovelModel; @property (nonatomic ,strong) TFBookStoreMenuView *headerView; @property (nonatomic ,assign) BOOL needRefresh; @end @implementation TFBookStoreNovelController - (void)viewDidLoad { [super viewDidLoad]; [self initialize]; [self createSubviews]; [self netRequest]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(netRequest) name:Notification_Restore_Network object:nil]; } - (void)initialize { self.needRefresh = YES; [self hiddenNavigationBar:YES]; self.hotwordArray = [NSArray array]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(netRequest) name:Notification_Channel_Change 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(0); make.width.mas_equalTo(self.view.mas_width); make.height.mas_equalTo(self.view.mas_height).with.offset(- PUB_TABBAR_HEIGHT); }]; WS(weakSelf) self.headerView = [[TFBookStoreMenuView alloc] init]; self.headerView.productionType = self.productionType; self.headerView.bannerrImageClickBlock = ^(TFBannerModel * _Nonnull bannerModel) { if ([TFBannerActionManager getBannerActionWithBannerModel:bannerModel productionType:TFProductionTypeNovel]) { [weakSelf.navigationController pushViewController:[TFBannerActionManager getBannerActionWithBannerModel:bannerModel productionType:TFProductionTypeNovel] animated:YES]; } }; self.headerView.menuButtonClickBlock = ^(TFMenuButtonType menuButtonType, NSString * _Nonnull menuButtonTitle) { switch (menuButtonType) { case TFMenuButtonTypeFree: { TFFreeViewController *free = [[TFFreeViewController alloc] init]; free.productionType = weakSelf.productionType; free.navTitle = menuButtonTitle; [weakSelf.navigationController pushViewController:free animated:YES]; } break; case TFMenuButtonTypeComplete: { TFCompleteViewController *complete = [[TFCompleteViewController alloc] init]; complete.productionType = weakSelf.productionType; complete.navTitle = menuButtonTitle; [weakSelf.navigationController pushViewController:complete animated:YES]; } break; case TFMenuButtonTypeSort: { TFSortViewController *sort = [[TFSortViewController alloc] init]; sort.productionType = weakSelf.productionType; sort.navTitle = menuButtonTitle; [weakSelf.navigationController pushViewController:sort animated:YES]; } break; case TFMenuButtonTypePopularity: { TFPopularityViewController *popularity = [[TFPopularityViewController alloc] init]; popularity.productionType = weakSelf.productionType; popularity.navTitle = menuButtonTitle; [weakSelf.navigationController pushViewController:popularity animated:YES]; } break; case TFMenuButtonTypeMember: { TFMemberViewController *member = [[TFMemberViewController alloc] init]; member.productionType = weakSelf.productionType; [weakSelf.navigationController pushViewController:member animated:YES]; } break; default: break; } }; self.mainTableViewGroup.tableHeaderView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH); [self.mainTableViewGroup setTableHeaderView:self.headerView]; self.mainTableViewGroup.mj_header = [TFRefreshHeader headerWithRefreshingBlock:^{ [weakSelf netRequest]; }]; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return self.bookStoreNovelModel.label.count; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 1; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { TFBookStoreLabelModel *labelModel = [self.bookStoreNovelModel.label objectOrNilAtIndex:indexPath.section]; if (labelModel.ad_type == 0) { return [self createCellWithTabelView:tableView indexPath:indexPath labelModel:labelModel]; } else { return [self createAdStyleCellWithTableView:tableView indexPath:indexPath adModel:labelModel]; } } - (UITableViewCell *)createCellWithTabelView:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath labelModel:(TFBookStoreLabelModel *)labelModel { Class cellClass = TFBookStoreNovelStyleOneCell.class; switch (labelModel.style) { case 1: cellClass = TFBookStoreNovelStyleOneCell.class; break; case 2: cellClass = TFBookStoreNovelStyleTwoCell.class; break; case 3: cellClass = TFBookStoreNovelStyleThreeCell.class; break; case 4: cellClass = TFBookStoreNovelStyleFourCell.class; break; default: cellClass = TFBookStoreNovelStyleOneCell.class; break; } WS(weakSelf) TFBookStoreNovelBasicViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(cellClass)]; if (!cell) { cell = [[cellClass alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass(cellClass)]; } cell.labelModel = labelModel; cell.cellDidSelectItemBlock = ^(NSInteger production_id) { TFNovelDetailViewController *novelDetail = [[TFNovelDetailViewController alloc] init]; novelDetail.book_id = production_id; [weakSelf.navigationController pushViewController:novelDetail animated:YES]; }; 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; return cell; } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { TFBookStoreLabelModel *labelModel = [self.bookStoreNovelModel.label objectOrNilAtIndex:section]; if (labelModel.ad_type != 0) { return kHalfMargin; } 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.bookStoreNovelModel.label objectOrNilAtIndex:section]; if (labelModel.can_more || labelModel.can_refresh) { if (section == self.bookStoreNovelModel.label.count - 1) { return 40; } return 30; } return CGFLOAT_MIN; } - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section { UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 30)]; view.backgroundColor = [UIColor whiteColor]; CGFloat buttonX = SCREEN_WIDTH / 4; CGFloat buttonW = SCREEN_WIDTH / 2; CGFloat buttonH = 30; TFBookStoreLabelModel *labelModel = [self.bookStoreNovelModel.label objectOrNilAtIndex:section]; if (!labelModel.can_more && !labelModel.can_refresh) { return view; } if (labelModel.style == 5) { // 排行榜 TFButton *moreButton = [TFButton initWithFrame:CGRectMake(kMargin, 0, SCREEN_WIDTH - 2 * kMargin, buttonH) btnTitle:TFLocalizedString(@"查看更多排行榜") btnImageName:@"comic_mall_more" backgroundColor:kGrayViewColor btnTintColor:kGrayTextLightColor btnStyle:TFButtonIndicatorTitleLeft cornerRadius:15 target:self action:@selector(rankButtonClick:)]; moreButton.graphicDistance = 5; moreButton.tag = section; [view addSubview:moreButton]; return view; } TFButton *moreButton = [TFButton initWithFrame:CGRectMake(buttonX, 0, 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; 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; 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, 0, (SCREEN_WIDTH - 3 * kMargin) / 2, buttonH); changeButton.frame = CGRectMake(2 * kMargin + (SCREEN_WIDTH - 3 * kMargin) / 2, 0, (SCREEN_WIDTH - 3 * kMargin) / 2, buttonH); [view addSubview:moreButton]; [view addSubview:changeButton]; } return view; } - (void)scrollViewDidScroll:(UIScrollView *)scrollView { CGFloat pointY = scrollView.contentOffset.y; self.scrollViewContentOffsetY = pointY; [UIApplication sharedApplication].statusBarHidden = NO; } - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate { CGFloat pointY = scrollView.contentOffset.y; self.scrollViewContentOffsetY = pointY; } #pragma mark - 点击事件 - (void)moreButtonClick:(UIButton *)sender { TFBookStoreLabelModel *t_labelModel = [self.bookStoreNovelModel.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:(TFButton *)sender { [sender startSpin]; [self changeBookRecommendRequestAtIndex:sender.tag complete:^{ [sender stopSpin]; }]; } - (void)rankButtonClick:(UIButton *)sender { TFPopularityViewController *vc = [[TFPopularityViewController alloc] init]; vc.productionType = self.productionType; [self.navigationController pushViewController:vc animated:YES]; } - (void)netRequest { WS(weakSelf) NSMutableDictionary *params = [NSMutableDictionary dictionary]; params[@"channel_id"] = self.channel != 0 ? [TFUtilsHelper formatStringWithInteger:self.channel] : @(TFSystemInfoManager.sexChannel); [TFNetworkTools POSTQuick:Book_Mall_Center parameters:params model:TFBookStoreModel.class success:^(BOOL isSuccess, TFBookStoreModel * _Nullable t_model, BOOL isCache, TFNetworkRequestModel * _Nonnull requestModel) { if (weakSelf.bookStoreNovelModel && isCache) return; if (isSuccess) { weakSelf.bookStoreNovelModel = t_model; weakSelf.headerView.mallCenterModel = t_model; weakSelf.hotwordArray = t_model.hot_word; [[NSNotificationCenter defaultCenter] postNotificationName:Notification_Reload_Mall_Hot_Word object:nil]; } [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]; [weakSelf.mainTableViewGroup reloadData]; }]; } - (void)changeBookRecommendRequestAtIndex:(NSInteger)index complete:(void(^)(void))complete { TFBookStoreLabelModel *t_labelModel = [self.bookStoreNovelModel.label objectOrNilAtIndex:index]; if (t_labelModel.recommend_id <= 0) { return; } WS(weakSelf) NSMutableDictionary *params = [NSMutableDictionary dictionary]; params[@"recommend_id"] = [TFUtilsHelper formatStringWithInteger:t_labelModel.recommend_id]; [TFNetworkTools POST:Book_Refresh parameters:params model:TFBookStoreLabelModel.class success:^(BOOL isSuccess, TFBookStoreLabelModel * _Nullable t_model, TFNetworkRequestModel * _Nonnull requestModel) { if (isSuccess) { [weakSelf.bookStoreNovelModel.label objectAtIndex:index].list = t_model.list; TFBookStoreNovelBasicViewCell *cell = [weakSelf.mainTableViewGroup cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:index]]; t_model.style = cell.labelModel.style; cell.labelModel = t_model; !complete ?: complete(); } } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { !complete ?: complete(); }]; } - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; } @end