ReaderViewController.m 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  1. //
  2. // ReaderViewController.m
  3. // Reader v2.8.6
  4. //
  5. // Created by Julius Oklamcak on 2011-07-01.
  6. // Copyright © 2011-2015 Julius Oklamcak. All rights reserved.
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining a copy
  9. // of this software and associated documentation files (the "Software"), to deal
  10. // in the Software without restriction, including without limitation the rights to
  11. // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  12. // of the Software, and to permit persons to whom the Software is furnished to
  13. // do so, subject to the following conditions:
  14. //
  15. // The above copyright notice and this permission notice shall be included in all
  16. // copies or substantial portions of the Software.
  17. //
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  19. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  22. // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  23. // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. //
  25. #import "ReaderConstants.h"
  26. #import "ReaderViewController.h"
  27. #import "ThumbsViewController.h"
  28. #import "ReaderMainToolbar.h"
  29. #import "ReaderMainPagebar.h"
  30. #import "ReaderContentView.h"
  31. #import "ReaderThumbCache.h"
  32. #import "ReaderThumbQueue.h"
  33. #import <MessageUI/MessageUI.h>
  34. @interface ReaderViewController () <UIScrollViewDelegate, UIGestureRecognizerDelegate, MFMailComposeViewControllerDelegate, UIDocumentInteractionControllerDelegate,
  35. ReaderMainToolbarDelegate, ReaderMainPagebarDelegate, ReaderContentViewDelegate, ThumbsViewControllerDelegate>
  36. @end
  37. @implementation ReaderViewController
  38. {
  39. ReaderDocument *document;
  40. UIScrollView *theScrollView;
  41. ReaderMainToolbar *mainToolbar;
  42. ReaderMainPagebar *mainPagebar;
  43. NSMutableDictionary *contentViews;
  44. UIUserInterfaceIdiom userInterfaceIdiom;
  45. NSInteger currentPage, minimumPage, maximumPage;
  46. UIDocumentInteractionController *documentInteraction;
  47. UIPrintInteractionController *printInteraction;
  48. CGFloat scrollViewOutset;
  49. CGSize lastAppearSize;
  50. NSDate *lastHideTime;
  51. BOOL ignoreDidScroll;
  52. }
  53. #pragma mark - Constants
  54. #define STATUS_HEIGHT 20.0f
  55. #define TOOLBAR_HEIGHT 44.0f
  56. #define PAGEBAR_HEIGHT 48.0f
  57. #define SCROLLVIEW_OUTSET_SMALL 4.0f
  58. #define SCROLLVIEW_OUTSET_LARGE 8.0f
  59. #define TAP_AREA_SIZE 48.0f
  60. #pragma mark - Properties
  61. @synthesize delegate;
  62. #pragma mark - ReaderViewController methods
  63. - (void)updateContentSize:(UIScrollView *)scrollView
  64. {
  65. CGFloat contentHeight = scrollView.bounds.size.height; // Height
  66. CGFloat contentWidth = (scrollView.bounds.size.width * maximumPage);
  67. scrollView.contentSize = CGSizeMake(contentWidth, contentHeight);
  68. }
  69. - (void)updateContentViews:(UIScrollView *)scrollView
  70. {
  71. [self updateContentSize:scrollView]; // Update content size first
  72. [contentViews enumerateKeysAndObjectsUsingBlock: // Enumerate content views
  73. ^(NSNumber *key, ReaderContentView *contentView, BOOL *stop)
  74. {
  75. NSInteger page = [key integerValue]; // Page number value
  76. CGRect viewRect = CGRectZero; viewRect.size = scrollView.bounds.size;
  77. viewRect.origin.x = (viewRect.size.width * (page - 1)); // Update X
  78. contentView.frame = CGRectInset(viewRect, scrollViewOutset, 0.0f);
  79. }
  80. ];
  81. NSInteger page = currentPage; // Update scroll view offset to current page
  82. CGPoint contentOffset = CGPointMake((scrollView.bounds.size.width * (page - 1)), 0.0f);
  83. if (CGPointEqualToPoint(scrollView.contentOffset, contentOffset) == false) // Update
  84. {
  85. scrollView.contentOffset = contentOffset; // Update content offset
  86. }
  87. [mainToolbar setBookmarkState:[document.bookmarks containsIndex:page]];
  88. [mainPagebar updatePagebar]; // Update page bar
  89. }
  90. - (void)addContentView:(UIScrollView *)scrollView page:(NSInteger)page
  91. {
  92. CGRect viewRect = CGRectZero; viewRect.size = scrollView.bounds.size;
  93. viewRect.origin.x = (viewRect.size.width * (page - 1)); viewRect = CGRectInset(viewRect, scrollViewOutset, 0.0f);
  94. NSURL *fileURL = document.fileURL; NSString *phrase = document.password; NSString *guid = document.guid; // Document properties
  95. ReaderContentView *contentView = [[ReaderContentView alloc] initWithFrame:viewRect fileURL:fileURL page:page password:phrase]; // ReaderContentView
  96. contentView.message = self; [contentViews setObject:contentView forKey:[NSNumber numberWithInteger:page]]; [scrollView addSubview:contentView];
  97. [contentView showPageThumb:fileURL page:page password:phrase guid:guid]; // Request page preview thumb
  98. }
  99. - (void)layoutContentViews:(UIScrollView *)scrollView
  100. {
  101. CGFloat viewWidth = scrollView.bounds.size.width; // View width
  102. CGFloat contentOffsetX = scrollView.contentOffset.x; // Content offset X
  103. NSInteger pageB = ((contentOffsetX + viewWidth - 1.0f) / viewWidth); // Pages
  104. NSInteger pageA = (contentOffsetX / viewWidth); pageB += 2; // Add extra pages
  105. if (pageA < minimumPage) pageA = minimumPage; if (pageB > maximumPage) pageB = maximumPage;
  106. NSRange pageRange = NSMakeRange(pageA, (pageB - pageA + 1)); // Make page range (A to B)
  107. NSMutableIndexSet *pageSet = [NSMutableIndexSet indexSetWithIndexesInRange:pageRange];
  108. for (NSNumber *key in [contentViews allKeys]) // Enumerate content views
  109. {
  110. NSInteger page = [key integerValue]; // Page number value
  111. if ([pageSet containsIndex:page] == NO) // Remove content view
  112. {
  113. ReaderContentView *contentView = [contentViews objectForKey:key];
  114. [contentView removeFromSuperview]; [contentViews removeObjectForKey:key];
  115. }
  116. else // Visible content view - so remove it from page set
  117. {
  118. [pageSet removeIndex:page];
  119. }
  120. }
  121. NSInteger pages = pageSet.count;
  122. if (pages > 0) // We have pages to add
  123. {
  124. NSEnumerationOptions options = 0; // Default
  125. if (pages == 2) // Handle case of only two content views
  126. {
  127. if ((maximumPage > 2) && ([pageSet lastIndex] == maximumPage)) options = NSEnumerationReverse;
  128. }
  129. else if (pages == 3) // Handle three content views - show the middle one first
  130. {
  131. NSMutableIndexSet *workSet = [pageSet mutableCopy]; options = NSEnumerationReverse;
  132. [workSet removeIndex:[pageSet firstIndex]]; [workSet removeIndex:[pageSet lastIndex]];
  133. NSInteger page = [workSet firstIndex]; [pageSet removeIndex:page];
  134. [self addContentView:scrollView page:page];
  135. }
  136. [pageSet enumerateIndexesWithOptions:options usingBlock: // Enumerate page set
  137. ^(NSUInteger page, BOOL *stop)
  138. {
  139. [self addContentView:scrollView page:page];
  140. }
  141. ];
  142. }
  143. }
  144. - (void)handleScrollViewDidEnd:(UIScrollView *)scrollView
  145. {
  146. CGFloat viewWidth = scrollView.bounds.size.width; // Scroll view width
  147. CGFloat contentOffsetX = scrollView.contentOffset.x; // Content offset X
  148. NSInteger page = (contentOffsetX / viewWidth); page++; // Page number
  149. if (page != currentPage) // Only if on different page
  150. {
  151. currentPage = page; document.pageNumber = [NSNumber numberWithInteger:page];
  152. [contentViews enumerateKeysAndObjectsUsingBlock: // Enumerate content views
  153. ^(NSNumber *key, ReaderContentView *contentView, BOOL *stop)
  154. {
  155. if ([key integerValue] != page) [contentView zoomResetAnimated:NO];
  156. }
  157. ];
  158. [mainToolbar setBookmarkState:[document.bookmarks containsIndex:page]];
  159. [mainPagebar updatePagebar]; // Update page bar
  160. }
  161. }
  162. - (void)showDocumentPage:(NSInteger)page
  163. {
  164. if (page != currentPage) // Only if on different page
  165. {
  166. if ((page < minimumPage) || (page > maximumPage)) return;
  167. currentPage = page; document.pageNumber = [NSNumber numberWithInteger:page];
  168. CGPoint contentOffset = CGPointMake((theScrollView.bounds.size.width * (page - 1)), 0.0f);
  169. if (CGPointEqualToPoint(theScrollView.contentOffset, contentOffset) == true)
  170. [self layoutContentViews:theScrollView];
  171. else
  172. [theScrollView setContentOffset:contentOffset];
  173. [contentViews enumerateKeysAndObjectsUsingBlock: // Enumerate content views
  174. ^(NSNumber *key, ReaderContentView *contentView, BOOL *stop)
  175. {
  176. if ([key integerValue] != page) [contentView zoomResetAnimated:NO];
  177. }
  178. ];
  179. [mainToolbar setBookmarkState:[document.bookmarks containsIndex:page]];
  180. [mainPagebar updatePagebar]; // Update page bar
  181. }
  182. }
  183. - (void)showDocument
  184. {
  185. [self updateContentSize:theScrollView]; // Update content size first
  186. [self showDocumentPage:[document.pageNumber integerValue]]; // Show page
  187. document.lastOpen = [NSDate date]; // Update document last opened date
  188. }
  189. - (void)closeDocument
  190. {
  191. if (printInteraction != nil) [printInteraction dismissAnimated:NO];
  192. [document archiveDocumentProperties]; // Save any ReaderDocument changes
  193. [[ReaderThumbQueue sharedInstance] cancelOperationsWithGUID:document.guid];
  194. [[ReaderThumbCache sharedInstance] removeAllObjects]; // Empty the thumb cache
  195. if ([delegate respondsToSelector:@selector(dismissReaderViewController:)] == YES)
  196. {
  197. [delegate dismissReaderViewController:self]; // Dismiss the ReaderViewController
  198. }
  199. else // We have a "Delegate must respond to -dismissReaderViewController:" error
  200. {
  201. NSAssert(NO, @"Delegate must respond to -dismissReaderViewController:");
  202. }
  203. }
  204. #pragma mark - UIViewController methods
  205. - (instancetype)initWithReaderDocument:(ReaderDocument *)object
  206. {
  207. if ((self = [super initWithNibName:nil bundle:nil])) // Initialize superclass
  208. {
  209. if ((object != nil) && ([object isKindOfClass:[ReaderDocument class]])) // Valid object
  210. {
  211. userInterfaceIdiom = [UIDevice currentDevice].userInterfaceIdiom; // User interface idiom
  212. NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter]; // Default notification center
  213. [notificationCenter addObserver:self selector:@selector(applicationWillResign:) name:UIApplicationWillTerminateNotification object:nil];
  214. [notificationCenter addObserver:self selector:@selector(applicationWillResign:) name:UIApplicationWillResignActiveNotification object:nil];
  215. scrollViewOutset = ((userInterfaceIdiom == UIUserInterfaceIdiomPad) ? SCROLLVIEW_OUTSET_LARGE : SCROLLVIEW_OUTSET_SMALL);
  216. [object updateDocumentProperties]; document = object; // Retain the supplied ReaderDocument object for our use
  217. [ReaderThumbCache touchThumbCacheWithGUID:object.guid]; // Touch the document thumb cache directory
  218. }
  219. else // Invalid ReaderDocument object
  220. {
  221. self = nil;
  222. }
  223. }
  224. return self;
  225. }
  226. - (void)dealloc
  227. {
  228. [[NSNotificationCenter defaultCenter] removeObserver:self];
  229. }
  230. - (void)viewDidLoad
  231. {
  232. [super viewDidLoad];
  233. assert(document != nil); // Must have a valid ReaderDocument
  234. self.view.backgroundColor = [UIColor grayColor]; // Neutral gray
  235. UIView *fakeStatusBar = nil; CGRect viewRect = self.view.bounds; // View bounds
  236. if ([self respondsToSelector:@selector(edgesForExtendedLayout)]) // iOS 7+
  237. {
  238. if ([self prefersStatusBarHidden] == NO) // Visible status bar
  239. {
  240. CGRect statusBarRect = viewRect; statusBarRect.size.height = STATUS_HEIGHT;
  241. fakeStatusBar = [[UIView alloc] initWithFrame:statusBarRect]; // UIView
  242. fakeStatusBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  243. fakeStatusBar.backgroundColor = [UIColor blackColor];
  244. fakeStatusBar.contentMode = UIViewContentModeRedraw;
  245. fakeStatusBar.userInteractionEnabled = NO;
  246. viewRect.origin.y += STATUS_HEIGHT; viewRect.size.height -= STATUS_HEIGHT;
  247. }
  248. }
  249. CGRect scrollViewRect = CGRectInset(viewRect, -scrollViewOutset, 0.0f);
  250. theScrollView = [[UIScrollView alloc] initWithFrame:scrollViewRect]; // All
  251. theScrollView.autoresizesSubviews = NO; theScrollView.contentMode = UIViewContentModeRedraw;
  252. theScrollView.showsHorizontalScrollIndicator = NO; theScrollView.showsVerticalScrollIndicator = NO;
  253. theScrollView.scrollsToTop = NO; theScrollView.delaysContentTouches = NO; theScrollView.pagingEnabled = YES;
  254. theScrollView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
  255. theScrollView.backgroundColor = [UIColor clearColor]; theScrollView.delegate = self;
  256. [self.view addSubview:theScrollView];
  257. CGRect toolbarRect = viewRect; toolbarRect.size.height = TOOLBAR_HEIGHT;
  258. mainToolbar = [[ReaderMainToolbar alloc] initWithFrame:toolbarRect document:document]; // ReaderMainToolbar
  259. mainToolbar.delegate = self; // ReaderMainToolbarDelegate
  260. //TWS [self.view addSubview:mainToolbar];
  261. CGRect pagebarRect = self.view.bounds; pagebarRect.size.height = PAGEBAR_HEIGHT;
  262. pagebarRect.origin.y = (self.view.bounds.size.height - pagebarRect.size.height);
  263. mainPagebar = [[ReaderMainPagebar alloc] initWithFrame:pagebarRect document:document]; // ReaderMainPagebar
  264. mainPagebar.delegate = self; // ReaderMainPagebarDelegate
  265. [self.view addSubview:mainPagebar];
  266. if (fakeStatusBar != nil) [self.view addSubview:fakeStatusBar]; // Add status bar background view
  267. UITapGestureRecognizer *singleTapOne = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
  268. singleTapOne.numberOfTouchesRequired = 1; singleTapOne.numberOfTapsRequired = 1; singleTapOne.delegate = self;
  269. [self.view addGestureRecognizer:singleTapOne];
  270. UITapGestureRecognizer *doubleTapOne = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
  271. doubleTapOne.numberOfTouchesRequired = 1; doubleTapOne.numberOfTapsRequired = 2; doubleTapOne.delegate = self;
  272. [self.view addGestureRecognizer:doubleTapOne];
  273. UITapGestureRecognizer *doubleTapTwo = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
  274. doubleTapTwo.numberOfTouchesRequired = 2; doubleTapTwo.numberOfTapsRequired = 2; doubleTapTwo.delegate = self;
  275. [self.view addGestureRecognizer:doubleTapTwo];
  276. [singleTapOne requireGestureRecognizerToFail:doubleTapOne]; // Single tap requires double tap to fail
  277. contentViews = [NSMutableDictionary new]; lastHideTime = [NSDate date];
  278. minimumPage = 1; maximumPage = [document.pageCount integerValue];
  279. }
  280. - (void)viewWillAppear:(BOOL)animated
  281. {
  282. [super viewWillAppear:animated];
  283. if (CGSizeEqualToSize(lastAppearSize, CGSizeZero) == false)
  284. {
  285. if (CGSizeEqualToSize(lastAppearSize, self.view.bounds.size) == false)
  286. {
  287. [self updateContentViews:theScrollView]; // Update content views
  288. }
  289. lastAppearSize = CGSizeZero; // Reset view size tracking
  290. }
  291. //TWS (IOS7)
  292. self.tabBarController.tabBar.hidden = YES;
  293. }
  294. - (void)viewDidAppear:(BOOL)animated
  295. {
  296. [super viewDidAppear:animated];
  297. if (CGSizeEqualToSize(theScrollView.contentSize, CGSizeZero) == true)
  298. {
  299. [self performSelector:@selector(showDocument) withObject:nil afterDelay:0.0];
  300. }
  301. #if (READER_DISABLE_IDLE == TRUE) // Option
  302. [UIApplication sharedApplication].idleTimerDisabled = YES;
  303. #endif // end of READER_DISABLE_IDLE Option
  304. }
  305. - (void)viewWillDisappear:(BOOL)animated
  306. {
  307. [super viewWillDisappear:animated];
  308. lastAppearSize = self.view.bounds.size; // Track view size
  309. #if (READER_DISABLE_IDLE == TRUE) // Option
  310. [UIApplication sharedApplication].idleTimerDisabled = NO;
  311. #endif // end of READER_DISABLE_IDLE Option
  312. }
  313. - (void)viewDidDisappear:(BOOL)animated
  314. {
  315. [super viewDidDisappear:animated];
  316. }
  317. - (void)viewDidUnload
  318. {
  319. #ifdef DEBUG
  320. NSLog(@"%s", __FUNCTION__);
  321. #endif
  322. mainToolbar = nil; mainPagebar = nil;
  323. theScrollView = nil; contentViews = nil; lastHideTime = nil;
  324. documentInteraction = nil; printInteraction = nil;
  325. lastAppearSize = CGSizeZero; currentPage = 0;
  326. [super viewDidUnload];
  327. }
  328. - (BOOL)prefersStatusBarHidden
  329. {
  330. return YES;
  331. }
  332. - (UIStatusBarStyle)preferredStatusBarStyle
  333. {
  334. return UIStatusBarStyleLightContent;
  335. }
  336. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  337. {
  338. return YES;
  339. }
  340. - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
  341. {
  342. if (userInterfaceIdiom == UIUserInterfaceIdiomPad) if (printInteraction != nil) [printInteraction dismissAnimated:NO];
  343. ignoreDidScroll = YES;
  344. }
  345. - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration
  346. {
  347. if (CGSizeEqualToSize(theScrollView.contentSize, CGSizeZero) == false)
  348. {
  349. [self updateContentViews:theScrollView]; lastAppearSize = CGSizeZero;
  350. }
  351. }
  352. - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
  353. {
  354. ignoreDidScroll = NO;
  355. }
  356. - (void)updateContentViews
  357. {
  358. if (currentPage > 0) {
  359. [self updateContentViews:theScrollView];
  360. }
  361. }
  362. - (void)didReceiveMemoryWarning
  363. {
  364. #ifdef DEBUG
  365. NSLog(@"%s", __FUNCTION__);
  366. #endif
  367. [super didReceiveMemoryWarning];
  368. }
  369. #pragma mark - UIScrollViewDelegate methods
  370. - (void)scrollViewDidScroll:(UIScrollView *)scrollView
  371. {
  372. if (ignoreDidScroll == NO) [self layoutContentViews:scrollView];
  373. }
  374. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
  375. {
  376. [self handleScrollViewDidEnd:scrollView];
  377. }
  378. - (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
  379. {
  380. [self handleScrollViewDidEnd:scrollView];
  381. }
  382. #pragma mark - UIGestureRecognizerDelegate methods
  383. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)recognizer shouldReceiveTouch:(UITouch *)touch
  384. {
  385. if ([touch.view isKindOfClass:[UIScrollView class]]) return YES;
  386. return NO;
  387. }
  388. #pragma mark - UIGestureRecognizer action methods
  389. - (void)decrementPageNumber
  390. {
  391. if ((maximumPage > minimumPage) && (currentPage != minimumPage))
  392. {
  393. CGPoint contentOffset = theScrollView.contentOffset; // Offset
  394. contentOffset.x -= theScrollView.bounds.size.width; // View X--
  395. [theScrollView setContentOffset:contentOffset animated:YES];
  396. }
  397. }
  398. - (void)incrementPageNumber
  399. {
  400. if ((maximumPage > minimumPage) && (currentPage != maximumPage))
  401. {
  402. CGPoint contentOffset = theScrollView.contentOffset; // Offset
  403. contentOffset.x += theScrollView.bounds.size.width; // View X++
  404. [theScrollView setContentOffset:contentOffset animated:YES];
  405. }
  406. }
  407. - (void)handleSingleTap:(UITapGestureRecognizer *)recognizer
  408. {
  409. if (recognizer.state == UIGestureRecognizerStateRecognized)
  410. {
  411. CGRect viewRect = recognizer.view.bounds; // View bounds
  412. CGPoint point = [recognizer locationInView:recognizer.view]; // Point
  413. CGRect areaRect = CGRectInset(viewRect, TAP_AREA_SIZE, 0.0f); // Area rect
  414. if (CGRectContainsPoint(areaRect, point) == true) // Single tap is inside area
  415. {
  416. NSNumber *key = [NSNumber numberWithInteger:currentPage]; // Page number key
  417. ReaderContentView *targetView = [contentViews objectForKey:key]; // View
  418. id target = [targetView processSingleTap:recognizer]; // Target object
  419. if (target != nil) // Handle the returned target object
  420. {
  421. if ([target isKindOfClass:[NSURL class]]) // Open a URL
  422. {
  423. NSURL *url = (NSURL *)target; // Cast to a NSURL object
  424. if (url.scheme == nil) // Handle a missing URL scheme
  425. {
  426. NSString *www = url.absoluteString; // Get URL string
  427. if ([www hasPrefix:@"www"] == YES) // Check for 'www' prefix
  428. {
  429. NSString *http = [[NSString alloc] initWithFormat:@"http://%@", www];
  430. url = [NSURL URLWithString:http]; // Proper http-based URL
  431. }
  432. }
  433. if ([[UIApplication sharedApplication] openURL:url] == NO)
  434. {
  435. #ifdef DEBUG
  436. NSLog(@"%s '%@'", __FUNCTION__, url); // Bad or unknown URL
  437. #endif
  438. }
  439. }
  440. else // Not a URL, so check for another possible object type
  441. {
  442. if ([target isKindOfClass:[NSNumber class]]) // Goto page
  443. {
  444. NSInteger number = [target integerValue]; // Number
  445. [self showDocumentPage:number]; // Show the page
  446. }
  447. }
  448. }
  449. else // Nothing active tapped in the target content view
  450. {
  451. if ([lastHideTime timeIntervalSinceNow] < -0.75) // Delay since hide
  452. {
  453. if ((mainToolbar.alpha < 1.0f) || (mainPagebar.alpha < 1.0f)) // Hidden
  454. {
  455. [mainToolbar showToolbar]; [mainPagebar showPagebar]; // Show
  456. }
  457. }
  458. }
  459. return;
  460. }
  461. CGRect nextPageRect = viewRect;
  462. nextPageRect.size.width = TAP_AREA_SIZE;
  463. nextPageRect.origin.x = (viewRect.size.width - TAP_AREA_SIZE);
  464. if (CGRectContainsPoint(nextPageRect, point) == true) // page++
  465. {
  466. [self incrementPageNumber]; return;
  467. }
  468. CGRect prevPageRect = viewRect;
  469. prevPageRect.size.width = TAP_AREA_SIZE;
  470. if (CGRectContainsPoint(prevPageRect, point) == true) // page--
  471. {
  472. [self decrementPageNumber]; return;
  473. }
  474. }
  475. }
  476. - (void)handleDoubleTap:(UITapGestureRecognizer *)recognizer
  477. {
  478. if (recognizer.state == UIGestureRecognizerStateRecognized)
  479. {
  480. CGRect viewRect = recognizer.view.bounds; // View bounds
  481. CGPoint point = [recognizer locationInView:recognizer.view]; // Point
  482. CGRect zoomArea = CGRectInset(viewRect, TAP_AREA_SIZE, TAP_AREA_SIZE); // Area
  483. if (CGRectContainsPoint(zoomArea, point) == true) // Double tap is inside zoom area
  484. {
  485. NSNumber *key = [NSNumber numberWithInteger:currentPage]; // Page number key
  486. ReaderContentView *targetView = [contentViews objectForKey:key]; // View
  487. switch (recognizer.numberOfTouchesRequired) // Touches count
  488. {
  489. case 1: // One finger double tap: zoom++
  490. {
  491. [targetView zoomIncrement:recognizer]; break;
  492. }
  493. case 2: // Two finger double tap: zoom--
  494. {
  495. [targetView zoomDecrement:recognizer]; break;
  496. }
  497. }
  498. return;
  499. }
  500. CGRect nextPageRect = viewRect;
  501. nextPageRect.size.width = TAP_AREA_SIZE;
  502. nextPageRect.origin.x = (viewRect.size.width - TAP_AREA_SIZE);
  503. if (CGRectContainsPoint(nextPageRect, point) == true) // page++
  504. {
  505. [self incrementPageNumber]; return;
  506. }
  507. CGRect prevPageRect = viewRect;
  508. prevPageRect.size.width = TAP_AREA_SIZE;
  509. if (CGRectContainsPoint(prevPageRect, point) == true) // page--
  510. {
  511. [self decrementPageNumber]; return;
  512. }
  513. }
  514. }
  515. #pragma mark - ReaderContentViewDelegate methods
  516. - (void)contentView:(ReaderContentView *)contentView touchesBegan:(NSSet *)touches
  517. {
  518. if ((mainToolbar.alpha > 0.0f) || (mainPagebar.alpha > 0.0f))
  519. {
  520. if (touches.count == 1) // Single touches only
  521. {
  522. UITouch *touch = [touches anyObject]; // Touch info
  523. CGPoint point = [touch locationInView:self.view]; // Touch location
  524. CGRect areaRect = CGRectInset(self.view.bounds, TAP_AREA_SIZE, TAP_AREA_SIZE);
  525. if (CGRectContainsPoint(areaRect, point) == false) return;
  526. }
  527. [mainToolbar hideToolbar]; [mainPagebar hidePagebar]; // Hide
  528. lastHideTime = [NSDate date]; // Set last hide time
  529. }
  530. }
  531. #pragma mark - ReaderMainToolbarDelegate methods
  532. - (void)tappedInToolbar:(ReaderMainToolbar *)toolbar doneButton:(UIButton *)button
  533. {
  534. #if (READER_STANDALONE == FALSE) // Option
  535. [self closeDocument]; // Close ReaderViewController
  536. #endif // end of READER_STANDALONE Option
  537. }
  538. - (void)tappedInToolbar:(ReaderMainToolbar *)toolbar thumbsButton:(UIButton *)button
  539. {
  540. #if (READER_ENABLE_THUMBS == TRUE) // Option
  541. [[UIApplication sharedApplication] setStatusBarHidden:YES];
  542. if (printInteraction != nil) [printInteraction dismissAnimated:NO];
  543. ThumbsViewController *thumbsViewController = [[ThumbsViewController alloc] initWithReaderDocument:document];
  544. thumbsViewController.title = self.title; thumbsViewController.delegate = self; // ThumbsViewControllerDelegate
  545. thumbsViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
  546. thumbsViewController.modalPresentationStyle = UIModalPresentationFullScreen;
  547. [self presentViewController:thumbsViewController animated:NO completion:NULL];
  548. #endif // end of READER_ENABLE_THUMBS Option
  549. }
  550. - (void)tappedInToolbar:(ReaderMainToolbar *)toolbar exportButton:(UIButton *)button
  551. {
  552. if (printInteraction != nil) [printInteraction dismissAnimated:YES];
  553. NSURL *fileURL = document.fileURL; // Document file URL
  554. documentInteraction = [UIDocumentInteractionController interactionControllerWithURL:fileURL];
  555. documentInteraction.delegate = self; // UIDocumentInteractionControllerDelegate
  556. [documentInteraction presentOpenInMenuFromRect:button.bounds inView:button animated:YES];
  557. }
  558. - (void)tappedInToolbar:(ReaderMainToolbar *)toolbar printButton:(UIButton *)button
  559. {
  560. if ([UIPrintInteractionController isPrintingAvailable] == YES)
  561. {
  562. NSURL *fileURL = document.fileURL; // Document file URL
  563. if ([UIPrintInteractionController canPrintURL:fileURL] == YES)
  564. {
  565. printInteraction = [UIPrintInteractionController sharedPrintController];
  566. UIPrintInfo *printInfo = [UIPrintInfo printInfo];
  567. printInfo.duplex = UIPrintInfoDuplexLongEdge;
  568. printInfo.outputType = UIPrintInfoOutputGeneral;
  569. printInfo.jobName = document.fileName;
  570. printInteraction.printInfo = printInfo;
  571. printInteraction.printingItem = fileURL;
  572. printInteraction.showsPageRange = YES;
  573. if (userInterfaceIdiom == UIUserInterfaceIdiomPad) // Large device printing
  574. {
  575. [printInteraction presentFromRect:button.bounds inView:button animated:YES completionHandler:
  576. ^(UIPrintInteractionController *pic, BOOL completed, NSError *error)
  577. {
  578. #ifdef DEBUG
  579. if ((completed == NO) && (error != nil)) NSLog(@"%s %@", __FUNCTION__, error);
  580. #endif
  581. }
  582. ];
  583. }
  584. else // Handle printing on small device
  585. {
  586. [printInteraction presentAnimated:YES completionHandler:
  587. ^(UIPrintInteractionController *pic, BOOL completed, NSError *error)
  588. {
  589. #ifdef DEBUG
  590. if ((completed == NO) && (error != nil)) NSLog(@"%s %@", __FUNCTION__, error);
  591. #endif
  592. }
  593. ];
  594. }
  595. }
  596. }
  597. }
  598. - (void)tappedInToolbar:(ReaderMainToolbar *)toolbar emailButton:(UIButton *)button
  599. {
  600. if ([MFMailComposeViewController canSendMail] == NO) return;
  601. if (printInteraction != nil) [printInteraction dismissAnimated:YES];
  602. unsigned long long fileSize = [document.fileSize unsignedLongLongValue];
  603. if (fileSize < 15728640ull) // Check attachment size limit (15MB)
  604. {
  605. NSURL *fileURL = document.fileURL; NSString *fileName = document.fileName;
  606. NSData *attachment = [NSData dataWithContentsOfURL:fileURL options:(NSDataReadingMapped|NSDataReadingUncached) error:nil];
  607. if (attachment != nil) // Ensure that we have valid document file attachment data available
  608. {
  609. MFMailComposeViewController *mailComposer = [MFMailComposeViewController new];
  610. [mailComposer addAttachmentData:attachment mimeType:@"application/pdf" fileName:fileName];
  611. [mailComposer setSubject:fileName]; // Use the document file name for the subject
  612. mailComposer.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
  613. mailComposer.modalPresentationStyle = UIModalPresentationFormSheet;
  614. mailComposer.mailComposeDelegate = self; // MFMailComposeViewControllerDelegate
  615. [self presentViewController:mailComposer animated:YES completion:NULL];
  616. }
  617. }
  618. }
  619. - (void)tappedInToolbar:(ReaderMainToolbar *)toolbar markButton:(UIButton *)button
  620. {
  621. #if (READER_BOOKMARKS == TRUE) // Option
  622. if (printInteraction != nil) [printInteraction dismissAnimated:YES];
  623. if ([document.bookmarks containsIndex:currentPage]) // Remove bookmark
  624. {
  625. [document.bookmarks removeIndex:currentPage]; [mainToolbar setBookmarkState:NO];
  626. }
  627. else // Add the bookmarked page number to the bookmark index set
  628. {
  629. [document.bookmarks addIndex:currentPage]; [mainToolbar setBookmarkState:YES];
  630. }
  631. #endif // end of READER_BOOKMARKS Option
  632. }
  633. #pragma mark - MFMailComposeViewControllerDelegate methods
  634. - (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
  635. {
  636. #ifdef DEBUG
  637. if ((result == MFMailComposeResultFailed) && (error != NULL)) NSLog(@"%@", error);
  638. #endif
  639. [self dismissViewControllerAnimated:YES completion:NULL];
  640. }
  641. #pragma mark - UIDocumentInteractionControllerDelegate methods
  642. - (void)documentInteractionControllerDidDismissOpenInMenu:(UIDocumentInteractionController *)controller
  643. {
  644. documentInteraction = nil;
  645. }
  646. #pragma mark - ThumbsViewControllerDelegate methods
  647. - (void)thumbsViewController:(ThumbsViewController *)viewController gotoPage:(NSInteger)page
  648. {
  649. #if (READER_ENABLE_THUMBS == TRUE) // Option
  650. [self showDocumentPage:page];
  651. #endif // end of READER_ENABLE_THUMBS Option
  652. }
  653. - (void)dismissThumbsViewController:(ThumbsViewController *)viewController
  654. {
  655. #if (READER_ENABLE_THUMBS == TRUE) // Option
  656. [self dismissViewControllerAnimated:NO completion:NULL];
  657. #endif // end of READER_ENABLE_THUMBS Option
  658. }
  659. #pragma mark - ReaderMainPagebarDelegate methods
  660. - (void)pagebar:(ReaderMainPagebar *)pagebar gotoPage:(NSInteger)page
  661. {
  662. [self showDocumentPage:page];
  663. }
  664. #pragma mark - UIApplication notification methods
  665. - (void)applicationWillResign:(NSNotification *)notification
  666. {
  667. [document archiveDocumentProperties]; // Save any ReaderDocument changes
  668. if (userInterfaceIdiom == UIUserInterfaceIdiomPad) if (printInteraction != nil) [printInteraction dismissAnimated:NO];
  669. }
  670. @end