CPDFViewBaseController.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. //
  2. // CPDFViewBaseController.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. // This notice may not be removed from this file.
  11. //
  12. #import "CPDFViewBaseController.h"
  13. #import <ComPDFKit/ComPDFKit.h>
  14. #import <compdfkit_tools/compdfkit_tools.h>
  15. @interface CPDFViewBaseController ()<UISearchBarDelegate,CPDFViewDelegate,CPDFListViewDelegate, CSearchToolbarDelegate, CPDFDisplayViewDelegate, CPDFBOTAViewControllerDelegate,CPDFSearchResultsDelegate, CPDFThumbnailViewControllerDelegate,CPDFPopMenuViewDelegate,UIDocumentPickerDelegate,CPDFPopMenuDelegate>
  16. @property(nonatomic, strong) NSString *filePath;
  17. @property(nonatomic, strong) CPDFListView *pdfListView;
  18. @property(nonatomic, strong) CNavigationRightView *rightView;
  19. @property(nonatomic, strong) CSearchToolbar *searchToolbar;
  20. @property(nonatomic, strong) CActivityIndicatorView *loadingView;
  21. @property(nonatomic, strong) CPDFPopMenu *popMenu;
  22. @property(nonatomic, strong) UIBarButtonItem * leftBarButtonItem;
  23. @property(nonatomic, strong) NSString *password;
  24. @end
  25. @implementation CPDFViewBaseController
  26. #pragma mark - Initializers
  27. - (instancetype)initWithFilePath:(NSString *)filePath password:(nullable NSString *)password{
  28. if(self = [super init]) {
  29. self.filePath = filePath;
  30. self.password = password;
  31. }
  32. return self;
  33. }
  34. - (void)viewDidLoad {
  35. [super viewDidLoad];
  36. self.view.backgroundColor = [CPDFColorUtils CPDFViewControllerBackgroundColor];
  37. [self initWitPDFListView];
  38. [self initWitNavigation];
  39. [self initWitNavigationTitle];
  40. [self initWithSearchTool];
  41. [self reloadDocumentWithFilePath:self.filePath password:self.password completion:^(BOOL result) {
  42. }];
  43. }
  44. #pragma mark - Private method
  45. - (void)initWitPDFListView {
  46. self.pdfListView = [[CPDFListView alloc] initWithFrame:self.view.bounds];
  47. self.pdfListView.performDelegate = self;
  48. self.pdfListView.delegate = self;
  49. self.pdfListView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  50. [self.view addSubview:self.pdfListView];
  51. }
  52. - (void)initWitNavigation {
  53. UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"CPDFThunbnailImageEnter" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil] style:UIBarButtonItemStylePlain target:self action:@selector(buttonItemClicked_thumbnail:)];
  54. self.navigationItem.leftBarButtonItem = leftItem;
  55. self.leftBarButtonItem = leftItem;
  56. __block typeof(self) blockSelf = self;
  57. self.rightView = [[CNavigationRightView alloc] initWithDefaultItemsClickBack:^(NSUInteger tag) {
  58. switch (tag) {
  59. case CNavigationRightTypeSearch:
  60. [blockSelf navigationRightItemSearch];
  61. break;
  62. case CNavigationRightTypeBota:
  63. [blockSelf navigationRightItemBota];
  64. break;
  65. default:
  66. case CNavigationRightTypeMore:
  67. [blockSelf navigationRightItemMore];
  68. break;
  69. }
  70. }];
  71. }
  72. - (void)initWithSearchTool {
  73. self.searchToolbar = [[CSearchToolbar alloc] initWithPDFView:self.pdfListView];
  74. self.searchToolbar.delegate = self;
  75. }
  76. - (void)enterPDFSetting {
  77. [self.popMenu hideMenu];
  78. CPDFDisplayViewController *displayVc = [[CPDFDisplayViewController alloc] initWithPDFView:self.pdfListView];
  79. displayVc.delegate = self;
  80. AAPLCustomPresentationController *presentationController NS_VALID_UNTIL_END_OF_SCOPE;
  81. presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:displayVc presentingViewController:self];
  82. displayVc.transitioningDelegate = presentationController;
  83. [self presentViewController:displayVc animated:YES completion:nil];
  84. }
  85. - (void)enterPDFInfo {
  86. [self.popMenu hideMenu];
  87. CPDFInfoViewController * infoVc = [[CPDFInfoViewController alloc] initWithPDFView:self.pdfListView];
  88. AAPLCustomPresentationController *presentationController NS_VALID_UNTIL_END_OF_SCOPE;
  89. presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:infoVc presentingViewController:self];
  90. infoVc.transitioningDelegate = presentationController;
  91. [self presentViewController:infoVc animated:YES completion:nil];
  92. }
  93. - (void)enterPDFShare {
  94. [self.popMenu hideMenu];
  95. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  96. if(self.pdfListView.document.isModified) {
  97. [self.pdfListView.document writeToURL:self.pdfListView.document.documentURL];
  98. }
  99. dispatch_async(dispatch_get_main_queue(), ^{
  100. [self shareAction];
  101. });
  102. });
  103. }
  104. - (void)enterPDFAddFile {
  105. [self.popMenu hideMenu];
  106. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  107. if(self.pdfListView.document.isModified) {
  108. [self.pdfListView.document writeToURL:self.pdfListView.document.documentURL];
  109. }
  110. dispatch_async(dispatch_get_main_queue(), ^{
  111. NSArray *documentTypes = @[@"com.adobe.pdf"];
  112. UIDocumentPickerViewController *documentPickerViewController = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:documentTypes inMode:UIDocumentPickerModeOpen];
  113. documentPickerViewController.delegate = self;
  114. [self presentViewController:documentPickerViewController animated:YES completion:nil];
  115. });
  116. });
  117. }
  118. #pragma mark - Public method
  119. - (void)initWitNavigationTitle {
  120. CNavigationBarTitleButton * navTitleButton = [[CNavigationBarTitleButton alloc] init];
  121. self.titleButton = navTitleButton;
  122. self.navigationTitle = NSLocalizedString(@"Viewer", nil);
  123. [navTitleButton setTitle:self.navigationTitle forState:UIControlStateNormal];
  124. [navTitleButton setTitleColor:[CPDFColorUtils CAnyReverseBackgooundColor] forState:UIControlStateNormal];
  125. self.titleButton.frame = CGRectMake(0, 0, 100, 30);
  126. self.navigationItem.titleView = self.titleButton;
  127. }
  128. - (void)reloadDocumentWithFilePath:(NSString *)filePath password:(nullable NSString *)password completion:(void (^)(BOOL result))completion {
  129. [self.navigationController.view setUserInteractionEnabled:NO];
  130. if (![self.loadingView superview]) {
  131. [self.view addSubview:self.loadingView];
  132. }
  133. [self.loadingView startAnimating];
  134. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  135. NSURL *url = [NSURL fileURLWithPath:filePath];
  136. CPDFDocument *document = [[CPDFDocument alloc] initWithURL:url];
  137. if([document isLocked]) {
  138. [document unlockWithPassword:password];
  139. }
  140. dispatch_async(dispatch_get_main_queue(), ^{
  141. [self.navigationController.view setUserInteractionEnabled:YES];
  142. [self.loadingView stopAnimating];
  143. [self.loadingView removeFromSuperview];
  144. if (document.error && document.error.code != CPDFDocumentPasswordError) {
  145. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  146. [self.navigationController popViewControllerAnimated:YES];
  147. }];
  148. UIAlertController *alert = [UIAlertController alertControllerWithTitle:@""
  149. message:NSLocalizedString(@"Sorry PDF Reader Can't open this pdf file!", nil)
  150. preferredStyle:UIAlertControllerStyleAlert];
  151. [alert addAction:okAction];
  152. if (completion) {
  153. completion(NO);
  154. }
  155. } else {
  156. self.pdfListView.document = document;
  157. if (completion) {
  158. completion(YES);
  159. }
  160. }
  161. });
  162. });
  163. }
  164. - (void)setTitleRefresh {
  165. }
  166. #pragma mark - Action
  167. - (void)navigationRightItemSearch {
  168. [self.searchToolbar showInView:self.navigationController.navigationBar];
  169. self.navigationTitle = @"";
  170. self.titleButton.hidden = YES;
  171. self.navigationItem.rightBarButtonItem = nil;
  172. self.navigationItem.leftBarButtonItem = nil;
  173. }
  174. - (void)navigationRightItemBota {
  175. CPDFBOTAViewController *botaViewController = [[CPDFBOTAViewController alloc] initWithPDFView:self.pdfListView];
  176. botaViewController.delegate = self;
  177. AAPLCustomPresentationController *presentationController NS_VALID_UNTIL_END_OF_SCOPE;
  178. presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:botaViewController presentingViewController:self];
  179. botaViewController.transitioningDelegate = presentationController;
  180. [self presentViewController:botaViewController animated:YES completion:nil];
  181. }
  182. - (void)navigationRightItemMore {
  183. CPDFPopMenuView * menuView = [[CPDFPopMenuView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
  184. menuView.delegate = self;
  185. self.popMenu = [CPDFPopMenu popMenuWithContentView:menuView];
  186. self.popMenu.dimCoverLayer = YES;
  187. self.popMenu.delegate = self;
  188. if (@available(iOS 11.0, *)) {
  189. [self.popMenu showMenuInRect:CGRectMake(self.view.frame.size.width - self.view.safeAreaInsets.right - 250, CGRectGetMaxY(self.navigationController.navigationBar.frame), 250, 200)];
  190. } else {
  191. // Fallback on earlier versions
  192. [self.popMenu showMenuInRect:CGRectMake(self.view.frame.size.width - 250, CGRectGetMaxY(self.navigationController.navigationBar.frame), 250, 200)];
  193. }
  194. }
  195. - (void)buttonItemClicked_thumbnail:(id)sender {
  196. CPDFThumbnailViewController *thumbnailViewController = [[CPDFThumbnailViewController alloc] initWithPDFView:self.pdfListView];
  197. thumbnailViewController.delegate = self;
  198. AAPLCustomPresentationController *presentationController NS_VALID_UNTIL_END_OF_SCOPE;
  199. presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:thumbnailViewController presentingViewController:self];
  200. thumbnailViewController.transitioningDelegate = presentationController;
  201. [self presentViewController:thumbnailViewController animated:YES completion:nil];
  202. }
  203. #pragma mark - CPDFViewDelegate
  204. - (void)PDFViewDocumentDidLoaded:(CPDFView *)pdfView {
  205. UIBarButtonItem* rightItem = [[UIBarButtonItem alloc] initWithCustomView:self.rightView];
  206. self.navigationItem.rightBarButtonItem = rightItem;
  207. }
  208. #pragma mark - CSearchToolbarDelegate
  209. - (void)searchToolbar:(CSearchToolbar *)searchToolbar onSearchQueryResults:(NSArray *)results {
  210. if ([results count] < 1) {
  211. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", nil)
  212. style:UIAlertActionStyleCancel
  213. handler:nil];
  214. UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil
  215. message:NSLocalizedString(@"your have‘t search result", nil)
  216. preferredStyle:UIAlertControllerStyleAlert];
  217. [alert addAction:cancelAction];
  218. [self presentViewController:alert animated:YES completion:nil];
  219. return;
  220. }
  221. CPDFSearchResultsViewController* searchResultController = [[CPDFSearchResultsViewController alloc] initWithResultArray:results keyword:searchToolbar.searchKeyString document:self.pdfListView.document];
  222. searchResultController.delegate = self;
  223. AAPLCustomPresentationController *presentationController NS_VALID_UNTIL_END_OF_SCOPE;
  224. presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:searchResultController presentingViewController:self];
  225. searchResultController.transitioningDelegate = presentationController;
  226. [self presentViewController:searchResultController animated:YES completion:nil];
  227. }
  228. - (void)searchToolbarOnExitSearch:(CSearchToolbar *)searchToolbar {
  229. if([searchToolbar superview]) {
  230. [searchToolbar removeFromSuperview];
  231. UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:self.rightView];
  232. self.navigationItem.rightBarButtonItem = rightItem;
  233. self.title = self.navigationTitle;
  234. self.navigationItem.leftBarButtonItem = self.leftBarButtonItem;
  235. }
  236. self.navigationTitle = NSLocalizedString(@"Viewer",nil);
  237. self.titleButton.hidden = NO;
  238. }
  239. #pragma mark - CPDFDisplayViewDelegate
  240. - (void)displayViewControllerDismiss:(CPDFDisplayViewController *)displayViewController {
  241. [self.navigationController popToRootViewControllerAnimated:YES];
  242. }
  243. #pragma mark - CPDFBOTAViewControllerDelegate
  244. - (void)botaViewControllerDismiss:(CPDFBOTAViewController *)botaViewController {
  245. [self.navigationController dismissViewControllerAnimated:YES completion:nil];
  246. }
  247. #pragma mark - CPDFSearchResultsDelegate
  248. - (void)searchResultsView:(CPDFSearchResultsViewController *)resultVC forSelection:(CPDFSelection *)selection indexPath:(NSIndexPath *)indexPath {
  249. NSInteger pageIndex = [self.pdfListView.document indexForPage:selection.page];
  250. [self.pdfListView goToPageIndex:pageIndex animated:NO];
  251. [self.pdfListView setHighlightedSelection:selection animated:YES];
  252. }
  253. - (void)searchResultsViewControllerDismiss:(CPDFSearchResultsViewController *)searchResultsViewController {
  254. [self.navigationController popViewControllerAnimated:YES];
  255. }
  256. #pragma mark - CPDFPopMenuDelegate
  257. - (void)menuDidClosedIn:(CPDFPopMenu *)menu isClosed:(BOOL)isClosed {
  258. }
  259. #pragma mark - CPDFThumbnailViewControllerDelegate
  260. - (void)thumbnailViewController:(CPDFThumbnailViewController *)thumbnailViewController pageIndex:(NSInteger)pageIndex {
  261. [self.pdfListView goToPageIndex:pageIndex animated:NO];
  262. [self.navigationController popViewControllerAnimated:YES];
  263. }
  264. #pragma mark - CPDFMenuViewdelegate
  265. - (void)menuDidClickAtView:(CPDFPopMenuView *)view clickType:(CPDFPopMenuViewType)viewType {
  266. switch (viewType) {
  267. case CPDFPopMenuViewTypeSetting: {
  268. [self enterPDFSetting];
  269. }
  270. break;
  271. case CPDFPopMenuViewTypeInfo:{
  272. [self enterPDFInfo];
  273. }
  274. break;
  275. case CPDFPopMenuViewTypeShare: {
  276. [self enterPDFShare];
  277. }
  278. break;
  279. case CPDFPopMenuViewTypeAddFile: {
  280. [self enterPDFAddFile];
  281. }
  282. default:
  283. break;
  284. }
  285. }
  286. - (void)shareAction {
  287. UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:@[self.pdfListView.document.documentURL] applicationActivities:nil];
  288. activityVC.definesPresentationContext = YES;
  289. [self presentViewController:activityVC animated:YES completion:nil];
  290. activityVC.completionWithItemsHandler = ^(UIActivityType _Nullable activityType, BOOL completed, NSArray * _Nullable returnedItems, NSError * _Nullable activityError) {
  291. if (completed) {
  292. NSLog(@"Success!");
  293. } else {
  294. NSLog(@"Failed Or Canceled!");
  295. }
  296. };
  297. }
  298. #pragma mark - UIDocumentPickerDelegate
  299. - (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentsAtURLs:(NSArray<NSURL *> *)urls{
  300. __weak typeof(self) weakSelf = self;
  301. BOOL fileUrlAuthozied = [urls.firstObject startAccessingSecurityScopedResource];
  302. if(fileUrlAuthozied){
  303. NSFileCoordinator *fileCoordinator = [[NSFileCoordinator alloc] init];
  304. NSError *error;
  305. [fileCoordinator coordinateReadingItemAtURL:urls.firstObject options:0 error:&error byAccessor:^(NSURL *newURL) {
  306. NSString *documentFolder = [NSHomeDirectory() stringByAppendingFormat:@"/%@/%@", @"Documents",@"Files"];
  307. if (![[NSFileManager defaultManager] fileExistsAtPath:documentFolder])
  308. [[NSFileManager defaultManager] createDirectoryAtURL:[NSURL fileURLWithPath:documentFolder] withIntermediateDirectories:YES attributes:nil error:nil];
  309. NSString * documentPath = [documentFolder stringByAppendingPathComponent:[newURL lastPathComponent]];
  310. if (![[NSFileManager defaultManager] fileExistsAtPath:documentPath]) {
  311. [[NSFileManager defaultManager] copyItemAtPath:newURL.path toPath:documentPath error:NULL];
  312. }
  313. [weakSelf reloadDocumentWithFilePath:documentPath password:nil completion:^(BOOL result) {
  314. [weakSelf.pdfListView reloadInputViews];
  315. }];
  316. [self dismissViewControllerAnimated:YES completion:NULL];
  317. }];
  318. [urls.firstObject stopAccessingSecurityScopedResource];
  319. }
  320. [self setTitleRefresh];
  321. }
  322. - (void)documentPickerWasCancelled:(UIDocumentPickerViewController *)controller {
  323. [self setTitleRefresh];
  324. }
  325. @end