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.

457 lines
20 KiB

//
// TFBookStoreViewController.m
// WXReader
//
// Created by 谢腾飞 on 2020/12/2.
// Copyright © 2020 xtfei_2011@126.com. All rights reserved.
//
#import "TFBookStoreViewController.h"
#import "AppDelegate.h"
#if TF_Enable_Comic
#import "TFBookStoreComicController.h"
#endif
#if TF_Enable_Book
#import "TFBookStoreNovelController.h"
#endif
#if TF_Enable_Audio
#import "TFBookStoreAudioController.h"
#import "TFAudioPlayViewController.h"
#endif
#import "TFSearchViewController.h"
#import "TFLimitFreeViewController.h"
#import "TFWebViewController.h"
#import "TFCollectionManager.h"
#import "TFRecommendBookModel.h"
#import "NSObject+Observer.h"
#import "AppDelegate+TFStartTimes.h"
@interface TFBookStoreViewController ()<SGPageTitleViewDelegate, SGPageContentCollectionViewDelegate>
@property (nonatomic ,strong) NSMutableArray *childControllers;
@property (nonatomic ,weak) UIImageView *searchImageView;
@property (nonatomic ,strong) SGPageTitleView *pageTitleView;
@property (nonatomic ,strong) SGPageContentCollectionView *pageContentView;
@property (nonatomic ,assign) CGFloat contentOffsetY;
@end
@implementation TFBookStoreViewController
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self changeNavBarColorWithContentOffsetY:self.contentOffsetY];
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self initialize];
[self createSubViews];
// 首次安装增加推荐书籍
WS(weakSelf)
[TFNetworkManager networkingStatus:^(BOOL status) {
if (status) {
AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
if ([delegate startTimes] == 1) {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
[weakSelf netRequest];
});
}
}
}];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self changeNavBarColorWithContentOffsetY:self.contentOffsetY];
[[NSNotificationCenter defaultCenter] postNotificationName:Notification_Show_Tabbar object:nil];
}
- (void)initialize
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pushFromMallCenter:) name:NSNotification_Rack_JumpToMallCenter object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadSearchBar) name:Notification_Reload_Mall_Hot_Word object:nil];
[self hiddenNavigationBarLeftButton];
[self hiddenSeparator];
}
- (void)createSubViews
{
self.navigationBar.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0];
WS(weakSelf)
#if TF_Enable_Book
TFBookStoreNovelController *bookStoreNovel = [[TFBookStoreNovelController alloc] init];
bookStoreNovel.productionType = TFProductionTypeNovel;
bookStoreNovel.channel = 0;
[bookStoreNovel addObserver:KEY_PATH(bookStoreNovel, scrollViewContentOffsetY) complete:^(TFBookStoreNovelController * _Nonnull obj, id _Nullable oldVal, id _Nullable newVal) {
[weakSelf changeNavBarColorWithContentOffsetY:[newVal floatValue]];
}];
[self addChildViewController:bookStoreNovel];
#endif
#if TF_Enable_Comic
TFBookStoreComicController *bookStoreComic = [[TFBookStoreComicController alloc] init];
bookStoreComic.productionType = TFProductionTypeComic;
bookStoreComic.channel = 0;
[bookStoreComic addObserver:KEY_PATH(bookStoreComic, scrollViewContentOffsetY) complete:^(TFBookStoreComicController * _Nonnull obj, id _Nullable oldVal, id _Nullable newVal) {
[weakSelf changeNavBarColorWithContentOffsetY:[newVal floatValue]];
}];
[self addChildViewController:bookStoreComic];
#endif
#if TF_Enable_Audio
TFBookStoreAudioController *bookStoreAudio = [[TFBookStoreAudioController alloc] init];
bookStoreAudio.productionType = TFProductionTypeAudio;
bookStoreAudio.channel = 0;
[bookStoreAudio addObserver:KEY_PATH(bookStoreAudio, scrollViewContentOffsetY) complete:^(TFBookStoreAudioController * _Nonnull obj, id _Nullable oldVal, id _Nullable newVal) {
[weakSelf changeNavBarColorWithContentOffsetY:[newVal floatValue]];
}];
[self addChildViewController:bookStoreAudio];
#endif
NSMutableArray *titleArr = [NSMutableArray array];
self.childControllers = [NSMutableArray array];
// 临时记录单站点状态
NSMutableArray *t_titleArr = [NSMutableArray array];
NSMutableArray *t_chailArr = [NSMutableArray array];
for (NSNumber *siteNumber in [TFUtilsHelper getSiteState]) {
#if TF_Enable_Book
if ([siteNumber integerValue] == 1) {
[titleArr addObject:TFLocalizedString(@"小说")];
[self.childControllers addObject:bookStoreNovel];
TFBookStoreNovelController *boyVC = [[TFBookStoreNovelController alloc] init];
boyVC.channel = 1;
boyVC.productionType = TFProductionTypeNovel;
[boyVC addObserver:KEY_PATH(boyVC, scrollViewContentOffsetY) complete:^(TFBookStoreNovelController * _Nonnull obj, id _Nullable oldVal, id _Nullable newVal) {
[weakSelf changeNavBarColorWithContentOffsetY:[newVal floatValue]];
}];
TFBookStoreNovelController *girlVC = [[TFBookStoreNovelController alloc] init];
girlVC.channel = 2;
girlVC.productionType = TFProductionTypeNovel;
[girlVC addObserver:KEY_PATH(girlVC, scrollViewContentOffsetY) complete:^(TFBookStoreNovelController * _Nonnull obj, id _Nullable oldVal, id _Nullable newVal) {
[weakSelf changeNavBarColorWithContentOffsetY:[newVal floatValue]];
}];
[t_titleArr addObjectsFromArray:@[TFLocalizedString(@"男生"), TFLocalizedString(@"女生")]];
[t_chailArr addObjectsFromArray:@[boyVC, girlVC]];
}
#endif
#if TF_Enable_Comic
if ([siteNumber integerValue] == 2) {
[titleArr addObject:TFLocalizedString(@"漫画")];
[self.childControllers addObject:bookStoreComic];
TFBookStoreComicController *boyVC = [[TFBookStoreComicController alloc] init];
boyVC.channel = 1;
boyVC.productionType = TFProductionTypeComic;
[boyVC addObserver:KEY_PATH(boyVC, scrollViewContentOffsetY) complete:^(TFBookStoreComicController * _Nonnull obj, id _Nullable oldVal, id _Nullable newVal) {
[weakSelf changeNavBarColorWithContentOffsetY:[newVal floatValue]];
}];
TFBookStoreComicController *girlVC = [[TFBookStoreComicController alloc] init];
girlVC.channel = 2;
girlVC.productionType = TFProductionTypeComic;
[girlVC addObserver:KEY_PATH(girlVC, scrollViewContentOffsetY) complete:^(TFBookStoreComicController * _Nonnull obj, id _Nullable oldVal, id _Nullable newVal) {
[weakSelf changeNavBarColorWithContentOffsetY:[newVal floatValue]];
}];
[t_titleArr addObjectsFromArray:@[TFLocalizedString(@"男生"), TFLocalizedString(@"女生")]];
[t_chailArr addObjectsFromArray:@[boyVC, girlVC]];
}
#endif
#if TF_Enable_Audio
if ([siteNumber integerValue] == 3) {
[titleArr addObject:TFLocalizedString(@"听书")];
[self.childControllers addObject:bookStoreAudio];
TFBookStoreAudioController *boyVC = [[TFBookStoreAudioController alloc] init];
boyVC.channel = 1;
boyVC.productionType = TFProductionTypeAudio;
[boyVC addObserver:KEY_PATH(boyVC, scrollViewContentOffsetY) complete:^(TFBookStoreAudioController * _Nonnull obj, id _Nullable oldVal, id _Nullable newVal) {
[weakSelf changeNavBarColorWithContentOffsetY:[newVal floatValue]];
}];
TFBookStoreAudioController *girlVC = [[TFBookStoreAudioController alloc] init];
girlVC.channel = 2;
girlVC.productionType = TFProductionTypeAudio;
[girlVC addObserver:KEY_PATH(girlVC, scrollViewContentOffsetY) complete:^(TFBookStoreAudioController * _Nonnull obj, id _Nullable oldVal, id _Nullable newVal) {
[weakSelf changeNavBarColorWithContentOffsetY:[newVal floatValue]];
}];
[t_titleArr addObjectsFromArray:@[TFLocalizedString(@"男生"), TFLocalizedString(@"女生")]];
[t_chailArr addObjectsFromArray:@[boyVC, girlVC]];
}
#endif
}
if ([TFUtilsHelper getSiteState].count == 1) {
titleArr = t_titleArr;
self.childControllers = t_chailArr;
}
self.pageContentView = [[SGPageContentCollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) parentVC:self childVCs:self.childControllers];
self.pageContentView.delegatePageContentCollectionView = self;
[self.view addSubview:self.pageContentView];
self.pageConfigure.indicatorColor = [UIColor whiteColor];
self.pageConfigure.indicatorStyle = SGIndicatorStyleDynamic;
self.pageConfigure.indicatorHeight = 3;
self.pageConfigure.indicatorFixedWidth = 10;
self.pageConfigure.indicatorDynamicWidth = 14;
self.pageConfigure.indicatorToBottomDistance = 3;
self.pageConfigure.titleSelectedColor = [UIColor whiteColor];
self.pageConfigure.titleFont = kBoldFont16;
self.pageConfigure.titleTextZoom = YES;
self.pageConfigure.titleTextZoomRatio = 0.2;
self.pageConfigure.titleColor = kColorRGBA(255, 255, 255, 0.9);
CGFloat width = 0;
for (NSString *obj in titleArr) {
CGFloat t_width = [TFViewHelper getDynamicWidthWithLabelFont:kBoldFont16 labelHeight:43.0 labelText:obj maxWidth:140.0];
width += t_width;
width += kLabelHeight;
}
self.pageTitleView = [SGPageTitleView pageTitleViewWithFrame:CGRectMake(kHalfMargin, (is_iPhoneX ? kQuarterMargin : kHalfMargin) + kHalfMargin + PUB_NAVBAR_OFFSET, width, TFPageView_H) delegate:self titleNames:titleArr configure:self.pageConfigure];
self.pageTitleView.backgroundColor = [UIColor clearColor];
[self.navigationBar addSubview:self.pageTitleView];
if ([TFUtilsHelper getSiteState].count > 1) {
self.sexChooseButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.sexChooseButton.adjustsImageWhenHighlighted = NO;
[self.sexChooseButton setImageEdgeInsets:UIEdgeInsetsMake(10, 20, 10, 0)];
[self.sexChooseButton addTarget:self action:@selector(secChooseButtonClick) forControlEvents:UIControlEventTouchUpInside];
[self.navigationBar addSubview:self.sexChooseButton];
if (TFSystemInfoManager.sexChannel == 1) { //
[self.sexChooseButton setImage:[UIImage imageNamed:@"comic_mall_boy_dark"] forState:UIControlStateNormal];
} else { //
[self.sexChooseButton setImage:[UIImage imageNamed:@"comic_mall_girl_dark"] forState:UIControlStateNormal];
}
[self.sexChooseButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.mas_equalTo(self.navigationBar.mas_right).with.offset(- kHalfMargin - kQuarterMargin);
make.centerY.mas_equalTo(self.pageTitleView.mas_centerY).with.offset(2);
make.width.height.mas_equalTo(44);
}];
}
UIImageView *searchImageView = [[UIImageView alloc] init];
searchImageView.image = [[UIImage imageNamed:@"public_search"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[searchImageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(searchClick)]];
searchImageView.backgroundColor = [UIColor clearColor];
searchImageView.userInteractionEnabled = YES;
searchImageView.tintColor = kWhiteColor;
[self.navigationBar addSubview:searchImageView];
self.searchImageView = searchImageView;
[searchImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(self.pageTitleView).offset(2);
if ([TFUtilsHelper getSiteState].count > 1) {
make.right.equalTo(self.sexChooseButton.mas_left);
} else {
make.right.equalTo(self.navigationBar).offset(- kHalfMargin - kQuarterMargin);
}
make.width.height.mas_equalTo(24);
}];
}
- (void)searchClick
{
TFSearchViewController *vc = [[TFSearchViewController alloc] init];
vc.productionType = self.productionType;
[self.navigationController pushViewController:vc animated:YES];
}
- (void)secChooseButtonClick
{
if (TFSystemInfoManager.sexChannel == 1) { //
if (self.isNavDark) {
[self.sexChooseButton setImage:[UIImage imageNamed:@"comic_mall_girl"] forState:UIControlStateNormal];
} else {
[self.sexChooseButton setImage:[UIImage imageNamed:@"comic_mall_girl_dark"] forState:UIControlStateNormal];
}
[TFPromptManager showPromptViewWithStatus:TFPromptStatusSuccess promptTitle:TFLocalizedString(@"已切换至女频")];
TFSystemInfoManager.sexChannel = 2;
} else { //
if (self.isNavDark) {
[self.sexChooseButton setImage:[UIImage imageNamed:@"comic_mall_boy"] forState:UIControlStateNormal];
} else {
[self.sexChooseButton setImage:[UIImage imageNamed:@"comic_mall_boy_dark"] forState:UIControlStateNormal];
}
[TFPromptManager showPromptViewWithStatus:TFPromptStatusSuccess promptTitle:TFLocalizedString(@"已切换至男频")];
TFSystemInfoManager.sexChannel = 1;
}
[[NSNotificationCenter defaultCenter] postNotificationName:Notification_Channel_Change object:nil];
}
- (void)pageTitleView:(SGPageTitleView *)pageTitleView selectedIndex:(NSInteger)selectedIndex
{
[self.pageContentView setPageContentCollectionViewCurrentIndex:selectedIndex];
}
- (void)pageContentCollectionView:(SGPageContentCollectionView *)pageContentCollectionView progress:(CGFloat)progress originalIndex:(NSInteger)originalIndex targetIndex:(NSInteger)targetIndex
{
[self.pageTitleView setPageTitleViewWithProgress:progress originalIndex:originalIndex targetIndex:targetIndex];
}
- (void)pageContentCollectionView:(SGPageContentCollectionView *)pageContentCollectionView index:(NSInteger)index
{
[self reloadSearchBar];
}
- (void)reloadSearchBar
{
TFBasicViewController *vc = [self.childControllers objectOrNilAtIndex:self.pageTitleView.signBtnIndex];
self.productionType = vc.productionType;
CGFloat contentOffsetY = [[vc valueForKey:@"scrollViewContentOffsetY"] integerValue];
[self changeNavBarColorWithContentOffsetY:contentOffsetY];
}
- (void)netRequest
{
// 书架增加推荐书籍
[TFNetworkTools POST:Shelf_Recommend parameters:nil model:TFRecommendBookModel.class success:^(BOOL isSuccess, TFRecommendBookModel *_Nullable t_model, TFNetworkRequestModel *requestModel) {
if (isSuccess) {
if ([[TFCollectionManager shareManagerWithProductionType:TFProductionTypeNovel] getAllCollection].count == 0) {
for (TFProductionModel *model in t_model.book) {
model.productionType = TFProductionTypeNovel;
model.is_recommend = YES;
[[TFCollectionManager shareManagerWithProductionType:TFProductionTypeNovel] addCollectionWithProductionModel:model atIndex:0];
}
}
if ([[TFCollectionManager shareManagerWithProductionType:TFProductionTypeComic] getAllCollection].count == 0) {
for (TFProductionModel *model in t_model.comic) {
model.productionType = TFProductionTypeComic;
model.is_recommend = YES;
[[TFCollectionManager shareManagerWithProductionType:TFProductionTypeComic] addCollectionWithProductionModel:model atIndex:0];
}
}
if ([[TFCollectionManager shareManagerWithProductionType:TFProductionTypeAudio] getAllCollection].count == 0) {
for (TFProductionModel *model in t_model.audio) {
model.productionType = TFProductionTypeAudio;
model.is_recommend = YES;
[[TFCollectionManager shareManagerWithProductionType:TFProductionTypeAudio] addCollectionWithProductionModel:model atIndex:0];
}
}
}
} failure:nil];
}
- (void)changeNavBarColorWithContentOffsetY:(CGFloat)contentOffsetY
{
_contentOffsetY = contentOffsetY;
CGFloat alpha = [TFColorHelper getAlphaWithContentOffsetY:fabs(contentOffsetY)];
CGFloat rbgColor = [TFColorHelper getColorWithContentOffsetY:fabs(contentOffsetY)];
self.navigationBar.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:alpha];
[self.pageTitleView resetTitleColor:kColorRGBA(rbgColor, rbgColor, rbgColor, 1) titleSelectedColor:kColorRGBA(rbgColor, rbgColor, rbgColor, 1) indicatorColor:kColorRGBA(rbgColor, rbgColor, rbgColor, 1)];
self.searchImageView.tintColor = kColorRGBA(rbgColor, rbgColor, rbgColor, 1);
if (fabs(contentOffsetY) > 60) {
self.isNavDark = YES;
} else {
self.isNavDark = NO;
}
}
- (void)setIsNavDark:(BOOL)isNavDark
{
if (isNavDark) {
if ([[TFViewHelper getCurrentViewController] isEqual:self]) {
[self setStatusBarDefaultStyle];
}
if ([TFUtilsHelper getSiteState].count > 1) {
if (TFSystemInfoManager.sexChannel == 1) { //
[self.sexChooseButton setImage:[UIImage imageNamed:@"comic_mall_boy"] forState:UIControlStateNormal];
} else { //
[self.sexChooseButton setImage:[UIImage imageNamed:@"comic_mall_girl"] forState:UIControlStateNormal];
}
}
} else {
if ([[TFViewHelper getCurrentViewController] isEqual:self]) {
[self setStatusBarLightContentStyle];
}
if ([TFUtilsHelper getSiteState].count > 1) {
if (TFSystemInfoManager.sexChannel == 1) { //
[self.sexChooseButton setImage:[UIImage imageNamed:@"comic_mall_boy_dark"] forState:UIControlStateNormal];
} else { //
[self.sexChooseButton setImage:[UIImage imageNamed:@"comic_mall_girl_dark"] forState:UIControlStateNormal];
}
}
}
_isNavDark = isNavDark;
}
- (void)pushFromMallCenter:(NSNotification *)notification
{
#if TF_Enable_Comic
// 跳转到漫画相关内容
if ([notification.object isEqualToString:@"comic"]) {
for (NSInteger i = 0; i < self.pageContentView.childViewControllers.count ; i ++) {
if ([[self.pageContentView.childViewControllers objectOrNilAtIndex:i] isKindOfClass:[TFBookStoreComicController class]]) {
self.pageTitleView.resetSelectedIndex = i;
break;
}
}
}
#endif
#if TF_Enable_Book
// 跳转到小说相关内容
if ([notification.object isEqualToString:@"novel"]) {
for (NSInteger i = 0; i < self.pageContentView.childViewControllers.count ; i ++) {
if ([[self.pageContentView.childViewControllers objectOrNilAtIndex:i] isKindOfClass:[TFBookStoreNovelController class]]) {
self.pageTitleView.resetSelectedIndex = i;
break;
}
}
}
#endif
#if TF_Enable_Audio
// 跳转到听书相关内容
if ([notification.object isEqualToString:@"audio"]) {
for (NSInteger i = 0; i < self.pageContentView.childViewControllers.count ; i ++) {
if ([[self.pageContentView.childViewControllers objectOrNilAtIndex:i] isKindOfClass:[TFBookStoreAudioController class]]) {
self.pageTitleView.resetSelectedIndex = i;
break;
}
}
}
#endif
}
@end