PDFBookmarkViewController.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. //
  2. // PDFBookmarkViewController.m
  3. // PDFReader
  4. //
  5. // Copyright © 2014-2022 PDF Technologies, Inc. All Rights Reserved.
  6. //
  7. // The PDF Reader Sample applications are licensed with a modified BSD license.
  8. // Please see License for details. This notice may not be removed from this file.
  9. //
  10. #import "PDFBookmarkViewController.h"
  11. #import "PDFFilterView.h"
  12. #import "PDFBookmarkEditViewController.h"
  13. @interface PDFBookmarkViewCell : UITableViewCell
  14. @property (nonatomic,assign) IBOutlet UILabel *indexLabel;
  15. @property (nonatomic,assign) IBOutlet UILabel *titleLabel;
  16. @property (nonatomic,assign) IBOutlet UILabel *dateLabel;
  17. @end
  18. @implementation PDFBookmarkViewCell
  19. @end
  20. @interface PDFBookmarkViewController () <UITableViewDataSource,UITableViewDelegate>
  21. @property (nonatomic,retain) NSArray *bookmarks;
  22. @property (nonatomic,retain) UITableView *tableView;
  23. @property (nonatomic,retain) UIButton *sortButton;
  24. @property (nonatomic,retain) PDFFilterView *filterView;
  25. @property (nonatomic,retain) UILabel *noDataLabel;
  26. @property (nonatomic,assign) NSInteger sortType;
  27. @end
  28. @implementation PDFBookmarkViewController
  29. #pragma mark - Init Methods
  30. - (void)dealloc {
  31. [_bookmarks release];
  32. _tableView.delegate = nil;
  33. _tableView.dataSource = nil;
  34. [_tableView release];
  35. [_sortButton release];
  36. [_filterView release];
  37. [_noDataLabel release];
  38. [super dealloc];
  39. }
  40. #pragma mark - UIViewController Methods
  41. - (void)viewDidLoad {
  42. [super viewDidLoad];
  43. // Do any additional setup after loading the view.
  44. self.tableView = [[[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain] autorelease];
  45. self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
  46. self.tableView.dataSource = self;
  47. self.tableView.delegate = self;
  48. self.tableView.rowHeight = UITableViewAutomaticDimension;
  49. self.tableView.estimatedRowHeight = 60;
  50. self.tableView.tableFooterView = [[[UIView alloc] init] autorelease];
  51. [self.tableView registerNib:[UINib nibWithNibName:@"PDFBookmarkViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
  52. [self.view addSubview:self.tableView];
  53. self.noDataLabel = [[[UILabel alloc] init] autorelease];
  54. self.noDataLabel.textColor = [UIColor grayColor];
  55. self.noDataLabel.text = NSLocalizedString(@"No bookmarks", nil);
  56. [self.noDataLabel sizeToFit];
  57. self.noDataLabel.center = CGPointMake(self.view.bounds.size.width/2.0, self.view.bounds.size.height/2.0);
  58. self.noDataLabel.autoresizingMask = UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
  59. [self.view addSubview:self.noDataLabel];
  60. UITableViewHeaderFooterView *filterView = [[[UITableViewHeaderFooterView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)] autorelease];
  61. filterView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  62. filterView.backgroundColor = [UIColor whiteColor];
  63. self.tableView.tableHeaderView = filterView;
  64. self.tableView.contentInset = UIEdgeInsetsMake(-44, 0, 0, 0);
  65. self.tableView.contentOffset = CGPointMake(0, 44);
  66. self.sortButton = [UIButton buttonWithType:UIButtonTypeSystem];
  67. self.sortButton.frame = filterView.bounds;
  68. self.sortButton.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
  69. [self.sortButton setTitle:NSLocalizedString(@"Filter", nil) forState:UIControlStateNormal];
  70. [self.sortButton setTitleColor:[UIColor colorWithRed:74.0/255.0 green:74.0/255.0 blue:74.0/255.0 alpha:1.0] forState:UIControlStateNormal];
  71. self.sortButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
  72. self.sortButton.titleEdgeInsets = UIEdgeInsetsMake(0, 15, 0, 0);
  73. [self.sortButton addTarget:self action:@selector(buttonItemClicked_Filter:) forControlEvents:UIControlEventTouchUpInside];
  74. [filterView.contentView addSubview:self.sortButton];
  75. UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.sortButton.bounds.size.width-31, (self.sortButton.bounds.size.height-16)/2.0, 16, 16)];
  76. imageView.image = [UIImage imageNamed:@"btn_bota_more.png"];
  77. imageView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
  78. [self.sortButton addSubview:imageView];
  79. UIView *line = [[[UIView alloc] initWithFrame:CGRectMake(0, filterView.bounds.size.height-1, filterView.bounds.size.width, 1)] autorelease];
  80. line.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleTopMargin;
  81. line.backgroundColor = [UIColor colorWithRed:230.0/255.0 green:230.0/255.0 blue:230.0/255.0 alpha:1.0];
  82. [filterView addSubview:line];
  83. }
  84. - (void)viewDidAppear:(BOOL)animated {
  85. [super viewDidAppear:animated];
  86. [self reloadData];
  87. }
  88. #pragma mark - Button Event Actions
  89. - (void)buttonItemClicked_Filter:(id)sender {
  90. if (self.filterView) {
  91. [self.filterView dismiss];
  92. self.filterView = nil;
  93. return;
  94. }
  95. NSArray *selectedIndexes = nil;
  96. if (self.sortType == 0) {
  97. selectedIndexes = @[@(0)];
  98. } else if (self.sortType == 1) {
  99. selectedIndexes = @[@(1)];
  100. } else if (self.sortType == 2) {
  101. selectedIndexes = @[@(2)];
  102. }
  103. PDFFilterView *filterView = [[PDFFilterView alloc] init];
  104. filterView.items = @[NSLocalizedString(@"Date", nil),
  105. NSLocalizedString(@"Page", nil),
  106. NSLocalizedString(@"Title", nil)];
  107. filterView.selectedIndexes = selectedIndexes;
  108. filterView.didSelected = ^(NSInteger index) {
  109. if (index >= 0) {
  110. [self setSelectedIndex:index];
  111. [filterView dismiss];
  112. }
  113. self.filterView = nil;
  114. };
  115. filterView.frame = CGRectMake(0, 44, self.view.bounds.size.width, self.view.bounds.size.height-44);
  116. [filterView showInView:self.view];
  117. [filterView release];
  118. self.filterView = filterView;
  119. }
  120. - (void)setSelectedIndex:(NSInteger)index {
  121. if (index == 0) {
  122. self.sortType = 0;
  123. [self.sortButton setTitle:NSLocalizedString(@"Date", nil) forState:UIControlStateNormal];
  124. } else if (index == 1) {
  125. self.sortType = 1;
  126. [self.sortButton setTitle:NSLocalizedString(@"Page", nil) forState:UIControlStateNormal];
  127. } else if (index == 2) {
  128. self.sortType = 2;
  129. [self.sortButton setTitle:NSLocalizedString(@"Title", nil) forState:UIControlStateNormal];
  130. }
  131. [self reloadData];
  132. }
  133. - (void)reloadData {
  134. self.bookmarks = [self.pdfViewController.pdfView.document bookmarks];
  135. NSInteger (*sortFunction)(id, id, void *) = NULL;
  136. const NSInteger bookmarkCount = [self.bookmarks count];
  137. if (self.sortType == 0) {
  138. if (bookmarkCount > 1) {
  139. sortFunction = sortByDate;
  140. }
  141. } else if (self.sortType == 1) {
  142. if (bookmarkCount > 1) {
  143. sortFunction = sortByPage;
  144. }
  145. } else if (self.sortType == 2) {
  146. if (bookmarkCount > 1) {
  147. sortFunction = sortByName;
  148. }
  149. } else {
  150. return;
  151. }
  152. if (sortFunction) {
  153. self.bookmarks = [self.bookmarks sortedArrayUsingFunction:sortFunction
  154. context:NULL];
  155. }
  156. [self.tableView reloadData];
  157. }
  158. static NSInteger sortByDate(CPDFBookmark *dict1, CPDFBookmark *dict2, void *context) {
  159. return [dict2.date compare:dict1.date];
  160. }
  161. static NSInteger sortByPage(CPDFBookmark *dict1, CPDFBookmark *dict2, void *context) {
  162. NSInteger value1 = dict1.pageIndex;
  163. NSInteger value2 = dict2.pageIndex;
  164. if (value1 < value2) {
  165. return NSOrderedAscending;
  166. } else if (value1 > value2) {
  167. return NSOrderedDescending;
  168. } else {
  169. return NSOrderedSame;
  170. }
  171. }
  172. static NSInteger sortByName(CPDFBookmark *dict1, CPDFBookmark *dict2, void *context) {
  173. return [dict1.label compare:dict2.label options:NSCaseInsensitiveSearch];
  174. }
  175. #pragma mark - UITableViewDataSource
  176. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  177. if (self.bookmarks.count > 0) {
  178. self.noDataLabel.hidden = YES;
  179. } else {
  180. self.noDataLabel.hidden = NO;
  181. }
  182. return self.bookmarks.count;
  183. }
  184. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  185. PDFBookmarkViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
  186. CPDFBookmark *bookmark = self.bookmarks[indexPath.row];
  187. cell.indexLabel.text = [NSString stringWithFormat:@"%ld", bookmark.pageIndex+1];
  188. cell.titleLabel.text = bookmark.label;
  189. NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
  190. formatter.dateFormat = @"yyyy-MM-dd hh:mm a";
  191. cell.dateLabel.text = [formatter stringFromDate:bookmark.date];
  192. cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
  193. return cell;
  194. }
  195. #pragma mark - UITableViewDelegate
  196. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
  197. CPDFBookmark *bookmark = self.bookmarks[indexPath.row];
  198. NSInteger pageIndex = bookmark.pageIndex;
  199. [self.pdfViewController.pdfView.document removeBookmarkForPageIndex:pageIndex];
  200. [self reloadData];
  201. }
  202. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  203. CPDFBookmark *bookmark = self.bookmarks[indexPath.row];
  204. NSInteger pageIndex = bookmark.pageIndex;
  205. [self dismissViewControllerAnimated:YES completion:^{
  206. [self.pdfViewController.pdfView goToPageIndex:pageIndex animated:NO];
  207. }];
  208. }
  209. - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
  210. CPDFBookmark *bookmark = self.bookmarks[indexPath.row];
  211. PDFBookmarkEditViewController *vc = [[PDFBookmarkEditViewController alloc] init];
  212. vc.text = bookmark.label;
  213. vc.callback = ^(NSString *text) {
  214. bookmark.label = text;
  215. [self reloadData];
  216. };
  217. [self.navigationController pushViewController:vc animated:YES];
  218. [vc release];
  219. }
  220. #pragma mark - UIScrollViewDelegate
  221. - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
  222. if (scrollView == self.tableView) {
  223. CGFloat sectionHeaderHeight = 44;
  224. if (scrollView.contentOffset.y <= sectionHeaderHeight &&
  225. scrollView.contentOffset.y >= 0) {
  226. scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
  227. } else if (scrollView.contentOffset.y >= sectionHeaderHeight) {
  228. scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
  229. } else if (scrollView.contentOffset.y <= 0) {
  230. scrollView.contentOffset = CGPointMake(0, 0);
  231. }
  232. }
  233. }
  234. - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
  235. if (scrollView == self.tableView) {
  236. CGFloat sectionHeaderHeight = 44;
  237. if (scrollView.contentOffset.y >= 0 &&
  238. scrollView.contentOffset.y <= sectionHeaderHeight/2.0) {
  239. [UIView animateWithDuration:0.3 animations:^{
  240. scrollView.contentOffset = CGPointMake(0, 0);
  241. }];
  242. } else if (scrollView.contentOffset.y >= sectionHeaderHeight/2.0 &&
  243. scrollView.contentOffset.y <= sectionHeaderHeight) {
  244. [UIView animateWithDuration:0.3 animations:^{
  245. scrollView.contentOffset = CGPointMake(0, sectionHeaderHeight);
  246. }];
  247. }
  248. }
  249. }
  250. @end