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.
 

146 lines
5.4 KiB

//
// TFNovelBookMarkViewController.m
// TFReader
//
// Created by 谢腾飞 on 2020/12/15.
// Copyright © 2020 xtfei_2011@126.com. All rights reserved.
//
#import "TFNovelBookMarkViewController.h"
#import "TFBookMarkModel.h"
#import "NSObject+Observer.h"
#import "TFReadRecordManager.h"
#import "TFReaderBookManager.h"
#import "TFReaderSettingHelper.h"
#import "NSString+TFExtension.h"
#import "TYTextContainer.h"
#import "NSAttributedString+TReaderPage.h"
#import "TFReadNovelViewController.h"
#import "TFNovelBookMarkViewCell.h"
@interface TFNovelBookMarkViewController ()<UITableViewDataSource, UITableViewDelegate>
@end
@implementation TFNovelBookMarkViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self createSubviews];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSMutableArray<TFBookMarkModel *> *t_arr = [[TFReadRecordManager bookMark:[TFUtilsHelper formatStringWithInteger:self.bookModel.production_id]] mutableCopy];
t_arr = [[t_arr sortedArrayUsingComparator:^NSComparisonResult(TFBookMarkModel * _Nonnull obj1, TFBookMarkModel * _Nonnull obj2) {
if ([obj1.timestamp integerValue] < [obj2.timestamp integerValue]) {
return NSOrderedDescending;
}
return NSOrderedAscending;
}] mutableCopy];
NSSortDescriptor *sort1 = [NSSortDescriptor sortDescriptorWithKey:KEY_PATH(t_arr.firstObject, chapterSort) ascending:YES];
t_arr = [[t_arr sortedArrayUsingDescriptors:@[sort1]] mutableCopy];
self.dataSourceArray = t_arr;
[self.mainTableView reloadData];
[self.mainTableView xtfei_endLoading];
}
- (void)createSubviews {
[self setBasicLayout];
[self.view addSubview:self.mainTableView];
self.mainTableView.delegate = self;
self.mainTableView.dataSource = self;
self.mainTableView.contentInset = UIEdgeInsetsMake(0, 0, PUB_TABBAR_OFFSET, 0);
[self.mainTableView registerClass:[TFNovelBookMarkViewCell class] forCellReuseIdentifier:@"Identifier"];
[self.mainTableView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.view);
}];
[self setEmptyOnView:self.mainTableView title:TFLocalizedString(@"暂无书签记录") buttonTitle:@"" tapBlock:^{}];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.dataSourceArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
TFNovelBookMarkViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Identifier" forIndexPath:indexPath];
[cell setBookMarkModel:self.dataSourceArray[indexPath.row]];
return cell;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleDelete;
}
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
return TFLocalizedString(@"删除");
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
TFBookMarkModel *t_model = self.dataSourceArray[indexPath.row];
[self.dataSourceArray removeObjectAtIndex:indexPath.row];
if (@available(iOS 11.0, *)) {
[tableView performBatchUpdates:^{
[tableView deleteRowAtIndexPath:indexPath withRowAnimation:UITableViewRowAnimationLeft];
} completion:^(BOOL finished) {
[TFReadRecordManager removeBookMark:t_model];
[tableView xtfei_endLoading];
}];
} else {
[CATransaction begin];
[CATransaction setCompletionBlock:^{
[TFReadRecordManager removeBookMark:t_model];
[tableView xtfei_endLoading];
}];
[tableView beginUpdates];
[tableView deleteRowAtIndexPath:indexPath withRowAnimation:UITableViewRowAnimationLeft];
[tableView endUpdates];
[CATransaction commit];
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
TFBookMarkModel *t_model = self.dataSourceArray[indexPath.row];
if (self.isReader) {
[[NSNotificationCenter defaultCenter] postNotificationName:NSNOtification_Book_Mark object:@{[NSString stringWithFormat:@"%zd", t_model.chapterSort] : [NSString stringWithFormat:@"%zd", t_model.specificIndex]}];
[self.navigationController popViewControllerAnimated:YES];
} else {
TFReadNovelViewController *vc = [[TFReadNovelViewController alloc] initWithSpecificIndex:t_model.specificIndex chapterSort:t_model.chapterSort];
vc.bookModel = self.bookModel;
vc.book_id = self.bookModel.production_id;
[self.navigationController pushViewController:vc animated:YES];
}
}
//section头部间距
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return kHalfMargin;
}
//section头部视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, kHalfMargin)];
view.backgroundColor = [UIColor clearColor];
return view;
}
- (void)setBasicLayout {
[self hiddenNavigationBar:YES];
}
@end