小说绘上架版本

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,58 @@
//
// WXYZ_AudioSettingHelper.h
// WXReader
//
// Created by Andrew on 2020/3/18.
// Copyright © 2020 Andrew. All rights reserved.
//
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface WXYZ_AudioSettingHelper : NSObject
interface_singleton
// 倒计时
- (NSInteger)getReadTiming;
- (void)setReadTimingWithIndex:(NSInteger)readTimingIndex;
- (NSArray *)getReadTimingKeys;
- (NSArray *)getReadTimingValues;
// 开始倒计时
- (void)startTimingFinishBlock:(void(^)(void))finishBlock;
- (void)getCurrentTimingBlock:(void(^)(NSUInteger currentTime))currentTimeBlock finishBlock:(void(^)(void))finishBlock;
// 声音
- (NSString *)getReadVoiceTitleWithProducitionType:(TFProductionType)productionType;
- (NSInteger)getReadVoiceWithProducitionType:(TFProductionType)productionType;
- (void)setReadVoiceWithIndex:(NSInteger)readVoiceIndex producitionType:(TFProductionType)productionType;
- (NSArray *)getReadVoiceKeysWithProducitionType:(TFProductionType)productionType;
- (NSArray *)getReadVoiceValuesWithProducitionType:(TFProductionType)productionType;
// 语速
- (NSInteger)getReadSpeedWithProducitionType:(TFProductionType)productionType;
- (void)setReadSpeedWithIndex:(NSInteger)readSpeedIndex producitionType:(TFProductionType)productionType;
- (NSArray *)getReadSpeedKeysWithProducitionType:(TFProductionType)productionType;
- (NSArray *)getReadSpeedValuesWithProducitionType:(TFProductionType)productionType;
- (void)playPageViewShow:(BOOL)show productionType:(TFProductionType)productionType;
- (BOOL)isPlayPageShowingWithProductionType:(TFProductionType)productionType;
@end
NS_ASSUME_NONNULL_END
@@ -0,0 +1,206 @@
//
// WXYZ_AudioSettingHelper.m
// WXReader
//
// Created by Andrew on 2020/3/18.
// Copyright © 2020 Andrew. All rights reserved.
//
#import "WXYZ_AudioSettingHelper.h"
#import "WXYZ_Player.h"
#import "WXYZ_GCDTimer.h"
#import <MediaPlayer/MediaPlayer.h>
@interface WXYZ_AudioSettingHelper ()
@property (nonatomic, strong) WXYZ_GCDTimer *countdownTimer;
@property (nonatomic, assign) NSInteger read_timing; // 听书倒计时(有声和ai的计时同步)
@end
@implementation WXYZ_AudioSettingHelper
implementation_singleton(WXYZ_AudioSettingHelper)
// 倒计时
- (NSInteger)getReadTiming
{
return self.read_timing;
}
- (void)setReadTimingWithIndex:(NSInteger)readTimingIndex
{
_read_timing = readTimingIndex;
}
- (NSArray *)getReadTimingKeys
{
return @[TFLocalizedString(@"不开启"), TFLocalizedString(@"听完本章节"), TFLocalizedString(@"15分钟"), TFLocalizedString(@"30分钟"), TFLocalizedString(@"60分钟"), TFLocalizedString(@"90分钟")];
}
- (NSArray *)getReadTimingValues
{
return @[@"0", @"1", @"900", @"1800", @"3600", @"5400"];
}
// 声音
- (NSString *)getReadVoiceTitleWithProducitionType:(TFProductionType)productionType
{
return [[self getReadVoiceKeysWithProducitionType:productionType] objectAtIndex:[self getReadVoiceWithProducitionType:productionType]];
}
- (NSInteger)getReadVoiceWithProducitionType:(TFProductionType)productionType
{
if (productionType == TFProductionTypeAudio) {
return 0;
} else {
NSString *string = [[NSUserDefaults standardUserDefaults] objectForKey:@"xtfei_ai_read_voice"];
return string?[string integerValue] : 0;
}
}
- (void)setReadVoiceWithIndex:(NSInteger)readVoiceIndex producitionType:(TFProductionType)productionType
{
if (productionType == TFProductionTypeAudio) {
} else {
[[NSUserDefaults standardUserDefaults] setObject:[TFUtilsHelper formatStringWithInteger:readVoiceIndex] forKey:@"xtfei_ai_read_voice"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
}
- (NSArray *)getReadVoiceKeysWithProducitionType:(TFProductionType)productionType
{
if (productionType == TFProductionTypeAudio) {
return @[];
} else {
return @[TFLocalizedString(@"情感女声"), TFLocalizedString(@"标准女声"), TFLocalizedString(@"标准男声")];
}
}
- (NSArray *)getReadVoiceValuesWithProducitionType:(TFProductionType)productionType
{
if (productionType == TFProductionTypeAudio) {
return @[];
} else {
return @[@"xiaoyan", @"vixq", @"aisbabyxu"];
}
}
// 语速
- (NSInteger)getReadSpeedWithProducitionType:(TFProductionType)productionType
{
if (productionType == TFProductionTypeAudio) {
NSString *string = [[NSUserDefaults standardUserDefaults] objectForKey:@"xtfei_audio_read_speed"];
return string?[string integerValue]:2;
} else {
NSString *string = [[NSUserDefaults standardUserDefaults] objectForKey:@"xtfei_ai_read_speed"];
return string?[string integerValue]:2;
}
}
- (void)setReadSpeedWithIndex:(NSInteger)readSpeedIndex producitionType:(TFProductionType)productionType
{
if (productionType == TFProductionTypeAudio) {
[[NSUserDefaults standardUserDefaults] setObject:[TFUtilsHelper formatStringWithInteger:readSpeedIndex] forKey:@"xtfei_audio_read_speed"];
[[NSUserDefaults standardUserDefaults] synchronize];
} else {
[[NSUserDefaults standardUserDefaults] setObject:[TFUtilsHelper formatStringWithInteger:readSpeedIndex] forKey:@"xtfei_ai_read_speed"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
}
- (NSArray *)getReadSpeedKeysWithProducitionType:(TFProductionType)productionType
{
if (productionType == TFProductionTypeAudio) {
return @[TFLocalizedString(@"0.5倍"), TFLocalizedString(@"0.75倍"), TFLocalizedString(@"1倍"), TFLocalizedString(@"1.25倍"), TFLocalizedString(@"1.5倍")];
} else {
return @[TFLocalizedString(@"0.5倍"), TFLocalizedString(@"0.75倍"), TFLocalizedString(@"1倍"), TFLocalizedString(@"1.25倍"), TFLocalizedString(@"1.5倍")];
}
}
- (NSArray *)getReadSpeedValuesWithProducitionType:(TFProductionType)productionType
{
if (productionType == TFProductionTypeAudio) {
return @[@"0.5", @"0.80", @"1.0", @"1.25", @"1.5"];
} else {
return @[@"0.5", @"0.80", @"1.0", @"1.25", @"1.5"];
}
}
- (void)startTimingFinishBlock:(void(^)(void))finishBlock
{
CGFloat duration = [[[self getReadTimingValues] objectAtIndex:self.read_timing] floatValue];
if (self.read_timing == 0 || self.read_timing == 1) {
[self.countdownTimer stopTimer];
if (finishBlock) {
finishBlock();
}
return;
}
[self.countdownTimer stopTimer];
[self.countdownTimer startTimerWithTimeDuration:duration];
}
- (void)getCurrentTimingBlock:(void(^)(NSUInteger currentTime))currentTimeBlock finishBlock:(void(^)(void))finishBlock
{
// if ((self.read_timing == 0 || self.read_timing == 1) && ) {
// if (finishBlock) {
// finishBlock();
// }
// }
self.countdownTimer.timerRunningBlock = ^(NSUInteger runTimes, CGFloat currentTime) {
if (currentTimeBlock) {
currentTimeBlock([[NSNumber numberWithFloat:currentTime] integerValue]);
}
};
WS(weakSelf)
self.countdownTimer.timerFinishedBlock = ^{
weakSelf.read_timing = 0;
if (finishBlock) {
finishBlock();
}
};
}
- (WXYZ_GCDTimer *)countdownTimer
{
if (!_countdownTimer) {
_countdownTimer = [[WXYZ_GCDTimer alloc] initCountdownTimerWithTimeDuration:10 immediatelyCallBack:YES];
}
return _countdownTimer;
}
static BOOL aiPlayPageShow = NO;
static BOOL audioPlayPageShow = NO;
- (void)playPageViewShow:(BOOL)show productionType:(TFProductionType)productionType
{
if (productionType == TFProductionTypeAi) {
aiPlayPageShow = show;
}
if (productionType == TFProductionTypeAudio) {
audioPlayPageShow = show;
}
}
- (BOOL)isPlayPageShowingWithProductionType:(TFProductionType)productionType
{
if (productionType == TFProductionTypeAi) {
return aiPlayPageShow;
}
if (productionType == TFProductionTypeAudio) {
return audioPlayPageShow;
}
return NO;
}
@end
@@ -0,0 +1,25 @@
//
// WXYZ_AudioDownloadTableViewCell.h
// WXReader
//
// Created by Andrew on 2020/3/20.
// Copyright © 2020 Andrew. All rights reserved.
//
#import "TFBasicTableViewCell.h"
#import "WXYZ_DownloadManagerEnumProtocol.h"
NS_ASSUME_NONNULL_BEGIN
@interface WXYZ_AudioDownloadTableViewCell : TFBasicTableViewCell
@property (nonatomic, strong) TFProductionChapterModel *chapterModel;
// 是否是缓存状态
@property (nonatomic, assign) BOOL isCacheState;
@property (nonatomic, assign) WXYZ_ProductionDownloadState cellDownloadState;
@end
NS_ASSUME_NONNULL_END
@@ -0,0 +1,146 @@
//
// WXYZ_AudioDownloadTableViewCell.m
// WXReader
//
// Created by Andrew on 2020/3/20.
// Copyright © 2020 Andrew. All rights reserved.
//
#import "WXYZ_AudioDownloadTableViewCell.h"
#import "WXYZ_AudioDownloadManager.h"
@implementation WXYZ_AudioDownloadTableViewCell
{
UIImageView *selectImageView;
UILabel *audioChapterLabel;
UIImageView *fileIcon;
UILabel *fileLabel;
}
- (void)createSubviews
{
[super createSubviews];
selectImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"audio_download_unselect"]];
[self.contentView addSubview:selectImageView];
[selectImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(self.contentView.mas_left).with.offset(kMargin);
make.centerY.mas_equalTo(self.contentView.mas_centerY);
make.width.height.mas_equalTo(20);
}];
audioChapterLabel = [[UILabel alloc] init];
audioChapterLabel.textAlignment = NSTextAlignmentLeft;
audioChapterLabel.textColor = kBlackColor;
audioChapterLabel.font = kMainFont;
audioChapterLabel.numberOfLines = 2;
[self.contentView addSubview:audioChapterLabel];
[audioChapterLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(selectImageView.mas_right).with.offset(kMargin);
make.top.mas_equalTo(self.contentView.mas_top).with.offset(kHalfMargin);
make.right.mas_equalTo(self.contentView.mas_right).with.offset(- kMargin);
make.height.mas_equalTo(15);
}];
fileIcon = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"audio_file_icon"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]];
fileIcon.userInteractionEnabled = YES;
fileIcon.tintColor = kGrayTextColor;
[self.contentView addSubview:fileIcon];
[fileIcon mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(audioChapterLabel.mas_left);
make.top.mas_equalTo(audioChapterLabel.mas_bottom).with.offset(kQuarterMargin);
make.width.mas_equalTo(15);
make.height.mas_equalTo(15);
}];
fileLabel = [[UILabel alloc] init];
fileLabel.textColor = kGrayTextColor;
fileLabel.textAlignment = NSTextAlignmentLeft;
fileLabel.font = kFont10;
[self.contentView addSubview:fileLabel];
[fileLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(fileIcon.mas_right).with.offset(kQuarterMargin);
make.centerY.mas_equalTo(fileIcon.mas_centerY);
make.right.mas_equalTo(self.contentView.mas_right).with.offset(-kMargin);
make.height.mas_equalTo(fileIcon.mas_height);
make.bottom.mas_equalTo(self.contentView.mas_bottom).with.offset(-kHalfMargin).priorityLow();
}];
}
- (void)setChapterModel:(TFProductionChapterModel *)chapterModel
{
_chapterModel = chapterModel;
fileLabel.text = [TFUtilsHelper convertFileSize:chapterModel.size];
NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:chapterModel.chapter_title ? : @""];
if (chapterModel.is_preview == 1 && [[WXYZ_AudioDownloadManager sharedManager] getChapterDownloadStateWithProduction_id:chapterModel.production_id chapter_id:chapterModel.chapter_id] != WXYZ_ProductionDownloadStateDownloaded) {
NSTextAttachment *attch = [[NSTextAttachment alloc] init];
attch.image = [UIImage imageNamed:TFLocalizedString(@"audio_directory_pay")];
attch.bounds = CGRectMake(0, 0, kGeometricWidth(10, 246, 90), 10);
NSAttributedString *string = [NSAttributedString attributedStringWithAttachment:attch];
[attri insertAttributedString:string atIndex:0];
}
audioChapterLabel.attributedText = attri;
}
- (void)setCellDownloadState:(WXYZ_ProductionDownloadState)cellDownloadState
{
if (_cellDownloadState != cellDownloadState) {
_cellDownloadState = cellDownloadState;
switch (cellDownloadState) {
case WXYZ_ProductionDownloadStateNormal:
{
selectImageView.image = [UIImage imageNamed:@"audio_download_unselect"];
audioChapterLabel.textColor = kBlackColor;
self.backgroundColor = kWhiteColor;
}
break;
case WXYZ_ProductionDownloadStateDownloading:
{
selectImageView.image = [UIImage imageNamed:@"audio_downloading_list"];
audioChapterLabel.textColor = kBlackColor;
self.backgroundColor = kWhiteColor;
}
break;
case WXYZ_ProductionDownloadStateDownloaded:
{
if (self.isCacheState) {
selectImageView.image = [UIImage imageNamed:@"audio_download_unselect"];
audioChapterLabel.textColor = kBlackColor;
self.backgroundColor = kWhiteColor;
} else {
selectImageView.image = [UIImage imageNamed:@"audio_download_unenable"];
audioChapterLabel.textColor = kGrayTextColor;
self.backgroundColor = kGrayViewColor;
}
}
break;
case WXYZ_ProductionDownloadStateSelected:
{
selectImageView.image = [UIImage imageNamed:@"audio_download_select"];
audioChapterLabel.textColor = kBlackColor;
self.backgroundColor = kWhiteColor;
}
break;
case WXYZ_ProductionDownloadStateFail:
selectImageView.image = [UIImage imageNamed:@"audio_download_fail_list"];
audioChapterLabel.textColor = kBlackColor;
self.backgroundColor = kWhiteColor;
break;
default:
break;
}
}
}
@end
@@ -0,0 +1,20 @@
//
// WXYZ_AudioDownloadViewController.h
// WXReader
//
// Created by Andrew on 2020/3/20.
// Copyright © 2020 Andrew. All rights reserved.
//
#import "TFDownloadViewController.h"
NS_ASSUME_NONNULL_BEGIN
@interface WXYZ_AudioDownloadViewController : TFDownloadViewController
// 预防作品model为空的情况
@property (nonatomic, assign) NSInteger production_id;
@end
NS_ASSUME_NONNULL_END
@@ -0,0 +1,517 @@
//
// WXYZ_AudioDownloadViewController.m
// WXReader
//
// Created by Andrew on 2020/3/20.
// Copyright © 2020 Andrew. All rights reserved.
//
#import "WXYZ_AudioDownloadViewController.h"
#import "WXYZ_AudioDownloadTableViewCell.h"
#import "WXYZ_AudioPlayPageMenuView.h"
#import "WXYZ_AudioDownloadManager.h"
#import "TFReadRecordManager.h"
#import "WXYZ_ChapterBottomPayBar.h"
@interface WXYZ_AudioDownloadViewController () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) TFProductionModel *audioModel;
@property (nonatomic, assign) CGFloat totalMemorySize;
@property (nonatomic, assign) BOOL payBarShowing;
@property (nonatomic, strong) UIButton *downloadButton;
@property (nonatomic, strong) UIActivityIndicatorView *downloadIndicatorView;
@property (nonatomic, strong) WXYZ_AudioDownloadManager *audioDownloadManager;
@property (nonatomic, weak) UILabel *headerTitleLabel;
@end
@implementation WXYZ_AudioDownloadViewController
{
UILabel *memoryBarView;
TFButton *selectAllButton;
}
- (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.totalMemorySize = 0;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(netRequest) name:Notification_Login_Success object:nil];
}
- (void)createSubviews
{
self.mainTableView.delegate = self;
self.mainTableView.dataSource = self;
[self.view addSubview:self.mainTableView];
[self.mainTableView 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);
}];
memoryBarView = [[UILabel alloc] init];
memoryBarView.numberOfLines = 2;
memoryBarView.backgroundColor = kColorRGB(251, 251, 251);
memoryBarView.font = kFont10;
memoryBarView.textAlignment = NSTextAlignmentCenter;
memoryBarView.textColor = kGrayTextColor;
memoryBarView.text =[NSString stringWithFormat:@"%@%@", TFLocalizedString(@"可用空间"), [TFUtilsHelper getRemainingMemorySpace]];
[self.view addSubview:memoryBarView];
[memoryBarView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(0);
make.top.mas_equalTo(self.mainTableView.mas_bottom);
make.width.mas_equalTo(SCREEN_WIDTH);
make.height.mas_equalTo(20);
}];
selectAllButton = [[TFButton alloc] initWithFrame:CGRectZero buttonTitle:TFLocalizedString(@"全选") buttonImageName:@"audio_download_unselect" buttonIndicator:TFButtonIndicatorImageLeftBothLeft];
selectAllButton.buttonTitleColor = kBlackColor;
selectAllButton.buttonTitleFont = kMainFont;
selectAllButton.graphicDistance = 10;
selectAllButton.buttonImageScale = 0.4;
selectAllButton.hidden = YES;
selectAllButton.tag = 0;
selectAllButton.backgroundColor = [UIColor whiteColor];
[selectAllButton addTarget:self action:@selector(checkallClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:selectAllButton];
[selectAllButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(kMargin);
make.top.equalTo(memoryBarView.mas_bottom);
make.height.mas_equalTo(PUB_TABBAR_HEIGHT - PUB_TABBAR_OFFSET);
}];
self.downloadButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.downloadButton.backgroundColor = kGrayTextLightColor;
self.downloadButton.enabled = NO;
[self.downloadButton setTitle:TFLocalizedString(@"立即下载") forState:UIControlStateNormal];
[self.downloadButton setTitleColor:kWhiteColor forState:UIControlStateNormal];
[self.downloadButton.titleLabel setFont:kMainFont];
[self.downloadButton addTarget:self action:@selector(downloadClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.downloadButton];
[self.downloadButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.mas_equalTo(self.view.mas_right);
make.top.mas_equalTo(selectAllButton.mas_top);
make.width.mas_equalTo(SCREEN_WIDTH / 3);
make.height.mas_equalTo(selectAllButton.mas_height);
}];
[selectAllButton mas_updateConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(self.downloadButton.mas_left).offset(-kHalfMargin);
}];
self.downloadIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:(UIActivityIndicatorViewStyleGray)];
self.downloadIndicatorView.hidesWhenStopped = YES;
[self.downloadButton 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)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.dataSourceArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
TFProductionChapterModel *chapterModel = [self.dataSourceArray objectOrNilAtIndex:indexPath.row];
static NSString *cellName = @"WXYZ_AudioDownloadTableViewCell";
WXYZ_AudioDownloadTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellName];
if (!cell) {
cell = [[WXYZ_AudioDownloadTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellName];
}
cell.chapterModel = chapterModel;
cell.cellDownloadState = [[self.selectSourceDictionary objectForKey:[TFUtilsHelper formatStringWithInteger:chapterModel.chapter_id]] integerValue];
cell.hiddenEndLine = self.dataSourceArray.count - 1 == indexPath.row;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
//section头间距
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 30;
}
//section头视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 30)];
view.backgroundColor = [UIColor clearColor];
UIView *mainView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 30)];
mainView.backgroundColor = kColorRGB(251, 251, 251);
[view addSubview:mainView];
UILabel *headerTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(kMargin, 0, SCREEN_WIDTH / 2, 30)];
self.headerTitleLabel = headerTitleLabel;
headerTitleLabel.backgroundColor = [UIColor clearColor];
headerTitleLabel.font = kFont12;
// headerTitleLabel.text = [NSString stringWithFormat:@"%@ %zd %@", TFLocalizedString(@"已下载"), [[WXYZ_AudioDownloadManager sharedManager] getDownloadChapterCountWithProduction_id:self.audioModel.production_id], TFLocalizedString(@"章")];
headerTitleLabel.text = [NSString stringWithFormat:@"%@ %zd %@", TFLocalizedString(@""), self.dataSourceArray.count, TFLocalizedString(@"")];
headerTitleLabel.textColor = kGrayTextColor;
[mainView addSubview:headerTitleLabel];
CGFloat width = [TFViewHelper getDynamicWidthWithLabelFont:kFont12 labelHeight:30.0 labelText:TFLocalizedString(@"选章标题") maxWidth:240.0];
width += kMargin;
TFButton *selectionButton = [[TFButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - width - kHalfMargin, 0, width, 30) buttonTitle:TFLocalizedString(@"选章标题") buttonImageName:@"audio_selection" buttonIndicator:TFButtonIndicatorTitleRight];
selectionButton.tag = 0;
selectionButton.buttonTintColor = kGrayTextColor;
selectionButton.buttonTitleFont = kFont12;
selectionButton.graphicDistance = 2;
selectionButton.buttonImageScale = 0.4;
[selectionButton addTarget:self action:@selector(selectionButtonClick) forControlEvents:UIControlEventTouchUpInside];
[mainView addSubview:selectionButton];
return view;
}
//section底部间距
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return CGFLOAT_MIN;
}
//section底部视图
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, CGFLOAT_MIN)];
view.backgroundColor = [UIColor clearColor];
return view;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
TFProductionChapterModel *chapterModel = [self.dataSourceArray objectOrNilAtIndex:indexPath.row];
WXYZ_AudioDownloadTableViewCell *cell = [tableView cellForRowAtIndexPath: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;
}
}
- (void)checkallClick:(TFButton *)sender
{
self.totalMemorySize = 0;
for (TFProductionChapterModel *chapterModel in self.dataSourceArray) {
if ([[WXYZ_AudioDownloadManager sharedManager] getChapterDownloadStateWithProduction_id:chapterModel.production_id chapter_id:chapterModel.chapter_id] == WXYZ_ProductionDownloadStateNormal) {
self.totalMemorySize = self.totalMemorySize + chapterModel.size;
}
}
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)selectionButtonClick
{
WS(weakSelf)
WXYZ_AudioPlayPageMenuView *chooseMenuView = [[WXYZ_AudioPlayPageMenuView alloc] init];
chooseMenuView.menuListArray = self.dataSourceArray;
chooseMenuView.totalChapter = self.audioModel.total_chapters;
chooseMenuView.chooseMenuBlock = ^(WXYZ_MenuType menuType, NSInteger chooseIndex) {
/* 滚动指定段的指定row 到 指定位置*/
[weakSelf.mainTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:chooseIndex * 30 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
};
[chooseMenuView showWithMenuType:WXYZ_MenuTypeAudioSelection];
}
- (void)initDownloadManager
{
WS(weakSelf)
self.audioDownloadManager = [WXYZ_AudioDownloadManager sharedManager];
self.audioDownloadManager.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.audioDownloadManager.downloadMissionStateChangeBlock = ^(WXYZ_DownloadMissionState state, NSInteger production_id, NSArray * _Nonnull chapter_ids) {
SS(strongSelf)
switch (state) {
// 任务开始
case WXYZ_DownloadStateMissionStart:
{
[weakSelf.downloadIndicatorView stopAnimating];
[weakSelf.downloadButton setTitle:TFLocalizedString(@"立即下载") forState:UIControlStateNormal];
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 setTitle:TFLocalizedString(@"立即下载") forState:UIControlStateNormal];
for (NSString *t_chapter_id in chapter_ids) {
[weakSelf setCollectionCellDownloadStateWithChapter_id:[t_chapter_id integerValue] downloadState:WXYZ_ProductionDownloadStateNormal];
}
}
break;
case WXYZ_DownloadStateMissionShouldPay:
{
[weakSelf.downloadIndicatorView stopAnimating];
[weakSelf.downloadButton setTitle:TFLocalizedString(@"立即下载") forState:UIControlStateNormal];
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:TFProductionTypeAudio];
payBar.paySuccessChaptersBlock = ^(NSArray<NSString *> * _Nonnull success_chapter_ids) {
[strongSelf 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(@"下载完成")];
weakSelf.headerTitleLabel.text = [NSString stringWithFormat:TFLocalizedString(@"已下载%zd章"), [[WXYZ_AudioDownloadManager sharedManager] getDownloadChapterCountWithProduction_id:weakSelf.audioModel.production_id]];
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 setTitle:@"" forState:UIControlStateNormal];
[self reloadToolBar];
[self.audioDownloadManager downloadChaptersWithProductionModel:self.audioModel production_id:self.audioModel.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;
}
}
if (selectIndex > 0) {
memoryBarView.text = [NSString stringWithFormat:TFLocalizedString(@"已选择%@条声音,占用%@/可用空间%@"), [TFUtilsHelper formatStringWithInteger:selectIndex], [TFUtilsHelper convertFileSize:self.totalMemorySize * 1024], [TFUtilsHelper getRemainingMemorySpace]];
[memoryBarView mas_updateConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(memoryBarView.intrinsicContentSize.height);
}];
} else {
memoryBarView.text = [NSString stringWithFormat:@"%@%@", TFLocalizedString(@"可用空间"), [TFUtilsHelper getRemainingMemorySpace]];
[memoryBarView mas_updateConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(memoryBarView.intrinsicContentSize.height);
}];
}
if (downloadedIndex == self.dataSourceArray.count) {
selectAllButton.hidden = YES;
self.downloadButton.enabled = NO;
self.downloadButton.selected = NO;
self.downloadButton.backgroundColor = [UIColor whiteColor];
[self.downloadButton setTitle:TFLocalizedString(@"已全部下载") forState:UIControlStateNormal];
[self.downloadButton setTitleColor:kGrayTextLightColor forState:UIControlStateNormal];
[self.downloadButton mas_remakeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self.view);
make.top.mas_equalTo(selectAllButton.mas_top);
make.width.mas_equalTo(SCREEN_WIDTH);
make.height.mas_equalTo(selectAllButton.mas_height);
}];
return;
}
self.downloadButton.enabled = NO;
self.downloadButton.backgroundColor = kGrayTextLightColor;
[self.downloadButton mas_updateConstraints:^(MASConstraintMaker *make) {
make.width.mas_equalTo(SCREEN_WIDTH / 3);
}];
selectAllButton.selected = NO;
selectAllButton.enabled = YES;
selectAllButton.hidden = NO;
selectAllButton.buttonTitle = TFLocalizedString(@"全选");
selectAllButton.buttonImageName = @"audio_download_unselect";
if (selectIndex > 0 && normalIndex == 0) { // 有选中的文件并且没有未选中的文件
self.downloadButton.enabled = YES;
self.downloadButton.backgroundColor = kMainColor;
selectAllButton.selected = YES;
selectAllButton.buttonTitle = TFLocalizedString(@"取消全选");
selectAllButton.buttonImageName = @"audio_download_select";
} else if (normalIndex > 0 && selectIndex > 0) { // 有未选中的文件
self.downloadButton.enabled = YES;
self.downloadButton.backgroundColor = kMainColor;
}
}
- (void)setCollectionCellDownloadStateWithChapter_id:(NSInteger)chapter_id downloadState:(WXYZ_ProductionDownloadState)downloadState
{
WXYZ_AudioDownloadTableViewCell *cell = (WXYZ_AudioDownloadTableViewCell *)[self.mainTableView cellForRowAtIndexPath:(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 POST:Audio_Catalog parameters:@{@"audio_id":[TFUtilsHelper formatStringWithInteger:self.production_id]} model:TFProductionModel.class success:^(BOOL isSuccess, TFProductionModel * _Nullable t_model, TFNetworkRequestModel * _Nonnull requestModel) {
if (isSuccess) {
if (t_model.chapter_list.count > 0) {
weakSelf.audioModel = t_model;
weakSelf.dataSourceArray = [NSMutableArray arrayWithArray:t_model.chapter_list];
[weakSelf resetSelectSourceDicWithDataSourceArray:t_model.chapter_list productionType:TFProductionTypeAudio];
}
} else {
[TFPromptManager showPromptViewWithStatus:TFPromptStatusError promptTitle:requestModel.msg];
}
[weakSelf.mainTableView reloadData];
[weakSelf reloadToolBar];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
[weakSelf.mainTableView reloadData];
}];
}
@end
@@ -0,0 +1,19 @@
//
// WXYZ_AudioPlayPageMenuDirectoryTableViewCell.h
// WXReader
//
// Created by Andrew on 2020/3/19.
// Copyright © 2020 Andrew. All rights reserved.
//
#import "TFBasicTableViewCell.h"
NS_ASSUME_NONNULL_BEGIN
@interface WXYZ_AudioPlayPageMenuDirectoryTableViewCell : TFBasicTableViewCell
@property (nonatomic, strong) TFProductionChapterModel *chapterListModel;
@end
NS_ASSUME_NONNULL_END
@@ -0,0 +1,136 @@
//
// WXYZ_AudioPlayPageMenuDirectoryTableViewCell.m
// WXReader
//
// Created by Andrew on 2020/3/19.
// Copyright © 2020 Andrew. All rights reserved.
//
#import "WXYZ_AudioPlayPageMenuDirectoryTableViewCell.h"
#import "TFReadRecordManager.h"
@implementation WXYZ_AudioPlayPageMenuDirectoryTableViewCell
{
UIImageView *leftIcon;
UILabel *chapterNameLabel;
#if TF_Super_Member_Mode && TF_Recharge_Mode
UIImageView *lockIcon;
#endif
}
- (void)createSubviews
{
[super createSubviews];
leftIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"audio_directory_list"]];
leftIcon.userInteractionEnabled = YES;
leftIcon.hidden = YES;
[self.contentView addSubview:leftIcon];
[leftIcon mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(self.contentView.mas_left).with.offset(kMargin);
make.centerY.mas_equalTo(self.contentView.mas_centerY);
make.width.mas_equalTo(CGFLOAT_MIN);
make.height.mas_equalTo(kGeometricHeight(kLabelHeight / 2, 50, 32));
}];
#if TF_Super_Member_Mode && TF_Recharge_Mode
lockIcon = [[UIImageView alloc] init];
lockIcon.image = [[UIImage imageNamed:@"book_directory_lock"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
lockIcon.tintColor = kColorRGB(139, 140, 146);
lockIcon.hidden = YES;
[self.contentView addSubview:lockIcon];
[lockIcon 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(15);
}];
#endif
// 章节名称
chapterNameLabel = [[UILabel alloc] init];
chapterNameLabel.textColor = kBlackColor;
chapterNameLabel.font = kMainFont;
chapterNameLabel.textAlignment = NSTextAlignmentLeft;
[self.contentView addSubview:chapterNameLabel];
[chapterNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(leftIcon.mas_right).with.offset(kHalfMargin);
make.top.mas_equalTo(self.contentView.mas_top);
#if TF_Super_Member_Mode && TF_Recharge_Mode
make.right.mas_equalTo(lockIcon.mas_left).with.offset(- 5);
#else
make.right.mas_equalTo(self.contentView.mas_right).with.offset(- kMargin);
#endif
make.height.mas_equalTo(self.contentView.mas_height);
}];
}
- (void)setChapterListModel:(TFProductionChapterModel *)chapterListModel
{
_chapterListModel = chapterListModel;
chapterNameLabel.text = chapterListModel.chapter_title?:@"";
#if TF_Super_Member_Mode && TF_Recharge_Mode
if (chapterListModel.is_preview == 1) {
lockIcon.hidden = NO;
} else {
lockIcon.hidden = YES;
}
#endif
if (chapterListModel.chapter_id == [[TFReadRecordManager shareManagerWithProductionType:self.productionType] getReadingRecordChapter_idWithProduction_id:chapterListModel.production_id]) {
chapterNameLabel.textColor = kMainColor;
leftIcon.hidden = NO;
[leftIcon mas_updateConstraints:^(MASConstraintMaker *make) {
make.width.mas_equalTo(kLabelHeight / 2);
}];
[chapterNameLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(leftIcon.mas_right).with.offset(kHalfMargin);
make.top.mas_equalTo(self.contentView.mas_top);
#if TF_Super_Member_Mode && TF_Recharge_Mode
make.right.mas_equalTo(lockIcon.mas_left).with.offset(- 5);
#else
make.right.mas_equalTo(self.contentView.mas_right).with.offset(- kMargin);
#endif
make.height.mas_equalTo(self.contentView.mas_height);
}];
} else if ([[TFReadRecordManager shareManagerWithProductionType:self.productionType] chapterHasReadedWithProduction_id:chapterListModel.production_id chapter_id:chapterListModel.chapter_id]) {
chapterNameLabel.textColor = kGrayTextColor;
leftIcon.hidden = YES;
[leftIcon mas_updateConstraints:^(MASConstraintMaker *make) {
make.width.mas_equalTo(CGFLOAT_MIN);
}];
[chapterNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(self.mas_left).with.offset(kMargin);
make.top.mas_equalTo(self.contentView.mas_top);
#if TF_Super_Member_Mode && TF_Recharge_Mode
make.right.mas_equalTo(lockIcon.mas_left).with.offset(- 5);
#else
make.right.mas_equalTo(self.contentView.mas_right).with.offset(- kMargin);
#endif
make.height.mas_equalTo(self.contentView.mas_height);
}];
} else {
chapterNameLabel.textColor = kBlackColor;
leftIcon.hidden = YES;
[leftIcon mas_updateConstraints:^(MASConstraintMaker *make) {
make.width.mas_equalTo(CGFLOAT_MIN);
}];
[chapterNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(self.mas_left).with.offset(kMargin);
make.top.mas_equalTo(self.contentView.mas_top);
#if TF_Super_Member_Mode && TF_Recharge_Mode
make.right.mas_equalTo(lockIcon.mas_left).with.offset(- 5);
#else
make.right.mas_equalTo(self.contentView.mas_right).with.offset(- kMargin);
#endif
make.height.mas_equalTo(self.contentView.mas_height);
}];
}
}
@end
@@ -0,0 +1,21 @@
//
// WXYZ_AudioPlayPageMenuTableViewCell.h
// WXReader
//
// Created by Andrew on 2020/3/18.
// Copyright © 2020 Andrew. All rights reserved.
//
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface WXYZ_AudioPlayPageMenuTableViewCell : TFBasicTableViewCell
@property (nonatomic, copy) NSString *cellTitleString;
@property (nonatomic, assign) BOOL cellSelected;
@end
NS_ASSUME_NONNULL_END
@@ -0,0 +1,59 @@
//
// WXYZ_AudioPlayPageMenuTableViewCell.m
// WXReader
//
// Created by Andrew on 2020/3/18.
// Copyright © 2020 Andrew. All rights reserved.
//
#import "WXYZ_AudioPlayPageMenuTableViewCell.h"
@implementation WXYZ_AudioPlayPageMenuTableViewCell
{
UILabel *titleLabel;
UIImageView *chooseIndicator;
}
- (void)createSubviews
{
[super createSubviews];
chooseIndicator = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"audio_menu_choose"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]];
chooseIndicator.userInteractionEnabled = YES;
chooseIndicator.hidden = YES;
chooseIndicator.tintColor = kMainColor;
[self.contentView addSubview:chooseIndicator];
[chooseIndicator mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.mas_equalTo(self.contentView.mas_right).with.offset(- kMargin);
make.centerY.mas_equalTo(self.contentView.mas_centerY);
make.width.height.mas_equalTo(20);
}];
titleLabel = [[UILabel alloc] init];
titleLabel.textColor = kBlackColor;
titleLabel.textAlignment = NSTextAlignmentLeft;
titleLabel.font = kMainFont;
[self.contentView addSubview:titleLabel];
[titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(self.contentView.mas_left).with.offset(kMargin);
make.top.mas_equalTo(self.contentView.mas_top);
make.right.mas_equalTo(chooseIndicator.mas_left).with.offset(- kHalfMargin);
make.bottom.mas_equalTo(self.contentView.mas_bottom);
}];
}
- (void)setCellTitleString:(NSString *)cellTitleString
{
_cellTitleString = cellTitleString;
titleLabel.text = cellTitleString;
}
- (void)setCellSelected:(BOOL)cellSelected
{
_cellSelected = cellSelected;
chooseIndicator.hidden = !cellSelected;
}
@end
@@ -0,0 +1,44 @@
//
// WXYZ_AudioPlayPageMenuView.h
// WXReader
//
// Created by Andrew on 2020/3/18.
// Copyright © 2020 Andrew. All rights reserved.
//
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
typedef NS_ENUM(NSUInteger, WXYZ_MenuType) {
WXYZ_MenuTypeAudioSpeed, // 有声语速
WXYZ_MenuTypeAudioDirectory, // 有声目录
WXYZ_MenuTypeAudioSelection, // 有声选章
WXYZ_MenuTypeAiSpeed, // ai语速
WXYZ_MenuTypeAiVoice, // ai声音
WXYZ_MenuTypeAiDirectory, // ai目录
WXYZ_MenuTypeTiming // 定时
};
@interface WXYZ_AudioPlayPageMenuView : UIView <UIGestureRecognizerDelegate, UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) NSArray *menuListArray;
/// 总章节数量
@property (nonatomic, assign) NSInteger totalChapter;
@property (nonatomic, copy) void (^chooseMenuBlock)(WXYZ_MenuType menuType, NSInteger chooseIndex);
@property (nonatomic, copy) void (^chooseDirectoryMenuBlock)(NSInteger chapter_id, NSInteger chooseIndex);
@property (nonatomic, copy) void (^timingCompleteBlock)(BOOL finish);
- (void)showWithMenuType:(WXYZ_MenuType)menuType;
- (void)close;
@end
NS_ASSUME_NONNULL_END
@@ -0,0 +1,564 @@
//
// WXYZ_AudioPlayPageMenuView.m
// WXReader
//
// Created by Andrew on 2020/3/18.
// Copyright © 2020 Andrew. All rights reserved.
//
#import "WXYZ_AudioPlayPageMenuView.h"
#import "WXYZ_AudioPlayPageMenuTableViewCell.h"
#import "WXYZ_AudioPlayPageMenuDirectoryTableViewCell.h"
#import "TFButton.h"
#import "WXYZ_AudioSettingHelper.h"
#import "TFReadRecordManager.h"
#define Menu_Cell_Height 44
#define Menu_Header_Height 40
#define Menu_Footer_Height (40 + PUB_TABBAR_OFFSET)
@interface WXYZ_AudioPlayPageMenuView ()
/// 是倒序
@property (nonatomic, assign) BOOL isInvert;
@end
@implementation WXYZ_AudioPlayPageMenuView
{
WXYZ_MenuType _menuType;
UITapGestureRecognizer *tap;
UIView *backView;
UITableView *mainTableView;
UIButton *closeButton;
UILabel *headerTitleLabel;
UIView *sectionHeaderView;
UILabel *sectionHeaderTitle;
}
- (instancetype)init
{
if (self = [super init]) {
[self initialize];
[self createSubviews];
}
return self;
}
- (void)initialize
{
self.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
self.backgroundColor = kBlackTransparentColor;
self.tag = 111;
[kMainWindow addSubview:self];
tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(close)];
tap.numberOfTapsRequired = 1;
tap.delegate = self;
[self addGestureRecognizer:tap];
}
- (void)createSubviews
{
backView = [[UIView alloc] init];
backView.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT / 2);
backView.backgroundColor = kWhiteColor;
[backView addRoundingCornersWithRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight];
[self addSubview:backView];
mainTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
mainTableView.backgroundColor = [UIColor whiteColor];
mainTableView.delegate = self;
mainTableView.dataSource = self;
mainTableView.showsVerticalScrollIndicator = NO;
mainTableView.showsHorizontalScrollIndicator = NO;
mainTableView.estimatedRowHeight = 100;
mainTableView.sectionFooterHeight = 10;
mainTableView.rowHeight = UITableViewAutomaticDimension;
mainTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
if (@available(iOS 11.0, *)) {
mainTableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
} else {
// Fallback on earlier versions
}
[backView addSubview:mainTableView];
[mainTableView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(backView.mas_left);
make.top.mas_equalTo(backView.mas_top);
make.width.mas_equalTo(backView.mas_width);
make.height.mas_equalTo(backView.mas_height).offset(-Menu_Footer_Height);
}];
headerTitleLabel = [[UILabel alloc] init];
headerTitleLabel.textAlignment = NSTextAlignmentCenter;
headerTitleLabel.textColor = kBlackColor;
headerTitleLabel.font = kBoldFont15;
headerTitleLabel.backgroundColor = [UIColor whiteColor];
[backView addSubview:headerTitleLabel];
[headerTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(backView.mas_left);
make.top.mas_equalTo(backView.mas_top);
make.width.mas_equalTo(backView.mas_width);
make.height.mas_equalTo(Menu_Header_Height);
}];
{
UIView *line = [[UIView alloc] init];
line.backgroundColor = kGrayLineColor;
[backView addSubview:line];
[line mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(0);
make.bottom.mas_equalTo(headerTitleLabel.mas_bottom);
make.width.mas_equalTo(SCREEN_WIDTH);
make.height.mas_equalTo(kCellLineHeight);
}];
}
closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
closeButton.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, Menu_Footer_Height);
closeButton.backgroundColor = kWhiteColor;
[closeButton setTitle:TFLocalizedString(@"关闭") forState:UIControlStateNormal];
[closeButton setTitleColor:kBlackColor forState:UIControlStateNormal];
[closeButton setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, PUB_TABBAR_OFFSET, 0)];
[closeButton.titleLabel setFont:kMainFont];
[closeButton addTarget:self action:@selector(close) forControlEvents:UIControlEventTouchUpInside];
[backView addSubview:closeButton];
[closeButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(backView.mas_left);
make.bottom.mas_equalTo(backView.mas_bottom);
make.width.mas_equalTo(backView.mas_width);
make.height.mas_equalTo(Menu_Footer_Height);
}];
{
UIView *line = [[UIView alloc] init];
line.backgroundColor = kGrayLineColor;
[backView addSubview:line];
[line mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(0);
make.top.mas_equalTo(closeButton.mas_top);
make.width.mas_equalTo(SCREEN_WIDTH);
make.height.mas_equalTo(kCellLineHeight);
}];
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.menuListArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (_menuType == WXYZ_MenuTypeAudioDirectory || _menuType == WXYZ_MenuTypeAiDirectory) {
static NSString *cellName = @"WXYZ_AudioPlayPageMenuDirectoryTableViewCell";
WXYZ_AudioPlayPageMenuDirectoryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellName];
if (!cell) {
cell = [[WXYZ_AudioPlayPageMenuDirectoryTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellName];
}
cell.productionType = _menuType == WXYZ_MenuTypeAiDirectory?TFProductionTypeAi:TFProductionTypeAudio;
cell.chapterListModel = [self.menuListArray objectAtIndex:indexPath.row];
cell.hiddenEndLine = indexPath.row == self.menuListArray.count - 1;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
} else {
static NSString *cellName = @"WXYZ_AudioPlayPageMenuTableViewCell";
WXYZ_AudioPlayPageMenuTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellName];
if (!cell) {
cell = [[WXYZ_AudioPlayPageMenuTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellName];
}
cell.cellTitleString = [self.menuListArray objectAtIndex:indexPath.row];
cell.hiddenEndLine = indexPath.row == self.menuListArray.count - 1;
switch (_menuType) {
case WXYZ_MenuTypeTiming:
cell.cellSelected = [[WXYZ_AudioSettingHelper sharedManager] getReadTiming] == indexPath.row;
break;
case WXYZ_MenuTypeAudioSpeed:
cell.cellSelected = [[WXYZ_AudioSettingHelper sharedManager] getReadSpeedWithProducitionType:TFProductionTypeAudio] == indexPath.row;
break;
case WXYZ_MenuTypeAiSpeed:
cell.cellSelected = [[WXYZ_AudioSettingHelper sharedManager] getReadSpeedWithProducitionType:TFProductionTypeAi] == indexPath.row;
break;
case WXYZ_MenuTypeAiVoice:
cell.cellSelected = [[WXYZ_AudioSettingHelper sharedManager] getReadVoiceWithProducitionType:TFProductionTypeAi] == indexPath.row;
break;
default:
break;
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return Menu_Cell_Height;
}
//section头间距
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if (_menuType == WXYZ_MenuTypeAudioDirectory || _menuType == WXYZ_MenuTypeAiDirectory) {
return Menu_Header_Height + 40;
}
return Menu_Header_Height;
}
//section头视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
if (!sectionHeaderView) {
sectionHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, Menu_Header_Height)];
sectionHeaderView.backgroundColor = [UIColor clearColor];
if (_menuType == WXYZ_MenuTypeAudioDirectory || _menuType == WXYZ_MenuTypeAiDirectory) {
sectionHeaderView.backgroundColor = [UIColor whiteColor];
sectionHeaderTitle = [[UILabel alloc] initWithFrame:CGRectMake(kMargin, Menu_Header_Height, SCREEN_WIDTH / 2, 40)];
sectionHeaderTitle.text = [NSString stringWithFormat:TFLocalizedString(@"共%@章"), [TFUtilsHelper formatStringWithInteger:self.totalChapter]];
sectionHeaderTitle.textColor = kGrayTextColor;
sectionHeaderTitle.textAlignment = NSTextAlignmentLeft;
sectionHeaderTitle.font = kFont12;
[sectionHeaderView addSubview:sectionHeaderTitle];
CGFloat width = [TFViewHelper getDynamicWidthWithLabelFont:kFont12 labelHeight:40.0 labelText:TFLocalizedString(@"正序") maxWidth:SCREEN_WIDTH / 2.0];
width += kLabelHeight;
TFButton *sortButton = [[TFButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - kHalfMargin - width, Menu_Header_Height, width, 40) buttonTitle:TFLocalizedString(@"正序") buttonImageName:@"comic_positive_order" buttonIndicator:TFButtonIndicatorImageRightBothRight];
sortButton.buttonImageScale = 0.3;
sortButton.buttonTitleFont = kFont12;
sortButton.graphicDistance = 5;
sortButton.buttonTitleColor = kGrayTextColor;
sortButton.tag = 0;
[sortButton addTarget:self action:@selector(changeDirectorySort:) forControlEvents:UIControlEventTouchUpInside];
[sectionHeaderView addSubview:sortButton];
}
}
if (sectionHeaderTitle) {
sectionHeaderTitle.text = [NSString stringWithFormat:TFLocalizedString(@"共%@章"), [TFUtilsHelper formatStringWithInteger:self.totalChapter]];
}
return sectionHeaderView;
}
//section底部间距
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return CGFLOAT_MIN;
}
//section底部视图
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, CGFLOAT_MIN)];
view.backgroundColor = [UIColor clearColor];
return view;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
switch (_menuType) {
case WXYZ_MenuTypeTiming:
[[WXYZ_AudioSettingHelper sharedManager] setReadTimingWithIndex:indexPath.row];
break;
case WXYZ_MenuTypeAudioSpeed:
[[WXYZ_AudioSettingHelper sharedManager] setReadSpeedWithIndex:indexPath.row producitionType:TFProductionTypeAudio];
break;
case WXYZ_MenuTypeAiSpeed:
[[WXYZ_AudioSettingHelper sharedManager] setReadSpeedWithIndex:indexPath.row producitionType:TFProductionTypeAi];
break;
case WXYZ_MenuTypeAiVoice:
[[WXYZ_AudioSettingHelper sharedManager] setReadVoiceWithIndex:indexPath.row producitionType:TFProductionTypeAi];
break;
default:
break;
}
if (_menuType == WXYZ_MenuTypeAudioDirectory || _menuType == WXYZ_MenuTypeAiDirectory) {
TFProductionChapterModel *listModel = [self.menuListArray objectAtIndex:indexPath.row];
if (listModel.chapter_id > 0) {
if (self.chooseDirectoryMenuBlock) {
self.chooseDirectoryMenuBlock(listModel.chapter_id, indexPath.row);
}
}
} else {
if (self.chooseMenuBlock) {
self.chooseMenuBlock(_menuType, indexPath.row);
}
}
[tableView reloadData];
[self close];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if (touch.view.tag != 111) {
return NO;
} else {
return YES;
}
}
- (void)changeDirectorySort:(TFButton *)sender
{
if (sender.tag == 0) {
sender.tag = 1;
sender.buttonImageName = @"comic_reverse_order";
sender.buttonTitle = TFLocalizedString(@"倒序");
self.isInvert = YES;
} else {
sender.tag = 0;
sender.buttonImageName = @"comic_positive_order";
sender.buttonTitle = TFLocalizedString(@"正序");
self.isInvert = NO;
}
if (_menuType == WXYZ_MenuTypeAiDirectory) {
TFProductionChapterModel *t_model = self.menuListArray.firstObject;
NSDictionary *params = @{
@"book_id" : @(t_model.production_id),
@"order_by" : sender.tag ? @(2) : @(1)
};
WS(weakSelf)
[TFNetworkTools POST:Book_New_Catalog parameters:params model:TFCatalogModel.class success:^(BOOL isSuccess, TFCatalogModel * _Nullable t_model, TFNetworkRequestModel * _Nonnull requestModel) {
if (isSuccess) {
weakSelf.totalChapter = t_model.total_chapter;
NSMutableArray<TFProductionChapterModel *> *t_arr = [NSMutableArray array];
for (TFCatalogListModel *list in t_model.list) {
TFProductionChapterModel *model = [[TFProductionChapterModel alloc] init];
model.production_id = [list.book_id integerValue];
model.chapter_id = [list.chapter_id integerValue];
model.next_chapter = [list.next_chapter integerValue];
model.last_chapter = [list.previou_chapter integerValue];
model.chapter_title = list.title;
model.total_words = [list.words integerValue];
model.is_vip = list.vip;
model.update_time = list.update_time;
model.display_order = [NSString stringWithFormat:@"%zd", list.display_order];
model.can_read = list.can_read;
model.is_preview = list.preview;
[t_arr addObject:model];
}
weakSelf.menuListArray = t_arr;
[mainTableView reloadData];
[mainTableView hideRefreshHeader];
[mainTableView showRefreshFooter];
}
} failure:nil];
} else {
self.menuListArray = [[self.menuListArray reverseObjectEnumerator] allObjects];
[mainTableView reloadData];
[mainTableView hideRefreshHeader];
[mainTableView showRefreshFooter];
}
}
- (void)showWithMenuType:(WXYZ_MenuType)menuType
{
_menuType = menuType;
switch (menuType) {
case WXYZ_MenuTypeTiming:
self.menuListArray = [[WXYZ_AudioSettingHelper sharedManager] getReadTimingKeys];
headerTitleLabel.text = TFLocalizedString(@"定时");
break;
case WXYZ_MenuTypeAudioSpeed:
self.menuListArray = [[WXYZ_AudioSettingHelper sharedManager] getReadSpeedKeysWithProducitionType:TFProductionTypeAudio];
headerTitleLabel.text = TFLocalizedString(@"语速");
break;
case WXYZ_MenuTypeAiSpeed:
self.menuListArray = [[WXYZ_AudioSettingHelper sharedManager] getReadSpeedKeysWithProducitionType:TFProductionTypeAi];
headerTitleLabel.text = TFLocalizedString(@"语速");
break;
case WXYZ_MenuTypeAiVoice:
{
self.menuListArray = [[WXYZ_AudioSettingHelper sharedManager] getReadVoiceKeysWithProducitionType:TFProductionTypeAi];
headerTitleLabel.text = TFLocalizedString(@"声音");
}
break;
case WXYZ_MenuTypeAudioDirectory:
case WXYZ_MenuTypeAiDirectory:
{
headerTitleLabel.text = TFLocalizedString(@"章节目录");
WS(weakSelf)
// [mainTableView addFooterRefreshWithRefreshingBlock:^{
// if (weakSelf.isInvert) {
// [weakSelf requestCatalogWithScrollType:@"2"];
// } else {
// [weakSelf requestCatalogWithScrollType:@"1"];
// }
// }];
mainTableView.mj_footer = [MJRefreshFooter footerWithRefreshingBlock:^{
if (weakSelf.isInvert) {
[weakSelf requestCatalogWithScrollType:@"2"];
} else {
[weakSelf requestCatalogWithScrollType:@"1"];
}
}];
}
break;
case WXYZ_MenuTypeAudioSelection:
{
if (self.menuListArray.count > 0) {
NSMutableArray *t_array = [NSMutableArray array];
for (int i = 0; i < (self.menuListArray.count / 30) + (self.menuListArray.count % 30 == 0?0:1); i ++) {
[t_array addObject:[NSString stringWithFormat:@"%@-%@", [TFUtilsHelper formatStringWithInteger:(i * 30 + 1)], [TFUtilsHelper formatStringWithInteger:((i + 1) * 30) <= self.menuListArray.count?((i + 1) * 30):self.menuListArray.count]]];
}
self.menuListArray = [t_array copy];
}
headerTitleLabel.text = TFLocalizedString(@"选章");
}
break;
default:
break;
}
// 计算tableview高度
CGFloat tableViewHeight = self.menuListArray.count * Menu_Cell_Height + Menu_Header_Height + Menu_Footer_Height + ((_menuType == WXYZ_MenuTypeAudioDirectory || _menuType == WXYZ_MenuTypeAiDirectory)?80:0);
if (tableViewHeight > SCREEN_HEIGHT / 2) {
tableViewHeight = SCREEN_HEIGHT / 2;
}
[UIView animateWithDuration:kAnimatedDuration animations:^{
backView.frame = CGRectMake(0, SCREEN_HEIGHT - tableViewHeight, SCREEN_WIDTH, tableViewHeight);
}];
[mainTableView reloadData];
[mainTableView layoutIfNeeded];
if (menuType == WXYZ_MenuTypeAiDirectory) {
// 移动到当前播放位置
for (int i = 0; i < self.menuListArray.count; i ++) {
TFProductionChapterModel *chapter_model = [self.menuListArray objectAtIndex:i];
if (chapter_model.chapter_id == [[TFReadRecordManager shareManagerWithProductionType:TFProductionTypeAi] getReadingRecordChapter_idWithProduction_id:chapter_model.production_id]) {
[mainTableView scrollToRow:i inSection:0 atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
}
} else if (menuType == WXYZ_MenuTypeAudioDirectory) {
// 移动到当前播放位置
for (int i = 0; i < self.menuListArray.count; i ++) {
TFProductionChapterModel *chapter_model = [self.menuListArray objectAtIndex:i];
if (chapter_model.chapter_id == [[TFReadRecordManager shareManagerWithProductionType:TFProductionTypeAudio] getReadingRecordChapter_idWithProduction_id:chapter_model.production_id]) {
[mainTableView scrollToRow:i inSection:0 atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
}
}
}
- (void)close
{
[UIView animateWithDuration:kAnimatedDuration animations:^{
backView.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, backView.height);
} completion:^(BOOL finished) {
for (UIView *v in [self subviews]) {
[v removeFromSuperview];
}
[self removeFromSuperview];
}];
}
// scrollType 1:向下加载;2:向上加载
- (void)requestCatalogWithScrollType:(NSString *)scrollType {
BOOL loadDown = [scrollType isEqualToString:@"1"];
NSString *chapterID = @"";
TFProductionChapterModel *t_model = nil;
if (loadDown) {
t_model = self.menuListArray.lastObject;
chapterID = [NSString stringWithFormat:@"%zd", t_model.next_chapter];
} else {
t_model = self.menuListArray.lastObject;
chapterID = [NSString stringWithFormat:@"%zd", t_model.last_chapter];
}
if ([chapterID isEqualToString:@"0"]) {
[mainTableView endRefreshing];
if (loadDown) {
[mainTableView hideRefreshFooter];
} else {
[mainTableView hideRefreshHeader];
}
return;
}
NSDictionary *params = @{
@"book_id" : @(t_model.production_id),
@"chapter_id" : chapterID,
@"scroll_type" : scrollType,
};
WS(weakSelf)
[TFNetworkTools POST:Book_New_Catalog parameters:params model:TFCatalogModel.class success:^(BOOL isSuccess, TFCatalogModel * _Nullable t_model, TFNetworkRequestModel * _Nonnull requestModel) {
[mainTableView endRefreshing];
if (isSuccess) {
weakSelf.totalChapter = t_model.total_chapter;
NSMutableArray<TFProductionChapterModel *> *t_arr = [NSMutableArray array];
for (TFCatalogListModel *list in t_model.list) {
TFProductionChapterModel *model = [[TFProductionChapterModel alloc] init];
model.production_id = [list.book_id integerValue];
model.chapter_id = [list.chapter_id integerValue];
model.next_chapter = [list.next_chapter integerValue];
model.last_chapter = [list.previou_chapter integerValue];
model.chapter_title = list.title;
model.total_words = [list.words integerValue];
model.is_vip = list.vip;
model.update_time = list.update_time;
model.display_order = [NSString stringWithFormat:@"%zd", list.display_order];
model.can_read = list.can_read;
model.is_preview = list.preview;
[t_arr addObject:model];
}
if ([scrollType isEqualToString:@"2"]) {
t_arr = [[[t_arr reverseObjectEnumerator] allObjects] mutableCopy];
}
weakSelf.menuListArray = [weakSelf.menuListArray arrayByAddingObjectsFromArray:t_arr];
[mainTableView reloadData];
[mainTableView endRefreshing];
if ([scrollType isEqualToString:@"1"]) {
if (t_arr.lastObject.next_chapter == 0) {
[mainTableView hideRefreshFooter];
}
if (t_arr.firstObject.last_chapter == 0) {
[mainTableView hideRefreshHeader];
}
} else {
if (t_arr.lastObject.last_chapter == 0) {
[mainTableView hideRefreshFooter];
}
if (t_arr.firstObject.next_chapter == 0) {
[mainTableView hideRefreshHeader];
}
}
} else {
[TFPromptManager showPromptViewWithStatus:TFPromptStatusError promptTitle:requestModel.msg];
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
[TFPromptManager showPromptWithError:error defaultText:nil];
[mainTableView endRefreshing];
}];
}
@end
@@ -0,0 +1,19 @@
//
// WXYZ_AudioDownloadCacheDetailViewController.h
// WXReader
//
// Created by Andrew on 2020/4/1.
// Copyright © 2020 Andrew. All rights reserved.
//
#import "TFDownloadViewController.h"
NS_ASSUME_NONNULL_BEGIN
@interface WXYZ_AudioDownloadCacheDetailViewController : TFDownloadViewController
@property (nonatomic, strong) TFProductionModel *audioModel;
@end
NS_ASSUME_NONNULL_END
@@ -0,0 +1,266 @@
//
// WXYZ_AudioDownloadCacheDetailViewController.m
// WXReader
//
// Created by Andrew on 2020/4/1.
// Copyright © 2020 Andrew. All rights reserved.
//
#import "WXYZ_AudioDownloadCacheDetailViewController.h"
#import "WXYZ_AudioDownloadTableViewCell.h"
#import "WXYZ_AudioDownloadManager.h"
@interface WXYZ_AudioDownloadCacheDetailViewController () <UITableViewDelegate, UITableViewDataSource>
@end
@implementation WXYZ_AudioDownloadCacheDetailViewController
{
UIButton *deleteButton;
TFButton *selectAllButton;
}
- (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(@"选择章节")];
}
- (void)createSubviews
{
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.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);
}];
selectAllButton = [[TFButton alloc] initWithFrame:CGRectZero buttonTitle:TFLocalizedString(@"全选") buttonImageName:@"audio_download_unselect" buttonIndicator:TFButtonIndicatorImageLeftBothLeft];
selectAllButton.buttonTitleColor = kBlackColor;
selectAllButton.buttonTitleFont = kMainFont;
selectAllButton.graphicDistance = 10;
selectAllButton.buttonImageScale = 0.4;
selectAllButton.tag = 0;
selectAllButton.backgroundColor = [UIColor whiteColor];
[selectAllButton addTarget:self action:@selector(checkallClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:selectAllButton];
[selectAllButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(kMargin);
make.top.mas_equalTo(self.mainTableView.mas_bottom);
make.width.mas_equalTo(100);
make.height.mas_equalTo(PUB_TABBAR_HEIGHT - PUB_TABBAR_OFFSET);
}];
deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
deleteButton.backgroundColor = kGrayTextLightColor;
deleteButton.enabled = NO;
[deleteButton setTitle:TFLocalizedString(@"删除") forState:UIControlStateNormal];
[deleteButton setTitleColor:kWhiteColor forState:UIControlStateNormal];
[deleteButton.titleLabel setFont:kMainFont];
[deleteButton addTarget:self action:@selector(deleteClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:deleteButton];
[deleteButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.mas_equalTo(self.view.mas_right);
make.top.mas_equalTo(selectAllButton.mas_top);
make.width.mas_equalTo(SCREEN_WIDTH / 3);
make.height.mas_equalTo(selectAllButton.mas_height);
}];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.dataSourceArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
TFProductionChapterModel *chapterModel = [self.dataSourceArray objectOrNilAtIndex:indexPath.row];
static NSString *cellName = @"WXYZ_AudioDownloadTableViewCell";
WXYZ_AudioDownloadTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellName];
if (!cell) {
cell = [[WXYZ_AudioDownloadTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellName];
}
cell.isCacheState = YES;
cell.chapterModel = chapterModel;
cell.cellDownloadState = [[self.selectSourceDictionary objectForKey:[TFUtilsHelper formatStringWithInteger:chapterModel.chapter_id]] integerValue];
cell.hiddenEndLine = self.dataSourceArray.count - 1 == indexPath.row;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 30;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 30)];
view.backgroundColor = kGrayViewColor;
UILabel *headerTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(kMargin, 0, SCREEN_WIDTH / 2, 30)];
headerTitleLabel.backgroundColor = [UIColor clearColor];
headerTitleLabel.font = kFont12;
headerTitleLabel.text = [NSString stringWithFormat:TFLocalizedString(@"共%@章"), [TFUtilsHelper formatStringWithInteger:self.dataSourceArray.count]];
headerTitleLabel.textColor = kGrayTextColor;
[view addSubview:headerTitleLabel];
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, CGFLOAT_MIN)];
view.backgroundColor = [UIColor clearColor];
return view;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
TFProductionChapterModel *chapterModel = [self.dataSourceArray objectOrNilAtIndex:indexPath.row];
WXYZ_AudioDownloadTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
switch (cell.cellDownloadState) {
case WXYZ_ProductionDownloadStateDownloaded:
case WXYZ_ProductionDownloadStateNormal:
[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;
}
}
- (void)checkallClick:(TFButton *)sender
{
for (NSString *t_chapter_id in self.selectSourceDictionary.allKeys) {
if (!sender.selected) { // 全选状态
if ([[self.selectSourceDictionary objectForKey:t_chapter_id] integerValue] == WXYZ_ProductionDownloadStateDownloaded || [[self.selectSourceDictionary objectForKey:t_chapter_id] integerValue] == WXYZ_ProductionDownloadStateNormal) {
[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)deleteClick
{
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_AudioDownloadManager sharedManager] removeDownloadChaptersWithProduction_id:self.audioModel.production_id chapter_ids:t_deleteArr];
[WXYZ_AudioDownloadManager 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:
case WXYZ_ProductionDownloadStateNormal:
downloadedIndex ++;
break;
case WXYZ_ProductionDownloadStateSelected:
selectIndex ++;
break;
default:
break;
}
}
deleteButton.enabled = NO;
deleteButton.backgroundColor = kGrayTextLightColor;
selectAllButton.selected = NO;
selectAllButton.enabled = YES;
selectAllButton.buttonTitle = TFLocalizedString(@"全选");
selectAllButton.buttonImageName = @"audio_download_unselect";
if (selectIndex > 0 && downloadedIndex == 0) { // 有选中的文件并且没有未选中的文件
deleteButton.enabled = YES;
deleteButton.backgroundColor = kMainColor;
selectAllButton.selected = YES;
selectAllButton.buttonTitle = TFLocalizedString(@"取消全选");
selectAllButton.buttonImageName = @"audio_download_select";
} else if (downloadedIndex > 0 && selectIndex > 0) { // 有未选中的文件
deleteButton.enabled = YES;
deleteButton.backgroundColor = kMainColor;
}
}
- (void)setCollectionCellDownloadStateWithChapter_id:(NSInteger)chapter_id downloadState:(WXYZ_ProductionDownloadState)downloadState
{
WXYZ_AudioDownloadTableViewCell *cell = (WXYZ_AudioDownloadTableViewCell *)[self.mainTableView cellForRowAtIndexPath:(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_AudioDownloadManager sharedManager] getDownloadChapterModelArrayWithProduction_id:self.audioModel.production_id] mutableCopy];
if (self.dataSourceArray.count == 0) {
[self.navigationController popViewControllerAnimated:NO];
return;
}
[self resetSelectSourceDicWithDataSourceArray:[self.dataSourceArray copy] productionType:TFProductionTypeAudio];
[self reloadToolBar];
[self.mainTableView reloadData];
}
@end
@@ -0,0 +1,46 @@
//
// WXYZ_AudioDownloadCacheTableViewCell.h
// WXReader
//
// Created by Andrew on 2020/4/1.
// Copyright © 2020 Andrew. All rights reserved.
//
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
typedef void(^CellSelectBlock)(TFProductionModel *productionModel, NSInteger production_id, NSString *name);
typedef void(^ImageViewSelectBlock)(NSInteger production_id);
typedef void(^ButtonSelectBlock)(TFProductionModel *productionModel);
@interface WXYZ_AudioDownloadCacheTableViewCell : TFBasicTableViewCell
@property (nonatomic, strong) TFProductionModel *productionModel;
@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,231 @@
//
// WXYZ_AudioDownloadCacheTableViewCell.m
// WXReader
//
// Created by Andrew on 2020/4/1.
// Copyright © 2020 Andrew. All rights reserved.
//
#import "WXYZ_AudioDownloadCacheTableViewCell.h"
#import "TFReadRecordManager.h"
#import "WXYZ_AudioDownloadManager.h"
#import "TFProductionCoverView.h"
@interface WXYZ_AudioDownloadCacheTableViewCell ()
@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 WXYZ_AudioDownloadCacheTableViewCell
- (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:TFProductionTypeAudio 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 = 0;
[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)setProductionModel:(TFProductionModel *)productionModel
{
_productionModel = productionModel;
self.coverImageView.coverImageUrl = productionModel.cover;
self.titleNameLabel.text = productionModel.name ? : @"";
self.subTitleLabel.text = [NSString stringWithFormat:TFLocalizedString(@"%@章/%@章"), [TFUtilsHelper formatStringWithInteger:[[WXYZ_AudioDownloadManager sharedManager] getDownloadChapterCountWithProduction_id:productionModel.production_id]], [TFUtilsHelper formatStringWithInteger:productionModel.total_chapters]];
if ([[TFReadRecordManager shareManagerWithProductionType:TFProductionTypeAudio] getReadingRecordChapterTitleWithProduction_id:productionModel.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(_productionModel.production_id);
}
} else {
if (self.cellSelectBlock) {
self.cellSelectBlock(_productionModel, _productionModel.production_id, _productionModel.name);
}
}
}
- (void)readButtonClick
{
if (self.buttonSelectBlock) {
self.buttonSelectBlock(_productionModel);
}
}
- (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.productionModel, 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) {
_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) {
_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,23 @@
//
// WXYZ_AudioDownloadCacheViewController.h
// WXReader
//
// Created by Andrew on 2020/3/21.
// Copyright © 2020 Andrew. All rights reserved.
//
NS_ASSUME_NONNULL_BEGIN
@interface WXYZ_AudioDownloadCacheViewController : 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,421 @@
//
// WXYZ_AudioDownloadCacheViewController.m
// WXReader
//
// Created by Andrew on 2020/3/21.
// Copyright © 2020 Andrew. All rights reserved.
//
#import "WXYZ_AudioDownloadCacheViewController.h"
#import "WXYZ_AudioDownloadCacheDetailViewController.h"
#import "TFAudioPlayViewController.h"
#import "WXYZ_AudioDownloadCacheTableViewCell.h"
#import "WXYZ_AudioDownloadManager.h"
#import "TFReadRecordManager.h"
#import "UIView+LayoutCallback.h"
#import "TFCollectionManager.h"
@interface WXYZ_AudioDownloadCacheViewController () <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 WXYZ_AudioDownloadCacheViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self initialize];
[self createSubviews];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self setStatusBarDefaultStyle];
self.dataSourceArray = [[[[[WXYZ_DownloadHelper sharedManager] getDownloadProductionArrayWithProductionType:TFProductionTypeAudio] reverseObjectEnumerator] allObjects] mutableCopy];
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:WXYZ_AudioDownloadCacheTableViewCell.class]) {
WXYZ_AudioDownloadCacheTableViewCell *celll = (WXYZ_AudioDownloadCacheTableViewCell *)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:^{
}];
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];
}
});
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self.mainTableView setEditing:NO animated:YES];
});
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.dataSourceArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
WS(weakSelf)
WXYZ_AudioDownloadCacheTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WXYZ_AudioDownloadCacheTableViewCell"];
if (!cell) {
cell = [[WXYZ_AudioDownloadCacheTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"WXYZ_AudioDownloadCacheTableViewCell"];
}
TFProductionModel *t_model = [self.dataSourceArray objectOrNilAtIndex:indexPath.row];
cell.productionModel = t_model;
[cell set_Editting:self.isEditting];
cell.cellSelectBlock = ^(TFProductionModel * _Nonnull productionModel, NSInteger production_id, NSString * _Nonnull name) {
productionModel.chapter_list = [[WXYZ_AudioDownloadManager sharedManager] getDownloadChapterModelArrayWithProduction_id:production_id];
WXYZ_AudioDownloadCacheDetailViewController *vc = [[WXYZ_AudioDownloadCacheDetailViewController alloc] init];
vc.audioModel = productionModel;
vc.navTitle = name;
[weakSelf.navigationController pushViewController:vc animated:YES];
};
cell.imageViewSelectBlock = ^(NSInteger production_id) {
TFAudioDetailViewController *vc = [[TFAudioDetailViewController alloc] init];
vc.audio_id = production_id;
[weakSelf.navigationController pushViewController:vc animated:YES];
};
cell.buttonSelectBlock = ^(TFProductionModel * _Nonnull productionModel) {
[[TFCollectionManager shareManagerWithProductionType:TFProductionTypeAudio] moveCollectionToTopWithProductionModel:t_model];
TFAudioPlayViewController *vc = [TFAudioPlayViewController sharedManager];
[vc loadDataWithAudio_id:productionModel.production_id chapter_id:0];
TFNavigationController *nav = [[TFNavigationController alloc] initWithRootViewController:vc];
[[TFViewHelper getWindowRootController] presentViewController:nav animated:YES completion:nil];
};
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];
};
cell.selectionStyle = UITableViewCellSelectionStyleNone;
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(@"删除");
}
- (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)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
TFProductionModel *t_model = [self.dataSourceArray objectOrNilAtIndex:indexPath.row];
[[WXYZ_AudioDownloadManager 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];
}
}
// 全选按钮点击事件
- (void)selectedAllEvent {
if ([self.edittingAll.titleLabel.text isEqualToString:TFLocalizedString(@"全选")]) {
for (UITableViewCell *cell in self.mainTableView.visibleCells) {
if ([cell isMemberOfClass:WXYZ_AudioDownloadCacheTableViewCell.class]) {
WXYZ_AudioDownloadCacheTableViewCell *tempCell = (WXYZ_AudioDownloadCacheTableViewCell *)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:WXYZ_AudioDownloadCacheTableViewCell.class]) {
WXYZ_AudioDownloadCacheTableViewCell *tempCell = (WXYZ_AudioDownloadCacheTableViewCell *)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_AudioDownloadManager sharedManager] removeDownloadProductionWithProduction_id:productionModel.production_id];
[self.dataSourceArray removeObject:productionModel];
[self.selectedArray removeObject:@(productionModel.production_id)];
[pathArr addObject:[NSIndexPath indexPathForRow:idx inSection:0]];
}
}];
if (@available(iOS 11.0, *)) {
[self.mainTableView performBatchUpdates:^{
[self.mainTableView deleteRowsAtIndexPaths:pathArr withRowAnimation:UITableViewRowAnimationLeft];
} completion:^(BOOL finished) {
if (!finished) return;
BOOL a = !_editStateBlock ?: _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(), ^{
BOOL a = !_editStateBlock ?: _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