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.
 

392 lines
15 KiB

//
// WXYZ_MoreView.m
// WXReader
//
// Created by LL on 2020/5/22.
// Copyright © 2020 Andrew. All rights reserved.
//
#import "WXYZ_MoreView.h"
#import "UIView+LayoutCallback.h"
#import "NSObject+Observer.h"
#import "TFReaderBookManager.h"
#import "TFCollectionManager.h"
#import "TFNovelDetailViewController.h"
#import "TFShareManager.h"
#import "TFReadRecordManager.h"
#import "TFBookMarkModel.h"
#import "WXYZ_BookReaderMenuBar.h"
@interface WXYZ_MoreCell : UICollectionViewCell
- (void)setDict:(NSDictionary <NSString *, NSString *> *)dict;
@end
typedef NS_ENUM(NSInteger, ReaderBookStyle) {
ReaderBookDetail = 0,
ReaderBookMark,
ReaderBookRack,
ReaderBookShare,
};
@interface WXYZ_MoreView ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, strong) NSArray<NSDictionary *> *moreDict;
@property (nonatomic, weak) UIView *mainView;
/// 背景视图
@property (nonatomic, weak) UIView *emptyView;
@property (nonatomic, strong) TFProductionModel *bookModel;
@property (nonatomic, strong) TFReaderBookManager *bookManager;
/// 书签状态
@property (nonatomic, assign) BOOL markStatus;
/// 书签Model
@property (nonatomic, strong) TFBookMarkModel *markModel;
/// 是不是封面
@property (nonatomic, assign) BOOL isCover;
@end
@implementation WXYZ_MoreView
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
[self initialize];
[self createSubviews];
}
return self;
}
- (void)initialize {
self.bookModel = [TFReaderBookManager sharedManager].bookModel;
self.bookManager = [TFReaderBookManager sharedManager];
// 章节内容
NSString *chapterContent = [self.bookManager getChapterContent];
// 页面内容
NSString *pageContent = [self.bookManager getChapterDetailContent];
NSRange range = [chapterContent rangeOfString:pageContent];
if (self.isCover) {// 封面不能添加书签
self.markStatus = NO;
} else {
// 获取当前章节所有的书签
NSArray<TFBookMarkModel *> *markArr = [TFReadRecordManager bookMark:[TFUtilsHelper formatStringWithInteger:self.bookManager.book_id] chapterID:[TFUtilsHelper formatStringWithInteger:self.bookManager.chapter_id]];
for (TFBookMarkModel *t_model in markArr) {
if (t_model.specificIndex >= range.location && t_model.specificIndex < (range.location + range.length)) {
self.markModel = t_model;
self.markStatus = YES;
break;
}
}
}
}
- (void)createSubviews {
self.backgroundColor = kBlackTransparentColor;
[kMainWindow addSubview:self];
[self mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(kMainWindow);
}];
UIView *emptyView = [[UIView alloc] init];
self.emptyView = emptyView;
emptyView.backgroundColor = [UIColor clearColor];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hide)];
[emptyView addGestureRecognizer:tap];
[self addSubview:emptyView];
[emptyView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self);
}];
UIView *mainView = [[UIView alloc] init];
self.mainView = mainView;
mainView.backgroundColor = [UIColor whiteColor];
mainView.frameBlock = ^(UIView * _Nonnull view) {
UIBezierPath *corner = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(12.0, 12.0)];
CAShapeLayer *layer = [CAShapeLayer layer];
layer.path = corner.CGPath;
view.layer.mask = layer;
};
[self addSubview:mainView];
[mainView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.mas_bottom);
make.left.right.equalTo(self);
make.height.mas_equalTo(100 + 25 + 25 + PUB_TABBAR_HEIGHT);
}];
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
flowLayout.estimatedItemSize = CGSizeMake(62.0, 60.0f);
CGFloat spacing = (SCREEN_WIDTH - (2 * kMargin) - (self.moreDict.count * flowLayout.estimatedItemSize.width)) / (self.moreDict.count - 1);
spacing = spacing < kMargin ? kMargin : spacing;
flowLayout.minimumInteritemSpacing = spacing;
flowLayout.minimumLineSpacing = kMargin;
UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
collectionView.dataSource = self;
collectionView.delegate = self;
[collectionView registerClass:[WXYZ_MoreCell class] forCellWithReuseIdentifier:@"Identifier"];
collectionView.backgroundColor = [UIColor clearColor];
collectionView.showsVerticalScrollIndicator = NO;
collectionView.showsHorizontalScrollIndicator = NO;
[mainView addSubview:collectionView];
[collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(mainView).offset(25);
make.left.equalTo(mainView).offset(kMargin);
make.right.equalTo(mainView).offset(- kMargin);
make.height.mas_equalTo(100);
}];
[collectionView addObserver:KEY_PATH(collectionView, contentSize) complete:^(UICollectionView * _Nonnull obj, id _Nullable oldVal, id _Nullable newVal) {
CGSize size = [newVal CGSizeValue];
[obj mas_updateConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(size.height);
}];
}];
UIButton *closeButton = [UIButton buttonWithType:UIButtonTypeSystem];
[closeButton setTitle:TFLocalizedString(@"关闭") forState:UIControlStateNormal];
[closeButton setTitleColor:kBlackColor forState:UIControlStateNormal];
[closeButton setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, PUB_TABBAR_OFFSET / 2, 0)];
closeButton.titleLabel.font = kFont16;
[mainView addSubview:closeButton];
[closeButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(PUB_TABBAR_HEIGHT);
make.left.width.equalTo(mainView);
make.top.equalTo(collectionView.mas_bottom).offset(25.0f);
}];
tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hide)];
[closeButton addGestureRecognizer:tap];
UIView *splitLine = [[UIView alloc] init];
splitLine.backgroundColor = kGrayLineColor;
[mainView addSubview:splitLine];
[splitLine mas_makeConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(0.5);
make.left.width.top.equalTo(closeButton);
}];
[self setNeedsLayout];
[self layoutIfNeeded];
}
- (void)show {
[UIView animateWithDuration:kAnimatedDuration animations:^{
[self.mainView mas_updateConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.mas_bottom).offset(- CGRectGetHeight(self.mainView.frame));
}];
[self.mainView.superview layoutIfNeeded];
}];
}
- (void)hide {
[UIView animateWithDuration:kAnimatedDuration animations:^{
[self.mainView mas_updateConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.mas_bottom);
}];
[self.mainView.superview layoutIfNeeded];
} completion:^(BOOL finished) {
if (finished) {
[self removeFromSuperview];
[[WXYZ_BookReaderMenuBar sharedManager] hiddend];
}
}];
}
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
CGPoint emptyPoint = [self convertPoint:point toView:self.mainView];
if ([self.mainView pointInside:emptyPoint withEvent:event]) {
return [super hitTest:point withEvent:event];
} else {
return self.emptyView;
}
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.moreDict.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
WXYZ_MoreCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Identifier" forIndexPath:indexPath];
[cell setDict:self.moreDict[indexPath.row].allValues.firstObject];
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
NSDictionary *dict = self.moreDict[indexPath.row];
ReaderBookStyle style = [dict.allKeys.firstObject integerValue];
switch (style) {
case ReaderBookDetail:
{
[self hide];
UINavigationController *nav = [TFViewHelper getCurrentNavigationController];
if (nav.viewControllers.count > 1) {
UIViewController *tmp = nav.viewControllers[nav.viewControllers.count - 2];
if ([tmp isKindOfClass:TFNovelDetailViewController.class]) {
[nav popViewControllerAnimated:YES];
return;
}
}
[[NSNotificationCenter defaultCenter] postNotificationName:NSNotification_Reader_Push object:@"TFNovelDetailViewController"];
}
break;
case ReaderBookMark:
{
if (self.isCover) {
[TFPromptManager showPromptViewWithStatus:TFPromptStatusError promptTitle:TFLocalizedString(@"当前页不支持加书签")];
return;
}
NSString *pageContent = [self.bookManager getChapterDetailContent];
if ([pageContent isEqualToString:@"\uFFFC"]) {
[TFPromptManager showPromptViewWithStatus:TFPromptStatusError promptTitle:TFLocalizedString(@"当前页不支持加书签")];
return;
}
NSDictionary *dict = nil;
if (self.markStatus) {
// 移除书签
[TFReadRecordManager removeBookMark:self.markModel];
dict = @{@(ReaderBookMark) : @{TFLocalizedString(@"添加书签") : @"book_add_mark"}};
self.markStatus = NO;
} else {
// 添加书签
self.markModel = [TFReadRecordManager addBookMark:[TFUtilsHelper formatStringWithInteger:self.bookManager.book_id] chapterID:[TFUtilsHelper formatStringWithInteger:self.bookManager.chapter_id] chapterSort:self.bookManager.currentChapterIndex chapterTitle:[self.bookManager getChapterTitle] chapterContent:[self.bookManager getChapterContent] pageContent:pageContent];
dict = @{@(ReaderBookMark) : @{TFLocalizedString(@"取消书签") : @"book_remove_mark"}};
self.markStatus = YES;
}
NSMutableArray<NSDictionary *> *arr = [NSMutableArray arrayWithArray:self.moreDict];
[arr replaceObjectAtIndex:indexPath.row withObject:dict];
self.moreDict = [arr copy];
[collectionView reloadData];
}
break;
case ReaderBookRack:
{
if ([[TFCollectionManager shareManagerWithProductionType:TFProductionTypeNovel] isCollectedWithProductionModel:self.bookModel]) break;
if ([[TFCollectionManager shareManagerWithProductionType:TFProductionTypeNovel] addCollectionWithProductionModel:self.bookModel atIndex:0]) {
[TFUtilsHelper synchronizationRackProductionWithProduction_id:self.bookModel.production_id productionType:TFProductionTypeNovel complete:nil];
NSDictionary *dict = @{@(ReaderBookRack) : @{TFLocalizedString(@"已加入书架") : @"book_remove_rack"}};
NSMutableArray<NSDictionary *> *arr = [NSMutableArray arrayWithArray:self.moreDict];
[arr replaceObjectAtIndex:indexPath.row withObject:dict];
self.moreDict = [arr copy];
[collectionView reloadData];
} else {
[TFPromptManager showPromptViewWithStatus:TFPromptStatusError promptTitle:TFLocalizedString(@"添加失败")];
}
}
break;
case ReaderBookShare:
{
[self hide];
[[NSNotificationCenter defaultCenter] postNotificationName:NSNotification_Hidden_ToolNav object:nil];
[[NSNotificationCenter defaultCenter] postNotificationName:NSNotification_Hidden_Bottom_ToolNav object:nil];
[TFShareManager shareWithProduction_id:NSStringFromInteger(self.bookModel.production_id) chapter_id:NSStringFromInteger(self.bookManager.chapter_id) type:TFShareTypeBook];
}
break;
default:
break;
};
}
- (NSArray<NSDictionary *> *)moreDict {
if (!_moreDict) {
NSMutableArray<NSDictionary *> *arr = [NSMutableArray array];
[arr addObject:@{@(ReaderBookDetail) : @{TFLocalizedString(@"书籍详情") : @"book_detail"}}];
if (self.markStatus) {
[arr addObject:@{@(ReaderBookMark) : @{TFLocalizedString(@"取消书签") : @"book_remove_mark"}}];
} else {
[arr addObject:@{@(ReaderBookMark) : @{TFLocalizedString(@"添加书签") : @"book_add_mark"}}];
}
if ([[TFCollectionManager shareManagerWithProductionType:TFProductionTypeNovel] isCollectedWithProductionModel:self.bookModel]) {
[arr addObject:@{@(ReaderBookRack) : @{TFLocalizedString(@"已加入书架") : @"book_remove_rack"}}];
} else {
[arr addObject:@{@(ReaderBookRack) : @{TFLocalizedString(@"加入书架") : @"book_add_rack"}}];
}
[arr addObject:@{@(ReaderBookShare) : @{TFLocalizedString(@"分享") : @"book_share"}}];
_moreDict = [arr copy];
}
return _moreDict;
}
- (BOOL)isCover {
return self.bookManager.currentChapterIndex == 0 && self.bookManager.currentPagerIndex == 0;
}
@end
@implementation WXYZ_MoreCell {
UIImageView *_coverImageView;
UILabel *_titleLabel;
}
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
[self createSubviews];
}
return self;
}
- (void)createSubviews {
_coverImageView = [[UIImageView alloc] init];
_coverImageView.contentMode = UIViewContentModeScaleAspectFill;
_coverImageView.clipsToBounds = YES;
_coverImageView.userInteractionEnabled = NO;
_coverImageView.backgroundColor = [UIColor clearColor];
[self.contentView addSubview:_coverImageView];
[_coverImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.left.width.equalTo(self.contentView);
make.height.equalTo(_coverImageView.mas_width);
}];
_titleLabel = [[UILabel alloc] init];
_titleLabel.textColor = kBlackColor;
_titleLabel.font = kFont13;
_titleLabel.backgroundColor = [UIColor clearColor];
[self.contentView addSubview:_titleLabel];
[_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(_coverImageView.mas_bottom).offset(14.0f);
make.centerX.equalTo(self.contentView);
make.bottom.equalTo(self.contentView).priorityLow();
}];
}
- (void)setDict:(NSDictionary <NSString *, NSString *> *)dict {
_coverImageView.image = [UIImage imageNamed:dict.allValues.firstObject];
_titleLabel.text = dict.allKeys.firstObject ?: @"";
}
- (UICollectionViewLayoutAttributes*)preferredLayoutAttributesFittingAttributes:(UICollectionViewLayoutAttributes*)layoutAttributes {
[self setNeedsLayout];
[self layoutIfNeeded];
CGSize size = [self.contentView systemLayoutSizeFittingSize:layoutAttributes.size];
CGRect cellFrame = layoutAttributes.frame;
cellFrame.size.height = size.height;
layoutAttributes.frame = cellFrame;
return layoutAttributes;
}
@end