小说绘上架版本

This commit is contained in:
xtfei2011
2021-02-07 11:24:08 +08:00
commit ee5c1c8b12
1762 changed files with 115892 additions and 0 deletions
@@ -0,0 +1,22 @@
//
// TFNovelBookMarkViewController.h
// TFReader
//
// Created by 谢腾飞 on 2020/12/15.
// Copyright © 2020 xtfei_2011@126.com. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "TFBasicViewController.h"
NS_ASSUME_NONNULL_BEGIN
@interface TFNovelBookMarkViewController : TFBasicViewController
@property (nonatomic ,strong) TFProductionModel *bookModel;
@property (nonatomic ,assign) BOOL isReader;
@end
NS_ASSUME_NONNULL_END
@@ -0,0 +1,146 @@
//
// TFNovelBookMarkViewController.m
// TFReader
//
// Created by 谢腾飞 on 2020/12/15.
// Copyright © 2020 xtfei_2011@126.com. All rights reserved.
//
#import "TFNovelBookMarkViewController.h"
#import "TFBookMarkModel.h"
#import "NSObject+Observer.h"
#import "TFReadRecordManager.h"
#import "TFReaderBookManager.h"
#import "TFReaderSettingHelper.h"
#import "NSString+TFExtension.h"
#import "TYTextContainer.h"
#import "NSAttributedString+TReaderPage.h"
#import "TFReadNovelViewController.h"
#import "TFNovelBookMarkViewCell.h"
@interface TFNovelBookMarkViewController ()<UITableViewDataSource, UITableViewDelegate>
@end
@implementation TFNovelBookMarkViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self createSubviews];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSMutableArray<TFBookMarkModel *> *t_arr = [[TFReadRecordManager bookMark:[TFUtilsHelper formatStringWithInteger:self.bookModel.production_id]] mutableCopy];
t_arr = [[t_arr sortedArrayUsingComparator:^NSComparisonResult(TFBookMarkModel * _Nonnull obj1, TFBookMarkModel * _Nonnull obj2) {
if ([obj1.timestamp integerValue] < [obj2.timestamp integerValue]) {
return NSOrderedDescending;
}
return NSOrderedAscending;
}] mutableCopy];
NSSortDescriptor *sort1 = [NSSortDescriptor sortDescriptorWithKey:KEY_PATH(t_arr.firstObject, chapterSort) ascending:YES];
t_arr = [[t_arr sortedArrayUsingDescriptors:@[sort1]] mutableCopy];
self.dataSourceArray = t_arr;
[self.mainTableView reloadData];
[self.mainTableView xtfei_endLoading];
}
- (void)createSubviews {
[self setBasicLayout];
[self.view addSubview:self.mainTableView];
self.mainTableView.delegate = self;
self.mainTableView.dataSource = self;
self.mainTableView.contentInset = UIEdgeInsetsMake(0, 0, PUB_TABBAR_OFFSET, 0);
[self.mainTableView registerClass:[TFNovelBookMarkViewCell class] forCellReuseIdentifier:@"Identifier"];
[self.mainTableView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.view);
}];
[self setEmptyOnView:self.mainTableView title:TFLocalizedString(@"暂无书签记录") buttonTitle:@"" tapBlock:^{}];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.dataSourceArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
TFNovelBookMarkViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Identifier" forIndexPath:indexPath];
[cell setBookMarkModel:self.dataSourceArray[indexPath.row]];
return cell;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleDelete;
}
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
return TFLocalizedString(@"删除");
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
TFBookMarkModel *t_model = self.dataSourceArray[indexPath.row];
[self.dataSourceArray removeObjectAtIndex:indexPath.row];
if (@available(iOS 11.0, *)) {
[tableView performBatchUpdates:^{
[tableView deleteRowAtIndexPath:indexPath withRowAnimation:UITableViewRowAnimationLeft];
} completion:^(BOOL finished) {
[TFReadRecordManager removeBookMark:t_model];
[tableView xtfei_endLoading];
}];
} else {
[CATransaction begin];
[CATransaction setCompletionBlock:^{
[TFReadRecordManager removeBookMark:t_model];
[tableView xtfei_endLoading];
}];
[tableView beginUpdates];
[tableView deleteRowAtIndexPath:indexPath withRowAnimation:UITableViewRowAnimationLeft];
[tableView endUpdates];
[CATransaction commit];
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
TFBookMarkModel *t_model = self.dataSourceArray[indexPath.row];
if (self.isReader) {
[[NSNotificationCenter defaultCenter] postNotificationName:NSNOtification_Book_Mark object:@{[NSString stringWithFormat:@"%zd", t_model.chapterSort] : [NSString stringWithFormat:@"%zd", t_model.specificIndex]}];
[self.navigationController popViewControllerAnimated:YES];
} else {
TFReadNovelViewController *vc = [[TFReadNovelViewController alloc] initWithSpecificIndex:t_model.specificIndex chapterSort:t_model.chapterSort];
vc.bookModel = self.bookModel;
vc.book_id = self.bookModel.production_id;
[self.navigationController pushViewController:vc animated:YES];
}
}
//section头部间距
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return kHalfMargin;
}
//section头部视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, kHalfMargin)];
view.backgroundColor = [UIColor clearColor];
return view;
}
- (void)setBasicLayout {
[self hiddenNavigationBar:YES];
}
@end
@@ -0,0 +1,28 @@
//
// TFNovelCatalogueBookmarkController.h
// TFReader
//
// Created by 谢腾飞 on 2020/12/15.
// Copyright © 2020 xtfei_2011@126.com. All rights reserved.
//
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@class TFProductionModel;
@interface TFNovelCatalogueBookmarkController : TFBasicViewController
@property (nonatomic ,strong) TFProductionModel *bookModel;
@property (nonatomic ,assign) BOOL isReader;
@property (nonatomic ,copy) NSString *book_id;
@property (nonatomic ,assign) NSUInteger currentIndex;
@property (nonatomic ,assign) BOOL isBookDetailPush;
@end
NS_ASSUME_NONNULL_END
@@ -0,0 +1,124 @@
//
// TFNovelCatalogueBookmarkController.m
// TFReader
//
// Created by 谢腾飞 on 2020/12/15.
// Copyright © 2020 xtfei_2011@126.com. All rights reserved.
//
#import "TFNovelCatalogueBookmarkController.h"
#import "TFNovelCatalogueViewController.h"
#import "TFNovelBookMarkViewController.h"
@interface TFNovelCatalogueBookmarkController ()<SGPageContentCollectionViewDelegate, SGPageTitleViewDelegate>
@property (nonatomic ,strong) SGPageTitleView *pageTitleView;
@property (nonatomic ,strong) SGPageContentCollectionView *pageContentCollectionView;
@property (nonatomic ,weak) TFNovelCatalogueViewController *bookDir;
@property (nonatomic ,weak) UIButton *rightButton;
@end
@implementation TFNovelCatalogueBookmarkController
- (void)viewDidLoad
{
[super viewDidLoad];
[self initialize];
[self createSubviews];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self setStatusBarDefaultStyle];
[[NSNotificationCenter defaultCenter] postNotificationName:Notification_Hidden_Tabbar object:nil];
}
- (void)initialize
{
[self setNavigationBarTitle:@""];
[self hiddenSeparator];
[self setNavigationBarRightButton:({
UIButton *rightBtn = [[UIButton alloc] init];
self.rightButton = rightBtn;
rightBtn.backgroundColor = [UIColor clearColor];
rightBtn.adjustsImageWhenHighlighted = NO;
[rightBtn setImage:[UIImage imageNamed:@"book_directory_order"] forState:UIControlStateNormal];
[rightBtn setImageEdgeInsets:UIEdgeInsetsMake(2, 2, 2, 2)];
[rightBtn addTarget:self action:@selector(orderChapter:) forControlEvents:UIControlEventTouchUpInside];
rightBtn;
})];
[self.rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(self.navigationBar.navTitleLabel);
make.right.equalTo(self.view).offset(-kMargin);
make.size.mas_equalTo(CGSizeMake(25, 25));
}];
}
- (void)createSubviews
{
TFNovelCatalogueViewController *bookDir = [[TFNovelCatalogueViewController alloc] init];
bookDir.isReader = self.isReader;
self.bookDir = bookDir;
if (self.bookModel) {
bookDir.bookModel = self.bookModel;
} else {
bookDir.book_id = self.book_id;
}
bookDir.isBookDetailPush = self.isBookDetailPush;
[self addChildViewController:bookDir];
TFNovelBookMarkViewController *bookMark = [[TFNovelBookMarkViewController alloc] init];
bookMark.isReader = self.isReader;
bookMark.bookModel = self.bookModel;
[self addChildViewController:bookMark];
self.pageConfigure.indicatorColor = kMainColor;
self.pageConfigure.indicatorStyle = SGIndicatorStyleDynamic;
self.pageConfigure.indicatorHeight = 3;
self.pageConfigure.bounces = NO;
self.pageConfigure.indicatorFixedWidth = 10;
self.pageConfigure.indicatorDynamicWidth = 14;
self.pageConfigure.indicatorToBottomDistance = 3;
self.pageConfigure.titleSelectedColor = kBlackColor;
self.pageConfigure.titleSelectedFont = kFont18;
self.pageConfigure.titleFont = kFont16;
self.pageConfigure.titleColor = kBlackColor;
self.pageContentCollectionView = [[SGPageContentCollectionView alloc] initWithFrame:CGRectMake(0, PUB_NAVBAR_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT - PUB_NAVBAR_HEIGHT) parentVC:self childVCs:@[bookDir, bookMark]];
_pageContentCollectionView.delegatePageContentCollectionView = self;
[self.view addSubview:self.pageContentCollectionView];
CGFloat width1 = [TFViewHelper getDynamicWidthWithLabelFont:kFont14 labelHeight:self.pageViewHeight labelText:TFLocalizedString(@"目录") maxWidth:120];
CGFloat width2 = [TFViewHelper getDynamicWidthWithLabelFont:kFont15 labelHeight:self.pageViewHeight labelText:TFLocalizedString(@"书签") maxWidth:120];
CGFloat width = width1 + width2 + kLabelHeight + kMargin;
self.pageTitleView = [SGPageTitleView pageTitleViewWithFrame:CGRectMake((SCREEN_WIDTH - width) / 2.0f, PUB_NAVBAR_OFFSET + kMargin, width, self.pageViewHeight) delegate:self titleNames:@[TFLocalizedString(@"目录"), TFLocalizedString(@"书签")] configure:self.pageConfigure];
self.pageTitleView.backgroundColor = kWhiteColor;
[self.navigationBar addSubview:self.pageTitleView];
}
- (void)pageTitleView:(SGPageTitleView *)pageTitleView selectedIndex:(NSInteger)selectedIndex
{
[self.pageContentCollectionView 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.rightButton.hidden = index != 0;
}
- (void)orderChapter:(UIButton *)sender {
[self.bookDir orderChapter:sender];
}
@end
@@ -0,0 +1,29 @@
//
// TFNovelCatalogueViewController.h
// TFReader
//
// Created by 谢腾飞 on 2020/12/15.
// Copyright © 2020 xtfei_2011@126.com. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "TFBasicViewController.h"
NS_ASSUME_NONNULL_BEGIN
@interface TFNovelCatalogueViewController : TFBasicViewController
@property (nonatomic ,strong) TFProductionModel *bookModel;
@property (nonatomic ,copy) NSString *book_id;
@property (nonatomic ,assign) BOOL isBookDetailPush;
/// 是不是从阅读器进入
@property (nonatomic ,assign) BOOL isReader;
- (void)orderChapter:(UIButton *)sender;
@end
NS_ASSUME_NONNULL_END
@@ -0,0 +1,512 @@
//
// TFNovelCatalogueViewController.m
// TFReader
//
// Created by 谢腾飞 on 2020/12/15.
// Copyright © 2020 xtfei_2011@126.com. All rights reserved.
//
#import "TFNovelCatalogueViewController.h"
#import "TFReadNovelViewController.h"
#import "TFNovelCatalogueViewCell.h"
#import "TFReaderSettingHelper.h"
#import "TFReaderBookManager.h"
#import "TFReadRecordManager.h"
#import "TFCatalogModel.h"
@interface TFNovelCatalogueViewController ()<UITableViewDelegate, UITableViewDataSource, UIGestureRecognizerDelegate>
{
BOOL isScroll;//进入目录滚动到指定位置,只进行一次
BOOL first;// 第一次进入目录
}
@property (nonatomic, strong) NSIndexPath *selectedIndexPath;
@property (nonatomic, assign) BOOL firstRequest;
/// 目录是不是逆序
@property (nonatomic, assign) BOOL isReverseOrder;
/// 已购买章节
@property (nonatomic, strong) NSMutableArray<NSString *> *purchasedChapters;
@property (nonatomic, strong) NSMutableArray<TFCatalogListModel *> *dataSourceArray;
@end
@implementation TFNovelCatalogueViewController
@synthesize dataSourceArray = _dataSourceArray;
- (void)viewDidLoad
{
[super viewDidLoad];
[self initialize];
[self createSubviews];
if (self.dataSourceArray.count == 0) {
[self requestCatalogWithDown:YES];
}
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSInteger index = 0;
if (self.dataSourceArray.count > 0) {
NSInteger chapter_id = [[TFReadRecordManager shareManagerWithProductionType:TFProductionTypeNovel] getReadingRecordChapter_idWithProduction_id:[self.book_id integerValue]];
for (TFCatalogListModel *t_model in self.dataSourceArray) {
if ([t_model.chapter_id integerValue] == chapter_id) {
break;
}
index++;
}
if (index == self.dataSourceArray.count) {// 如果没有阅读记录
index = 0;
}
}
[self.mainTableView reloadData];
dispatch_async(dispatch_get_main_queue(), ^{
if (self.dataSourceArray.count > 0) {
[self.mainTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0] animated:NO scrollPosition:UITableViewScrollPositionTop];
}
});
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
first = NO;
}
- (void)initialize
{
self.dataSourceArray = [NSMutableArray array];
self.purchasedChapters = [NSMutableArray array];
first = YES;
[self hiddenNavigationBar:YES];
if (self.bookModel) {
self.book_id = [TFUtilsHelper formatStringWithInteger:self.bookModel.production_id];
}
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(paySuccess:) name:Notification_Production_Pay_Success object:nil];
// 获取本地目录列表
if (![TFNetworkManager networkingStatus]) {
NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
path = [path stringByAppendingPathComponent:[TFUtilsHelper stringToMD5:@"book_catalog"]];
NSString *catalogName = [NSString stringWithFormat:@"%@_%@", self.book_id, @"catalog"];
NSString *fullPath = [path stringByAppendingFormat:@"/%@.plist", [TFUtilsHelper stringToMD5:catalogName]];
if ([[NSFileManager defaultManager] fileExistsAtPath:fullPath]) {
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:fullPath];
TFCatalogModel *catalog = [TFCatalogModel modelWithDictionary:dict];
self.dataSourceArray = [catalog.list mutableCopy];
} else {
self.dataSourceArray = [self.bookModel.list mutableCopy];
}
} else {
if (self.bookModel.list.count != 0) {
self.dataSourceArray = [self.bookModel.list mutableCopy];
}
}
[self setEmptyOnView:self.mainTableView title:TFLocalizedString(@"暂无目录列表") buttonTitle:@"" tapBlock:^{
}];
}
- (void)paySuccess:(NSNotification *)noti {
NSArray<NSString *> *t_arr = noti.object;
[self.purchasedChapters addObjectsFromArray:t_arr];
}
- (void)createSubviews {
self.mainTableView.delegate = self;
self.mainTableView.dataSource = self;
self.mainTableView.showsVerticalScrollIndicator = YES;
self.mainTableView.showsHorizontalScrollIndicator = NO;
[self.mainTableView registerClass:TFNovelCatalogueViewCell.class forCellReuseIdentifier:@"WXBookDirectoryTableViewCell"];
[self.view addSubview:self.mainTableView];
self.mainTableView.contentInset = UIEdgeInsetsMake(0, 0, PUB_TABBAR_OFFSET, 0);
[self.mainTableView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.view);
}];
WS(weakSelf)
self.mainTableView.mj_header = [TFRefreshHeader headerWithRefreshingBlock:^{
[weakSelf requestCatalogWithDown:NO];
}];
self.mainTableView.mj_footer = [TFRefreshFooter footerWithRefreshingBlock:^{
[weakSelf requestCatalogWithDown:YES];
}];
TFCatalogListModel *list = self.dataSourceArray.firstObject;
if ([list.previou_chapter isEqualToString:@"0"]) {
[self.mainTableView hideRefreshHeader];
}
list = self.dataSourceArray.lastObject;
if ([list.next_chapter isEqualToString:@"0"]) {
[self.mainTableView hideRefreshFooter];
}
if (self.dataSourceArray.count > 0) {
[self.mainTableView xtfei_hideEmptyView];
} else {
[self.mainTableView xtfei_showEmptyView];
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.dataSourceArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
TFNovelCatalogueViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WXBookDirectoryTableViewCell" forIndexPath:indexPath];
TFCatalogListModel *t_model = [self.dataSourceArray objectOrNilAtIndex:indexPath.row];
cell.hiddenEndLine = (indexPath.row == self.dataSourceArray.count - 1);
if ([self.purchasedChapters containsObject:t_model.chapter_id]) {
t_model.preview = NO;
[self.purchasedChapters removeObject:t_model.chapter_id];
}
cell.chapterModel = t_model;
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 44;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
WS(weakSelf)
self.selectedIndexPath = indexPath;
NSInteger chapterIndex = indexPath.row;
// if (self.isReverseOrder) {
// chapterIndex = self.dataSourceArray.count - 1 - chapterIndex;
// }
TFCatalogListModel *t_model = self.dataSourceArray[chapterIndex];
[[TFReaderSettingHelper sharedManager] setLocationMemoryOfChapterIndex:t_model.display_order pagerIndex:0 book_id:[self.book_id integerValue]];
[TFReaderBookManager sharedManager].currentChapterIndex = t_model.display_order;
[TFReaderBookManager sharedManager].currentPagerIndex = 0;
if (self.isReader) {
UIViewController *vc = nil;
for (UIViewController *obj in self.navigationController.viewControllers) {
if ([obj isKindOfClass:TFReadNovelViewController.class]) {
vc = obj;
break;
}
}
if (vc) {
[[NSNotificationCenter defaultCenter] postNotificationName:NSNotification_Retry_Chapter object:nil];
[self.navigationController popToViewController:vc animated:YES];
} else {
[self.navigationController popViewControllerAnimated:YES];
}
} else {
if (self.isBookDetailPush) {
TFReadNovelViewController *vc = [[TFReadNovelViewController alloc] init];
vc.book_id = [self.book_id integerValue];
vc.bookModel = self.bookModel;
[self.navigationController pushViewController:vc animated:YES];
} else {
[[NSNotificationCenter defaultCenter] postNotificationName:NSNotification_Retry_Chapter object:nil];
[weakSelf.navigationController popViewControllerAnimated:YES];
}
}
dispatch_async(dispatch_get_main_queue(), ^{
[tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];
});
}
//section头部间距
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return kHalfMargin;
}
//section头部视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *view = [[UIView alloc] init];
view.backgroundColor = [UIColor clearColor];
return view;
}
- (void)orderChapter:(UIButton *)sender {
if (!self.isReverseOrder) {
[sender setImage:[UIImage imageNamed:@"book_directory_reverse"] forState:UIControlStateNormal];
self.isReverseOrder = YES;
} else {
[sender setImage:[UIImage imageNamed:@"book_directory_order"] forState:UIControlStateNormal];
self.isReverseOrder = NO;
}
if (![TFNetworkManager networkingStatus]) {
self.dataSourceArray = [[[self.dataSourceArray reverseObjectEnumerator] allObjects] mutableCopy];
[self.mainTableView reloadData];
return;
}
NSDictionary *params = @{
@"book_id" : self.book_id,
@"order_by" : self.isReverseOrder ? @(2) : @(1)
};
WS(weakSelf)
[TFNetworkTools POST:Book_New_Catalog parameters:params model:TFCatalogModel.class success:^(BOOL isSuccess, TFCatalogModel * _Nullable t_model, TFNetworkRequestModel * _Nonnull requestModel) {
if (isSuccess) {
[weakSelf.dataSourceArray removeAllObjects];
[weakSelf.dataSourceArray addObjectsFromArray:t_model.list];
[weakSelf.mainTableView reloadData];
if (@available(iOS 11.0, *)) {
[weakSelf.mainTableView performBatchUpdates:nil completion:^(BOOL finished) {
[weakSelf.mainTableView scrollToTopAnimated:NO];
}];
} else {
dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf.mainTableView scrollToTopAnimated:NO];
});
}
[weakSelf.mainTableView xtfei_endLoading];
[weakSelf.mainTableView hideRefreshHeader];
[weakSelf.mainTableView showRefreshFooter];
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
[weakSelf.mainTableView xtfei_endLoading];
}];
}
// isDownYES 往下翻页,NO 往上翻页
- (void)requestCatalogWithDown:(BOOL)isDown
{
if (![TFNetworkManager networkingStatus]) {
[self.mainTableView endRefreshing];
return;
}
// 判断目录是不是倒序展示
if (self.isReverseOrder) {
isDown = !isDown;
}
// 章节id
NSString *chapter_id;
// 翻页方式
NSString *scrollType = isDown ? @"1" : @"2";
if (first) {// 判断是不是首次进入目录页面
if (self.dataSourceArray.count > 0) {
NSArray<TFCatalogListModel *> *list = self.dataSourceArray;
chapter_id = list.lastObject.next_chapter;
if ([chapter_id isEqualToString:@"0"]) {
[self.mainTableView endRefreshing];
[self.mainTableView hideRefreshFooter];
return;
}
} else {
chapter_id = [NSString stringWithFormat:@"%zd", [[TFReadRecordManager shareManagerWithProductionType:TFProductionTypeNovel] getReadingRecordChapter_idWithProduction_id:[self.book_id integerValue]]];
}
[self requestCatalogWithScrollType:scrollType chapter_id:chapter_id];
return;
}
// 获取正确的章节id
NSArray<TFCatalogListModel *> *list = self.dataSourceArray;
if (isDown && self.isReverseOrder) {// 往下翻页,倒序
chapter_id = list.firstObject.next_chapter;
}
if (isDown && !self.isReverseOrder) {// 往下翻页,正序
chapter_id = list.lastObject.next_chapter;
}
if (!isDown && self.isReverseOrder) {// 往上翻页,倒序
chapter_id = list.lastObject.previou_chapter;
}
if (!isDown && !self.isReverseOrder) {// 往上翻页,正序
chapter_id = list.firstObject.previou_chapter;
}
if ([chapter_id isEqualToString:@"0"]) {
[self.mainTableView endRefreshing];
if (isDown) {
[self.mainTableView hideRefreshFooter];
} else {
[self.mainTableView hideRefreshHeader];
}
return;
}
[self requestCatalogWithScrollType:scrollType chapter_id:chapter_id];
}
// scrollType 1:向下加载;2:向上加载
- (void)requestCatalogWithScrollType:(NSString *)scrollType chapter_id:(NSString *)chapter_id {
if (!self.book_id || !chapter_id) {
if (self.dataSourceArray.count > 0) {
[self.mainTableView xtfei_hideEmptyView];
} else {
[self.mainTableView xtfei_showEmptyView];
}
[self.mainTableView endRefreshing];
return;
}
NSDictionary *params = @{
@"book_id" : self.book_id,
@"chapter_id" : chapter_id,
@"scroll_type" : scrollType,
};
WS(weakSelf)
[TFNetworkTools POST:Book_New_Catalog parameters:params model:TFCatalogModel.class success:^(BOOL isSuccess, TFCatalogModel * _Nullable t_model, TFNetworkRequestModel * _Nonnull requestModel) {
[weakSelf.mainTableView endRefreshing];
if (isSuccess) {
BOOL isDown = [scrollType isEqualToString:@"1"];
if (isDown && t_model.list.count == 1 && weakSelf.dataSourceArray.count == 0) {
[weakSelf requestCatalogWithScrollType:@"2" chapter_id:chapter_id];
return;
}
[weakSelf updateTableViewWithList:t_model.list isDown:isDown firstRequest:weakSelf.firstRequest];
weakSelf.firstRequest = YES;
// 隐藏上拉/下拉刷新控件
if (isDown && self.isReverseOrder) {// 往下翻页,倒序
if ([t_model.list.lastObject.next_chapter isEqualToString:@"0"]) {
[weakSelf.mainTableView hideRefreshHeader];
}
}
if (isDown && !self.isReverseOrder) {// 往下翻页,正序
if ([t_model.list.lastObject.next_chapter isEqualToString:@"0"]) {
[weakSelf.mainTableView hideRefreshFooter];
}
}
if (!isDown && self.isReverseOrder) {// 往上翻页,倒序
if ([t_model.list.firstObject.previou_chapter isEqualToString:@"0"]) {
[weakSelf.mainTableView hideRefreshFooter];
}
}
if (!isDown && !self.isReverseOrder) {// 往上翻页,正序
if ([t_model.list.firstObject.previou_chapter isEqualToString:@"0"]) {
[weakSelf.mainTableView hideRefreshHeader];
}
}
} else {
[TFPromptManager showPromptViewWithStatus:TFPromptStatusError promptTitle:requestModel.msg];
}
if (weakSelf.dataSourceArray.count > 0) {
[weakSelf.mainTableView xtfei_hideEmptyView];
} else {
[weakSelf.mainTableView xtfei_showEmptyView];
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
[TFPromptManager showPromptWithError:error defaultText:nil];
[weakSelf.mainTableView endRefreshing];
if (weakSelf.dataSourceArray.count > 0) {
[weakSelf.mainTableView xtfei_hideEmptyView];
} else {
[weakSelf.mainTableView xtfei_showEmptyView];
}
}];
}
// 更新tableView
- (void)updateTableViewWithList:(NSArray<TFCatalogListModel *> *)list isDown:(BOOL)isDown firstRequest:(BOOL)firstRequest {
NSMutableArray<NSIndexPath *> *pathArr = [NSMutableArray array];
// 刷新前的数据个数
NSUInteger oldCount = self.dataSourceArray.count;
// 填充数据
if (isDown && self.isReverseOrder) {// 往下翻页,倒序
list = list.reverseObjectEnumerator.allObjects;
[self.dataSourceArray insertObjects:list atIndex:0];
for (NSUInteger i = 0; i < list.count; i++) {
[pathArr addObject:[NSIndexPath indexPathForRow:i inSection:0]];
}
}
if (isDown && !self.isReverseOrder) {// 往下翻页,正序
[self.dataSourceArray addObjectsFromArray:list];
for (NSUInteger i = oldCount; i < self.dataSourceArray.count; i++) {
[pathArr addObject:[NSIndexPath indexPathForRow:i inSection:0]];
}
}
if (!isDown && self.isReverseOrder) {// 往上翻页,倒序
list = list.reverseObjectEnumerator.allObjects;
[self.dataSourceArray addObjectsFromArray:list];
for (NSUInteger i = oldCount; i < self.dataSourceArray.count; i++) {
[pathArr addObject:[NSIndexPath indexPathForRow:i inSection:0]];
}
}
if (!isDown && !self.isReverseOrder) {// 往上翻页,正序
[self.dataSourceArray insertObjects:list atIndex:0];
for (NSUInteger i = 0; i < list.count; i++) {
[pathArr addObject:[NSIndexPath indexPathForRow:i inSection:0]];
}
}
self.bookModel.list = self.dataSourceArray;
// 判断是不是第一次刷新
if (self.mainTableView.visibleCells.count == 0) {
[self.mainTableView reloadData];
NSInteger chapter_id = [[TFReadRecordManager shareManagerWithProductionType:TFProductionTypeNovel] getReadingRecordChapter_idWithProduction_id:[self.book_id integerValue]];
NSInteger __block index = -1;
[self.dataSourceArray enumerateObjectsUsingBlock:^(TFCatalogListModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if ([obj.chapter_id integerValue] == chapter_id) {
index = idx;
*stop = YES;
}
}];
dispatch_async(dispatch_get_main_queue(), ^{
if (index != -1) {
[self.mainTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0] animated:NO scrollPosition:UITableViewScrollPositionTop];
}
});
if (isDown == NO) {
dispatch_async(dispatch_get_main_queue(), ^{
if ((NSInteger)self.dataSourceArray.count - 2 >= 0) {
[self.mainTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:self.dataSourceArray.count - 2 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionBottom];
}
});
}
return;
}
[UIView performWithoutAnimation:^{
[self.mainTableView insertRowsAtIndexPaths:pathArr withRowAnimation:UITableViewRowAnimationNone];
}];
if (isDown && self.isReverseOrder) {// 往下翻页,倒序
[self.mainTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:list.count inSection:0] animated:NO scrollPosition:UITableViewScrollPositionTop];
}
if (isDown && !self.isReverseOrder) {// 往下翻页,正序
[self.mainTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:oldCount inSection:0] animated:NO scrollPosition:UITableViewScrollPositionBottom];
}
if (!isDown && self.isReverseOrder) {// 往上翻页,倒序
[self.mainTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:oldCount inSection:0] animated:NO scrollPosition:UITableViewScrollPositionBottom];
}
if (!isDown && !self.isReverseOrder) {// 往上翻页,正序
[self.mainTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:list.count inSection:0] animated:NO scrollPosition:UITableViewScrollPositionTop];
}
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end