// // TFBookStoreComicController.m // TFReader // // Created by 谢腾飞 on 2020/12/16. // Copyright © 2020 xtfei_2011@126.com. All rights reserved. // #import "TFBookStoreComicController.h" #import "TFLimitFreeViewController.h" #import "TFBookStoreComicMaxStyleCell.h" #import "TFBookStoreComicMiddleStyleCell.h" #import "TFBookStoreComicNormalStyleCell.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" @interface TFBookStoreComicController () @property (nonatomic ,strong) TFBookStoreMenuView *headerView; @property (nonatomic ,strong) TFBookStoreModel *comicModel; @property (nonatomic ,assign) BOOL needRefresh; @end @implementation TFBookStoreComicController - (void)viewDidLoad { [super viewDidLoad]; [self initialize]; [self createSubviews]; [self netRequest]; } - (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]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(netRequest) name:Notification_Restore_Network 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:TFProductionTypeComic]) { [weakSelf.navigationController pushViewController:[TFBannerActionManager getBannerActionWithBannerModel:bannerModel productionType:TFProductionTypeComic] 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.comicModel.label.count; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 1; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { TFBookStoreLabelModel *labelModel = [self.comicModel.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 = TFBookStoreComicNormalStyleCell.class; switch (labelModel.style) { case 1: cellClass = TFBookStoreComicMiddleStyleCell.class; break; case 2: cellClass = TFBookStoreComicNormalStyleCell.class; break; case 3: cellClass = TFBookStoreComicMaxStyleCell.class; break; default: cellClass = TFBookStoreComicNormalStyleCell.class; break; } WS(weakSelf) TFBookStoreComicBasicViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(cellClass)]; if (!cell) { cell = [[cellClass alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass(cellClass)]; } cell.labelModel = labelModel; cell.cellDidSelectItemBlock = ^(NSInteger production_id) { TFComicDetailViewController *vc = [[TFComicDetailViewController alloc] init]; vc.comic_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.comicModel.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.comicModel.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)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; } - (void)moreButtonClick:(UIButton *)sender { TFBookStoreLabelModel *t_labelModel = [self.comicModel.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)netRequest { WS(weakSelf) [TFNetworkTools POSTQuick:Comic_Mall parameters:@{@"channel_id":self.channel != 0?[TFUtilsHelper formatStringWithInteger:self.channel]:@(TFSystemInfoManager.sexChannel)} model:TFBookStoreModel.class success:^(BOOL isSuccess, TFBookStoreModel * _Nullable t_model, BOOL isCache, TFNetworkRequestModel * _Nonnull requestModel) { if (isSuccess) { weakSelf.comicModel = 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)changeRecommendRequestAtIndex:(NSInteger)index complete:(void(^)(void))complete { TFBookStoreLabelModel *t_labelModel = [self.comicModel.label objectOrNilAtIndex:index]; NSString *recommend_id = [TFUtilsHelper formatStringWithInteger:t_labelModel.recommend_id]; if (recommend_id.length <= 0 || [recommend_id isEqualToString:@"0"]) { return; } WS(weakSelf) [TFNetworkTools POST:Comic_Refresh parameters:@{@"recommend_id":recommend_id} model:TFBookStoreLabelModel.class success:^(BOOL isSuccess, TFBookStoreLabelModel * _Nullable t_model, TFNetworkRequestModel * _Nonnull requestModel) { if (isSuccess) { if (t_model.list.count > 0) { [weakSelf.comicModel.label objectAtIndex:index].list = t_model.list; 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(); }]; } @end