You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
270 lines
11 KiB
270 lines
11 KiB
4 years ago
|
//
|
||
|
// TFReadNovelTextController.m
|
||
|
// TFReader
|
||
|
//
|
||
|
// Created by 谢腾飞 on 2020/12/15.
|
||
|
// Copyright © 2020 xtfei_2011@126.com. All rights reserved.
|
||
|
//
|
||
|
|
||
|
#import "TFReadNovelTextController.h"
|
||
|
#import "TYAttributedLabel.h"
|
||
|
#import "TFReaderBookManager.h"
|
||
|
#import "TFReaderSettingHelper.h"
|
||
|
#import "WXYZ_BatteryView.h"
|
||
|
#import "TFEmptyViewHeader.h"
|
||
|
#import "WXYZ_BookReaderMenuPayView.h"
|
||
|
#import "NSAttributedString+TReaderPage.h"
|
||
|
#import "TFNetworkManager.h"
|
||
|
|
||
|
#import "TFReaderBookManager.h"
|
||
|
|
||
|
@interface TFReadNovelTextController ()
|
||
|
{
|
||
|
UILabel *emptyTitleLabel;
|
||
|
UIButton *emptyButton;
|
||
|
}
|
||
|
|
||
|
@property (nonatomic ,strong) UILabel *chapterTitleLabel;
|
||
|
|
||
|
@property (nonatomic ,strong) UILabel *bookNameLabel;
|
||
|
|
||
|
@property (nonatomic ,strong) UILabel *pageNumberLabel;
|
||
|
|
||
|
@property (nonatomic ,strong) TYAttributedLabel *chapterContentLabel;
|
||
|
|
||
|
@property (nonatomic ,strong) WXYZ_BatteryView *battery;
|
||
|
|
||
|
@property (nonatomic ,strong) WXYZ_BookReaderMenuPayView *payView;
|
||
|
|
||
|
@end
|
||
|
|
||
|
@implementation TFReadNovelTextController
|
||
|
|
||
|
- (void)viewDidLoad
|
||
|
{
|
||
|
[super viewDidLoad];
|
||
|
|
||
|
self.view.backgroundColor = [[TFReaderSettingHelper sharedManager] getReaderBackgroundColor];
|
||
|
|
||
|
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadFrame) name:Notification_Check_Setting_Update object:nil];
|
||
|
|
||
|
WS(weakSelf)
|
||
|
[TFReaderSettingHelper sharedManager].readerBackgroundViewChanged = ^() {
|
||
|
[weakSelf reloadSubviews];
|
||
|
[[NSNotificationCenter defaultCenter] postNotificationName:NSNotification_Retry_Chapter object:nil];
|
||
|
};
|
||
|
}
|
||
|
|
||
|
- (void)setContentString:(NSAttributedString *)contentString
|
||
|
{
|
||
|
if ([contentString.string isEqualToString:k_Chapter_RequstFail] || !contentString) {
|
||
|
[self createEmptyView];
|
||
|
[[NSNotificationCenter defaultCenter] postNotificationName:NSNotification_EmptyView_Changed object:@1];
|
||
|
return;
|
||
|
}
|
||
|
[self reloadSubviews];
|
||
|
|
||
|
if ([[TFReaderBookManager sharedManager] isPreviewChapter] && contentString.length > 1) {
|
||
|
CGSize size = CGSizeMake(CGRectGetWidth(self.chapterContentLabel.bounds), CGRectGetHeight(self.chapterContentLabel.bounds) / 2.0);
|
||
|
NSArray<NSValue *> *t_arr = [contentString pageRangeArrayWithConstrainedToSize:size];
|
||
|
if (t_arr.count > 0) {
|
||
|
NSRange range = [t_arr.firstObject rangeValue];
|
||
|
contentString = [contentString attributedSubstringFromRange:range];
|
||
|
}
|
||
|
|
||
|
[self payView];
|
||
|
} else {
|
||
|
TFReaderBookManager *manager = [TFReaderBookManager sharedManager];
|
||
|
NSString *chapter_id = [NSString stringWithFormat:@"%zd", manager.chapter_id];
|
||
|
[[NSNotificationCenter defaultCenter] postNotificationName:Notification_Production_Pay_Success object:@[chapter_id]];
|
||
|
_payView.hidden = YES;
|
||
|
}
|
||
|
|
||
|
_contentString = contentString;
|
||
|
self.chapterContentLabel.attributedText = contentString;
|
||
|
|
||
|
// 总章节页数
|
||
|
NSUInteger totalChapterPagerCount = [[TFReaderBookManager sharedManager] totalChapterPagerCount];
|
||
|
NSUInteger readedPagerCount = ([[TFReaderBookManager sharedManager] currentPagerIndex] + 1) + (([[TFReaderBookManager sharedManager] currentChapterPagerCount]) * ([[TFReaderBookManager sharedManager] currentChapterIndex]));
|
||
|
|
||
|
float progressValue = [[NSNumber numberWithUnsignedInteger:readedPagerCount] floatValue] / [[NSNumber numberWithUnsignedInteger:totalChapterPagerCount] floatValue] * 100;
|
||
|
dispatch_async(dispatch_get_main_queue(), ^{
|
||
|
self.pageNumberLabel.text = [NSString stringWithFormat:@"%.1lf%@", progressValue < 0.1?0.1f:progressValue, @"%"];
|
||
|
});
|
||
|
|
||
|
[[NSNotificationCenter defaultCenter] postNotificationName:NSNotification_EmptyView_Changed object:@0];
|
||
|
}
|
||
|
|
||
|
- (void)reloadSubviews
|
||
|
{
|
||
|
kCodeSync({
|
||
|
self.view.backgroundColor = [[TFReaderSettingHelper sharedManager] getReaderBackgroundColor];
|
||
|
|
||
|
UIColor *textColor = [[TFReaderSettingHelper sharedManager] getReaderTextColor];
|
||
|
UIColor *titleTextColor = [[TFReaderSettingHelper sharedManager] getReaderTitleTextColor];
|
||
|
|
||
|
self.chapterContentLabel.font = [UIFont systemFontOfSize:[[TFReaderSettingHelper sharedManager] getReaderFontSize]];
|
||
|
self.chapterContentLabel.linesSpacing = [[TFReaderSettingHelper sharedManager] getReaderLinesSpacing];
|
||
|
|
||
|
self.bookNameLabel.text = [[TFReaderBookManager sharedManager] getBookName];
|
||
|
self.bookNameLabel.textColor = titleTextColor;
|
||
|
|
||
|
self.chapterTitleLabel.text = [[TFReaderBookManager sharedManager] getChapterTitle];
|
||
|
self.chapterTitleLabel.textColor = titleTextColor;
|
||
|
|
||
|
self.chapterContentLabel.textColor = textColor;
|
||
|
|
||
|
self.battery.batteryTintColor = titleTextColor;
|
||
|
|
||
|
self.pageNumberLabel.textColor = [[TFReaderSettingHelper sharedManager] getReaderTitleTextColor];
|
||
|
self.pageNumberLabel.hidden = NO;
|
||
|
|
||
|
emptyButton.hidden = YES;
|
||
|
emptyTitleLabel.hidden = YES;
|
||
|
|
||
|
if ([TFReaderBookManager sharedManager].currentChapterIndex == 0 && [TFReaderBookManager sharedManager].currentPagerIndex == 0) {
|
||
|
self.chapterTitleLabel.hidden = YES;
|
||
|
self.battery.hidden = YES;
|
||
|
self.pageNumberLabel.hidden = YES;
|
||
|
self.bookNameLabel.hidden = YES;
|
||
|
} else {
|
||
|
self.chapterTitleLabel.hidden = NO;
|
||
|
self.battery.hidden = NO;
|
||
|
self.pageNumberLabel.hidden = NO;
|
||
|
self.bookNameLabel.hidden = NO;
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
|
||
|
- (void)reloadFrame
|
||
|
{
|
||
|
[self reloadSubviews];
|
||
|
_pageNumberLabel.frame = CGRectMake(SCREEN_WIDTH / 2, [[TFReaderSettingHelper sharedManager] getReaderViewBottom] + kHalfMargin, (SCREEN_WIDTH - 2 * kMargin) / 2, kMargin);
|
||
|
_battery.frame = CGRectMake(kMargin, [[TFReaderSettingHelper sharedManager] getReaderViewBottom] + kHalfMargin, SCREEN_WIDTH / 2, kMargin);
|
||
|
_bookNameLabel.frame = CGRectMake(SCREEN_WIDTH / 3, [[TFReaderSettingHelper sharedManager] getReaderViewBottom] + kHalfMargin, SCREEN_WIDTH / 3, kMargin);
|
||
|
_chapterContentLabel.frame = [[TFReaderSettingHelper sharedManager] getReaderViewFrame];
|
||
|
}
|
||
|
|
||
|
- (UILabel *)pageNumberLabel
|
||
|
{
|
||
|
if (!_pageNumberLabel) {
|
||
|
_pageNumberLabel = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH / 2, [[TFReaderSettingHelper sharedManager] getReaderViewBottom] + kHalfMargin, (SCREEN_WIDTH - 2 * kMargin) / 2, kMargin)];
|
||
|
_pageNumberLabel.text = @"0.1%";
|
||
|
_pageNumberLabel.font = kFont12;
|
||
|
_pageNumberLabel.hidden = YES;
|
||
|
_pageNumberLabel.textAlignment = NSTextAlignmentRight;
|
||
|
[self.view addSubview:_pageNumberLabel];
|
||
|
}
|
||
|
return _pageNumberLabel;
|
||
|
}
|
||
|
|
||
|
- (WXYZ_BatteryView *)battery
|
||
|
{
|
||
|
if (!_battery) {
|
||
|
_battery = [[WXYZ_BatteryView alloc] initWithFrame:CGRectMake(kMargin, [[TFReaderSettingHelper sharedManager] getReaderViewBottom] + kHalfMargin, SCREEN_WIDTH / 2, kMargin)];
|
||
|
[self.view addSubview:_battery];
|
||
|
}
|
||
|
return _battery;
|
||
|
}
|
||
|
|
||
|
- (UILabel *)bookNameLabel
|
||
|
{
|
||
|
if (!_bookNameLabel) {
|
||
|
_bookNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH / 3, [[TFReaderSettingHelper sharedManager] getReaderViewBottom] + kHalfMargin, SCREEN_WIDTH / 3, kMargin)];
|
||
|
_bookNameLabel.font = kFont12;
|
||
|
_bookNameLabel.textAlignment = NSTextAlignmentCenter;
|
||
|
[self.view addSubview:_bookNameLabel];
|
||
|
}
|
||
|
return _bookNameLabel;
|
||
|
}
|
||
|
|
||
|
- (UILabel *)chapterTitleLabel
|
||
|
{
|
||
|
if (!_chapterTitleLabel) {
|
||
|
_chapterTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(kMargin, kMargin + PUB_NAVBAR_OFFSET, (SCREEN_WIDTH - 2 * kMargin) / 2, kMargin)];
|
||
|
_chapterTitleLabel.font = kFont12;
|
||
|
_chapterTitleLabel.textAlignment = NSTextAlignmentLeft;
|
||
|
[self.view addSubview:_chapterTitleLabel];
|
||
|
}
|
||
|
return _chapterTitleLabel;
|
||
|
}
|
||
|
|
||
|
- (TYAttributedLabel *)chapterContentLabel
|
||
|
{
|
||
|
if (!_chapterContentLabel) {
|
||
|
_chapterContentLabel = nil;
|
||
|
kCodeSync({
|
||
|
_chapterContentLabel = [[TYAttributedLabel alloc] init];
|
||
|
_chapterContentLabel.frame = [[TFReaderSettingHelper sharedManager] getReaderViewFrame];
|
||
|
_chapterContentLabel.font = [UIFont systemFontOfSize:[[TFReaderSettingHelper sharedManager] getReaderFontSize]];
|
||
|
_chapterContentLabel.backgroundColor = [UIColor clearColor];
|
||
|
_chapterContentLabel.linesSpacing = [[TFReaderSettingHelper sharedManager] getReaderLinesSpacing];
|
||
|
_chapterContentLabel.lineBreakMode = NSLineBreakByWordWrapping;
|
||
|
[self.view addSubview:_chapterContentLabel];
|
||
|
});
|
||
|
}
|
||
|
return _chapterContentLabel;
|
||
|
}
|
||
|
|
||
|
- (WXYZ_BookReaderMenuPayView *)payView
|
||
|
{
|
||
|
if (!_payView) {
|
||
|
_payView = [[WXYZ_BookReaderMenuPayView alloc] init];
|
||
|
[self.view addSubview:_payView];
|
||
|
}
|
||
|
_payView.hidden = NO;
|
||
|
return _payView;
|
||
|
}
|
||
|
|
||
|
- (void)createEmptyView
|
||
|
{
|
||
|
if (!emptyTitleLabel) {
|
||
|
emptyTitleLabel = [[UILabel alloc] init];
|
||
|
emptyTitleLabel.textColor = [TFReaderSettingHelper sharedManager].getReaderTextColor;
|
||
|
emptyTitleLabel.font = kFont16;
|
||
|
emptyTitleLabel.text = TFLocalizedString(@"内容加载失败");
|
||
|
emptyTitleLabel.textAlignment = NSTextAlignmentCenter;
|
||
|
[self.view addSubview:emptyTitleLabel];
|
||
|
|
||
|
[emptyTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
|
||
|
make.bottom.mas_equalTo(self.view.mas_centerY).with.offset(- kHalfMargin);
|
||
|
make.left.mas_equalTo(0);
|
||
|
make.width.mas_equalTo(SCREEN_WIDTH);
|
||
|
make.height.mas_equalTo(kLabelHeight);
|
||
|
}];
|
||
|
}
|
||
|
|
||
|
if (!emptyButton) {
|
||
|
emptyButton = [UIButton buttonWithType:UIButtonTypeCustom];
|
||
|
emptyButton.backgroundColor = [UIColor clearColor];
|
||
|
emptyButton.layer.cornerRadius = 15;
|
||
|
emptyButton.layer.borderColor = [TFReaderSettingHelper sharedManager].getReaderTextColor.CGColor;
|
||
|
emptyButton.layer.borderWidth = 1.0;
|
||
|
[emptyButton setTitle:TFLocalizedString(@"重试") forState:UIControlStateNormal];
|
||
|
[emptyButton setTitleColor:[TFReaderSettingHelper sharedManager].getReaderTextColor forState:UIControlStateNormal];
|
||
|
[emptyButton.titleLabel setFont:kFont12];
|
||
|
[emptyButton addTarget:self action:@selector(retryClick:) forControlEvents:UIControlEventTouchUpInside];
|
||
|
[self.view addSubview:emptyButton];
|
||
|
|
||
|
[emptyButton mas_makeConstraints:^(MASConstraintMaker *make) {
|
||
|
make.centerX.mas_equalTo(self.view.mas_centerX);
|
||
|
make.top.mas_equalTo(emptyTitleLabel.mas_bottom);
|
||
|
make.width.mas_equalTo(80);
|
||
|
make.height.mas_equalTo(kLabelHeight);
|
||
|
}];
|
||
|
}
|
||
|
|
||
|
emptyButton.hidden = NO;
|
||
|
emptyTitleLabel.hidden = NO;
|
||
|
}
|
||
|
|
||
|
- (void)retryClick:(UIButton *)sender
|
||
|
{
|
||
|
if (![TFNetworkManager networkingStatus] || [TFNetworkManager currentNetworkStatus] == kCTCellularDataRestrictedStateUnknown) {
|
||
|
return;
|
||
|
}
|
||
|
[[NSNotificationCenter defaultCenter] postNotificationName:NSNotification_Retry_Chapter object:@"1"];
|
||
|
}
|
||
|
|
||
|
@end
|