|
@@ -0,0 +1,207 @@
|
|
|
+//
|
|
|
+// CPDFBookmarkViewController.m
|
|
|
+// compdfkit-tools
|
|
|
+//
|
|
|
+// Copyright © 2014-2023 PDF Technologies, Inc. All Rights Reserved.
|
|
|
+//
|
|
|
+// THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
|
|
|
+// AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
|
|
|
+// UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
|
|
|
+//
|
|
|
+
|
|
|
+#import "CPDFBookmarkViewController.h"
|
|
|
+#import "CPDFBookmarkViewCell.h"
|
|
|
+
|
|
|
+#import <ComPDFKit/ComPDFKit.h>
|
|
|
+
|
|
|
+@interface CPDFBookmarkViewController () <UITableViewDelegate, UITableViewDataSource>
|
|
|
+
|
|
|
+@property (nonatomic, strong) NSArray *bookmarks;
|
|
|
+
|
|
|
+@property (nonatomic, strong) UITableView *tableView;
|
|
|
+
|
|
|
+@property (nonatomic, strong) UILabel *noDataLabel;
|
|
|
+
|
|
|
+@property (nonatomic, strong) UIButton *addBookmarkBtn;
|
|
|
+
|
|
|
+@end
|
|
|
+
|
|
|
+@implementation CPDFBookmarkViewController
|
|
|
+
|
|
|
+#pragma mark - Initializers
|
|
|
+
|
|
|
+- (instancetype)initWithPDFView:(CPDFView *)pdfView {
|
|
|
+ if (self = [super init]) {
|
|
|
+ _pdfView = pdfView;
|
|
|
+ }
|
|
|
+ return self;
|
|
|
+}
|
|
|
+
|
|
|
+#pragma mark - UIViewController Methods
|
|
|
+
|
|
|
+- (void)viewDidLoad {
|
|
|
+ [super viewDidLoad];
|
|
|
+ // Do any additional setup after loading the view.
|
|
|
+ _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
|
|
|
+ _tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
|
|
|
+ _tableView.delegate = self;
|
|
|
+ _tableView.dataSource = self;
|
|
|
+ _tableView.rowHeight = UITableViewAutomaticDimension;
|
|
|
+ _tableView.estimatedRowHeight = 60;
|
|
|
+ _tableView.tableFooterView = [[UIView alloc] init];
|
|
|
+// [_tableView setEditing:YES animated:YES];
|
|
|
+ [self.view addSubview:self.tableView];
|
|
|
+
|
|
|
+ _noDataLabel = [[UILabel alloc] init];
|
|
|
+ _noDataLabel.textColor = [UIColor grayColor];
|
|
|
+ _noDataLabel.text = NSLocalizedString(@"No bookmark", nil);
|
|
|
+ [_noDataLabel sizeToFit];
|
|
|
+ _noDataLabel.center = CGPointMake(self.view.bounds.size.width/2.0, self.view.bounds.size.height/2.0);
|
|
|
+ _noDataLabel.autoresizingMask = UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
|
|
|
+ [self.view addSubview:self.noDataLabel];
|
|
|
+
|
|
|
+ _addBookmarkBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width - self.view.safeAreaInsets.right - 60, self.view.frame.size.height - self.view.safeAreaInsets.bottom - 60, 60, 60)];
|
|
|
+ _addBookmarkBtn.layer.cornerRadius = 30;
|
|
|
+ _addBookmarkBtn.layer.masksToBounds = YES;
|
|
|
+ _addBookmarkBtn.layer.borderWidth = 1.0;
|
|
|
+ _addBookmarkBtn.layer.borderColor = [UIColor blueColor].CGColor;
|
|
|
+ _addBookmarkBtn.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin;
|
|
|
+ [_addBookmarkBtn setImage:[UIImage imageNamed:@"CPDFBookmarkImageAdd" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil] forState:UIControlStateNormal];
|
|
|
+ [_addBookmarkBtn addTarget:self action:@selector(buttonItemClicked_add:) forControlEvents:UIControlEventTouchUpInside];
|
|
|
+ [self.view addSubview:self.addBookmarkBtn];
|
|
|
+
|
|
|
+ [self createGestureRecognizer];
|
|
|
+}
|
|
|
+
|
|
|
+- (void)viewDidAppear:(BOOL)animated {
|
|
|
+ [super viewDidAppear:animated];
|
|
|
+
|
|
|
+ [self reloadData];
|
|
|
+}
|
|
|
+
|
|
|
+#pragma mark - Private Methods
|
|
|
+
|
|
|
+- (void)reloadData {
|
|
|
+ if ([self.pdfView.document bookmarks].count > 0) {
|
|
|
+ self.bookmarks = [self.pdfView.document bookmarks];
|
|
|
+ [self.tableView reloadData];
|
|
|
+ self.tableView.hidden = NO;
|
|
|
+ self.noDataLabel.hidden = YES;
|
|
|
+ } else {
|
|
|
+ self.tableView.hidden = YES;
|
|
|
+ self.noDataLabel.hidden = NO;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+- (void)createGestureRecognizer {
|
|
|
+ [self.addBookmarkBtn setUserInteractionEnabled:YES];
|
|
|
+
|
|
|
+ UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panaddBookmarkBtn:)];
|
|
|
+ [self.addBookmarkBtn addGestureRecognizer:panRecognizer];
|
|
|
+}
|
|
|
+
|
|
|
+- (void)panaddBookmarkBtn:(UIPanGestureRecognizer *)recognizer {
|
|
|
+ CGPoint point = [recognizer translationInView:_tableView];
|
|
|
+ CGRect documentFrame = _tableView.frame;
|
|
|
+ documentFrame.origin.x -= _tableView.frame.origin.x;
|
|
|
+ documentFrame.origin.y -= _tableView.frame.origin.y;
|
|
|
+
|
|
|
+ [_addBookmarkBtn setCenter:CGPointMake(_addBookmarkBtn.center.x + point.x, _addBookmarkBtn.center.y + point.y)];
|
|
|
+
|
|
|
+ if (!CGRectContainsRect(documentFrame,_addBookmarkBtn.frame)) {
|
|
|
+ [_addBookmarkBtn setCenter:CGPointMake(_addBookmarkBtn.center.x - point.x, _addBookmarkBtn.center.y - point.y)];
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+#pragma mark - Action
|
|
|
+
|
|
|
+- (void)buttonItemClicked_add:(id)sender {
|
|
|
+ if (![self.pdfView.document bookmarkForPageIndex:self.pdfView.currentPageIndex]) {
|
|
|
+ [self.pdfView.document addBookmark:[NSString stringWithFormat:@"my page %lu", self.pdfView.currentPageIndex+1] forPageIndex:self.pdfView.currentPageIndex];
|
|
|
+ [self reloadData];
|
|
|
+ } else {
|
|
|
+ UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", nil)
|
|
|
+ style:UIAlertActionStyleCancel
|
|
|
+ handler:nil];
|
|
|
+ UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil
|
|
|
+ message:NSLocalizedString(@"You've bookmarked about this", nil)
|
|
|
+ preferredStyle:UIAlertControllerStyleAlert];
|
|
|
+ [alert addAction:cancelAction];
|
|
|
+
|
|
|
+ [self presentViewController:alert animated:YES completion:nil];
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+#pragma mark - UITableViewDataSource
|
|
|
+
|
|
|
+- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
|
|
|
+ if (self.bookmarks.count > 0) {
|
|
|
+ self.noDataLabel.hidden = YES;
|
|
|
+ } else {
|
|
|
+ self.noDataLabel.hidden = NO;
|
|
|
+ }
|
|
|
+ return self.bookmarks.count;
|
|
|
+}
|
|
|
+
|
|
|
+- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
|
+ CPDFBookmarkViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
|
|
|
+ if (cell == nil) {
|
|
|
+ cell = [[CPDFBookmarkViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
|
|
|
+ }
|
|
|
+
|
|
|
+ CPDFBookmark *bookmark = self.bookmarks[indexPath.row];
|
|
|
+ cell.pageIndexLabel.text = [NSString stringWithFormat:@"page %ld", bookmark.pageIndex+1];
|
|
|
+ cell.titleLabel.text = bookmark.label;
|
|
|
+
|
|
|
+ return cell;
|
|
|
+}
|
|
|
+
|
|
|
+#pragma mark - UITableViewDelegate
|
|
|
+
|
|
|
+- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
|
+ return YES;
|
|
|
+}
|
|
|
+
|
|
|
+- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
|
+ UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"delete" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
|
|
|
+ CPDFBookmarkViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
|
|
|
+ [self.pdfView.document removeBookmarkForPageIndex:cell.pageIndexLabel.text.floatValue - 1];
|
|
|
+ NSMutableArray *bookmarks = (NSMutableArray *)self.bookmarks;
|
|
|
+ [bookmarks removeObjectAtIndex:indexPath.row];
|
|
|
+ [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:YES];
|
|
|
+ [self reloadData];
|
|
|
+ }];
|
|
|
+
|
|
|
+ UITableViewRowAction *editAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"edit" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
|
|
|
+ UIAlertController *alert = [UIAlertController alertControllerWithTitle:@" " message:nil preferredStyle:UIAlertControllerStyleAlert];
|
|
|
+ [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {}];
|
|
|
+
|
|
|
+ UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil) style:UIAlertActionStyleCancel handler:nil];
|
|
|
+
|
|
|
+ UIAlertAction *addAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Add", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
|
|
|
+ CPDFBookmarkViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
|
|
|
+ cell.titleLabel.text = alert.textFields.firstObject.text;
|
|
|
+ }];
|
|
|
+
|
|
|
+ [alert addAction:cancelAction];
|
|
|
+ [alert addAction:addAction];
|
|
|
+
|
|
|
+ [self presentViewController:alert animated:YES completion:nil];
|
|
|
+ }];
|
|
|
+
|
|
|
+ deleteAction.backgroundColor = [UIColor redColor];
|
|
|
+ editAction.backgroundColor = [UIColor blueColor];
|
|
|
+ return @[deleteAction, editAction];
|
|
|
+}
|
|
|
+
|
|
|
+- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
|
+ return UITableViewCellEditingStyleDelete;
|
|
|
+}
|
|
|
+
|
|
|
+- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
|
+ if ([self.delegate respondsToSelector:@selector(boomarkViewController:pageIndex:)]) {
|
|
|
+ [self.delegate boomarkViewController:self pageIndex:indexPath.row + 1];
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+@end
|