小说绘上架版本

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,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