ReaderViewController.m 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928
  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. UISwipeGestureRecognizer *swipeUpDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeUpDown:)];
  277. swipeUpDown.direction = UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionUp;
  278. [self.view addGestureRecognizer:swipeUpDown];
  279. [singleTapOne requireGestureRecognizerToFail:doubleTapOne]; // Single tap requires double tap to fail
  280. contentViews = [NSMutableDictionary new]; lastHideTime = [NSDate date];
  281. minimumPage = 1; maximumPage = [document.pageCount integerValue];
  282. }
  283. - (void)viewWillAppear:(BOOL)animated
  284. {
  285. [super viewWillAppear:animated];
  286. if (CGSizeEqualToSize(lastAppearSize, CGSizeZero) == false)
  287. {
  288. if (CGSizeEqualToSize(lastAppearSize, self.view.bounds.size) == false)
  289. {
  290. [self updateContentViews:theScrollView]; // Update content views
  291. }
  292. lastAppearSize = CGSizeZero; // Reset view size tracking
  293. }
  294. }
  295. - (void)viewDidAppear:(BOOL)animated
  296. {
  297. [super viewDidAppear:animated];
  298. if (CGSizeEqualToSize(theScrollView.contentSize, CGSizeZero) == true)
  299. {
  300. [self performSelector:@selector(showDocument) withObject:nil afterDelay:0.0];
  301. }
  302. #if (READER_DISABLE_IDLE == TRUE) // Option
  303. [UIApplication sharedApplication].idleTimerDisabled = YES;
  304. #endif // end of READER_DISABLE_IDLE Option
  305. }
  306. - (void)viewWillDisappear:(BOOL)animated
  307. {
  308. [super viewWillDisappear:animated];
  309. lastAppearSize = self.view.bounds.size; // Track view size
  310. #if (READER_DISABLE_IDLE == TRUE) // Option
  311. [UIApplication sharedApplication].idleTimerDisabled = NO;
  312. #endif // end of READER_DISABLE_IDLE Option
  313. }
  314. - (void)viewDidDisappear:(BOOL)animated
  315. {
  316. [super viewDidDisappear:animated];
  317. }
  318. - (void)viewDidUnload
  319. {
  320. #ifdef DEBUG
  321. NSLog(@"%s", __FUNCTION__);
  322. #endif
  323. mainToolbar = nil; mainPagebar = nil;
  324. theScrollView = nil; contentViews = nil; lastHideTime = nil;
  325. documentInteraction = nil; printInteraction = nil;
  326. lastAppearSize = CGSizeZero; currentPage = 0;
  327. [super viewDidUnload];
  328. }
  329. - (BOOL)prefersStatusBarHidden
  330. {
  331. return YES;
  332. }
  333. - (UIStatusBarStyle)preferredStatusBarStyle
  334. {
  335. return UIStatusBarStyleLightContent;
  336. }
  337. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  338. {
  339. return YES;
  340. }
  341. - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
  342. {
  343. if (userInterfaceIdiom == UIUserInterfaceIdiomPad) if (printInteraction != nil) [printInteraction dismissAnimated:NO];
  344. ignoreDidScroll = YES;
  345. }
  346. - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration
  347. {
  348. if (CGSizeEqualToSize(theScrollView.contentSize, CGSizeZero) == false)
  349. {
  350. [self updateContentViews:theScrollView]; lastAppearSize = CGSizeZero;
  351. }
  352. }
  353. - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
  354. {
  355. ignoreDidScroll = NO;
  356. }
  357. //TWS
  358. - (void)updateContentViews
  359. {
  360. if (currentPage > 0) {
  361. [self updateContentViews:theScrollView];
  362. }
  363. }
  364. - (void)didReceiveMemoryWarning
  365. {
  366. #ifdef DEBUG
  367. NSLog(@"%s", __FUNCTION__);
  368. #endif
  369. [super didReceiveMemoryWarning];
  370. }
  371. #pragma mark - UIScrollViewDelegate methods
  372. - (void)scrollViewDidScroll:(UIScrollView *)scrollView
  373. {
  374. if (ignoreDidScroll == NO) [self layoutContentViews:scrollView];
  375. }
  376. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
  377. {
  378. [self handleScrollViewDidEnd:scrollView];
  379. }
  380. - (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
  381. {
  382. [self handleScrollViewDidEnd:scrollView];
  383. }
  384. #pragma mark - UIGestureRecognizerDelegate methods
  385. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)recognizer shouldReceiveTouch:(UITouch *)touch
  386. {
  387. if ([touch.view isKindOfClass:[UIScrollView class]]) return YES;
  388. return NO;
  389. }
  390. #pragma mark - UIGestureRecognizer action methods
  391. - (void)decrementPageNumber
  392. {
  393. if ((maximumPage > minimumPage) && (currentPage != minimumPage))
  394. {
  395. CGPoint contentOffset = theScrollView.contentOffset; // Offset
  396. contentOffset.x -= theScrollView.bounds.size.width; // View X--
  397. [theScrollView setContentOffset:contentOffset animated:YES];
  398. }
  399. }
  400. - (void)incrementPageNumber
  401. {
  402. if ((maximumPage > minimumPage) && (currentPage != maximumPage))
  403. {
  404. CGPoint contentOffset = theScrollView.contentOffset; // Offset
  405. contentOffset.x += theScrollView.bounds.size.width; // View X++
  406. [theScrollView setContentOffset:contentOffset animated:YES];
  407. }
  408. }
  409. - (void)handleSingleTap:(UITapGestureRecognizer *)recognizer
  410. {
  411. if (recognizer.state == UIGestureRecognizerStateRecognized)
  412. {
  413. //TWS
  414. if ([self.delegate respondsToSelector:@selector(handleSingleTapReader)])
  415. [self.delegate handleSingleTapReader];
  416. CGRect viewRect = recognizer.view.bounds; // View bounds
  417. CGPoint point = [recognizer locationInView:recognizer.view]; // Point
  418. CGRect areaRect = CGRectInset(viewRect, TAP_AREA_SIZE, 0.0f); // Area rect
  419. if (CGRectContainsPoint(areaRect, point) == true) // Single tap is inside area
  420. {
  421. NSNumber *key = [NSNumber numberWithInteger:currentPage]; // Page number key
  422. ReaderContentView *targetView = [contentViews objectForKey:key]; // View
  423. id target = [targetView processSingleTap:recognizer]; // Target object
  424. if (target != nil) // Handle the returned target object
  425. {
  426. if ([target isKindOfClass:[NSURL class]]) // Open a URL
  427. {
  428. NSURL *url = (NSURL *)target; // Cast to a NSURL object
  429. if (url.scheme == nil) // Handle a missing URL scheme
  430. {
  431. NSString *www = url.absoluteString; // Get URL string
  432. if ([www hasPrefix:@"www"] == YES) // Check for 'www' prefix
  433. {
  434. NSString *http = [[NSString alloc] initWithFormat:@"http://%@", www];
  435. url = [NSURL URLWithString:http]; // Proper http-based URL
  436. }
  437. }
  438. if ([[UIApplication sharedApplication] openURL:url] == NO)
  439. {
  440. #ifdef DEBUG
  441. NSLog(@"%s '%@'", __FUNCTION__, url); // Bad or unknown URL
  442. #endif
  443. }
  444. }
  445. else // Not a URL, so check for another possible object type
  446. {
  447. if ([target isKindOfClass:[NSNumber class]]) // Goto page
  448. {
  449. NSInteger number = [target integerValue]; // Number
  450. [self showDocumentPage:number]; // Show the page
  451. }
  452. }
  453. }
  454. else // Nothing active tapped in the target content view
  455. {
  456. if ([lastHideTime timeIntervalSinceNow] < -0.75) // Delay since hide
  457. {
  458. if ((mainToolbar.alpha < 1.0f) || (mainPagebar.alpha < 1.0f)) // Hidden
  459. {
  460. [mainToolbar showToolbar]; [mainPagebar showPagebar]; // Show
  461. }
  462. }
  463. }
  464. return;
  465. }
  466. CGRect nextPageRect = viewRect;
  467. nextPageRect.size.width = TAP_AREA_SIZE;
  468. nextPageRect.origin.x = (viewRect.size.width - TAP_AREA_SIZE);
  469. if (CGRectContainsPoint(nextPageRect, point) == true) // page++
  470. {
  471. [self incrementPageNumber]; return;
  472. }
  473. CGRect prevPageRect = viewRect;
  474. prevPageRect.size.width = TAP_AREA_SIZE;
  475. if (CGRectContainsPoint(prevPageRect, point) == true) // page--
  476. {
  477. [self decrementPageNumber]; return;
  478. }
  479. }
  480. }
  481. - (void)handleDoubleTap:(UITapGestureRecognizer *)recognizer
  482. {
  483. if (recognizer.state == UIGestureRecognizerStateRecognized)
  484. {
  485. CGRect viewRect = recognizer.view.bounds; // View bounds
  486. CGPoint point = [recognizer locationInView:recognizer.view]; // Point
  487. CGRect zoomArea = CGRectInset(viewRect, TAP_AREA_SIZE, TAP_AREA_SIZE); // Area
  488. if (CGRectContainsPoint(zoomArea, point) == true) // Double tap is inside zoom area
  489. {
  490. NSNumber *key = [NSNumber numberWithInteger:currentPage]; // Page number key
  491. ReaderContentView *targetView = [contentViews objectForKey:key]; // View
  492. switch (recognizer.numberOfTouchesRequired) // Touches count
  493. {
  494. case 1: // One finger double tap: zoom++
  495. {
  496. [targetView zoomIncrement:recognizer]; break;
  497. }
  498. case 2: // Two finger double tap: zoom--
  499. {
  500. [targetView zoomDecrement:recognizer]; break;
  501. }
  502. }
  503. return;
  504. }
  505. CGRect nextPageRect = viewRect;
  506. nextPageRect.size.width = TAP_AREA_SIZE;
  507. nextPageRect.origin.x = (viewRect.size.width - TAP_AREA_SIZE);
  508. if (CGRectContainsPoint(nextPageRect, point) == true) // page++
  509. {
  510. [self incrementPageNumber]; return;
  511. }
  512. CGRect prevPageRect = viewRect;
  513. prevPageRect.size.width = TAP_AREA_SIZE;
  514. if (CGRectContainsPoint(prevPageRect, point) == true) // page--
  515. {
  516. [self decrementPageNumber]; return;
  517. }
  518. }
  519. }
  520. - (void)handleSwipeUpDown:(UITapGestureRecognizer *)recognizer
  521. {
  522. //TWS
  523. if ([self.delegate respondsToSelector:@selector(handleSwipeUpDown)])
  524. [self.delegate handleSwipeUpDown];
  525. }
  526. #pragma mark - ReaderContentViewDelegate methods
  527. - (void)contentView:(ReaderContentView *)contentView touchesBegan:(NSSet *)touches
  528. {
  529. if ((mainToolbar.alpha > 0.0f) || (mainPagebar.alpha > 0.0f))
  530. {
  531. if (touches.count == 1) // Single touches only
  532. {
  533. UITouch *touch = [touches anyObject]; // Touch info
  534. CGPoint point = [touch locationInView:self.view]; // Touch location
  535. CGRect areaRect = CGRectInset(self.view.bounds, TAP_AREA_SIZE, TAP_AREA_SIZE);
  536. if (CGRectContainsPoint(areaRect, point) == false) return;
  537. }
  538. [mainToolbar hideToolbar]; [mainPagebar hidePagebar]; // Hide
  539. lastHideTime = [NSDate date]; // Set last hide time
  540. }
  541. }
  542. #pragma mark - ReaderMainToolbarDelegate methods
  543. - (void)tappedInToolbar:(ReaderMainToolbar *)toolbar doneButton:(UIButton *)button
  544. {
  545. #if (READER_STANDALONE == FALSE) // Option
  546. [self closeDocument]; // Close ReaderViewController
  547. #endif // end of READER_STANDALONE Option
  548. }
  549. - (void)tappedInToolbar:(ReaderMainToolbar *)toolbar thumbsButton:(UIButton *)button
  550. {
  551. #if (READER_ENABLE_THUMBS == TRUE) // Option
  552. //TWS
  553. [[UIApplication sharedApplication] setStatusBarHidden:YES];
  554. if (printInteraction != nil) [printInteraction dismissAnimated:NO];
  555. ThumbsViewController *thumbsViewController = [[ThumbsViewController alloc] initWithReaderDocument:document];
  556. thumbsViewController.title = self.title; thumbsViewController.delegate = self; // ThumbsViewControllerDelegate
  557. thumbsViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
  558. thumbsViewController.modalPresentationStyle = UIModalPresentationFullScreen;
  559. [self presentViewController:thumbsViewController animated:NO completion:NULL];
  560. #endif // end of READER_ENABLE_THUMBS Option
  561. }
  562. - (void)tappedInToolbar:(ReaderMainToolbar *)toolbar exportButton:(UIButton *)button
  563. {
  564. if (printInteraction != nil) [printInteraction dismissAnimated:YES];
  565. NSURL *fileURL = document.fileURL; // Document file URL
  566. documentInteraction = [UIDocumentInteractionController interactionControllerWithURL:fileURL];
  567. documentInteraction.delegate = self; // UIDocumentInteractionControllerDelegate
  568. [documentInteraction presentOpenInMenuFromRect:button.bounds inView:button animated:YES];
  569. }
  570. - (void)tappedInToolbar:(ReaderMainToolbar *)toolbar printButton:(UIButton *)button
  571. {
  572. if ([UIPrintInteractionController isPrintingAvailable] == YES)
  573. {
  574. NSURL *fileURL = document.fileURL; // Document file URL
  575. if ([UIPrintInteractionController canPrintURL:fileURL] == YES)
  576. {
  577. printInteraction = [UIPrintInteractionController sharedPrintController];
  578. UIPrintInfo *printInfo = [UIPrintInfo printInfo];
  579. printInfo.duplex = UIPrintInfoDuplexLongEdge;
  580. printInfo.outputType = UIPrintInfoOutputGeneral;
  581. printInfo.jobName = document.fileName;
  582. printInteraction.printInfo = printInfo;
  583. printInteraction.printingItem = fileURL;
  584. printInteraction.showsPageRange = YES;
  585. if (userInterfaceIdiom == UIUserInterfaceIdiomPad) // Large device printing
  586. {
  587. [printInteraction presentFromRect:button.bounds inView:button animated:YES completionHandler:
  588. ^(UIPrintInteractionController *pic, BOOL completed, NSError *error)
  589. {
  590. #ifdef DEBUG
  591. if ((completed == NO) && (error != nil)) NSLog(@"%s %@", __FUNCTION__, error);
  592. #endif
  593. }
  594. ];
  595. }
  596. else // Handle printing on small device
  597. {
  598. [printInteraction presentAnimated:YES completionHandler:
  599. ^(UIPrintInteractionController *pic, BOOL completed, NSError *error)
  600. {
  601. #ifdef DEBUG
  602. if ((completed == NO) && (error != nil)) NSLog(@"%s %@", __FUNCTION__, error);
  603. #endif
  604. }
  605. ];
  606. }
  607. }
  608. }
  609. }
  610. - (void)tappedInToolbar:(ReaderMainToolbar *)toolbar emailButton:(UIButton *)button
  611. {
  612. if ([MFMailComposeViewController canSendMail] == NO) return;
  613. if (printInteraction != nil) [printInteraction dismissAnimated:YES];
  614. unsigned long long fileSize = [document.fileSize unsignedLongLongValue];
  615. if (fileSize < 15728640ull) // Check attachment size limit (15MB)
  616. {
  617. NSURL *fileURL = document.fileURL; NSString *fileName = document.fileName;
  618. NSData *attachment = [NSData dataWithContentsOfURL:fileURL options:(NSDataReadingMapped|NSDataReadingUncached) error:nil];
  619. if (attachment != nil) // Ensure that we have valid document file attachment data available
  620. {
  621. MFMailComposeViewController *mailComposer = [MFMailComposeViewController new];
  622. [mailComposer addAttachmentData:attachment mimeType:@"application/pdf" fileName:fileName];
  623. [mailComposer setSubject:fileName]; // Use the document file name for the subject
  624. mailComposer.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
  625. mailComposer.modalPresentationStyle = UIModalPresentationFormSheet;
  626. mailComposer.mailComposeDelegate = self; // MFMailComposeViewControllerDelegate
  627. [self presentViewController:mailComposer animated:YES completion:NULL];
  628. }
  629. }
  630. }
  631. - (void)tappedInToolbar:(ReaderMainToolbar *)toolbar markButton:(UIButton *)button
  632. {
  633. #if (READER_BOOKMARKS == TRUE) // Option
  634. if (printInteraction != nil) [printInteraction dismissAnimated:YES];
  635. if ([document.bookmarks containsIndex:currentPage]) // Remove bookmark
  636. {
  637. [document.bookmarks removeIndex:currentPage]; [mainToolbar setBookmarkState:NO];
  638. }
  639. else // Add the bookmarked page number to the bookmark index set
  640. {
  641. [document.bookmarks addIndex:currentPage]; [mainToolbar setBookmarkState:YES];
  642. }
  643. #endif // end of READER_BOOKMARKS Option
  644. }
  645. #pragma mark - MFMailComposeViewControllerDelegate methods
  646. - (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
  647. {
  648. #ifdef DEBUG
  649. if ((result == MFMailComposeResultFailed) && (error != NULL)) NSLog(@"%@", error);
  650. #endif
  651. [self dismissViewControllerAnimated:YES completion:NULL];
  652. }
  653. #pragma mark - UIDocumentInteractionControllerDelegate methods
  654. - (void)documentInteractionControllerDidDismissOpenInMenu:(UIDocumentInteractionController *)controller
  655. {
  656. documentInteraction = nil;
  657. }
  658. #pragma mark - ThumbsViewControllerDelegate methods
  659. - (void)thumbsViewController:(ThumbsViewController *)viewController gotoPage:(NSInteger)page
  660. {
  661. #if (READER_ENABLE_THUMBS == TRUE) // Option
  662. [self showDocumentPage:page];
  663. #endif // end of READER_ENABLE_THUMBS Option
  664. }
  665. - (void)dismissThumbsViewController:(ThumbsViewController *)viewController
  666. {
  667. #if (READER_ENABLE_THUMBS == TRUE) // Option
  668. [self dismissViewControllerAnimated:NO completion:NULL];
  669. #endif // end of READER_ENABLE_THUMBS Option
  670. }
  671. #pragma mark - ReaderMainPagebarDelegate methods
  672. - (void)pagebar:(ReaderMainPagebar *)pagebar gotoPage:(NSInteger)page
  673. {
  674. [self showDocumentPage:page];
  675. }
  676. #pragma mark - UIApplication notification methods
  677. - (void)applicationWillResign:(NSNotification *)notification
  678. {
  679. [document archiveDocumentProperties]; // Save any ReaderDocument changes
  680. if (userInterfaceIdiom == UIUserInterfaceIdiomPad) if (printInteraction != nil) [printInteraction dismissAnimated:NO];
  681. }
  682. @end