CPDFAnnotationViewController.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. //
  2. // CPDFAnnotationViewController.m
  3. // compdfkit-tools
  4. //
  5. // Copyright © 2014-2023 PDF Technologies, Inc. All Rights Reserved.
  6. //
  7. // THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
  8. // AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
  9. // UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
  10. //
  11. #import "CPDFAnnotationViewController.h"
  12. #import "CAnnotListHeaderInSection.h"
  13. #import "CPDFAnnotationListCell.h"
  14. #import "CActivityIndicatorView.h"
  15. #import <ComPDFKit/ComPDFKit.h>
  16. @interface CPDFAnnotationViewController ()<UITableViewDelegate, UITableViewDataSource>
  17. @property (nonatomic, strong) UITableView* tableView;
  18. @property (nonatomic, strong) NSMutableArray* sequenceList;
  19. @property (nonatomic, strong) NSMutableDictionary* annotsDict;
  20. @property (nonatomic, strong) NSMutableDictionary* totalAnnotlistDict;
  21. @property (nonatomic, strong) NSMutableArray *selectIndexArray;
  22. @property (nonatomic, strong) UILabel* emptyLabel;
  23. @property (nonatomic, strong) CPDFView *pdfView;
  24. @property (nonatomic, strong) CActivityIndicatorView* activityView;
  25. @property (nonatomic,assign) BOOL stopLoadAnnots;
  26. @end
  27. @implementation CPDFAnnotationViewController
  28. - (instancetype)initWithPDFView:(CPDFView *)pdfView {
  29. if (self = [super init]) {
  30. self.pdfView = pdfView;
  31. }
  32. return self;
  33. }
  34. - (void)viewDidLoad {
  35. [super viewDidLoad];
  36. _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
  37. _tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  38. _tableView.delegate = self;
  39. _tableView.dataSource = self;
  40. _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
  41. _tableView.rowHeight = UITableViewAutomaticDimension;
  42. _tableView.estimatedRowHeight = 60;
  43. _tableView.tableFooterView = [[UIView alloc] init];
  44. [self.view addSubview:self.tableView];
  45. self.emptyLabel.hidden = YES;
  46. // Do any additional setup after loading the view.
  47. }
  48. - (void)viewWillAppear:(BOOL)animated {
  49. [super viewWillAppear:animated];
  50. self.stopLoadAnnots = NO;
  51. [self.activityView startAnimating];
  52. [self loadAndRefreshAnnots];
  53. }
  54. - (void)viewWillDisappear:(BOOL)animated {
  55. [super viewWillDisappear:animated];
  56. self.stopLoadAnnots = YES;
  57. }
  58. #pragma mark - Asset
  59. - (UILabel *)emptyLabel {
  60. if (!_emptyLabel) {
  61. _emptyLabel = [[UILabel alloc] init];
  62. if (@available(iOS 13.0, *)) {
  63. [_emptyLabel setTextColor:[UIColor labelColor]];
  64. } else {
  65. [_emptyLabel setTextColor:[UIColor blackColor]];
  66. }
  67. _emptyLabel.font = [UIFont boldSystemFontOfSize:16.0];
  68. _emptyLabel.text = NSLocalizedString(@"No annotations", nil);
  69. [_emptyLabel sizeToFit];
  70. [self.view addSubview:_emptyLabel];
  71. [self.view bringSubviewToFront:_emptyLabel];
  72. _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);
  73. _emptyLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
  74. }
  75. return _emptyLabel;
  76. }
  77. - (CActivityIndicatorView *)activityView {
  78. if (!_activityView) {
  79. _activityView = [[CActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
  80. _activityView.center = self.view.center;
  81. _activityView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
  82. }
  83. return _activityView;
  84. }
  85. - (void)loadAndRefreshAnnots {
  86. self.stopLoadAnnots = NO;
  87. self.totalAnnotlistDict = [[NSMutableDictionary alloc] init];
  88. self.annotsDict = [[NSMutableDictionary alloc] init];
  89. self.sequenceList = [[NSMutableArray alloc] init];
  90. [self.activityView startAnimating];
  91. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  92. NSInteger pageCount = self.pdfView.document.pageCount;
  93. NSInteger currentPage = 0;
  94. for (NSInteger i=0; i < pageCount; i++) {
  95. if (self.stopLoadAnnots) {
  96. break;
  97. }
  98. currentPage = i;
  99. CPDFPage *page = [self.pdfView.document pageAtIndex:i];
  100. NSArray *annotations = page.annotations;
  101. NSMutableArray *annotsInpage = [NSMutableArray array];
  102. for (CPDFAnnotation *annotation in annotations) {
  103. if (![annotation isKindOfClass:[CPDFWidgetAnnotation class]] &&
  104. ![annotation isKindOfClass:[CPDFLinkAnnotation class]] &&
  105. ![annotation isKindOfClass:[CPDFSignatureAnnotation class]]) {
  106. [annotsInpage addObject:annotation];
  107. }
  108. }
  109. if ([annotsInpage count] > 0) {
  110. NSArray *sortArray = [self annotSort:(NSArray *)annotsInpage];
  111. if (sortArray) {
  112. [self.totalAnnotlistDict setObject:[NSMutableArray arrayWithArray:sortArray] forKey:[NSNumber numberWithInteger:i]];
  113. [self.sequenceList addObject:[NSNumber numberWithInteger:i]];
  114. }
  115. }
  116. if (currentPage == pageCount - 1) {
  117. self.stopLoadAnnots = YES;
  118. }
  119. }
  120. [self.totalAnnotlistDict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
  121. [self.annotsDict setObject:[NSMutableArray arrayWithArray:obj] forKey:key];
  122. }];
  123. dispatch_async(dispatch_get_main_queue(), ^{
  124. [self.activityView stopAnimating];
  125. [self.tableView reloadData];
  126. });
  127. });
  128. }
  129. - (NSMutableArray *)selectIndexArray {
  130. if (!_selectIndexArray) {
  131. _selectIndexArray = [[NSMutableArray alloc] init];
  132. }
  133. return _selectIndexArray;
  134. }
  135. static NSInteger sortByDate(CPDFAnnotation *annot1, CPDFAnnotation *annot2, void *context) {
  136. return NSOrderedAscending;
  137. }
  138. - (NSArray *)annotSort:(NSArray *)array {
  139. NSArray *result;
  140. NSInteger (*sortFunction)(id, id, void *) = NULL;
  141. sortFunction = sortByDate;
  142. if (sortFunction) {
  143. result = [array sortedArrayUsingFunction:sortFunction context:NULL];
  144. } else {
  145. result = array;
  146. }
  147. return result;
  148. }
  149. #pragma mark - tableView Delegate
  150. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  151. NSInteger count = [self.sequenceList count];
  152. if (count < 1) {
  153. self.emptyLabel.hidden = NO;
  154. } else {
  155. self.emptyLabel.hidden = YES;
  156. }
  157. return count;
  158. }
  159. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  160. if ([self.sequenceList count] == section)
  161. return 1;
  162. if (section >= self.sequenceList.count) {
  163. return 0;
  164. }
  165. NSNumber *key = [self.sequenceList objectAtIndex:section];
  166. NSArray *val = [self.annotsDict objectForKey:key];
  167. return [val count];
  168. }
  169. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  170. if ([self.sequenceList count] == section) {
  171. return 0;
  172. }
  173. return 44.0;
  174. }
  175. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  176. {
  177. if ([self.sequenceList count] == indexPath.section){
  178. return 44;
  179. }
  180. NSNumber *key = [self.sequenceList objectAtIndex:indexPath.section];
  181. NSArray *val = [self.annotsDict objectForKey:key];
  182. CPDFAnnotation *annot = [val objectAtIndex:indexPath.row];
  183. if ([annot isKindOfClass:[CPDFMarkupAnnotation class]]){
  184. return 44 + 60;
  185. }else if (([annot contents] && ![[annot contents] isEqualToString:@""])){
  186. return 44 + 60;
  187. }else{
  188. return 40;
  189. }
  190. }
  191. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  192. if ([self.sequenceList count] == section)
  193. return nil;
  194. if (section >= self.sequenceList.count) {
  195. return nil;
  196. }
  197. NSNumber *key = [self.sequenceList objectAtIndex:section];
  198. NSArray *val = [self.annotsDict objectForKey:key];
  199. CAnnotListHeaderInSection *headerView = [[CAnnotListHeaderInSection alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 44.0)];
  200. [headerView setPageNumber:[key integerValue] + 1];
  201. [headerView setAnnotsCount:[val count]];
  202. return headerView;
  203. }
  204. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  205. CPDFAnnotationListCell *cell = (CPDFAnnotationListCell*)[tableView dequeueReusableCellWithIdentifier:@"cell"];
  206. if (cell == nil) {
  207. cell = [[CPDFAnnotationListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
  208. }
  209. [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
  210. if ([self.sequenceList count] == indexPath.section){
  211. }else{
  212. NSNumber *key = [self.sequenceList objectAtIndex:indexPath.section];
  213. NSArray *val = [self.annotsDict objectForKey:key];
  214. CPDFAnnotation *annot = [val objectAtIndex:indexPath.row];
  215. [cell updateCellWithAnnotation:annot];
  216. }
  217. return cell;
  218. }
  219. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  220. if ([self.sequenceList count] == indexPath.section){
  221. return;
  222. }
  223. NSNumber *key = [self.sequenceList objectAtIndex:indexPath.section];
  224. NSArray *val = [self.annotsDict objectForKey:key];
  225. CPDFAnnotation *annot = [val objectAtIndex:indexPath.row];
  226. if ([self.delegate respondsToSelector:@selector(annotationViewController:jumptoPage:selectAnnot:)]) {
  227. [self.delegate annotationViewController:self jumptoPage:[key integerValue] selectAnnot:annot];
  228. }
  229. }
  230. - (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
  231. UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"Delete" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
  232. NSNumber *key = [self.sequenceList objectAtIndex:indexPath.section];
  233. NSArray *val = [self.annotsDict objectForKey:key];
  234. CPDFAnnotation *annot = [val objectAtIndex:indexPath.row];
  235. [annot.page removeAnnotation:annot];
  236. [self.pdfView setNeedsDisplayForPage:annot.page];
  237. [self loadAndRefreshAnnots];
  238. }];
  239. deleteAction.backgroundColor = [UIColor redColor];
  240. return @[deleteAction];
  241. }
  242. @end