You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

303 lines
12 KiB

//
// TFDiscoverAudioViewController.m
// TFReader
//
// Created by 谢腾飞 on 2020/12/16.
// Copyright © 2020 xtfei_2011@126.com. All rights reserved.
//
#import "TFDiscoverAudioViewController.h"
#import "TFLimitFreeViewController.h"
#import "TFBookStoreNovelStyleOneCell.h"
#import "TFBookStoreNovelStyleTwoCell.h"
#import "TFBookStoreNovelStyleThreeCell.h"
#import "TFBookStoreNovelStyleFourCell.h"
#import "TFPublicAdvertisementViewCell.h"
#import "TFDiscoverHeaderView.h"
#import "TFBannerActionManager.h"
@interface TFDiscoverAudioViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic ,strong) TFBookStoreModel *audioDiscoverModel;
@property (nonatomic ,strong) TFDiscoverHeaderView *headerView;
@property (nonatomic ,assign) BOOL needRefresh;
@end
@implementation TFDiscoverAudioViewController
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self setStatusBarDefaultStyle];
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self initialize];
[self createSubviews];
[self netRequest];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self setStatusBarDefaultStyle];
}
- (void)initialize
{
self.needRefresh = YES;
[self hiddenNavigationBar:YES];
}
- (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) {
TFBasicViewController *vc = [TFBannerActionManager getBannerActionWithBannerModel:bannerModel productionType:TFProductionTypeAudio];
if (vc) {
[weakSelf.navigationController pushViewController:vc animated:YES];
}
};
[self.mainTableViewGroup setTableHeaderView:self.headerView];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.audioDiscoverModel.label.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
TFBookStoreLabelModel *labelModel = [self.audioDiscoverModel.label objectOrNilAtIndex:indexPath.section];
for (TFProductionModel *t_model in labelModel.list) {
t_model.productionType = self.productionType;
}
if (labelModel.ad_type == 0) {
switch (labelModel.style) {
case 1:
return [self createCellWithTabelView:tableView cellClass:TFBookStoreNovelStyleOneCell.class indexPath:indexPath labelModel:labelModel];
break;
case 2:
return [self createCellWithTabelView:tableView cellClass:TFBookStoreNovelStyleTwoCell.class indexPath:indexPath labelModel:labelModel];
break;
case 3:
return [self createCellWithTabelView:tableView cellClass:TFBookStoreNovelStyleThreeCell.class indexPath:indexPath labelModel:labelModel];
break;
case 4:
return [self createCellWithTabelView:tableView cellClass:TFBookStoreNovelStyleFourCell.class indexPath:indexPath labelModel:labelModel];
break;
default:
return [self createCellWithTabelView:tableView cellClass:TFBookStoreNovelStyleOneCell.class indexPath:indexPath labelModel:labelModel];
break;
}
} else {
return [self createAdStyleCellWithTableView:tableView indexPath:indexPath adModel:labelModel];
}
}
- (UITableViewCell *)createCellWithTabelView:(UITableView *)tableView cellClass:(Class)cellClass indexPath:(NSIndexPath *)indexPath labelModel:(TFBookStoreLabelModel *)labelModel
{
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) {
TFAudioDetailViewController *vc = [[TFAudioDetailViewController alloc] init];
vc.audio_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.audioDiscoverModel.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.audioDiscoverModel.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.audioDiscoverModel.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.audioDiscoverModel.label objectOrNilAtIndex:index];
if (t_labelModel.recommend_id <= 0) {
return;
}
WS(weakSelf)
[TFNetworkTools POST:Audio_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 && t_model.list.count > 0) {
for (TFProductionModel *obj in t_model.list) {
obj.productionType = weakSelf.productionType;
}
[weakSelf.audioDiscoverModel.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();
} else {
!complete ?: complete();
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
!complete ?: complete();
}];
}
- (void)netRequest
{
WS(weakSelf)
[TFNetworkTools POSTQuick:Audio_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.audioDiscoverModel = 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];
}];
}
@end