123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310 |
- //
- // CPDFAnnotationViewController.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 "CPDFAnnotationViewController.h"
- #import "CAnnotListHeaderInSection.h"
- #import "CPDFAnnotationListCell.h"
- #import "CActivityIndicatorView.h"
- #import <ComPDFKit/ComPDFKit.h>
- @interface CPDFAnnotationViewController ()<UITableViewDelegate, UITableViewDataSource>
- @property (nonatomic, strong) UITableView* tableView;
- @property (nonatomic, strong) NSMutableArray* sequenceList;
- @property (nonatomic, strong) NSMutableDictionary* annotsDict;
- @property (nonatomic, strong) NSMutableDictionary* totalAnnotlistDict;
- @property (nonatomic, strong) NSMutableArray *selectIndexArray;
- @property (nonatomic, strong) UILabel* emptyLabel;
- @property (nonatomic, strong) CPDFView *pdfView;
- @property (nonatomic, strong) CActivityIndicatorView* activityView;
- @property (nonatomic,assign) BOOL stopLoadAnnots;
- @end
- @implementation CPDFAnnotationViewController
- - (instancetype)initWithPDFView:(CPDFView *)pdfView {
- if (self = [super init]) {
- self.pdfView = pdfView;
- }
- return self;
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
-
- _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
- _tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
- _tableView.delegate = self;
- _tableView.dataSource = self;
- _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
- _tableView.rowHeight = UITableViewAutomaticDimension;
- _tableView.estimatedRowHeight = 60;
- _tableView.tableFooterView = [[UIView alloc] init];
- [self.view addSubview:self.tableView];
-
- self.emptyLabel.hidden = YES;
- // Do any additional setup after loading the view.
- }
- - (void)viewWillAppear:(BOOL)animated {
- [super viewWillAppear:animated];
-
- self.stopLoadAnnots = NO;
-
- [self.activityView startAnimating];
-
- [self loadAndRefreshAnnots];
- }
- - (void)viewWillDisappear:(BOOL)animated {
- [super viewWillDisappear:animated];
-
- self.stopLoadAnnots = YES;
- }
- #pragma mark - Asset
- - (UILabel *)emptyLabel {
- if (!_emptyLabel) {
- _emptyLabel = [[UILabel alloc] init];
- if (@available(iOS 13.0, *)) {
- [_emptyLabel setTextColor:[UIColor labelColor]];
- } else {
- [_emptyLabel setTextColor:[UIColor blackColor]];
- }
- _emptyLabel.font = [UIFont boldSystemFontOfSize:16.0];
- _emptyLabel.text = NSLocalizedString(@"No annotations", nil);
- [_emptyLabel sizeToFit];
- [self.view addSubview:_emptyLabel];
- [self.view bringSubviewToFront:_emptyLabel];
- _emptyLabel.frame = CGRectMake((self.view.frame.size.width - _emptyLabel.frame.size.width)/2, (self.view.frame.size.height - _emptyLabel.frame.size.height)/2, _emptyLabel.frame.size.width, _emptyLabel.frame.size.height);
- _emptyLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
- }
- return _emptyLabel;
- }
- - (CActivityIndicatorView *)activityView {
- if (!_activityView) {
- _activityView = [[CActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
- _activityView.center = self.view.center;
- _activityView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
- }
- return _activityView;
- }
- - (void)loadAndRefreshAnnots {
- self.stopLoadAnnots = NO;
- self.totalAnnotlistDict = [[NSMutableDictionary alloc] init];
- self.annotsDict = [[NSMutableDictionary alloc] init];
- self.sequenceList = [[NSMutableArray alloc] init];
-
- [self.activityView startAnimating];
-
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
- NSInteger pageCount = self.pdfView.document.pageCount;
- NSInteger currentPage = 0;
- for (NSInteger i=0; i < pageCount; i++) {
- if (self.stopLoadAnnots) {
- break;
- }
-
- currentPage = i;
- CPDFPage *page = [self.pdfView.document pageAtIndex:i];
- NSArray *annotations = page.annotations;
- NSMutableArray *annotsInpage = [NSMutableArray array];
- for (CPDFAnnotation *annotation in annotations) {
- if (![annotation isKindOfClass:[CPDFWidgetAnnotation class]] &&
- ![annotation isKindOfClass:[CPDFLinkAnnotation class]] &&
- ![annotation isKindOfClass:[CPDFSignatureAnnotation class]]) {
- [annotsInpage addObject:annotation];
- }
- }
- if ([annotsInpage count] > 0) {
- NSArray *sortArray = [self annotSort:(NSArray *)annotsInpage];
- if (sortArray) {
- [self.totalAnnotlistDict setObject:[NSMutableArray arrayWithArray:sortArray] forKey:[NSNumber numberWithInteger:i]];
- [self.sequenceList addObject:[NSNumber numberWithInteger:i]];
- }
- }
-
- if (currentPage == pageCount - 1) {
- self.stopLoadAnnots = YES;
- }
- }
- [self.totalAnnotlistDict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
- [self.annotsDict setObject:[NSMutableArray arrayWithArray:obj] forKey:key];
- }];
- dispatch_async(dispatch_get_main_queue(), ^{
- [self.activityView stopAnimating];
- [self.tableView reloadData];
- });
- });
- }
- - (NSMutableArray *)selectIndexArray {
- if (!_selectIndexArray) {
- _selectIndexArray = [[NSMutableArray alloc] init];
- }
- return _selectIndexArray;
- }
- static NSInteger sortByDate(CPDFAnnotation *annot1, CPDFAnnotation *annot2, void *context) {
- return NSOrderedAscending;
- }
- - (NSArray *)annotSort:(NSArray *)array {
- NSArray *result;
- NSInteger (*sortFunction)(id, id, void *) = NULL;
- sortFunction = sortByDate;
-
- if (sortFunction) {
- result = [array sortedArrayUsingFunction:sortFunction context:NULL];
- } else {
- result = array;
- }
-
- return result;
- }
- #pragma mark - tableView Delegate
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
- NSInteger count = [self.sequenceList count];
- if (count < 1) {
- self.emptyLabel.hidden = NO;
- } else {
- self.emptyLabel.hidden = YES;
- }
- return count;
- }
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
- if ([self.sequenceList count] == section)
- return 1;
-
- if (section >= self.sequenceList.count) {
- return 0;
- }
- NSNumber *key = [self.sequenceList objectAtIndex:section];
- NSArray *val = [self.annotsDict objectForKey:key];
-
- return [val count];
- }
- - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
- if ([self.sequenceList count] == section) {
- return 0;
- }
- return 44.0;
- }
- - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- if ([self.sequenceList count] == indexPath.section){
- return 44;
- }
-
- NSNumber *key = [self.sequenceList objectAtIndex:indexPath.section];
- NSArray *val = [self.annotsDict objectForKey:key];
-
- CPDFAnnotation *annot = [val objectAtIndex:indexPath.row];
- if ([annot isKindOfClass:[CPDFMarkupAnnotation class]]){
- return 44 + 60;
-
- }else if (([annot contents] && ![[annot contents] isEqualToString:@""])){
- return 44 + 60;
- }else{
- return 40;
- }
- }
- - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
- if ([self.sequenceList count] == section)
- return nil;
-
- if (section >= self.sequenceList.count) {
- return nil;
- }
-
- NSNumber *key = [self.sequenceList objectAtIndex:section];
- NSArray *val = [self.annotsDict objectForKey:key];
-
- CAnnotListHeaderInSection *headerView = [[CAnnotListHeaderInSection alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 44.0)];
- [headerView setPageNumber:[key integerValue] + 1];
- [headerView setAnnotsCount:[val count]];
-
- return headerView;
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- CPDFAnnotationListCell *cell = (CPDFAnnotationListCell*)[tableView dequeueReusableCellWithIdentifier:@"cell"];
- if (cell == nil) {
- cell = [[CPDFAnnotationListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
- }
- [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
- if ([self.sequenceList count] == indexPath.section){
-
- }else{
- NSNumber *key = [self.sequenceList objectAtIndex:indexPath.section];
- NSArray *val = [self.annotsDict objectForKey:key];
- CPDFAnnotation *annot = [val objectAtIndex:indexPath.row];
-
- [cell updateCellWithAnnotation:annot];
- }
- return cell;
- }
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
- if ([self.sequenceList count] == indexPath.section){
- return;
- }
-
- NSNumber *key = [self.sequenceList objectAtIndex:indexPath.section];
- NSArray *val = [self.annotsDict objectForKey:key];
- CPDFAnnotation *annot = [val objectAtIndex:indexPath.row];
-
- if ([self.delegate respondsToSelector:@selector(annotationViewController:jumptoPage:selectAnnot:)]) {
- [self.delegate annotationViewController:self jumptoPage:[key integerValue] selectAnnot:annot];
- }
- }
- - (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
- UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"Delete" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
-
- NSNumber *key = [self.sequenceList objectAtIndex:indexPath.section];
- NSArray *val = [self.annotsDict objectForKey:key];
- CPDFAnnotation *annot = [val objectAtIndex:indexPath.row];
- [annot.page removeAnnotation:annot];
- [self.pdfView setNeedsDisplayForPage:annot.page];
- [self loadAndRefreshAnnots];
- }];
-
-
- deleteAction.backgroundColor = [UIColor redColor];
- return @[deleteAction];
- }
- @end
|