小说绘上架版本

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,20 @@
//
// TFComicDownloadContentChoiceController.h
// TFReader
//
// Created by 谢腾飞 on 2020/12/16.
// Copyright © 2020 xtfei_2011@126.com. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "TFDownloadViewController.h"
NS_ASSUME_NONNULL_BEGIN
@interface TFComicDownloadContentChoiceController : TFDownloadViewController
@property (nonatomic ,strong) TFProductionModel *comicModel;
@end
NS_ASSUME_NONNULL_END
@@ -0,0 +1,327 @@
//
// TFComicDownloadContentChoiceController.m
// TFReader
//
// Created by 谢腾飞 on 2020/12/16.
// Copyright © 2020 xtfei_2011@126.com. All rights reserved.
//
#import "TFComicDownloadContentChoiceController.h"
#import "TFComicDownloadViewCell.h"
#import "WXYZ_ChapterBottomPayBar.h"
#import "TFComicDownloadModel.h"
#import "WXYZ_ComicDownloadManager.h"
@interface TFComicDownloadContentChoiceController ()<UICollectionViewDelegate, UICollectionViewDataSource>
{
UIView *toolBarView;
YYLabel *toolBarLeftLabel;
UILabel *toolBarRightLabel;
UIButton *deleteButton;
UIButton *selectAllButton;
}
@end
@implementation TFComicDownloadContentChoiceController
- (void)viewDidLoad
{
[super viewDidLoad];
[self initialize];
[self createSubviews];
[self netRequest];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self setStatusBarDefaultStyle];
}
- (void)initialize
{
[self setNavigationBarTitle:self.navTitle?:TFLocalizedString(@"选择章节")];
self.view.backgroundColor = kGrayViewColor;
}
- (void)createSubviews
{
TFButton *sortButton = [[TFButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - kHalfMargin - 60, PUB_NAVBAR_HEIGHT - 30 - kQuarterMargin, 60, 30) buttonTitle:TFLocalizedString(@"正序") buttonImageName:@"comic_positive_order" buttonIndicator:TFButtonIndicatorImageRightBothRight];
sortButton.selected = NO;
sortButton.buttonImageScale = 0.4;
sortButton.buttonTitleFont = kFont11;
sortButton.graphicDistance = 5;
sortButton.buttonTitleColor = kGrayTextColor;
[sortButton addTarget:self action:@selector(sortClick:) forControlEvents:UIControlEventTouchUpInside];
[self.navigationBar addSubview:sortButton];
self.mainCollectionViewFlowLayout.minimumLineSpacing = kHalfMargin;
self.mainCollectionViewFlowLayout.minimumInteritemSpacing = kHalfMargin;
self.mainCollectionViewFlowLayout.itemSize = CGSizeMake((SCREEN_WIDTH - 5 * kHalfMargin) / 4, 40);
self.mainCollectionViewFlowLayout.sectionInset = UIEdgeInsetsMake(kHalfMargin, kHalfMargin, kHalfMargin, kHalfMargin);
self.mainCollectionView.delegate = self;
self.mainCollectionView.dataSource = self;
self.mainCollectionView.alwaysBounceVertical = YES;
[self.mainCollectionView registerClass:[TFComicDownloadViewCell class] forCellWithReuseIdentifier:@"TFComicDownloadViewCell"];
[self.view addSubview:self.mainCollectionView];
[self.mainCollectionView 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_NAVBAR_HEIGHT - PUB_TABBAR_HEIGHT - 20);
}];
toolBarView = [[UIView alloc] init];
toolBarView.backgroundColor = kWhiteColor;
[self.view addSubview:toolBarView];
[toolBarView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(0);
make.bottom.mas_equalTo(self.view.mas_bottom);
make.width.mas_equalTo(self.view.mas_width);
make.height.mas_equalTo(PUB_TABBAR_HEIGHT + 20);
}];
toolBarLeftLabel = [[YYLabel alloc] init];
toolBarLeftLabel.textColor = kGrayTextColor;
toolBarLeftLabel.textContainerInset = UIEdgeInsetsMake(3, kHalfMargin, 0, 0);
toolBarLeftLabel.textAlignment = NSTextAlignmentLeft;
toolBarLeftLabel.font = kFont10;
toolBarLeftLabel.backgroundColor = kGrayDeepViewColor;
[toolBarView addSubview:toolBarLeftLabel];
[toolBarLeftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(0);
make.top.mas_equalTo(0);
make.width.mas_equalTo(toolBarView.mas_width);
make.height.mas_equalTo(20);
}];
toolBarRightLabel = [[UILabel alloc] init];
toolBarRightLabel.text = TFLocalizedString(@"已选0话");
toolBarRightLabel.textColor = kGrayTextColor;
toolBarRightLabel.textAlignment = NSTextAlignmentRight;
toolBarRightLabel.font = kFont10;
toolBarRightLabel.backgroundColor = [UIColor clearColor];
[toolBarView addSubview:toolBarRightLabel];
[toolBarRightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.mas_equalTo(toolBarView.mas_right).with.offset(- kHalfMargin);
make.top.mas_equalTo(0);
make.width.mas_equalTo(toolBarView.mas_width).with.multipliedBy(0.5);
make.height.mas_equalTo(20);
}];
selectAllButton = [UIButton buttonWithType:UIButtonTypeCustom];
[selectAllButton setTitle:TFLocalizedString(@"全选") forState:UIControlStateNormal];
[selectAllButton setTitleColor:kBlackColor forState:UIControlStateNormal];
[selectAllButton.titleLabel setFont:kMainFont];
[selectAllButton addTarget:self action:@selector(checkallClick:) forControlEvents:UIControlEventTouchUpInside];
[toolBarView addSubview:selectAllButton];
[selectAllButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(toolBarView.mas_left);
make.top.mas_equalTo(toolBarRightLabel.mas_bottom);
make.width.mas_equalTo(SCREEN_WIDTH / 2);
make.height.mas_equalTo(PUB_TABBAR_HEIGHT - PUB_TABBAR_OFFSET);
}];
deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
deleteButton.enabled = NO;
[deleteButton setTitle:TFLocalizedString(@"删除") forState:UIControlStateNormal];
[deleteButton setTitleColor:kGrayTextLightColor forState:UIControlStateNormal];
[deleteButton.titleLabel setFont:kMainFont];
[deleteButton addTarget:self action:@selector(deleteClick:) forControlEvents:UIControlEventTouchUpInside];
[toolBarView addSubview:deleteButton];
[deleteButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(selectAllButton.mas_right);
make.top.mas_equalTo(toolBarRightLabel.mas_bottom);
make.right.mas_equalTo(toolBarView.mas_right);
make.height.mas_equalTo(PUB_TABBAR_HEIGHT - PUB_TABBAR_OFFSET);
}];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return self.dataSourceArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"TFComicDownloadViewCell";
TFProductionChapterModel *listModel = [self.dataSourceArray objectOrNilAtIndex:indexPath.row];
listModel.can_read = YES;
TFComicDownloadViewCell __weak *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
cell.chapterModel = listModel;
cell.cellDownloadState = [[self.selectSourceDictionary objectForKey:[TFUtilsHelper formatStringWithInteger:listModel.chapter_id]] integerValue];
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
TFProductionChapterModel *listModel = [self.dataSourceArray objectOrNilAtIndex:indexPath.row];
TFComicDownloadViewCell *cell = (TFComicDownloadViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
switch (cell.cellDownloadState) {
case WXYZ_ProductionDownloadStateDownloaded:
[self setCollectionCellDownloadStateWithChapter_id:listModel.chapter_id downloadState:WXYZ_ProductionDownloadStateSelected];
[self reloadToolBar];
break;
case WXYZ_ProductionDownloadStateSelected:
[self setCollectionCellDownloadStateWithChapter_id:listModel.chapter_id downloadState:WXYZ_ProductionDownloadStateDownloaded];
[self reloadToolBar];
break;
default:
break;
}
}
#pragma mark - 点击事件
- (void)sortClick:(TFButton *)sender
{
if (sender.selected) {
sender.buttonImageName = @"comic_positive_order";
sender.buttonTitle = TFLocalizedString(@"正序");
} else {
sender.buttonImageName = @"comic_reverse_order";
sender.buttonTitle = TFLocalizedString(@"倒序");
}
sender.selected = !sender.selected;
self.dataSourceArray = [[[self.dataSourceArray reverseObjectEnumerator] allObjects] mutableCopy];
[self.mainCollectionView reloadData];
}
- (void)checkallClick:(UIButton *)sender
{
for (NSString *t_chapter_id in self.selectSourceDictionary.allKeys) {
if (!sender.selected) { // 全选状态
if ([[self.selectSourceDictionary objectForKey:t_chapter_id] integerValue] == WXYZ_ProductionDownloadStateDownloaded) {
[self setCollectionCellDownloadStateWithChapter_id:[t_chapter_id integerValue] downloadState:WXYZ_ProductionDownloadStateSelected];
}
} else {
if ([[self.selectSourceDictionary objectForKey:t_chapter_id] integerValue] == WXYZ_ProductionDownloadStateSelected) {
[self setCollectionCellDownloadStateWithChapter_id:[t_chapter_id integerValue] downloadState:WXYZ_ProductionDownloadStateDownloaded];
}
}
}
sender.selected = !sender.selected;
[self reloadToolBar];
}
- (void)deleteClick:(UIButton *)sender
{
NSMutableArray *t_deleteArr = [NSMutableArray array];
for (NSString *t_chapter_id in self.selectSourceDictionary.allKeys) {
if ([[self.selectSourceDictionary objectForKey:t_chapter_id] integerValue] == WXYZ_ProductionDownloadStateSelected) {
[t_deleteArr addObject:t_chapter_id];
}
}
WS(weakSelf)
[[WXYZ_ComicDownloadManager sharedManager] removeDownloadChaptersWithProduction_id:self.comicModel.production_id chapter_ids:t_deleteArr];
[WXYZ_ComicDownloadManager sharedManager].downloadDeleteFinishBlock = ^(NSArray * _Nonnull success_chapter_ids, NSArray * _Nonnull fail_chapter) {
[TFPromptManager showPromptViewWithStatus:TFPromptStatusSuccess promptTitle:TFLocalizedString(@"删除成功")];
[weakSelf netRequest];
};
}
- (void)reloadToolBar
{
NSInteger selectIndex = 0;
NSInteger downloadedIndex = 0;
for (NSString *t_key in self.selectSourceDictionary) {
switch ([[self.selectSourceDictionary objectForKey:t_key] integerValue]) {
case WXYZ_ProductionDownloadStateDownloaded:
downloadedIndex ++;
break;
case WXYZ_ProductionDownloadStateSelected:
selectIndex ++;
break;
default:
break;
}
}
toolBarLeftLabel.text = [NSString stringWithFormat:TFLocalizedString(@"已下载%@话"), [TFUtilsHelper formatStringWithInteger:self.dataSourceArray.count]];
toolBarRightLabel.text = [NSString stringWithFormat:TFLocalizedString(@"已选%@话"), [TFUtilsHelper formatStringWithInteger:selectIndex]];
deleteButton.enabled = NO;
[deleteButton setTitleColor:kGrayTextLightColor forState:UIControlStateNormal];
selectAllButton.enabled = YES;
selectAllButton.selected = NO;
[selectAllButton setTitle:TFLocalizedString(@"全选") forState:UIControlStateNormal];
[selectAllButton mas_updateConstraints:^(MASConstraintMaker *make) {
make.width.mas_equalTo(SCREEN_WIDTH / 2);
}];
if (selectIndex > 0 && downloadedIndex == 0) { // 有选中的文件并且没有未选中的文件
deleteButton.enabled = YES;
[deleteButton setTitleColor:kBlackColor forState:UIControlStateNormal];
selectAllButton.selected = YES;
[selectAllButton setTitle:TFLocalizedString(@"取消全选") forState:UIControlStateNormal];
[selectAllButton setTitleColor:kBlackColor forState:UIControlStateNormal];
} else if (downloadedIndex > 0 && selectIndex > 0) { // 有未选中的文件
deleteButton.enabled = YES;
[deleteButton setTitleColor:kBlackColor forState:UIControlStateNormal];
}
}
- (void)setCollectionCellDownloadStateWithChapter_id:(NSInteger)chapter_id downloadState:(WXYZ_ProductionDownloadState)downloadState
{
TFComicDownloadViewCell *cell = (TFComicDownloadViewCell *)[self.mainCollectionView cellForItemAtIndexPath:(NSIndexPath *)[self.cellIndexDictionary objectForKey:[TFUtilsHelper formatStringWithInteger:chapter_id]]];
cell.cellDownloadState = downloadState;
[self.selectSourceDictionary setObject:[TFUtilsHelper formatStringWithInteger:downloadState] forKey:[TFUtilsHelper formatStringWithInteger:chapter_id]];
}
- (void)netRequest
{
self.dataSourceArray = [[[WXYZ_ComicDownloadManager sharedManager] getDownloadChapterModelArrayWithProduction_id:self.comicModel.production_id] mutableCopy];
// 章节名称序号排序
NSString *tmp = @"";
NSInteger m = self.dataSourceArray.count;
for (NSInteger i = 0; i < m - 1; i++) {
for (NSInteger j = 0; j < m- i - 1; j++) {
if ([[self.dataSourceArray[j] display_label] integerValue] > [[self.dataSourceArray[j + 1] display_label] integerValue]) {
tmp = self.dataSourceArray[j + 1];
self.dataSourceArray[j+1] = self.dataSourceArray[j];
self.dataSourceArray[j] = tmp;
}
}
}
if (self.dataSourceArray.count == 0) {
[self.navigationController popViewControllerAnimated:NO];
return;
}
[self resetSelectSourceDicWithDataSourceArray:[self.dataSourceArray copy] productionType:TFProductionTypeComic];
[self reloadToolBar];
[self.mainCollectionView reloadData];
}
@end
@@ -0,0 +1,24 @@
//
// TFComicDownloadManagerController.h
// TFReader
//
// Created by 谢腾飞 on 2020/12/16.
// Copyright © 2020 xtfei_2011@126.com. All rights reserved.
//
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface TFComicDownloadManagerController : TFBasicViewController
/// 切换编辑状态
@property (nonatomic ,copy ,readonly) BOOL (^editStateBlock)(void);
@property (nonatomic ,copy) void(^changeEditStateBlock)(BOOL status);
@property (nonatomic ,assign) BOOL isEditting;
@end
NS_ASSUME_NONNULL_END
@@ -0,0 +1,422 @@
//
// TFComicDownloadManagerController.m
// TFReader
//
// Created by 谢腾飞 on 2020/12/16.
// Copyright © 2020 xtfei_2011@126.com. All rights reserved.
//
#import "TFComicDownloadManagerController.h"
#import "TFComicBrowseViewController.h"
#import "TFComicDownloadContentChoiceController.h"
#import "TFComicDownloadManagerCell.h"
#import "WXYZ_ComicDownloadManager.h"
#import "TFReadRecordManager.h"
#import "UIView+LayoutCallback.h"
#import "TFCollectionManager.h"
@interface TFComicDownloadManagerController ()<UITableViewDelegate, UITableViewDataSource>
/// 编辑页面
@property (nonatomic, weak) UIView *edittingView;
@property (nonatomic, weak) UIButton *edittingAll;
@property (nonatomic, weak) UIButton *edittingDelete;
@property (nonatomic, strong) NSMutableArray<NSNumber *> *selectedArray;
@property (nonatomic, weak) CALayer *topLayer;
@property (nonatomic, weak) CALayer *middleLayer;
@end
@implementation TFComicDownloadManagerController
- (void)viewDidLoad
{
[super viewDidLoad];
[self initialize];
[self createSubviews];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self setStatusBarDefaultStyle];
NSArray *t_sourceArray = [[[[WXYZ_DownloadHelper sharedManager] getDownloadProductionArrayWithProductionType:TFProductionTypeComic] reverseObjectEnumerator] allObjects];
self.dataSourceArray = [NSMutableArray array];
for (TFProductionModel *t_model in t_sourceArray) {
if ([[WXYZ_ComicDownloadManager sharedManager] getDownloadChapterCountWithProduction_id:t_model.production_id] > 0) {
[self.dataSourceArray addObject:t_model];
}
}
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.05 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
if (self.dataSourceArray.count == 0) {
[self.mainTableView xtfei_showEmptyView];
} else {
[self.mainTableView xtfei_hideEmptyView];
}
});
[self.mainTableView reloadData];
}
- (void)initialize
{
self.selectedArray = [NSMutableArray array];
// 设置编辑状态
WS(weakSelf)
_editStateBlock = ^() {
BOOL isEditting = NO;
if (weakSelf.mainTableView.visibleCells.count == 0) return NO;
for (UITableViewCell *cell in weakSelf.mainTableView.visibleCells) {
if ([cell isMemberOfClass:TFComicDownloadManagerCell.class]) {
TFComicDownloadManagerCell *celll = (TFComicDownloadManagerCell *)cell;
isEditting = !celll.isEditting;
weakSelf.isEditting = isEditting;
// 隐藏/显示底部编辑区域
if (isEditting) {
[UIView animateWithDuration:kAnimatedDuration animations:^{
[weakSelf.edittingView mas_updateConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(weakSelf.view);
}];
[weakSelf.edittingView.superview layoutIfNeeded];
}];
} else {
[UIView animateWithDuration:kAnimatedDuration animations:^{
[weakSelf.edittingView mas_updateConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(weakSelf.view).offset(CGRectGetHeight(weakSelf.edittingView .bounds));
}];
[weakSelf.edittingView.superview layoutIfNeeded];
}];
}
[celll setEditing:!celll.isEditting];
continue;
}
}
return isEditting;
};
[self hiddenNavigationBar:YES];
}
- (void)createSubviews
{
UIView *edittingView = [[UIView alloc] init];
self.edittingView = edittingView;
edittingView .backgroundColor = self.view.backgroundColor;
WS(weakSelf)
edittingView.frameBlock = ^(UIView * _Nonnull view) {
if (weakSelf.topLayer) return;
CALayer *layer = [CALayer layer];
layer.backgroundColor = kColorRGB(238, 238, 238).CGColor;
layer.frame = CGRectMake(0, 0, CGRectGetWidth(view.bounds), 0.5);
[view.layer addSublayer:layer];
weakSelf.topLayer = layer;
};
[self.view addSubview:edittingView ];
[edittingView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.width.bottom.equalTo(self.view);
}];
// 编辑状态下全选按钮
UIButton *edittingAll = [UIButton buttonWithType:UIButtonTypeCustom];
self.edittingAll = edittingAll;
[edittingAll setTitle:TFLocalizedString(@"全选") forState:UIControlStateNormal];
[edittingAll addTarget:self action:@selector(selectedAllEvent) forControlEvents:UIControlEventTouchUpInside];
edittingAll.frameBlock = ^(UIButton * _Nonnull button) {
if (weakSelf.middleLayer) return;
CALayer *layer = [CALayer layer];
layer.backgroundColor = kColorRGB(238, 238, 238).CGColor;
layer.frame = CGRectMake(CGRectGetMaxX(button.frame), 16.0f, 0.5, CGRectGetHeight(button.bounds) - 2 * 16.0f);
[button.layer addSublayer:layer];
weakSelf.middleLayer = layer;
};
[edittingAll setTitleColor:kBlackColor forState:UIControlStateNormal];
edittingAll.titleLabel.font = kFont14;
[edittingView addSubview:edittingAll];
[edittingAll mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(edittingView ).offset(1.0f);
make.left.equalTo(edittingView );
make.width.equalTo(edittingView ).multipliedBy(0.5f);
make.height.equalTo(edittingAll.mas_width).multipliedBy(60.0f / 187.5f);
make.bottom.equalTo(edittingView ).offset(-PUB_TABBAR_OFFSET).priorityLow();
}];
// 编辑状态下删除按钮
UIButton *edittingDelete = [UIButton buttonWithType:UIButtonTypeCustom];
self.edittingDelete = edittingDelete;
[edittingDelete setTitle:TFLocalizedString(@"删除") forState:UIControlStateNormal];
[edittingDelete addTarget:self action:@selector(deleteEvent) forControlEvents:UIControlEventTouchUpInside];
[edittingDelete setTitleColor:kGrayTextColor forState:UIControlStateNormal];
edittingDelete.titleLabel.font = kFont14;
[edittingView addSubview:edittingDelete];
[edittingDelete mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.top.equalTo(edittingAll);
make.left.equalTo(edittingAll.mas_right);
}];
[edittingView setNeedsLayout];
[edittingView layoutIfNeeded];
// 设置编辑区域默认高度
[edittingView mas_updateConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(self.view).offset(CGRectGetHeight(edittingView .bounds));
}];
self.mainTableView.delegate = self;
self.mainTableView.dataSource = self;
self.mainTableView.contentInset = UIEdgeInsetsMake(0, 0, PUB_TABBAR_OFFSET, 0);
[self.view addSubview:self.mainTableView];
[self.mainTableView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.left.right.equalTo(self.view);
make.bottom.equalTo(edittingView .mas_top);
}];
[self setEmptyOnView:self.mainTableView title:TFLocalizedString(@"还没有下载记录") tapBlock:nil];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.05 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
if (self.dataSourceArray.count == 0) {
[self.mainTableView xtfei_showEmptyView];
} else {
[self.mainTableView xtfei_hideEmptyView];
}
});
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.dataSourceArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
WS(weakSelf)
TFComicDownloadManagerCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TFComicDownloadManagerCell"];
if (!cell) {
cell = [[TFComicDownloadManagerCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TFComicDownloadManagerCell"];
}
TFProductionModel *t_model = [self.dataSourceArray objectOrNilAtIndex:indexPath.row];
cell.comicModel = t_model;
[cell set_Editting:self.isEditting];
cell.cellSelectBlock = ^(TFProductionModel *comicModel, NSString *comic_name) {
TFComicDownloadContentChoiceController *vc = [[TFComicDownloadContentChoiceController alloc] init];
vc.comicModel = comicModel;
vc.navTitle = comic_name;
[weakSelf.navigationController pushViewController:vc animated:YES];
};
cell.imageViewSelectBlock = ^(NSInteger comic_id) {
TFComicDetailViewController *vc = [[TFComicDetailViewController alloc] init];
vc.comic_id = comic_id;
[weakSelf.navigationController pushViewController:vc animated:YES];
};
cell.buttonSelectBlock = ^(TFProductionModel *comicModel) {
[[TFCollectionManager shareManagerWithProductionType:TFProductionTypeComic] moveCollectionToTopWithProductionModel:t_model];
TFComicBrowseViewController *vc = [[TFComicBrowseViewController alloc] init];
vc.comicProductionModel = comicModel;
vc.chapter_id = [[TFReadRecordManager shareManagerWithProductionType:TFProductionTypeComic] getReadingRecordChapter_idWithProduction_id:comicModel.production_id];
[weakSelf.navigationController pushViewController:vc animated:YES];
};
if ([self.selectedArray containsObject:@(t_model.production_id)]) {
[cell switchSelectedState:YES];
} else {
[cell switchSelectedState:NO];
}
cell.selecteEdittingCellBlock = ^(TFProductionModel * _Nonnull productionModel, BOOL isSelected) {
if (isSelected) {
[weakSelf.selectedArray addObject:@(productionModel.production_id)];
} else {
[weakSelf.selectedArray removeObject:@(productionModel.production_id)];
}
[weakSelf updateEdittingView];
};
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
{
TFProductionModel *t_model = [self.dataSourceArray objectOrNilAtIndex:indexPath.row];
[[WXYZ_ComicDownloadManager sharedManager] removeDownloadProductionWithProduction_id:t_model.production_id];
[self.dataSourceArray removeObject:t_model];
[self.mainTableView reloadData];
if (self.dataSourceArray.count > 0) {
[self.mainTableView xtfei_hideEmptyView];
} else {
[self.mainTableView xtfei_showEmptyView];
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return kHalfMargin;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, kHalfMargin)];
view.backgroundColor = [UIColor clearColor];
return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return CGFLOAT_MIN;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, PUB_TABBAR_OFFSET == 0 ? 10 : PUB_TABBAR_OFFSET)];
view.backgroundColor = [UIColor clearColor];
return view;
}
- (void)selectedAllEvent
{
if ([self.edittingAll.titleLabel.text isEqualToString:TFLocalizedString(@"全选")]) {
for (UITableViewCell *cell in self.mainTableView.visibleCells) {
if ([cell isMemberOfClass:TFComicDownloadManagerCell.class]) {
TFComicDownloadManagerCell *tempCell = (TFComicDownloadManagerCell *)cell;
[tempCell switchSelectedState:YES];
}
}
for (TFProductionModel *t_model in self.dataSourceArray) {
if (![self.selectedArray containsObject:@(t_model.production_id)]) {
[self.selectedArray addObject:@(t_model.production_id)];
}
}
} else {
for (UITableViewCell *cell in self.mainTableView.visibleCells) {
if ([cell isMemberOfClass:TFComicDownloadManagerCell.class]) {
TFComicDownloadManagerCell *tempCell = (TFComicDownloadManagerCell *)cell;
[tempCell switchSelectedState:NO];
}
}
[self.selectedArray removeAllObjects];
}
[self updateEdittingView];
}
// 删除按钮点击事件
- (void)deleteEvent {
if (self.selectedArray.count == 0) return;
NSMutableArray *arr = [self.dataSourceArray mutableCopy];
NSMutableArray<NSIndexPath *> *pathArr = [NSMutableArray array];
[arr enumerateObjectsUsingBlock:^(TFProductionModel * _Nonnull productionModel, NSUInteger idx, BOOL * _Nonnull stop) {
if ([self.selectedArray containsObject:@(productionModel.production_id)]) {
[[WXYZ_ComicDownloadManager sharedManager] removeDownloadProductionWithProduction_id:productionModel.production_id];
[self.dataSourceArray removeObject:productionModel];
[self.selectedArray removeObject:@(productionModel.production_id)];
[pathArr addObject:[NSIndexPath indexPathForRow:idx inSection:0]];
}
}];
WS(weakSelf)
if (@available(iOS 11.0, *)) {
[self.mainTableView performBatchUpdates:^{
[self.mainTableView deleteRowsAtIndexPaths:pathArr withRowAnimation:UITableViewRowAnimationLeft];
} completion:^(BOOL finished) {
if (!finished) return;
SS(strongSelf)
BOOL a = !strongSelf->_editStateBlock ?: strongSelf->_editStateBlock();
NSLog(@"%@", a?@"":@"");
[UIView animateWithDuration:kAnimatedDuration animations:^{
[self.edittingView mas_updateConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(self.view).offset(CGRectGetHeight(self.edittingView .bounds));
}];
[self.edittingView.superview layoutIfNeeded];
}];
!self.changeEditStateBlock ?: self.changeEditStateBlock(YES);
}];
} else {
[self.mainTableView beginUpdates];
[self.mainTableView deleteRowsAtIndexPaths:pathArr withRowAnimation:UITableViewRowAnimationLeft];
[self.mainTableView endUpdates];
dispatch_async(dispatch_get_main_queue(), ^{
SS(strongSelf)
BOOL a = !strongSelf->_editStateBlock ?: strongSelf->_editStateBlock();
NSLog(@"%@", a?@"":@"");
[UIView animateWithDuration:kAnimatedDuration animations:^{
[self.edittingView mas_updateConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(self.view).offset(CGRectGetHeight(self.edittingView .bounds));
}];
[self.edittingView.superview layoutIfNeeded];
}];
!self.changeEditStateBlock ?: self.changeEditStateBlock(YES);
});
}
if (self.dataSourceArray.count > 0) {
[self.mainTableView xtfei_hideEmptyView];
} else {
[self.mainTableView xtfei_showEmptyView];
}
self.isEditting = NO;
[self updateEdittingView];
}
// 更新底部编辑区域
- (void)updateEdittingView {
if (self.dataSourceArray.count > 0 && self.mainTableView.visibleCells.count == 0) return;
BOOL allSelected = YES;
for (TFProductionModel *t_model in self.dataSourceArray) {
if (![self.selectedArray containsObject:@(t_model.production_id)]) {
allSelected = NO;
break;
}
}
if (allSelected) {
[self.edittingAll setTitle:TFLocalizedString(@"取消全选") forState:UIControlStateNormal];
} else {
[self.edittingAll setTitle:TFLocalizedString(@"全选") forState:UIControlStateNormal];
}
if (self.selectedArray.count == 0) {
[self.edittingDelete setTitle:TFLocalizedString(@"删除") forState:UIControlStateNormal];
[self.edittingDelete setTitleColor:kGrayTextColor forState:UIControlStateNormal];
} else {
[self.edittingDelete setTitle:[NSString stringWithFormat:@"%@(%zd)", TFLocalizedString(@"删除"), self.selectedArray.count] forState:UIControlStateNormal];
[self.edittingDelete setTitleColor:kRedColor forState:UIControlStateNormal];
}
}
@end
@@ -0,0 +1,20 @@
//
// TFComicDownloadViewController.h
// TFReader
//
// Created by 谢腾飞 on 2020/12/15.
// Copyright © 2020 xtfei_2011@126.com. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "TFDownloadViewController.h"
NS_ASSUME_NONNULL_BEGIN
@interface TFComicDownloadViewController : TFDownloadViewController
@property (nonatomic ,strong) TFProductionModel *comicModel;
@end
NS_ASSUME_NONNULL_END
@@ -0,0 +1,483 @@
//
// TFComicDownloadViewController.m
// TFReader
//
// Created by 谢腾飞 on 2020/12/15.
// Copyright © 2020 xtfei_2011@126.com. All rights reserved.
//
#import "TFComicDownloadViewController.h"
#import "TFComicDownloadViewCell.h"
#import "WXYZ_ChapterBottomPayBar.h"
#import "TFComicDownloadModel.h"
#import "WXYZ_ComicDownloadManager.h"
#import "TFReadRecordManager.h"
@interface TFComicDownloadViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>
{
UIView *toolBarView;
YYLabel *toolBarLeftLabel;
UILabel *toolBarRightLabel;
UIButton *selectAllButton;
}
@property (nonatomic, strong) WXYZ_ComicDownloadManager *comicDownloadManager;
@property (nonatomic, strong) UIButton *downloadButton;
@property (nonatomic, strong) UIActivityIndicatorView *downloadIndicatorView;
@property (nonatomic, assign) BOOL payBarShowing;
@end
@implementation TFComicDownloadViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self initialize];
[self createSubviews];
[self initDownloadManager];
[self netRequest];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self setStatusBarDefaultStyle];
}
- (void)initialize
{
[self setNavigationBarTitle:TFLocalizedString(@"选择章节")];
self.view.backgroundColor = kGrayViewColor;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(netRequest) name:Notification_Login_Success object:nil];
}
- (void)createSubviews
{
CGFloat width = [TFViewHelper getDynamicWidthWithLabelFont:kFont11 labelHeight:30.0 labelText:TFLocalizedString(@"正序") maxWidth:240];
width += kLabelHeight;
TFButton *sortButton = [[TFButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - kHalfMargin - width, PUB_NAVBAR_HEIGHT - 30 - kQuarterMargin, width, 30) buttonTitle:TFLocalizedString(@"正序") buttonImageName:@"comic_positive_order" buttonIndicator:TFButtonIndicatorImageRightBothRight];
sortButton.selected = NO;
sortButton.buttonImageScale = 0.4;
sortButton.buttonTitleFont = kFont11;
sortButton.graphicDistance = 5;
sortButton.buttonTitleColor = kGrayTextColor;
[sortButton addTarget:self action:@selector(sortClick:) forControlEvents:UIControlEventTouchUpInside];
[self.navigationBar addSubview:sortButton];
self.mainCollectionViewFlowLayout.minimumLineSpacing = kHalfMargin;
self.mainCollectionViewFlowLayout.minimumInteritemSpacing = kHalfMargin;
self.mainCollectionViewFlowLayout.itemSize = CGSizeMake((SCREEN_WIDTH - 5 * kHalfMargin) / 4, 40);
self.mainCollectionViewFlowLayout.sectionInset = UIEdgeInsetsMake(kHalfMargin, kHalfMargin, kHalfMargin, kHalfMargin);
self.mainCollectionView.delegate = self;
self.mainCollectionView.dataSource = self;
self.mainCollectionView.alwaysBounceVertical = YES;
[self.mainCollectionView registerClass:[TFComicDownloadViewCell class] forCellWithReuseIdentifier:@"TFComicDownloadViewCell"];
[self.view addSubview:self.mainCollectionView];
[self.mainCollectionView 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_NAVBAR_HEIGHT - PUB_TABBAR_HEIGHT - 20);
}];
toolBarView = [[UIView alloc] init];
toolBarView.backgroundColor = kWhiteColor;
[self.view addSubview:toolBarView];
[toolBarView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(0);
make.bottom.mas_equalTo(self.view.mas_bottom);
make.width.mas_equalTo(self.view.mas_width);
make.height.mas_equalTo(PUB_TABBAR_HEIGHT + 20);
}];
toolBarLeftLabel = [[YYLabel alloc] init];
toolBarLeftLabel.textColor = kGrayTextColor;
toolBarLeftLabel.textContainerInset = UIEdgeInsetsMake(3, kHalfMargin, 0, 0);
toolBarLeftLabel.textAlignment = NSTextAlignmentLeft;
toolBarLeftLabel.font = kFont10;
toolBarLeftLabel.backgroundColor = kGrayDeepViewColor;
[toolBarView addSubview:toolBarLeftLabel];
[toolBarLeftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(0);
make.top.mas_equalTo(0);
make.width.mas_equalTo(toolBarView.mas_width);
make.height.mas_equalTo(20);
}];
toolBarRightLabel = [[UILabel alloc] init];
toolBarRightLabel.text = TFLocalizedString(@"已选0话");
toolBarRightLabel.textColor = kGrayTextColor;
toolBarRightLabel.textAlignment = NSTextAlignmentRight;
toolBarRightLabel.font = kFont10;
toolBarRightLabel.backgroundColor = [UIColor clearColor];
toolBarRightLabel.hidden = YES;
[toolBarView addSubview:toolBarRightLabel];
[toolBarRightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.mas_equalTo(toolBarView.mas_right).with.offset(- kHalfMargin);
make.top.mas_equalTo(0);
make.width.mas_equalTo(toolBarView.mas_width).with.multipliedBy(0.5);
make.height.mas_equalTo(20);
}];
selectAllButton = [UIButton buttonWithType:UIButtonTypeCustom];
[selectAllButton setTitle:TFLocalizedString(@"全选") forState:UIControlStateNormal];
[selectAllButton setTitleColor:kBlackColor forState:UIControlStateNormal];
[selectAllButton.titleLabel setFont:kMainFont];
[selectAllButton addTarget:self action:@selector(checkallClick:) forControlEvents:UIControlEventTouchUpInside];
[toolBarView addSubview:selectAllButton];
[selectAllButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(toolBarView.mas_left);
make.top.mas_equalTo(toolBarRightLabel.mas_bottom);
make.width.mas_equalTo(SCREEN_WIDTH / 2);
make.height.mas_equalTo(PUB_TABBAR_HEIGHT - PUB_TABBAR_OFFSET);
}];
self.downloadButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.downloadButton.enabled = NO;
[self.downloadButton setTitle:TFLocalizedString(@"下载") forState:UIControlStateNormal];
[self.downloadButton setTitleColor:kGrayTextLightColor forState:UIControlStateNormal];
[self.downloadButton.titleLabel setFont:kMainFont];
[self.downloadButton addTarget:self action:@selector(downloadClick) forControlEvents:UIControlEventTouchUpInside];
[toolBarView addSubview:self.downloadButton];
[self.downloadButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(selectAllButton.mas_right);
make.top.mas_equalTo(toolBarRightLabel.mas_bottom);
make.right.mas_equalTo(toolBarView.mas_right);
make.height.mas_equalTo(PUB_TABBAR_HEIGHT - PUB_TABBAR_OFFSET);
}];
self.downloadIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:(UIActivityIndicatorViewStyleGray)];
self.downloadIndicatorView.hidesWhenStopped = YES;
[toolBarView addSubview:self.downloadIndicatorView];
[self.downloadIndicatorView mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.mas_equalTo(self.downloadButton.mas_centerX);
make.centerY.mas_equalTo(self.downloadButton.mas_centerY);
make.width.height.mas_equalTo(self.downloadButton.mas_height);
}];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return self.dataSourceArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
TFProductionChapterModel *chapterModel = [self.dataSourceArray objectOrNilAtIndex:indexPath.row];
static NSString *cellIdentifier = @"TFComicDownloadViewCell";
TFComicDownloadViewCell __weak *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
cell.chapterModel = chapterModel;
cell.cellDownloadState = [[self.selectSourceDictionary objectForKey:[TFUtilsHelper formatStringWithInteger:chapterModel.chapter_id]] integerValue];
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
TFProductionChapterModel *chapterModel = [self.dataSourceArray objectOrNilAtIndex:indexPath.row];
TFComicDownloadViewCell *cell = (TFComicDownloadViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
switch (cell.cellDownloadState) {
case WXYZ_ProductionDownloadStateNormal:
case WXYZ_ProductionDownloadStateFail:
[self setCollectionCellDownloadStateWithChapter_id:chapterModel.chapter_id downloadState:WXYZ_ProductionDownloadStateSelected];
[self reloadToolBar];
break;
case WXYZ_ProductionDownloadStateSelected:
[self setCollectionCellDownloadStateWithChapter_id:chapterModel.chapter_id downloadState:WXYZ_ProductionDownloadStateNormal];
[self reloadToolBar];
break;
default:
break;
}
}
#pragma mark - 点击事件
- (void)sortClick:(TFButton *)sender
{
if (sender.selected) {
sender.buttonImageName = @"comic_positive_order";
sender.buttonTitle = TFLocalizedString(@"正序");
} else {
sender.buttonImageName = @"comic_reverse_order";
sender.buttonTitle = TFLocalizedString(@"倒序");
}
sender.selected = !sender.selected;
self.dataSourceArray = [[[self.dataSourceArray reverseObjectEnumerator] allObjects] mutableCopy];
[self.mainCollectionView reloadData];
}
- (void)checkallClick:(UIButton *)sender
{
for (NSString *t_chapter_id in self.selectSourceDictionary.allKeys) {
if (!sender.selected) { // 全选状态
if ([[self.selectSourceDictionary objectForKey:t_chapter_id] integerValue] == WXYZ_ProductionDownloadStateNormal || [[self.selectSourceDictionary objectForKey:t_chapter_id] integerValue] == WXYZ_ProductionDownloadStateFail) {
[self setCollectionCellDownloadStateWithChapter_id:[t_chapter_id integerValue] downloadState:WXYZ_ProductionDownloadStateSelected];
}
} else {
if ([[self.selectSourceDictionary objectForKey:t_chapter_id] integerValue] == WXYZ_ProductionDownloadStateSelected) {
[self setCollectionCellDownloadStateWithChapter_id:[t_chapter_id integerValue] downloadState:WXYZ_ProductionDownloadStateNormal];
}
}
}
sender.selected = !sender.selected;
[self reloadToolBar];
}
- (void)initDownloadManager
{
WS(weakSelf)
self.comicDownloadManager = [WXYZ_ComicDownloadManager sharedManager];
self.comicDownloadManager.downloadChapterStateChangeBlock = ^(WXYZ_DownloadChapterState state, NSInteger production_id, NSInteger chapter_id) {
dispatch_async(dispatch_get_main_queue(), ^{
switch (state) {
case WXYZ_DownloadStateChapterDownloadFinished: // 章节下载完成
{
[weakSelf setCollectionCellDownloadStateWithChapter_id:chapter_id downloadState:WXYZ_ProductionDownloadStateDownloaded];
}
break;
case WXYZ_DownloadStateChapterDownloadFail: // 下载失败
{
[weakSelf setCollectionCellDownloadStateWithChapter_id:chapter_id downloadState:WXYZ_ProductionDownloadStateFail];
}
break;
case WXYZ_DownloadStateChapterDownloadStart: // 开始下载
{
[weakSelf setCollectionCellDownloadStateWithChapter_id:chapter_id downloadState:WXYZ_ProductionDownloadStateDownloading];
}
break;
default:
break;
}
[weakSelf reloadToolBar];
});
};
self.comicDownloadManager.downloadMissionStateChangeBlock = ^(WXYZ_DownloadMissionState state, NSInteger production_id, NSArray * _Nonnull chapter_ids) {
switch (state) {
// 任务开始
case WXYZ_DownloadStateMissionStart:
{
[weakSelf.downloadIndicatorView stopAnimating];
weakSelf.downloadButton.hidden = NO;
for (NSString *t_chapter_id in chapter_ids) {
[weakSelf setCollectionCellDownloadStateWithChapter_id:[t_chapter_id integerValue] downloadState:WXYZ_ProductionDownloadStateDownloading];
}
}
break;
// 任务失败
case WXYZ_DownloadStateMissionFail:
{
[weakSelf.downloadIndicatorView stopAnimating];
weakSelf.downloadButton.hidden = NO;
for (NSString *t_chapter_id in chapter_ids) {
[weakSelf setCollectionCellDownloadStateWithChapter_id:[t_chapter_id integerValue] downloadState:WXYZ_ProductionDownloadStateFail];
}
}
break;
// 需要支付
case WXYZ_DownloadStateMissionShouldPay:
{
[weakSelf.downloadIndicatorView stopAnimating];
weakSelf.downloadButton.hidden = NO;
if (!weakSelf.payBarShowing) {
weakSelf.payBarShowing = YES;
TFProductionChapterModel *chapterModel = [[TFProductionChapterModel alloc] init];
chapterModel.production_id = production_id;
chapterModel.chapter_ids = chapter_ids;
WXYZ_ChapterBottomPayBar *payBar = [[WXYZ_ChapterBottomPayBar alloc] initWithChapterModel:chapterModel barType:WXYZ_BottomPayBarTypeDownload productionType:TFProductionTypeComic];
payBar.paySuccessChaptersBlock = ^(NSArray<NSString *> * _Nonnull success_chapter_ids) {
[weakSelf downloadClick];
};
payBar.payFailChaptersBlock = ^(NSArray<NSString *> * _Nonnull fail_chapter_ids) {
for (NSString *t_chapter_id in fail_chapter_ids) {
[weakSelf setCollectionCellDownloadStateWithChapter_id:[t_chapter_id integerValue] downloadState:WXYZ_ProductionDownloadStateNormal];
}
[weakSelf reloadToolBar];
[TFPromptManager showPromptViewWithStatus:TFPromptStatusError promptTitle:TFLocalizedString(@"购买失败")];
};
payBar.payCancleChapterBlock = ^(NSArray<NSString *> * _Nonnull fail_chapter_ids) {
for (NSString *t_chapter_id in fail_chapter_ids) {
[weakSelf setCollectionCellDownloadStateWithChapter_id:[t_chapter_id integerValue] downloadState:WXYZ_ProductionDownloadStateNormal];
}
[weakSelf reloadToolBar];
};
SS(strongSelf)
payBar.bottomPayBarHiddenBlock = ^{
strongSelf.payBarShowing = NO;
};
[payBar showBottomPayBar];
}
}
break;
case WXYZ_DownloadStateMissionFinished:
[TFPromptManager showPromptViewWithStatus:TFPromptStatusSuccess promptTitle:TFLocalizedString(@"下载完成")];
break;
default:
break;
}
weakSelf.downloadButton.userInteractionEnabled = YES;
};
}
- (void)downloadClick
{
self.downloadButton.userInteractionEnabled = NO;
NSMutableArray *chapter_ids = [NSMutableArray array];
for (NSString *t_chapter_id in self.selectSourceDictionary.allKeys) {
if ([[self.selectSourceDictionary objectForKey:t_chapter_id] integerValue] == WXYZ_ProductionDownloadStateSelected) {
[chapter_ids addObject:t_chapter_id];
}
}
[self.downloadIndicatorView startAnimating];
self.downloadButton.hidden = YES;
[self reloadToolBar];
[self.comicDownloadManager downloadChaptersWithProductionModel:self.comicModel production_id:self.comicModel.production_id chapter_ids:chapter_ids];
}
- (void)reloadToolBar
{
NSInteger normalIndex = 0;
NSInteger selectIndex = 0;
NSInteger downloadedIndex = 0;
for (NSString *t_key in self.selectSourceDictionary) {
switch ([[self.selectSourceDictionary objectForKey:t_key] integerValue]) {
case WXYZ_ProductionDownloadStateNormal:
normalIndex ++;
break;
case WXYZ_ProductionDownloadStateDownloaded:
downloadedIndex ++;
break;
case WXYZ_ProductionDownloadStateSelected:
selectIndex ++;
break;
default:
break;
}
}
toolBarRightLabel.text = [NSString stringWithFormat:TFLocalizedString(@"已选%@话"), [TFUtilsHelper formatStringWithInteger:selectIndex]];
toolBarRightLabel.hidden = (selectIndex == 0);
if (downloadedIndex == self.dataSourceArray.count) {
toolBarRightLabel.text = @"";
selectAllButton.enabled = NO;
selectAllButton.selected = NO;
[selectAllButton setTitle:TFLocalizedString(@"已全部下载") forState:UIControlStateNormal];
[selectAllButton setTitleColor:kGrayTextLightColor forState:UIControlStateNormal];
[selectAllButton mas_updateConstraints:^(MASConstraintMaker *make) {
make.width.mas_equalTo(SCREEN_WIDTH);
}];
return;
}
self.downloadButton.enabled = NO;
[self.downloadButton setTitleColor:kGrayTextLightColor forState:UIControlStateNormal];
selectAllButton.enabled = YES;
selectAllButton.selected = NO;
[selectAllButton setTitle:TFLocalizedString(@"全选") forState:UIControlStateNormal];
[selectAllButton mas_updateConstraints:^(MASConstraintMaker *make) {
make.width.mas_equalTo(SCREEN_WIDTH / 2);
}];
if (selectIndex > 0 && normalIndex == 0) { // 有选中的文件并且没有未选中的文件
self.downloadButton.enabled = YES;
[self.downloadButton setTitleColor:kBlackColor forState:UIControlStateNormal];
selectAllButton.selected = YES;
[selectAllButton setTitle:TFLocalizedString(@"取消全选") forState:UIControlStateNormal];
[selectAllButton setTitleColor:kBlackColor forState:UIControlStateNormal];
} else if (normalIndex > 0 && selectIndex > 0) { // 有未选中的文件
self.downloadButton.enabled = YES;
[self.downloadButton setTitleColor:kBlackColor forState:UIControlStateNormal];
}
}
- (void)setCollectionCellDownloadStateWithChapter_id:(NSInteger)chapter_id downloadState:(WXYZ_ProductionDownloadState)downloadState
{
TFComicDownloadViewCell *cell = (TFComicDownloadViewCell *)[self.mainCollectionView cellForItemAtIndexPath:(NSIndexPath *)[self.cellIndexDictionary objectForKey:[TFUtilsHelper formatStringWithInteger:chapter_id]]];
cell.cellDownloadState = downloadState;
[self.selectSourceDictionary setObject:[TFUtilsHelper formatStringWithInteger:downloadState] forKey:[TFUtilsHelper formatStringWithInteger:chapter_id]];
}
- (void)netRequest
{
WS(weakSelf)
[TFNetworkTools POSTQuick:Comic_DownloadOption parameters:@{@"comic_id":[TFUtilsHelper formatStringWithInteger:self.comicModel.production_id]} model:TFComicDownloadModel.class success:^(BOOL isSuccess, TFComicDownloadModel *_Nullable t_model, BOOL isCache, TFNetworkRequestModel *requestModel) {
if (isSuccess) {
toolBarLeftLabel.text = t_model.display_label?:@"";
if (t_model.down_list.count > 0) {
weakSelf.dataSourceArray = [NSMutableArray arrayWithArray:t_model.down_list];
[weakSelf resetSelectSourceDicWithDataSourceArray:t_model.down_list productionType:TFProductionTypeComic];
}
} else {
[TFPromptManager showPromptViewWithStatus:TFPromptStatusError promptTitle:requestModel.msg];
}
[weakSelf.mainCollectionView reloadData];
[weakSelf reloadToolBar];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
[weakSelf.mainCollectionView reloadData];
}];
}
@end
@@ -0,0 +1,24 @@
//
// TFComicDownloadModel.h
// TFReader
//
// Created by 谢腾飞 on 2020/12/15.
// Copyright © 2020 xtfei_2011@126.com. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface TFComicDownloadModel : NSObject
@property (nonatomic ,assign) NSInteger total_chapters;
@property (nonatomic ,copy) NSString *display_label;
@property (nonatomic ,strong) NSArray <TFProductionChapterModel *>*down_list;
@end
NS_ASSUME_NONNULL_END
@@ -0,0 +1,26 @@
//
// TFComicDownloadModel.m
// TFReader
//
// Created by 谢腾飞 on 2020/12/15.
// Copyright © 2020 xtfei_2011@126.com. All rights reserved.
//
#import "TFComicDownloadModel.h"
@implementation TFComicDownloadModel
+ (NSDictionary *)modelCustomPropertyMapper
{
return @{
@"total_chapters" : @"base_info.total_chapters",
@"display_label" : @"base_info.display_label"
};
}
+ (NSDictionary<NSString *,id> *)modelContainerPropertyGenericClass
{
return @{@"down_list" : [TFProductionChapterModel class]};
}
@end
@@ -0,0 +1,45 @@
//
// TFComicDownloadManagerCell.h
// TFReader
//
// Created by 谢腾飞 on 2020/12/16.
// Copyright © 2020 xtfei_2011@126.com. All rights reserved.
//
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
typedef void(^CellSelectBlock)(TFProductionModel *comicModel, NSString *comic_name);
typedef void(^ImageViewSelectBlock)(NSInteger comic_id);
typedef void(^ButtonSelectBlock)(TFProductionModel *comicModel);
@interface TFComicDownloadManagerCell : TFBasicTableViewCell
@property (nonatomic, strong) TFProductionModel *comicModel;
@property (nonatomic, copy) CellSelectBlock cellSelectBlock;
@property (nonatomic, copy) ImageViewSelectBlock imageViewSelectBlock;
@property (nonatomic, copy) ButtonSelectBlock buttonSelectBlock;
@property (nonatomic, assign, readonly) BOOL isEditting;
@property (nonatomic, assign, readonly) BOOL isSelected;
/// 选择编辑单元
@property (nonatomic, copy) void(^selecteEdittingCellBlock)(TFProductionModel *productionModel, BOOL isSelected);
- (void)setEditing:(BOOL)editing;
// 强行设置编辑状态
- (void)set_Editting:(BOOL)editting;
// 切换选中状态
- (void)switchSelectedState:(BOOL)state;
@end
NS_ASSUME_NONNULL_END
@@ -0,0 +1,236 @@
//
// TFComicDownloadManagerCell.m
// TFReader
//
// Created by 谢腾飞 on 2020/12/16.
// Copyright © 2020 xtfei_2011@126.com. All rights reserved.
//
#import "TFComicDownloadManagerCell.h"
#import "WXYZ_ComicDownloadManager.h"
#import "TFReadRecordManager.h"
@interface TFComicDownloadManagerCell ()
@property (nonatomic ,weak) UIView *mainView;
@property (nonatomic ,weak) TFProductionCoverView *coverImageView;
@property (nonatomic ,weak) UILabel *titleNameLabel;
@property (nonatomic ,weak) UILabel *subTitleLabel;
@property (nonatomic ,weak) UIButton *readButton;
@property (nonatomic ,weak) UIImageView *selectedView;
@end
@implementation TFComicDownloadManagerCell
- (void)createSubviews
{
[super createSubviews];
[self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cellTapClick:)]];
UIView *mainView = [[UIView alloc] init];
self.mainView = mainView;
mainView.backgroundColor = [UIColor clearColor];
[self.contentView addSubview:mainView];
[mainView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.contentView);
make.left.equalTo(self.contentView).offset(-16.0f);
make.width.equalTo(self.contentView).offset(16.0f);
make.height.equalTo(self.contentView);
}];
UIImageView *selectedView = [[UIImageView alloc] init];
self.selectedView = selectedView;
selectedView.image = [UIImage imageNamed:@"audio_download_unselect"];
[mainView addSubview:selectedView];
[selectedView mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.height.mas_equalTo(16.0f);
make.left.equalTo(mainView);
make.centerY.equalTo(mainView);
}];
TFProductionCoverView *coverImageView = [[TFProductionCoverView alloc] initWithProductionType:TFProductionTypeComic coverDirection:TFProductionCoverDirectionVertical];
self.coverImageView = coverImageView;
coverImageView.tag = 200;
[coverImageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cellTapClick:)]];
[mainView addSubview:coverImageView];
[coverImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(selectedView.mas_right).with.offset(kHalfMargin);
make.top.mas_equalTo(mainView.mas_top).with.offset(kQuarterMargin);
make.width.mas_equalTo(BOOK_WIDTH_SMALL - kMargin);
make.height.mas_equalTo(kGeometricHeight(BOOK_WIDTH_SMALL - kMargin, 3, 4));
make.bottom.mas_equalTo(mainView.mas_bottom).with.offset(- kQuarterMargin).priorityLow();
}];
UILabel *titleNameLabel = [[UILabel alloc] init];
self.titleNameLabel = titleNameLabel;
titleNameLabel.textColor = kBlackColor;
titleNameLabel.textAlignment = NSTextAlignmentLeft;
titleNameLabel.font = kBoldFont14;
titleNameLabel.numberOfLines = 0;
[mainView addSubview:titleNameLabel];
[titleNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(coverImageView.mas_right).with.offset(kHalfMargin);
make.top.mas_equalTo(coverImageView.mas_top);
make.right.mas_equalTo(mainView.mas_right).offset(-kQuarterMargin);
}];
UILabel *subTitleLabel = [[UILabel alloc] init];
self.subTitleLabel = subTitleLabel;
subTitleLabel.textColor = kGrayTextColor;
subTitleLabel.textAlignment = NSTextAlignmentLeft;
subTitleLabel.font = kFont12;
subTitleLabel.numberOfLines = 1;
[mainView addSubview:subTitleLabel];
[subTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.mas_equalTo(titleNameLabel);
make.bottom.mas_equalTo(coverImageView.mas_bottom);
}];
UIButton *readButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.readButton = readButton;
readButton.backgroundColor = kMainColor;
readButton.layer.cornerRadius = 12;
[readButton setTitle:TFLocalizedString(@"开始阅读") forState:UIControlStateNormal];
[readButton setTitleColor:kWhiteColor forState:UIControlStateNormal];
[readButton.titleLabel setFont:kFont12];
[readButton addTarget:self action:@selector(readButtonClick) forControlEvents:UIControlEventTouchUpInside];
[mainView addSubview:readButton];
[readButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.mas_equalTo(mainView.mas_right).with.offset(- kHalfMargin);
make.centerY.mas_equalTo(mainView.mas_centerY);
make.width.mas_equalTo(70);
make.height.mas_equalTo(24);
}];
[titleNameLabel mas_updateConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(self.readButton.mas_top).offset(-kQuarterMargin);
}];
[subTitleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.readButton.mas_bottom).offset(kQuarterMargin);
}];
}
- (void)setComicModel:(TFProductionModel *)comicModel
{
_comicModel = comicModel;
if (comicModel.vertical_cover.length > 0) {
self.coverImageView.coverImageUrl = comicModel.vertical_cover;
} else if (comicModel.horizontal_cover.length > 0) {
self.coverImageView.coverImageUrl = comicModel.horizontal_cover;
} else {
self.coverImageView.coverImageUrl = comicModel.cover;
}
self.titleNameLabel.text = comicModel.name?:@"";
self.subTitleLabel.text = [NSString stringWithFormat:TFLocalizedString(@"%@话/%@话"), [TFUtilsHelper formatStringWithInteger:[[WXYZ_ComicDownloadManager sharedManager] getDownloadChapterCountWithProduction_id:comicModel.production_id]], [TFUtilsHelper formatStringWithInteger:comicModel.total_chapters]];
if ([[TFReadRecordManager shareManagerWithProductionType:TFProductionTypeComic] getReadingRecordChapterTitleWithProduction_id:comicModel.production_id].length > 0) {
[self.readButton setTitle:TFLocalizedString(@"继续阅读") forState:UIControlStateNormal];
} else {
[self.readButton setTitle:TFLocalizedString(@"开始阅读") forState:UIControlStateNormal];
}
}
- (void)cellTapClick:(UITapGestureRecognizer *)tap
{
if (_isEditting) {
[self switchSelectedState:!_isSelected];
return;
}
if (tap.view.tag == 200) {
if (self.imageViewSelectBlock) {
self.imageViewSelectBlock(_comicModel.production_id);
}
} else {
if (self.cellSelectBlock) {
self.cellSelectBlock(_comicModel, _comicModel.name);
}
}
}
- (void)readButtonClick
{
if (self.buttonSelectBlock) {
self.buttonSelectBlock(_comicModel);
}
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
- (void)switchSelectedState:(BOOL)state {
UIImage *image = nil;
if (state) {
image = [UIImage imageNamed:@"audio_download_select"];
} else {
image = [UIImage imageNamed:@"audio_download_unselect"];
}
_isSelected = state;
self.selectedView.image = image;
!self.selecteEdittingCellBlock ?: self.selecteEdittingCellBlock(self.comicModel, state);
}
- (void)setEditing:(BOOL)editing {
if (editing && _isEditting == NO) {
self.readButton.hidden = editing;
[UIView animateWithDuration:0.2 animations:^{
[self.mainView mas_updateConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.contentView).offset(17.0f);
}];
[self.mainView.superview layoutIfNeeded];
} completion:^(BOOL finished) {
if (finished) {
self->_isEditting = YES;
}
}];
return;
}
if (!editing && _isEditting == YES) {
self.readButton.hidden = editing;
[UIView animateWithDuration:0.2 animations:^{
[self.mainView mas_updateConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.contentView).offset(-16.0f);
}];
[self.mainView.superview layoutIfNeeded];
} completion:^(BOOL finished) {
if (finished) {
self->_isEditting = NO;
}
}];
return;
}
}
- (void)set_Editting:(BOOL)editting {
if (editting) {
[self.mainView mas_updateConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.contentView).offset(17.0f);
}];
} else {
[self.mainView mas_updateConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.contentView).offset(-16.0f);
}];
}
[self.mainView.superview layoutIfNeeded];
_isEditting = editting;
}
@end
@@ -0,0 +1,22 @@
//
// TFComicDownloadViewCell.h
// TFReader
//
// Created by 谢腾飞 on 2020/12/15.
// Copyright © 2020 xtfei_2011@126.com. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "WXYZ_DownloadManagerEnumProtocol.h"
NS_ASSUME_NONNULL_BEGIN
@interface TFComicDownloadViewCell : UICollectionViewCell
@property (nonatomic ,strong) TFProductionChapterModel *chapterModel;
@property (nonatomic ,assign) WXYZ_ProductionDownloadState cellDownloadState;
@end
NS_ASSUME_NONNULL_END
@@ -0,0 +1,170 @@
//
// TFComicDownloadViewCell.m
// TFReader
//
// Created by 谢腾飞 on 2020/12/15.
// Copyright © 2020 xtfei_2011@126.com. All rights reserved.
//
#import "TFComicDownloadViewCell.h"
#import "WXYZ_ComicDownloadManager.h"
@interface TFComicDownloadViewCell ()
{
UILabel *chapterNumLabel;
UIImageView *lockImageView;
UILabel *cellStateLabel;
}
@end
@implementation TFComicDownloadViewCell
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
self.backgroundColor = [UIColor clearColor];
[self createSubViews];
}
return self;
}
- (void)createSubViews
{
chapterNumLabel = [[UILabel alloc] init];
chapterNumLabel.backgroundColor = kWhiteColor;
chapterNumLabel.textColor = kBlackColor;
chapterNumLabel.textAlignment = NSTextAlignmentCenter;
chapterNumLabel.font = kMainFont;
chapterNumLabel.layer.cornerRadius = 8;
chapterNumLabel.clipsToBounds = YES;
[self addSubview:chapterNumLabel];
[chapterNumLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.mas_equalTo(self);
}];
lockImageView = [[UIImageView alloc] init];
lockImageView.image = [[UIImage imageNamed:@"comic_lock"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
lockImageView.tintColor = kMainColor;
lockImageView.hidden = YES;
[chapterNumLabel addSubview:lockImageView];
[lockImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(kQuarterMargin);
make.top.mas_equalTo(kQuarterMargin);
make.width.height.mas_equalTo(self.mas_height).with.multipliedBy(0.3);
}];
cellStateLabel = [[UILabel alloc] init];
cellStateLabel.backgroundColor = [UIColor clearColor];
cellStateLabel.textColor = kWhiteColor;
cellStateLabel.textAlignment = NSTextAlignmentCenter;
cellStateLabel.font = kFont6;
cellStateLabel.layer.cornerRadius = (self.height * 0.2) / 2;
cellStateLabel.clipsToBounds = YES;
[chapterNumLabel addSubview:cellStateLabel];
[cellStateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.mas_equalTo(chapterNumLabel.mas_right).with.offset(- 2);
make.bottom.mas_equalTo(chapterNumLabel.mas_bottom).with.offset(- 2);
make.width.mas_equalTo(20);
make.height.mas_equalTo(self.mas_height).with.multipliedBy(0.2);
}];
}
- (void)setChapterModel:(TFProductionChapterModel *)chapterModel
{
_chapterModel = chapterModel;
chapterNumLabel.text = chapterModel.display_label?:@"";
if (chapterModel.can_read) {
lockImageView.hidden = YES;
} else {
lockImageView.hidden = NO;
}
chapterNumLabel.text = chapterModel.display_label?:@"";
}
- (void)setCellDownloadState:(WXYZ_ProductionDownloadState)cellDownloadState
{
_cellDownloadState = cellDownloadState;
lockImageView.hidden = self.chapterModel.can_read;
switch (cellDownloadState) {
case WXYZ_ProductionDownloadStateNormal:
{
cellStateLabel.backgroundColor = [UIColor clearColor];
cellStateLabel.hidden = YES;
cellStateLabel.text = @"";
lockImageView.tintColor = kMainColor;
chapterNumLabel.backgroundColor = kWhiteColor;
chapterNumLabel.textColor = kBlackColor;
}
break;
case WXYZ_ProductionDownloadStateDownloading:
{
cellStateLabel.backgroundColor = kColorRGBA(28, 220, 142, 1);
cellStateLabel.hidden = NO;
cellStateLabel.text = TFLocalizedString(@"下载中");
lockImageView.tintColor = kMainColor;
lockImageView.hidden = YES;
chapterNumLabel.backgroundColor = kGrayDeepViewColor;
chapterNumLabel.textColor = kGrayTextColor;
}
break;
case WXYZ_ProductionDownloadStateDownloaded:
{
cellStateLabel.backgroundColor = kColorRGBA(28, 220, 142, 0.5);
cellStateLabel.hidden = NO;
cellStateLabel.text = TFLocalizedString(@"本地");
lockImageView.tintColor = kMainColor;
lockImageView.hidden = YES;
chapterNumLabel.backgroundColor = kGrayDeepViewColor;
chapterNumLabel.textColor = kGrayTextColor;
}
break;
case WXYZ_ProductionDownloadStateFail:
{
cellStateLabel.backgroundColor = kRedColor;
cellStateLabel.hidden = NO;
cellStateLabel.text = TFLocalizedString(@"失败");
lockImageView.tintColor = kMainColor;
chapterNumLabel.backgroundColor = kWhiteColor;
chapterNumLabel.textColor = kBlackColor;
}
break;
case WXYZ_ProductionDownloadStateSelected:
{
cellStateLabel.backgroundColor = [UIColor clearColor];
cellStateLabel.hidden = YES;
cellStateLabel.text = @"";
lockImageView.tintColor = kWhiteColor;
chapterNumLabel.backgroundColor = kMainColor;
chapterNumLabel.textColor = kWhiteColor;
}
break;
default:
break;
}
[cellStateLabel mas_updateConstraints:^(MASConstraintMaker *make) {
make.width.mas_equalTo([TFViewHelper getDynamicWidthWithLabelFont:kFont6 labelHeight:(self.height * 0.2) labelText:cellStateLabel.text]);
}];
}
@end
@@ -0,0 +1,25 @@
//
// TFNovelDownloadManagerController.h
// TFReader
//
// Created by 谢腾飞 on 2020/12/16.
// Copyright © 2020 xtfei_2011@126.com. All rights reserved.
//
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface TFNovelDownloadManagerController : TFBasicViewController
@property (nonatomic ,assign) BOOL pushFromReader;
@property (nonatomic ,assign) BOOL isEditting;
// 切换编辑状态
@property (nonatomic ,copy ,readonly) BOOL (^editStateBlock)(void);
@property (nonatomic ,copy) void(^changeEditStateBlock)(BOOL status);
@end
NS_ASSUME_NONNULL_END
@@ -0,0 +1,457 @@
//
// TFNovelDownloadManagerController.m
// TFReader
//
// Created by 谢腾飞 on 2020/12/16.
// Copyright © 2020 xtfei_2011@126.com. All rights reserved.
//
#import "TFNovelDownloadManagerController.h"
#import "WXYZ_BookDownloadManager.h"
#import "TFNovelDownloadManagerCell.h"
#import "TFNovelDownloadManagerStateCell.h"
#import "TFReadNovelViewController.h"
#import "UIView+LayoutCallback.h"
#import "TFCollectionManager.h"
@interface TFNovelDownloadManagerController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic ,strong) NSMutableArray<NSNumber *> *selectedArray;
// 章节记录
@property (nonatomic ,strong) NSMutableDictionary *chapterDownloadDic;
@property (nonatomic ,weak) CALayer *topLayer;
@property (nonatomic ,weak) CALayer *middleLayer;
/// 编辑页面
@property (nonatomic ,weak) UIView *edittingView;
@property (nonatomic ,weak) UIButton *edittingAll;
@property (nonatomic ,weak) UIButton *edittingDelete;
@end
@implementation TFNovelDownloadManagerController
- (void)viewDidLoad
{
[super viewDidLoad];
[self netRequest];
[self initialize];
[self createSubviews];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self setStatusBarDefaultStyle];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] postNotificationName:Notification_Hidden_Tabbar object:nil];
}
- (void)initialize
{
self.selectedArray = [NSMutableArray array];
// 设置编辑状态
WS(weakSelf)
_editStateBlock = ^() {
BOOL isEditting = NO;
if (weakSelf.mainTableViewGroup.visibleCells.count == 0) return NO;
for (UITableViewCell *cell in weakSelf.mainTableViewGroup.visibleCells) {
if ([cell isMemberOfClass:TFNovelDownloadManagerCell.class]) {
TFNovelDownloadManagerCell *celll = (TFNovelDownloadManagerCell *)cell;
isEditting = !celll.isEditting;
weakSelf.isEditting = isEditting;
// 隐藏/显示底部编辑区域
if (isEditting) {
[UIView animateWithDuration:kAnimatedDuration animations:^{
[weakSelf.edittingView mas_updateConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(weakSelf.view);
}];
[weakSelf.edittingView.superview layoutIfNeeded];
}];
} else {
[UIView animateWithDuration:kAnimatedDuration animations:^{
[weakSelf.edittingView mas_updateConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(weakSelf.view).offset(CGRectGetHeight(weakSelf.edittingView .bounds));
}];
[weakSelf.edittingView.superview layoutIfNeeded];
}];
}
[celll setEditing:!celll.isEditting];
continue;
}
if ([cell isMemberOfClass:TFNovelDownloadManagerStateCell.class]) {
TFNovelDownloadManagerStateCell *celll = (TFNovelDownloadManagerStateCell *)cell;
[celll setEditing:!celll.isEditting];
continue;
}
}
return isEditting;
};
[self hiddenNavigationBar:YES];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(netRequest) name:Notification_Reload_Download_State object:nil];
}
- (void)createSubviews
{
UIView *edittingView = [[UIView alloc] init];
self.edittingView = edittingView;
edittingView .backgroundColor = self.view.backgroundColor;
WS(weakSelf)
edittingView .frameBlock = ^(UIView * _Nonnull view) {
if (weakSelf.topLayer) return;
CALayer *layer = [CALayer layer];
layer.backgroundColor = kColorRGB(238, 238, 238).CGColor;
layer.frame = CGRectMake(0, 0, CGRectGetWidth(view.bounds), 0.5);
[view.layer addSublayer:layer];
weakSelf.topLayer = layer;
};
[self.view addSubview:edittingView ];
[edittingView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.width.bottom.equalTo(self.view);
}];
// 编辑状态下全选按钮
UIButton *edittingAll = [UIButton buttonWithType:UIButtonTypeCustom];
self.edittingAll = edittingAll;
[edittingAll setTitle:TFLocalizedString(@"全选") forState:UIControlStateNormal];
[edittingAll addTarget:self action:@selector(selectedAllEvent) forControlEvents:UIControlEventTouchUpInside];
edittingAll.frameBlock = ^(UIButton * _Nonnull button) {
if (weakSelf.middleLayer) return;
CALayer *layer = [CALayer layer];
layer.backgroundColor = kColorRGB(238, 238, 238).CGColor;
layer.frame = CGRectMake(CGRectGetMaxX(button.frame), 16.0f, 0.5, CGRectGetHeight(button.bounds) - 2 * 16.0f);
[button.layer addSublayer:layer];
weakSelf.middleLayer = layer;
};
[edittingAll setTitleColor:kBlackColor forState:UIControlStateNormal];
edittingAll.titleLabel.font = kFont14;
[edittingView addSubview:edittingAll];
[edittingAll mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(edittingView ).offset(1.0f);
make.left.equalTo(edittingView );
make.width.equalTo(edittingView ).multipliedBy(0.5f);
make.height.equalTo(edittingAll.mas_width).multipliedBy(60.0f / 187.5f);
make.bottom.equalTo(edittingView ).offset(-PUB_TABBAR_OFFSET).priorityLow();
}];
// 编辑状态下删除按钮
UIButton *edittingDelete = [UIButton buttonWithType:UIButtonTypeCustom];
self.edittingDelete = edittingDelete;
[edittingDelete setTitle:TFLocalizedString(@"删除") forState:UIControlStateNormal];
[edittingDelete addTarget:self action:@selector(deleteEvent) forControlEvents:UIControlEventTouchUpInside];
[edittingDelete setTitleColor:kGrayTextColor forState:UIControlStateNormal];
edittingDelete.titleLabel.font = kFont14;
[edittingView addSubview:edittingDelete];
[edittingDelete mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.top.equalTo(edittingAll);
make.left.equalTo(edittingAll.mas_right);
}];
self.mainTableViewGroup.delegate = self;
self.mainTableViewGroup.dataSource = self;
[self.view addSubview:self.mainTableViewGroup];
[self.mainTableViewGroup mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.left.right.equalTo(self.view);
make.bottom.equalTo(edittingView .mas_top);
}];
[edittingView setNeedsLayout];
[edittingView layoutIfNeeded];
// 设置编辑区域默认高度
[edittingView mas_updateConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(self.view).offset(CGRectGetHeight(edittingView .bounds));
}];
[self setEmptyOnView:self.mainTableViewGroup title:TFLocalizedString(@"还没有下载记录") tapBlock:nil];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.dataSourceArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
TFNovelDownloadTaskListModel *listModel = [self.dataSourceArray objectOrNilAtIndex:section];
return listModel.task_list.count + 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
TFNovelDownloadTaskListModel *listModel = [self.dataSourceArray objectOrNilAtIndex:indexPath.section];
WS(weakSelf)
if (indexPath.row == 0) {
TFNovelDownloadManagerCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TFNovelDownloadManagerCell"];
if (!cell) {
cell = [[TFNovelDownloadManagerCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TFNovelDownloadManagerCell"];
}
cell.productionModel = listModel.productionModel;
[cell set_Editting:self.isEditting];
cell.openBookBlock = ^(NSString *book_id) {
TFReadNovelViewController *vc = [[TFReadNovelViewController alloc] init];
[[TFCollectionManager shareManagerWithProductionType:TFProductionTypeNovel] moveCollectionToTopWithProductionModel:listModel.productionModel];
vc.book_id = [book_id integerValue];
[weakSelf.navigationController pushViewController:vc animated:YES];
};
cell.cellSelectBlock = ^(NSInteger production_id) {
TFNovelDetailViewController *vc = [[TFNovelDetailViewController alloc] init];
vc.book_id = production_id;
[weakSelf.navigationController pushViewController:vc animated:YES];
};
if ([self.selectedArray containsObject:@(listModel.productionModel.production_id)]) {
[cell switchSelectedState:YES];
} else {
[cell switchSelectedState:NO];
}
cell.selecteEdittingCellBlock = ^(TFProductionModel * _Nonnull productionModel, BOOL isSelected) {
if (isSelected) {
[weakSelf.selectedArray addObject:@(productionModel.production_id)];
} else {
[weakSelf.selectedArray removeObject:@(productionModel.production_id)];
}
[weakSelf updateEdittingView];
};
if (self.pushFromReader) {
cell.openBook.hidden = YES;
} else {
cell.openBook.hidden = NO;
}
return cell;
} else {
TFNovelDownloadManagerStateCell *chapterCell = [tableView dequeueReusableCellWithIdentifier:@"TFNovelDownloadManagerStateCell"];
if (!chapterCell) {
chapterCell = [[TFNovelDownloadManagerStateCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TFNovelDownloadManagerStateCell"];
}
chapterCell.downloadTaskModel = [listModel.task_list objectOrNilAtIndex:indexPath.row - 1];
[chapterCell set_Editing:self.isEditting];
return chapterCell;
}
}
//section头部间距
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return CGFLOAT_MIN;
}
//section头部视图
- (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;
}
//section底部间距
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
if (section == self.dataSourceArray.count - 1) {
return PUB_TABBAR_OFFSET == 0 ? 10 : PUB_TABBAR_OFFSET;
}
return 0.01;
}
//section底部视图
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, section == self.dataSourceArray.count - 1?(PUB_TABBAR_OFFSET == 0 ? 10 : PUB_TABBAR_OFFSET):0.01)];
view.backgroundColor = [UIColor clearColor];
return view;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0) {
return YES;
}
return NO;
}
- (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
{
TFNovelDownloadTaskListModel *t_model = [self.dataSourceArray objectOrNilAtIndex:indexPath.section];
[[WXYZ_BookDownloadManager sharedManager] removeDownloadProductionWithProduction_id:t_model.productionModel.production_id];
[self.dataSourceArray removeObject:t_model];
[self.mainTableViewGroup reloadData];
if (self.dataSourceArray.count > 0) {
[self.mainTableViewGroup xtfei_hideEmptyView];
} else {
[self.mainTableViewGroup xtfei_showEmptyView];
}
}
- (void)netRequest
{
NSArray *downloandArray = [[[[WXYZ_DownloadHelper sharedManager] getDownloadProductionArrayWithProductionType:TFProductionTypeNovel] reverseObjectEnumerator] allObjects];
for (TFProductionModel *productionModel in downloandArray) {
TFNovelDownloadTaskListModel *downloadTaskListModel = [[WXYZ_BookDownloadManager sharedManager] getDownloadProductionModelWithProduction_id:productionModel.production_id];
if (downloadTaskListModel.task_list.count > 0) {
[self.dataSourceArray addObject:downloadTaskListModel];
}
}
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
dispatch_async(dispatch_get_main_queue(), ^{
[self.mainTableViewGroup reloadData];
if (self.dataSourceArray.count > 0) {
[self.mainTableViewGroup xtfei_hideEmptyView];
} else {
[self.mainTableViewGroup xtfei_showEmptyView];
}
});
});
}
// 全选按钮点击事件
- (void)selectedAllEvent {
if ([self.edittingAll.titleLabel.text isEqualToString:TFLocalizedString(@"全选")]) {
for (UITableViewCell *cell in self.mainTableViewGroup.visibleCells) {
if ([cell isMemberOfClass:TFNovelDownloadManagerCell.class]) {
TFNovelDownloadManagerCell *tempCell = (TFNovelDownloadManagerCell *)cell;
[tempCell switchSelectedState:YES];
}
}
for (TFNovelDownloadTaskListModel *t_model in self.dataSourceArray) {
if (![self.selectedArray containsObject:@(t_model.productionModel.production_id)]) {
[self.selectedArray addObject:@(t_model.productionModel.production_id)];
}
}
} else {
for (UITableViewCell *cell in self.mainTableViewGroup.visibleCells) {
if ([cell isMemberOfClass:TFNovelDownloadManagerCell.class]) {
TFNovelDownloadManagerCell *tempCell = (TFNovelDownloadManagerCell *)cell;
[tempCell switchSelectedState:NO];
}
}
[self.selectedArray removeAllObjects];
}
[self updateEdittingView];
}
// 删除按钮点击事件
- (void)deleteEvent {
if (self.selectedArray.count == 0) return;
NSMutableIndexSet *set = [[NSMutableIndexSet alloc] init];
NSMutableArray *arr = [self.dataSourceArray mutableCopy];
[arr enumerateObjectsUsingBlock:^(TFNovelDownloadTaskListModel * _Nonnull downloadModel, NSUInteger idx, BOOL * _Nonnull stop) {
if ([self.selectedArray containsObject:@(downloadModel.productionModel.production_id)]) {
[[WXYZ_BookDownloadManager sharedManager] removeDownloadProductionWithProduction_id:downloadModel.productionModel.production_id];
[self.dataSourceArray removeObject:downloadModel];
[self.selectedArray removeObject:@(downloadModel.productionModel.production_id)];
[set addIndex:idx];
}
}];
WS(weakSelf)
if (@available(iOS 11.0, *)) {
[self.mainTableViewGroup performBatchUpdates:^{
[self.mainTableViewGroup deleteSections:set withRowAnimation:UITableViewRowAnimationLeft];
} completion:^(BOOL finished) {
SS(strongSelf)
if (!finished) return;
BOOL a = !strongSelf->_editStateBlock ?: strongSelf->_editStateBlock();
NSLog(@"%@", a?@"":@"");
[UIView animateWithDuration:kAnimatedDuration animations:^{
[self.edittingView mas_updateConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(self.view).offset(CGRectGetHeight(self.edittingView .bounds));
}];
[self.edittingView.superview layoutIfNeeded];
}];
!self.changeEditStateBlock ?: self.changeEditStateBlock(YES);
}];
} else {
[self.mainTableViewGroup beginUpdates];
[self.mainTableViewGroup deleteSections:set withRowAnimation:UITableViewRowAnimationLeft];
[self.mainTableViewGroup endUpdates];
dispatch_async(dispatch_get_main_queue(), ^{
SS(strongSelf)
BOOL a = !strongSelf->_editStateBlock ?: strongSelf->_editStateBlock();
NSLog(@"%@", a?@"":@"");
[UIView animateWithDuration:kAnimatedDuration animations:^{
[self.edittingView mas_updateConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(self.view).offset(CGRectGetHeight(self.edittingView .bounds));
}];
[self.edittingView.superview layoutIfNeeded];
}];
!self.changeEditStateBlock ?: self.changeEditStateBlock(YES);
});
}
if (self.dataSourceArray.count > 0) {
[self.mainTableViewGroup xtfei_hideEmptyView];
} else {
[self.mainTableViewGroup xtfei_showEmptyView];
}
self.isEditting = NO;
[self updateEdittingView];
}
// 更新底部编辑区域
- (void)updateEdittingView {
if (self.dataSourceArray.count > 0 && self.mainTableViewGroup.visibleCells.count == 0) return;
BOOL allSelected = YES;
for (TFNovelDownloadTaskListModel *t_model in self.dataSourceArray) {
if (![self.selectedArray containsObject:@(t_model.productionModel.production_id)]) {
allSelected = NO;
break;
}
}
if (allSelected) {
[self.edittingAll setTitle:TFLocalizedString(@"取消全选") forState:UIControlStateNormal];
} else {
[self.edittingAll setTitle:TFLocalizedString(@"全选") forState:UIControlStateNormal];
}
if (self.selectedArray.count == 0) {
[self.edittingDelete setTitle:TFLocalizedString(@"删除") forState:UIControlStateNormal];
[self.edittingDelete setTitleColor:kGrayTextColor forState:UIControlStateNormal];
} else {
[self.edittingDelete setTitle:[NSString stringWithFormat:@"%@ (%zd)", TFLocalizedString(@"删除"), self.selectedArray.count] forState:UIControlStateNormal];
[self.edittingDelete setTitleColor:kRedColor forState:UIControlStateNormal];
}
}
@end
@@ -0,0 +1,54 @@
//
// TFNovelDownloadTaskListModel.h
// TFReader
//
// Created by 谢腾飞 on 2020/12/16.
// Copyright © 2020 xtfei_2011@126.com. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "WXYZ_DownloadManagerProtocol.h"
NS_ASSUME_NONNULL_BEGIN
@class TFDownloadTaskModel, TFProductionModel;
@interface TFNovelDownloadTaskListModel : NSObject
@property (nonatomic ,strong) TFProductionModel *productionModel;
@property (nonatomic ,strong) NSMutableArray <TFDownloadTaskModel *> *task_list;
@end
@interface TFDownloadTaskModel : NSObject
@property (nonatomic ,copy) NSString *label;
@property (nonatomic ,copy) NSString *start_chapter_id;
@property (nonatomic ,copy) NSString *tag;
@property (nonatomic ,assign) NSInteger down_num;
@property (nonatomic ,copy) NSString *file_name;
@property (nonatomic ,assign) NSInteger start_order;
@property (nonatomic ,assign) NSInteger end_order;
@property (nonatomic ,copy) NSString *url;
@property (nonatomic ,copy) NSString *download_title;
@property (nonatomic ,copy) NSString *dateString;
@property (nonatomic ,copy) NSString *file_size_title;
@property (nonatomic ,assign) CGFloat file_size;
@end
NS_ASSUME_NONNULL_END
@@ -0,0 +1,42 @@
//
// TFNovelDownloadTaskListModel.m
// TFReader
//
// Created by 谢腾飞 on 2020/12/16.
// Copyright © 2020 xtfei_2011@126.com. All rights reserved.
//
#import "TFNovelDownloadTaskListModel.h"
@implementation TFNovelDownloadTaskListModel
+ (NSDictionary<NSString *,id> *)modelContainerPropertyGenericClass{
return @{@"task_list" : [TFDownloadTaskModel class]};
}
+ (NSDictionary *)modelCustomPropertyMapper {
return @{
@"task_list" :@"down_option"
};
}
@end
@implementation TFDownloadTaskModel
+ (NSDictionary *)modelCustomPropertyMapper
{
return @{
@"start_chapter_id" :@"s_chapter"
};
}
- (NSString *)file_size_title
{
return [NSString stringWithFormat:@"%.2lfM", self.file_size / 1024.0 / 1024.0];
}
@end
@@ -0,0 +1,45 @@
//
// TFNovelDownloadManagerCell.h
// TFReader
//
// Created by 谢腾飞 on 2020/12/16.
// Copyright © 2020 xtfei_2011@126.com. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "TFNovelDownloadTaskListModel.h"
NS_ASSUME_NONNULL_BEGIN
typedef void(^OpenBookBlock)(NSString *book_id);
typedef void(^CellSelectBlock)(NSInteger production_id);
@interface TFNovelDownloadManagerCell : TFBasicTableViewCell
@property (nonatomic ,copy) CellSelectBlock cellSelectBlock;
@property (nonatomic ,strong) TFProductionModel *productionModel;
@property (nonatomic ,copy) OpenBookBlock openBookBlock;
@property (nonatomic ,strong) UIButton *openBook;
@property (nonatomic ,assign, readonly) BOOL isEditting;
@property (nonatomic ,assign, readonly) BOOL isSelected;
/// 选择编辑单元
@property (nonatomic ,copy) void(^selecteEdittingCellBlock)(TFProductionModel *productionModel, BOOL isSelected);
- (void)setEditing:(BOOL)editing;
// 强行设置编辑状态
- (void)set_Editting:(BOOL)editting;
// 切换选中状态
- (void)switchSelectedState:(BOOL)state;
@end
NS_ASSUME_NONNULL_END
@@ -0,0 +1,185 @@
//
// TFNovelDownloadManagerCell.m
// TFReader
//
// Created by 谢腾飞 on 2020/12/16.
// Copyright © 2020 xtfei_2011@126.com. All rights reserved.
//
#import "TFNovelDownloadManagerCell.h"
@interface TFNovelDownloadManagerCell ()
@property (nonatomic ,weak) TFProductionCoverView *bookImageView;
@property (nonatomic ,weak) UILabel *titleLabel;
@property (nonatomic ,weak) UIImageView *selectedView;
@property (nonatomic ,weak) UIView *mainView;
@end
@implementation TFNovelDownloadManagerCell
- (void)createSubviews
{
[super createSubviews];
self.selectionStyle = UITableViewCellSelectionStyleNone;
[self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(openBookClick)]];
UIView *mainView = [[UIView alloc] init];
self.mainView = mainView;
mainView.backgroundColor = [UIColor clearColor];
[self.contentView addSubview:mainView];
[mainView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.contentView);
make.left.equalTo(self.contentView).offset(-16.0f);
make.width.equalTo(self.contentView).offset(16.0f);
make.height.equalTo(self.contentView);
}];
UIImageView *selectedView = [[UIImageView alloc] init];
self.selectedView = selectedView;
selectedView.image = [UIImage imageNamed:@"audio_download_unselect"];
[mainView addSubview:selectedView];
[selectedView mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.height.mas_equalTo(16.0f);
make.left.equalTo(mainView);
make.centerY.equalTo(mainView);
}];
TFProductionCoverView *bookImageView = [[TFProductionCoverView alloc] initWithProductionType:TFProductionTypeNovel coverDirection:TFProductionCoverDirectionVertical];
self.bookImageView = bookImageView;
bookImageView.userInteractionEnabled = YES;
[bookImageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cellTapClick:)]];
[mainView addSubview:bookImageView];
[bookImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(selectedView.mas_right).with.offset(kMoreHalfMargin);
make.top.mas_equalTo(mainView.mas_top).with.offset(kHalfMargin);
make.width.mas_equalTo(BOOK_WIDTH_SMALL - kMargin);
make.height.mas_equalTo(kGeometricHeight(BOOK_WIDTH_SMALL - kMargin, 3, 4));
make.bottom.mas_equalTo(mainView.mas_bottom).with.offset(- kHalfMargin).priorityLow();
}];
UILabel *titleLabel = [[UILabel alloc] init];
self.titleLabel = titleLabel;
titleLabel.numberOfLines = 0;
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.font = kMainFont;
titleLabel.textColor = kBlackColor;
titleLabel.textAlignment = NSTextAlignmentLeft;
[mainView addSubview:titleLabel];
[titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(bookImageView.mas_right).with.offset(kHalfMargin);
make.top.height.equalTo(bookImageView);
}];
self.openBook = [UIButton buttonWithType:UIButtonTypeCustom];
self.openBook.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
[self.openBook setTitle:TFLocalizedString(@"打开") forState:UIControlStateNormal];
[self.openBook setTitleColor:kMainColor forState:UIControlStateNormal];
[self.openBook.titleLabel setFont:kMainFont];
[self.openBook addTarget:self action:@selector(openBookClick) forControlEvents:UIControlEventTouchUpInside];
[mainView addSubview:self.openBook];
[self.openBook mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.mas_equalTo(mainView.mas_right).with.offset(- kMargin);
make.centerY.mas_equalTo(mainView.mas_centerY);
}];
[titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(self.openBook.mas_left).offset(-kQuarterMargin);
}];
}
- (void)openBookClick
{
if (_isEditting) {
[self switchSelectedState:!_isSelected];
return;
}
if (self.openBookBlock) {
self.openBookBlock([TFUtilsHelper formatStringWithInteger:_productionModel.production_id]);
}
}
- (void)cellTapClick:(UITapGestureRecognizer *)tap
{
if (_isEditting) {
[self switchSelectedState:!_isSelected];
return;
}
!self.cellSelectBlock ?: self.cellSelectBlock(_productionModel.production_id);
}
- (void)setProductionModel:(TFProductionModel *)productionModel
{
_productionModel = productionModel;
self.bookImageView.coverImageUrl = productionModel.cover;
self.titleLabel.text = productionModel.name;
}
- (void)switchSelectedState:(BOOL)state {
UIImage *image = nil;
if (state) {
image = [UIImage imageNamed:@"audio_download_select"];
} else {
image = [UIImage imageNamed:@"audio_download_unselect"];
}
_isSelected = state;
self.selectedView.image = image;
!self.selecteEdittingCellBlock ?: self.selecteEdittingCellBlock(self.productionModel, state);
}
- (void)setEditing:(BOOL)editing {
if (editing && _isEditting == NO) {
self.openBook.hidden = editing;
[UIView animateWithDuration:0.2 animations:^{
[self.mainView mas_updateConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.contentView).offset(17.0f);
}];
[self.mainView.superview layoutIfNeeded];
} completion:^(BOOL finished) {
if (finished) {
self->_isEditting = YES;
}
}];
return;
}
if (!editing && _isEditting == YES) {
self.openBook.hidden = editing;
[UIView animateWithDuration:0.2 animations:^{
[self.mainView mas_updateConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.contentView).offset(-16.0f);
}];
[self.mainView.superview layoutIfNeeded];
} completion:^(BOOL finished) {
if (finished) {
self->_isEditting = NO;
}
}];
return;
}
}
- (void)set_Editting:(BOOL)editting {
if (editting) {
[self.mainView mas_updateConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.contentView).offset(17.0f);
}];
} else {
[self.mainView mas_updateConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.contentView).offset(-16.0f);
}];
}
[self.mainView.superview layoutIfNeeded];
_isEditting = editting;
}
@end
@@ -0,0 +1,34 @@
//
// TFNovelDownloadManagerStateCell.h
// TFReader
//
// Created by 谢腾飞 on 2020/12/16.
// Copyright © 2020 xtfei_2011@126.com. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "TFNovelDownloadTaskListModel.h"
NS_ASSUME_NONNULL_BEGIN
@interface TFNovelDownloadManagerStateCell : TFBasicTableViewCell
@property (nonatomic ,strong) TFDownloadTaskModel *downloadTaskModel;
@property (nonatomic ,strong) TFProductionModel *bookModel;
@property (nonatomic ,strong) UIButton *retryButton;
@property (nonatomic ,assign, readonly) BOOL isEditting;
/// 选择编辑单元
@property (nonatomic ,copy) void(^selecteEdittingCellBlock)(TFProductionModel *bookModel);
- (void)setEditing:(BOOL)editing;
// 强行设置编辑状态
- (void)set_Editing:(BOOL)editing;
@end
NS_ASSUME_NONNULL_END
@@ -0,0 +1,204 @@
//
// TFNovelDownloadManagerStateCell.m
// TFReader
//
// Created by 谢腾飞 on 2020/12/16.
// Copyright © 2020 xtfei_2011@126.com. All rights reserved.
//
#import "TFNovelDownloadManagerStateCell.h"
#import "WXYZ_BookDownloadManager.h"
@interface TFNovelDownloadManagerStateCell ()
@property (nonatomic ,weak) UIView *mainView;
@property (nonatomic ,weak) UILabel *chapterLabel;
@property (nonatomic ,weak) UILabel *subChapterLabel;
@end
@implementation TFNovelDownloadManagerStateCell
- (void)createSubviews
{
[super createSubviews];
self.selectionStyle = UITableViewCellSelectionStyleNone;
[self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cellTapClick)]];
UIView *mainView = [[UIView alloc] init];
self.mainView = mainView;
mainView.backgroundColor = [UIColor clearColor];
[self.contentView addSubview:mainView];
[mainView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.contentView);
make.width.equalTo(self.contentView);
}];
UILabel *chapterLabel = [[UILabel alloc] init];
self.chapterLabel = chapterLabel;
chapterLabel.textColor = kBlackColor;
chapterLabel.textAlignment = NSTextAlignmentLeft;
chapterLabel.numberOfLines = 0;
chapterLabel.font = kMainFont;
[mainView addSubview:chapterLabel];
[chapterLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(mainView.mas_left).with.offset(kMargin);
make.top.mas_equalTo(mainView.mas_top).with.offset(kHalfMargin);
}];
UILabel *subChapterLabel = [[UILabel alloc] init];
self.subChapterLabel = subChapterLabel;
subChapterLabel.textColor = kGrayTextLightColor;
subChapterLabel.textAlignment = NSTextAlignmentLeft;
subChapterLabel.numberOfLines = 0;
subChapterLabel.font = kFont11;
[mainView addSubview:subChapterLabel];
[subChapterLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.equalTo(chapterLabel);
make.top.mas_equalTo(chapterLabel.mas_bottom).offset(2.0f);
make.bottom.mas_equalTo(mainView.mas_bottom).with.offset(- kHalfMargin);
}];
[mainView addSubview:self.retryButton];
UILabel *stateLabel = [[UILabel alloc] init];
stateLabel.textAlignment = NSTextAlignmentRight;
stateLabel.textColor = kGrayTextLightColor;
stateLabel.font = kFont11;
stateLabel.text = TFLocalizedString(@"已下载");
[mainView addSubview:stateLabel];
[stateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.mas_equalTo(mainView.mas_right).with.offset(- kHalfMargin);
make.centerY.mas_equalTo(mainView.mas_centerY);
make.width.mas_equalTo(stateLabel.intrinsicContentSize.width);
make.height.mas_equalTo(mainView.mas_height);
}];
[self.retryButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.mas_equalTo(mainView.mas_right).with.offset(- kMargin);
make.centerY.mas_equalTo(mainView.mas_centerY);
}];
[chapterLabel mas_updateConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(self.retryButton.mas_left).offset(-kHalfMargin);
}];
}
- (void)cellTapClick {
// if (_isEditting) {
// !self.selecteEdittingCellBlock ?: self.selecteEdittingCellBlock(self.bookModel);
// return;
// }
}
- (void)retryButtonClick
{
if (self.retryButton.titleLabel.text.length == 0) return;
// if (_isEditting) {
// !self.selecteEdittingCellBlock ?: self.selecteEdittingCellBlock(self.bookModel);
// return;
// }
self.retryButton.enabled = NO;
WS(weakSelf)
WXYZ_BookDownloadManager *bookDownloadManager = [WXYZ_BookDownloadManager sharedManager];
[bookDownloadManager downloadChaptersWithProductionModel:self.bookModel downloadTaskModel:self.downloadTaskModel production_id:self.bookModel.production_id start_chapter_id:[_downloadTaskModel.start_chapter_id integerValue] downloadNum:_downloadTaskModel.down_num];
bookDownloadManager.downloadMissionStateChangeBlock = ^(WXYZ_DownloadMissionState state, NSInteger production_id, TFDownloadTaskModel * _Nonnull downloadTaskModel, NSArray<NSNumber *> * _Nullable chapterIDArray) {
if (state == WXYZ_DownloadStateMissionFail) {
[TFPromptManager showPromptViewWithStatus:TFPromptStatusError promptTitle:TFLocalizedString(@"重试失败")];
weakSelf.retryButton.enabled = YES;
}
};
}
- (void)setDownloadTaskModel:(TFDownloadTaskModel *)downloadTaskModel
{
_downloadTaskModel = downloadTaskModel;
self.chapterLabel.text = downloadTaskModel.download_title;
self.subChapterLabel.text = [NSString stringWithFormat:@"%@ %@", downloadTaskModel.dateString, downloadTaskModel.file_size_title];
WS(weakSelf)
WXYZ_BookDownloadManager *bookDownloadManager = [WXYZ_BookDownloadManager sharedManager];
bookDownloadManager.downloadMissionStateChangeBlock = ^(WXYZ_DownloadMissionState state, NSInteger production_id, TFDownloadTaskModel * _Nonnull downloadTaskModel, NSArray<NSNumber *> * _Nullable chapterIDArray) {
switch (state) {
case WXYZ_DownloadStateMissionFinished:
weakSelf.retryButton.enabled = NO;
[weakSelf.retryButton setTitle:TFLocalizedString(@"已下载") forState:UIControlStateNormal];
break;
case WXYZ_DownloadStateMissionFail:
[weakSelf.retryButton setTitle:TFLocalizedString(@"失败重试") forState:UIControlStateNormal];
weakSelf.retryButton.enabled = YES;
break;
default:
break;
}
};
}
- (UIButton *)retryButton
{
if (!_retryButton) {
_retryButton = [UIButton buttonWithType:UIButtonTypeCustom];
_retryButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
[_retryButton setTitleColor:kGrayTextLightColor forState:UIControlStateNormal];
[_retryButton.titleLabel setFont:kFont12];
[_retryButton addTarget:self action:@selector(retryButtonClick) forControlEvents:UIControlEventTouchUpInside];
}
return _retryButton;
}
- (void)setEditing:(BOOL)editing {
if (editing && _isEditting == NO) {
[UIView animateWithDuration:0.2 animations:^{
[self.mainView mas_updateConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.contentView).offset(16.0 + 17.0);
}];
[self.mainView.superview layoutIfNeeded];
} completion:^(BOOL finished) {
if (finished) {
self->_isEditting = YES;
}
}];
return;
}
if (!editing && _isEditting == YES) {
[UIView animateWithDuration:0.2 animations:^{
[self.mainView mas_updateConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.contentView);
}];
[self.mainView.superview layoutIfNeeded];
} completion:^(BOOL finished) {
if (finished) {
self->_isEditting = NO;
}
}];
return;
}
}
- (void)set_Editing:(BOOL)editing {
if (editing) {
[self.mainView mas_updateConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.contentView).offset(16.0 + 17.0);
}];
} else {
[self.mainView mas_updateConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.contentView);
}];
}
[self.mainView.superview layoutIfNeeded];
_isEditting = editing;
}
@end
@@ -0,0 +1,27 @@
//
// TFNovelDownloadMenuCell.h
// TFReader
//
// Created by 谢腾飞 on 2020/12/16.
// Copyright © 2020 xtfei_2011@126.com. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "TFNovelDownloadTaskListModel.h"
#import "WXYZ_BookDownloadManager.h"
NS_ASSUME_NONNULL_BEGIN
@interface TFNovelDownloadMenuCell : TFBasicTableViewCell
@property (nonatomic ,strong) TFDownloadTaskModel *optionModel;
@property (nonatomic ,assign) WXYZ_DownloadMissionState missionState;
@property (nonatomic ,assign) NSInteger book_id;
- (void)startDownloadLoading;
@end
NS_ASSUME_NONNULL_END
@@ -0,0 +1,156 @@
//
// TFNovelDownloadMenuCell.m
// TFReader
//
// Created by 谢腾飞 on 2020/12/16.
// Copyright © 2020 xtfei_2011@126.com. All rights reserved.
//
#import "TFNovelDownloadMenuCell.h"
@interface TFNovelDownloadMenuCell ()
{
UILabel *chapterLabel;
UILabel *tagView;
UILabel *downloadStateLabel;
UIActivityIndicatorView *activityIndicator;
}
@end
@implementation TFNovelDownloadMenuCell
- (void)createSubviews
{
[super createSubviews];
chapterLabel = [[UILabel alloc] init];
chapterLabel.textColor = kBlackColor;
chapterLabel.textAlignment = NSTextAlignmentLeft;
chapterLabel.font = kFont12;
[self.contentView addSubview:chapterLabel];
[chapterLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(self.contentView.mas_left).with.offset(kMargin);
make.top.mas_equalTo(self.contentView.mas_top);
make.height.mas_equalTo(40);
make.width.mas_equalTo(100);
}];
tagView = [[UILabel alloc] init];
tagView.hidden = YES;
tagView.textColor = kWhiteColor;
tagView.textAlignment = NSTextAlignmentCenter;
tagView.font = kFont8;
tagView.backgroundColor = kRedColor;
tagView.layer.cornerRadius = 4;
tagView.clipsToBounds = YES;
[self.contentView addSubview:tagView];
[tagView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(chapterLabel.mas_right);
make.centerY.mas_equalTo(chapterLabel.mas_centerY);
make.width.mas_equalTo(50);
make.height.mas_equalTo(15);
}];
downloadStateLabel = [[UILabel alloc] init];
downloadStateLabel.hidden = YES;
downloadStateLabel.textColor = kGrayTextLightColor;
downloadStateLabel.textAlignment = NSTextAlignmentRight;
downloadStateLabel.font = kFont12;
downloadStateLabel.backgroundColor = [UIColor clearColor];
[self.contentView addSubview:downloadStateLabel];
[downloadStateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.mas_equalTo(self.contentView.mas_right).with.offset(- kMargin);
make.top.mas_equalTo(0);
make.width.mas_equalTo(100);
make.height.mas_equalTo(40);
}];
activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:(UIActivityIndicatorViewStyleGray)];
activityIndicator.hidesWhenStopped = YES;
[self.contentView addSubview:activityIndicator];
//设置小菊花的frame
[activityIndicator mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.mas_equalTo(self.contentView.mas_right).with.offset(- kMargin);
make.centerY.mas_equalTo(self.contentView.mas_centerY);
make.height.width.mas_equalTo(20);
}];
}
- (void)startDownloadLoading
{
[activityIndicator startAnimating];
}
- (void)setOptionModel:(TFDownloadTaskModel *)optionModel
{
_optionModel = optionModel;
chapterLabel.text = optionModel.label?:@"";
[chapterLabel mas_updateConstraints:^(MASConstraintMaker *make) {
make.width.mas_equalTo([TFViewHelper getDynamicWidthWithLabelFont:kFont12 labelHeight:40 labelText:optionModel.label?:@""]);
}];
if (optionModel.tag && optionModel.tag.length > 0) {
tagView.hidden = NO;
tagView.text = optionModel.tag;
[tagView mas_updateConstraints:^(MASConstraintMaker *make) {
make.width.mas_equalTo([TFViewHelper getDynamicWidthWithLabelFont:kFont8 labelHeight:15 labelText:optionModel.tag]);
}];
} else {
tagView.hidden = YES;
}
chapterLabel.textColor = kBlackColor;
downloadStateLabel.hidden = YES;
self.userInteractionEnabled = YES;
switch ([[WXYZ_BookDownloadManager sharedManager] getDownloadMissionStateWithProduction_id:self.book_id downloadTaskModel:optionModel]) {
case WXYZ_ProductionDownloadStateDownloading:
chapterLabel.textColor = kGrayTextLightColor;
downloadStateLabel.hidden = NO;
downloadStateLabel.text = TFLocalizedString(@"正在下载");
self.userInteractionEnabled = NO;
break;
case WXYZ_ProductionDownloadStateDownloaded:
chapterLabel.textColor = kGrayTextLightColor;
downloadStateLabel.hidden = NO;
downloadStateLabel.text = TFLocalizedString(@"已下载");
self.userInteractionEnabled = NO;
break;
default:
break;
}
}
- (void)setMissionState:(WXYZ_DownloadMissionState)missionState
{
_missionState = missionState;
[activityIndicator stopAnimating];
switch (missionState) {
case WXYZ_DownloadStateMissionStart:
chapterLabel.textColor = kGrayTextLightColor;
downloadStateLabel.hidden = NO;
downloadStateLabel.text = TFLocalizedString(@"正在下载");
self.userInteractionEnabled = NO;
break;
case WXYZ_DownloadStateMissionFinished:
chapterLabel.textColor = kGrayTextLightColor;
downloadStateLabel.hidden = NO;
downloadStateLabel.text = TFLocalizedString(@"已下载");
self.userInteractionEnabled = NO;
break;
default:
chapterLabel.textColor = kBlackColor;
downloadStateLabel.hidden = YES;
self.userInteractionEnabled = YES;
break;
}
}
@end
@@ -0,0 +1,29 @@
//
// TFNovelDownloadMenuView.h
// TFReader
//
// Created by 谢腾飞 on 2020/12/16.
// Copyright © 2020 xtfei_2011@126.com. All rights reserved.
//
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@class TFProductionModel;
@interface TFNovelDownloadMenuView : UIView
@property (nonatomic ,copy) NSString *book_id;
@property (nonatomic ,copy) NSString *chapter_id;
@property (nonatomic ,copy) void (^menuBarDidHiddenBlock)(void);
- (void)showDownloadPayView;
- (void)hiddenDownloadPayView;
@end
NS_ASSUME_NONNULL_END
@@ -0,0 +1,346 @@
//
// TFNovelDownloadMenuView.m
// TFReader
//
// Created by 谢腾飞 on 2020/12/16.
// Copyright © 2020 xtfei_2011@126.com. All rights reserved.
//
#import "TFNovelDownloadMenuView.h"
#import "WXYZ_BookDownloadManager.h"
#import "TFNovelDownloadTaskListModel.h"
#import "TFNovelDownloadMenuCell.h"
#import "WXYZ_ChapterBottomPayBar.h"
#define DefaultBar_H 6 * Cell_Height + PUB_NAVBAR_OFFSET + 10
#define Cell_Height 40
#define animateDuration 0.4f
@interface TFNovelDownloadMenuView ()<UITableViewDelegate, UITableViewDataSource>
{
UIView *menuView;
}
@property (nonatomic ,strong) TFNovelDownloadTaskListModel *downloadModel;
@property (nonatomic ,strong) WXYZ_BookDownloadManager *bookDownloadManager;
@property (nonatomic ,strong) UIActivityIndicatorView *activityIndicator;
@property (nonatomic ,weak) UITableView *mainTableView;
@end
@implementation TFNovelDownloadMenuView
- (instancetype)init
{
if (self = [super init]) {
[self initialize];
}
return self;
}
- (void)initialize
{
self.backgroundColor = kColorRGBA(0, 0, 0, 0.0);
self.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
self.bookDownloadManager = [WXYZ_BookDownloadManager sharedManager];
}
- (void)showDownloadPayView
{
[self.activityIndicator startAnimating];
[self netRequest];
}
- (void)hiddenDownloadPayView
{
if (self.menuBarDidHiddenBlock) {
self.menuBarDidHiddenBlock();
}
[self removeAllSubviews];
[self removeFromSuperview];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
UIView *touchView = [[touches anyObject] view];
if (![touchView isEqual:menuView] && ![touchView isEqual:self.mainTableView] && ![touchView isKindOfClass:[NSClassFromString(@"UITableViewCellContentView") class]]) {
[self hiddenDownloadPayView];
}
}
- (void)createSubViews
{
self.backgroundColor = kBlackTransparentColor;
menuView = [[UIView alloc] init];
menuView.backgroundColor = [UIColor whiteColor];
[self addSubview:menuView];
[menuView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(0);
make.top.mas_equalTo(self.mas_bottom);
make.width.mas_equalTo(self.mas_width);
make.height.mas_equalTo(DefaultBar_H);
}];
self.activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:(UIActivityIndicatorViewStyleGray)];
self.activityIndicator.hidesWhenStopped = YES;
[menuView addSubview:self.activityIndicator];
//设置小菊花的frame
[self.activityIndicator mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.mas_equalTo(menuView.mas_centerX);
make.centerY.mas_equalTo(menuView.mas_centerY);
make.height.width.mas_equalTo(20);
}];
}
- (void)reloadViewData
{
[UIView animateWithDuration:animateDuration animations:^{
[self->menuView mas_remakeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(0);
make.bottom.mas_equalTo(self.mas_bottom);
make.width.mas_equalTo(self.mas_width);
make.height.mas_equalTo(DefaultBar_H);
}];
}];
UITableView *mainTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
self.mainTableView = mainTableView;
mainTableView.delegate = self;
mainTableView.dataSource = self;
mainTableView.scrollEnabled = NO;
mainTableView.backgroundColor = [UIColor whiteColor];
mainTableView.showsVerticalScrollIndicator = NO;
mainTableView.showsHorizontalScrollIndicator = NO;
mainTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[self addSubview:mainTableView];
[mainTableView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(0);
make.bottom.mas_equalTo(self.mas_bottom);
make.width.mas_equalTo(self.mas_width);
make.height.mas_equalTo(Cell_Height * (1 + self.downloadModel.task_list.count) + Cell_Height + PUB_TABBAR_OFFSET + 10);
}];
if (@available(iOS 11.0, *)) {
mainTableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
} else {
// Fallback on earlier versions
}
[menuView mas_updateConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(Cell_Height * (1 + self.downloadModel.task_list.count) + Cell_Height + PUB_TABBAR_OFFSET + 10);
}];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.downloadModel.task_list.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
TFNovelDownloadMenuCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TFNovelDownloadMenuCell"];
if (!cell) {
cell = [[TFNovelDownloadMenuCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TFNovelDownloadMenuCell"];
}
cell.book_id = [self.book_id integerValue];
cell.optionModel = [self.downloadModel.task_list objectOrNilAtIndex:indexPath.row];
cell.hiddenEndLine = (self.downloadModel.task_list.count - 1 == indexPath.row);
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
self.mainTableView.userInteractionEnabled = NO;
TFNovelDownloadMenuCell *cell = [tableView cellForRowAtIndexPath:indexPath];
[cell startDownloadLoading];
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];
TFProductionModel *t_model = [TFProductionModel modelWithDictionary:dict];
self.downloadModel.productionModel = t_model;
[self downloadChapterWithTaskModel:[self.downloadModel.task_list objectAtIndex:indexPath.row]];
} else {
WS(weakSelf)
[TFNetworkTools POST:Book_Catalog parameters:@{@"book_id":self.book_id} model:TFProductionModel.class success:^(BOOL isSuccess, TFProductionModel * _Nullable t_model, TFNetworkRequestModel * _Nonnull requestModel) {
if (isSuccess) {
weakSelf.downloadModel.productionModel = t_model;
[weakSelf downloadChapterWithTaskModel:[self.downloadModel.task_list objectAtIndex:indexPath.row]];
[weakSelf hiddenDownloadPayView];
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
}];
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return Cell_Height;
}
//section头部间距
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return Cell_Height;
}
//section头部视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UILabel *titleLabel = [[UILabel alloc] init];
titleLabel.backgroundColor = [UIColor whiteColor];
titleLabel.frame = CGRectMake(0, 0, SCREEN_WIDTH, Cell_Height);
titleLabel.text = TFLocalizedString(@"选择需要下载的章节");
titleLabel.textColor = kBlackColor;
titleLabel.font = kMainFont;
titleLabel.textAlignment = NSTextAlignmentCenter;
return titleLabel;
}
//section底部间距
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return Cell_Height + PUB_TABBAR_OFFSET + 10;
}
//section底部视图
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
UIView *view = [[UIView alloc] init];
view.frame = CGRectMake(0, 0, SCREEN_WIDTH, Cell_Height + PUB_TABBAR_OFFSET + 10);
view.backgroundColor = kColorRGBA(235, 235, 241, 1);
UIButton *downloadButton = [UIButton buttonWithType:UIButtonTypeCustom];
downloadButton.frame = CGRectMake(0, 10, SCREEN_WIDTH, Cell_Height + PUB_TABBAR_OFFSET);
downloadButton.backgroundColor = [UIColor whiteColor];
[downloadButton setTitle:TFLocalizedString(@"下载缓存") forState:UIControlStateNormal];
[downloadButton setTitleColor:kBlackColor forState:UIControlStateNormal];
[downloadButton setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, PUB_TABBAR_OFFSET / 2, 0)];
[downloadButton.titleLabel setFont:kMainFont];
[downloadButton addTarget:self action:@selector(downloadClick) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:downloadButton];
return view;
}
- (void)downloadClick
{
[[NSNotificationCenter defaultCenter] postNotificationName:Notification_Push_To_Download object:nil];
[self hiddenDownloadPayView];
}
- (void)netRequest
{
WS(weakSelf)
[TFNetworkTools POST:Book_Chapter_Download_Option parameters:@{@"book_id":self.book_id, @"chapter_id":self.chapter_id} model:nil success:^(BOOL isSuccess, NSDictionary * _Nullable t_model, TFNetworkRequestModel * _Nonnull requestModel) {
if (isSuccess) {
TFNovelDownloadTaskListModel *t_downloadModel = [TFNovelDownloadTaskListModel modelWithDictionary:[t_model objectForKey:@"data"]];
weakSelf.downloadModel = t_downloadModel;
[weakSelf.activityIndicator stopAnimating];
[weakSelf createSubViews];
[weakSelf reloadViewData];
} else if (Compare_Json_isEqualTo(requestModel.code, 703)) {
[TFPromptManager showPromptViewWithStatus:TFPromptStatusError promptTitle:requestModel.msg];
[weakSelf hiddenDownloadPayView];
}
} failure:nil];
}
- (void)downloadChapterWithTaskModel:(TFDownloadTaskModel *)taskModel
{
WS(weakSelf)
taskModel.dateString = [TFUtilsHelper currentDateStringWithFormat:@"yyyy-MM-dd"];
taskModel.download_title = [NSString stringWithFormat:@"%@ - %@%@", [TFUtilsHelper formatStringWithInteger:taskModel.start_order], [TFUtilsHelper formatStringWithInteger:taskModel.end_order], TFLocalizedString(@"")];
[self.bookDownloadManager downloadChaptersWithProductionModel:self.downloadModel.productionModel downloadTaskModel:taskModel production_id:[self.book_id integerValue] start_chapter_id:[taskModel.start_chapter_id integerValue] downloadNum:taskModel.down_num];
self.bookDownloadManager.downloadMissionStateChangeBlock = ^(WXYZ_DownloadMissionState state, NSInteger production_id, TFDownloadTaskModel * _Nonnull downloadTaskModel, NSArray<NSNumber *> * _Nullable chapterIDArray) {
switch (state) {
case WXYZ_DownloadStateMissionStart:
{
for (UIView *view in weakSelf.mainTableView.subviews) {
if ([view isKindOfClass:[TFNovelDownloadMenuCell class]]) {
TFNovelDownloadMenuCell *cell = (TFNovelDownloadMenuCell *)view;
if (cell.optionModel.label == downloadTaskModel.label) {
cell.missionState = state;
[weakSelf hiddenDownloadPayView];
}
}
}
}
break;
case WXYZ_DownloadStateMissionFinished:
{
[TFPromptManager showPromptViewWithStatus:TFPromptStatusSuccess promptTitle:TFLocalizedString(@"下载完成")];
// 更新本地目录
[TFNovelDownloadMenuView updateLocalCatalog:chapterIDArray];
for (UIView *view in weakSelf.mainTableView.subviews) {
if ([view isKindOfClass:[TFNovelDownloadMenuCell class]]) {
TFNovelDownloadMenuCell *cell = (TFNovelDownloadMenuCell *)view;
cell.optionModel = cell.optionModel;
}
}
}
break;
case WXYZ_DownloadStateMissionShouldPay:
{
SS(strongSelf)
[weakSelf hiddenDownloadPayView];
TFProductionChapterModel *t_model = [[TFProductionChapterModel alloc] init];
t_model.production_id = production_id;
t_model.chapter_id = [downloadTaskModel.start_chapter_id integerValue];
WXYZ_ChapterBottomPayBar *payBar = [[WXYZ_ChapterBottomPayBar alloc] initWithChapterModel:t_model barType:WXYZ_BottomPayBarTypeDownload productionType:TFProductionTypeNovel buyChapterNum:[[TFUtilsHelper formatStringWithInteger:taskModel.down_num] integerValue]];
payBar.paySuccessChaptersBlock = ^(NSArray<NSString *> * _Nonnull success_chapter_ids) {
[strongSelf downloadChapterWithTaskModel:taskModel];
};
[payBar showBottomPayBar];
}
break;
default:
break;
}
weakSelf.mainTableView.userInteractionEnabled = YES;
};
}
static NSString *_bookID;
- (void)setBook_id:(NSString *)book_id {
_book_id = book_id;
_bookID = book_id;
}
// 下载成功后请求目录并更新本地目录
+ (void)updateLocalCatalog:(NSArray<NSNumber *> *)catalogArr {
[TFNetworkTools POST:Book_Catalog parameters:@{@"book_id" : _bookID} model:TFProductionModel.class success:^(BOOL isSuccess, TFProductionModel * _Nullable t_model, TFNetworkRequestModel * _Nonnull requestModel) {
if (isSuccess) {
NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
path = [path stringByAppendingPathComponent:[TFUtilsHelper stringToMD5:@"book_catalog"]];
if (![[NSFileManager defaultManager] fileExistsAtPath:path]) {
[[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:@{} error:nil];
}
NSString *catalogName = [NSString stringWithFormat:@"%@_%@", _bookID, @"catalog"];
NSString *fullPath = [path stringByAppendingFormat:@"/%@.plist", [TFUtilsHelper stringToMD5:catalogName]];
[requestModel.data writeToFile:fullPath atomically:YES];
}
} failure:nil];
}
@end