123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928 |
- #import "ReaderConstants.h"
- #import "ReaderViewController.h"
- #import "ThumbsViewController.h"
- #import "ReaderMainToolbar.h"
- #import "ReaderMainPagebar.h"
- #import "ReaderContentView.h"
- #import "ReaderThumbCache.h"
- #import "ReaderThumbQueue.h"
- #import <MessageUI/MessageUI.h>
- @interface ReaderViewController () <UIScrollViewDelegate, UIGestureRecognizerDelegate, MFMailComposeViewControllerDelegate, UIDocumentInteractionControllerDelegate,
- ReaderMainToolbarDelegate, ReaderMainPagebarDelegate, ReaderContentViewDelegate, ThumbsViewControllerDelegate>
- @end
- @implementation ReaderViewController
- {
- ReaderDocument *document;
- UIScrollView *theScrollView;
- ReaderMainToolbar *mainToolbar;
- ReaderMainPagebar *mainPagebar;
- NSMutableDictionary *contentViews;
- UIUserInterfaceIdiom userInterfaceIdiom;
- NSInteger currentPage, minimumPage, maximumPage;
- UIDocumentInteractionController *documentInteraction;
- UIPrintInteractionController *printInteraction;
- CGFloat scrollViewOutset;
- CGSize lastAppearSize;
- NSDate *lastHideTime;
- BOOL ignoreDidScroll;
- }
- #pragma mark - Constants
- #define STATUS_HEIGHT 20.0f
- #define TOOLBAR_HEIGHT 44.0f
- #define PAGEBAR_HEIGHT 48.0f
- #define SCROLLVIEW_OUTSET_SMALL 4.0f
- #define SCROLLVIEW_OUTSET_LARGE 8.0f
- #define TAP_AREA_SIZE 48.0f
- #pragma mark - Properties
- @synthesize delegate;
- #pragma mark - ReaderViewController methods
- - (void)updateContentSize:(UIScrollView *)scrollView
- {
- CGFloat contentHeight = scrollView.bounds.size.height;
- CGFloat contentWidth = (scrollView.bounds.size.width * maximumPage);
- scrollView.contentSize = CGSizeMake(contentWidth, contentHeight);
- }
- - (void)updateContentViews:(UIScrollView *)scrollView
- {
- [self updateContentSize:scrollView];
- [contentViews enumerateKeysAndObjectsUsingBlock:
- ^(NSNumber *key, ReaderContentView *contentView, BOOL *stop)
- {
- NSInteger page = [key integerValue];
- CGRect viewRect = CGRectZero; viewRect.size = scrollView.bounds.size;
- viewRect.origin.x = (viewRect.size.width * (page - 1));
- contentView.frame = CGRectInset(viewRect, scrollViewOutset, 0.0f);
- }
- ];
- NSInteger page = currentPage;
- CGPoint contentOffset = CGPointMake((scrollView.bounds.size.width * (page - 1)), 0.0f);
- if (CGPointEqualToPoint(scrollView.contentOffset, contentOffset) == false)
- {
- scrollView.contentOffset = contentOffset;
- }
- [mainToolbar setBookmarkState:[document.bookmarks containsIndex:page]];
- [mainPagebar updatePagebar];
- }
- - (void)addContentView:(UIScrollView *)scrollView page:(NSInteger)page
- {
- CGRect viewRect = CGRectZero; viewRect.size = scrollView.bounds.size;
- viewRect.origin.x = (viewRect.size.width * (page - 1)); viewRect = CGRectInset(viewRect, scrollViewOutset, 0.0f);
- NSURL *fileURL = document.fileURL; NSString *phrase = document.password; NSString *guid = document.guid;
- ReaderContentView *contentView = [[ReaderContentView alloc] initWithFrame:viewRect fileURL:fileURL page:page password:phrase];
- contentView.message = self; [contentViews setObject:contentView forKey:[NSNumber numberWithInteger:page]]; [scrollView addSubview:contentView];
- [contentView showPageThumb:fileURL page:page password:phrase guid:guid];
- }
- - (void)layoutContentViews:(UIScrollView *)scrollView
- {
- CGFloat viewWidth = scrollView.bounds.size.width;
- CGFloat contentOffsetX = scrollView.contentOffset.x;
- NSInteger pageB = ((contentOffsetX + viewWidth - 1.0f) / viewWidth);
- NSInteger pageA = (contentOffsetX / viewWidth); pageB += 2;
- if (pageA < minimumPage) pageA = minimumPage; if (pageB > maximumPage) pageB = maximumPage;
- NSRange pageRange = NSMakeRange(pageA, (pageB - pageA + 1));
- NSMutableIndexSet *pageSet = [NSMutableIndexSet indexSetWithIndexesInRange:pageRange];
- for (NSNumber *key in [contentViews allKeys])
- {
- NSInteger page = [key integerValue];
- if ([pageSet containsIndex:page] == NO)
- {
- ReaderContentView *contentView = [contentViews objectForKey:key];
- [contentView removeFromSuperview]; [contentViews removeObjectForKey:key];
- }
- else
- {
- [pageSet removeIndex:page];
- }
- }
- NSInteger pages = pageSet.count;
- if (pages > 0)
- {
- NSEnumerationOptions options = 0;
- if (pages == 2)
- {
- if ((maximumPage > 2) && ([pageSet lastIndex] == maximumPage)) options = NSEnumerationReverse;
- }
- else if (pages == 3)
- {
- NSMutableIndexSet *workSet = [pageSet mutableCopy]; options = NSEnumerationReverse;
- [workSet removeIndex:[pageSet firstIndex]]; [workSet removeIndex:[pageSet lastIndex]];
- NSInteger page = [workSet firstIndex]; [pageSet removeIndex:page];
- [self addContentView:scrollView page:page];
- }
- [pageSet enumerateIndexesWithOptions:options usingBlock:
- ^(NSUInteger page, BOOL *stop)
- {
- [self addContentView:scrollView page:page];
- }
- ];
- }
- }
- - (void)handleScrollViewDidEnd:(UIScrollView *)scrollView
- {
- CGFloat viewWidth = scrollView.bounds.size.width;
- CGFloat contentOffsetX = scrollView.contentOffset.x;
- NSInteger page = (contentOffsetX / viewWidth); page++;
- if (page != currentPage)
- {
- currentPage = page; document.pageNumber = [NSNumber numberWithInteger:page];
- [contentViews enumerateKeysAndObjectsUsingBlock:
- ^(NSNumber *key, ReaderContentView *contentView, BOOL *stop)
- {
- if ([key integerValue] != page) [contentView zoomResetAnimated:NO];
- }
- ];
- [mainToolbar setBookmarkState:[document.bookmarks containsIndex:page]];
- [mainPagebar updatePagebar];
- }
- }
- - (void)showDocumentPage:(NSInteger)page
- {
- if (page != currentPage)
- {
- if ((page < minimumPage) || (page > maximumPage)) return;
- currentPage = page; document.pageNumber = [NSNumber numberWithInteger:page];
- CGPoint contentOffset = CGPointMake((theScrollView.bounds.size.width * (page - 1)), 0.0f);
- if (CGPointEqualToPoint(theScrollView.contentOffset, contentOffset) == true)
- [self layoutContentViews:theScrollView];
- else
- [theScrollView setContentOffset:contentOffset];
- [contentViews enumerateKeysAndObjectsUsingBlock:
- ^(NSNumber *key, ReaderContentView *contentView, BOOL *stop)
- {
- if ([key integerValue] != page) [contentView zoomResetAnimated:NO];
- }
- ];
- [mainToolbar setBookmarkState:[document.bookmarks containsIndex:page]];
- [mainPagebar updatePagebar];
- }
- }
- - (void)showDocument
- {
- [self updateContentSize:theScrollView];
- [self showDocumentPage:[document.pageNumber integerValue]];
- document.lastOpen = [NSDate date];
- }
- - (void)closeDocument
- {
- if (printInteraction != nil) [printInteraction dismissAnimated:NO];
- [document archiveDocumentProperties];
- [[ReaderThumbQueue sharedInstance] cancelOperationsWithGUID:document.guid];
- [[ReaderThumbCache sharedInstance] removeAllObjects];
- if ([delegate respondsToSelector:@selector(dismissReaderViewController:)] == YES)
- {
- [delegate dismissReaderViewController:self];
- }
- else
- {
- NSAssert(NO, @"Delegate must respond to -dismissReaderViewController:");
- }
- }
- #pragma mark - UIViewController methods
- - (instancetype)initWithReaderDocument:(ReaderDocument *)object
- {
- if ((self = [super initWithNibName:nil bundle:nil]))
- {
- if ((object != nil) && ([object isKindOfClass:[ReaderDocument class]]))
- {
- userInterfaceIdiom = [UIDevice currentDevice].userInterfaceIdiom;
- NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
- [notificationCenter addObserver:self selector:@selector(applicationWillResign:) name:UIApplicationWillTerminateNotification object:nil];
- [notificationCenter addObserver:self selector:@selector(applicationWillResign:) name:UIApplicationWillResignActiveNotification object:nil];
- scrollViewOutset = ((userInterfaceIdiom == UIUserInterfaceIdiomPad) ? SCROLLVIEW_OUTSET_LARGE : SCROLLVIEW_OUTSET_SMALL);
- [object updateDocumentProperties]; document = object;
- [ReaderThumbCache touchThumbCacheWithGUID:object.guid];
- }
- else
- {
- self = nil;
- }
- }
- return self;
- }
- - (void)dealloc
- {
- [[NSNotificationCenter defaultCenter] removeObserver:self];
- }
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- assert(document != nil);
- self.view.backgroundColor = [UIColor grayColor];
- UIView *fakeStatusBar = nil; CGRect viewRect = self.view.bounds;
- if ([self respondsToSelector:@selector(edgesForExtendedLayout)])
- {
- if ([self prefersStatusBarHidden] == NO)
- {
- CGRect statusBarRect = viewRect; statusBarRect.size.height = STATUS_HEIGHT;
- fakeStatusBar = [[UIView alloc] initWithFrame:statusBarRect];
- fakeStatusBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
- fakeStatusBar.backgroundColor = [UIColor blackColor];
- fakeStatusBar.contentMode = UIViewContentModeRedraw;
- fakeStatusBar.userInteractionEnabled = NO;
- viewRect.origin.y += STATUS_HEIGHT; viewRect.size.height -= STATUS_HEIGHT;
- }
- }
- CGRect scrollViewRect = CGRectInset(viewRect, -scrollViewOutset, 0.0f);
- theScrollView = [[UIScrollView alloc] initWithFrame:scrollViewRect];
- theScrollView.autoresizesSubviews = NO; theScrollView.contentMode = UIViewContentModeRedraw;
- theScrollView.showsHorizontalScrollIndicator = NO; theScrollView.showsVerticalScrollIndicator = NO;
- theScrollView.scrollsToTop = NO; theScrollView.delaysContentTouches = NO; theScrollView.pagingEnabled = YES;
- theScrollView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
- theScrollView.backgroundColor = [UIColor clearColor]; theScrollView.delegate = self;
- [self.view addSubview:theScrollView];
- CGRect toolbarRect = viewRect; toolbarRect.size.height = TOOLBAR_HEIGHT;
- mainToolbar = [[ReaderMainToolbar alloc] initWithFrame:toolbarRect document:document];
- mainToolbar.delegate = self;
-
- CGRect pagebarRect = self.view.bounds; pagebarRect.size.height = PAGEBAR_HEIGHT;
- pagebarRect.origin.y = (self.view.bounds.size.height - pagebarRect.size.height);
- mainPagebar = [[ReaderMainPagebar alloc] initWithFrame:pagebarRect document:document];
- mainPagebar.delegate = self;
- [self.view addSubview:mainPagebar];
- if (fakeStatusBar != nil) [self.view addSubview:fakeStatusBar];
- UITapGestureRecognizer *singleTapOne = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
- singleTapOne.numberOfTouchesRequired = 1; singleTapOne.numberOfTapsRequired = 1; singleTapOne.delegate = self;
- [self.view addGestureRecognizer:singleTapOne];
- UITapGestureRecognizer *doubleTapOne = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
- doubleTapOne.numberOfTouchesRequired = 1; doubleTapOne.numberOfTapsRequired = 2; doubleTapOne.delegate = self;
- [self.view addGestureRecognizer:doubleTapOne];
- UITapGestureRecognizer *doubleTapTwo = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
- doubleTapTwo.numberOfTouchesRequired = 2; doubleTapTwo.numberOfTapsRequired = 2; doubleTapTwo.delegate = self;
- [self.view addGestureRecognizer:doubleTapTwo];
-
- UISwipeGestureRecognizer *swipeUpDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeUpDown:)];
- swipeUpDown.direction = UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionUp;
- [self.view addGestureRecognizer:swipeUpDown];
- [singleTapOne requireGestureRecognizerToFail:doubleTapOne];
- contentViews = [NSMutableDictionary new]; lastHideTime = [NSDate date];
- minimumPage = 1; maximumPage = [document.pageCount integerValue];
- }
- - (void)viewWillAppear:(BOOL)animated
- {
- [super viewWillAppear:animated];
- if (CGSizeEqualToSize(lastAppearSize, CGSizeZero) == false)
- {
- if (CGSizeEqualToSize(lastAppearSize, self.view.bounds.size) == false)
- {
- [self updateContentViews:theScrollView];
- }
- lastAppearSize = CGSizeZero;
- }
- }
- - (void)viewDidAppear:(BOOL)animated
- {
- [super viewDidAppear:animated];
- if (CGSizeEqualToSize(theScrollView.contentSize, CGSizeZero) == true)
- {
- [self performSelector:@selector(showDocument) withObject:nil afterDelay:0.0];
- }
- #if (READER_DISABLE_IDLE == TRUE)
- [UIApplication sharedApplication].idleTimerDisabled = YES;
- #endif
- }
- - (void)viewWillDisappear:(BOOL)animated
- {
- [super viewWillDisappear:animated];
- lastAppearSize = self.view.bounds.size;
- #if (READER_DISABLE_IDLE == TRUE)
- [UIApplication sharedApplication].idleTimerDisabled = NO;
- #endif
- }
- - (void)viewDidDisappear:(BOOL)animated
- {
- [super viewDidDisappear:animated];
- }
- - (void)viewDidUnload
- {
- #ifdef DEBUG
- NSLog(@"%s", __FUNCTION__);
- #endif
- mainToolbar = nil; mainPagebar = nil;
- theScrollView = nil; contentViews = nil; lastHideTime = nil;
- documentInteraction = nil; printInteraction = nil;
- lastAppearSize = CGSizeZero; currentPage = 0;
- [super viewDidUnload];
- }
- - (BOOL)prefersStatusBarHidden
- {
- return YES;
- }
- - (UIStatusBarStyle)preferredStatusBarStyle
- {
- return UIStatusBarStyleLightContent;
- }
- - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
- {
- return YES;
- }
- - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
- {
- if (userInterfaceIdiom == UIUserInterfaceIdiomPad) if (printInteraction != nil) [printInteraction dismissAnimated:NO];
- ignoreDidScroll = YES;
- }
- - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration
- {
- if (CGSizeEqualToSize(theScrollView.contentSize, CGSizeZero) == false)
- {
- [self updateContentViews:theScrollView]; lastAppearSize = CGSizeZero;
- }
- }
- - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
- {
- ignoreDidScroll = NO;
- }
- - (void)updateContentViews
- {
- if (currentPage > 0) {
- [self updateContentViews:theScrollView];
- }
- }
- - (void)didReceiveMemoryWarning
- {
- #ifdef DEBUG
- NSLog(@"%s", __FUNCTION__);
- #endif
- [super didReceiveMemoryWarning];
- }
- #pragma mark - UIScrollViewDelegate methods
- - (void)scrollViewDidScroll:(UIScrollView *)scrollView
- {
- if (ignoreDidScroll == NO) [self layoutContentViews:scrollView];
- }
- - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
- {
- [self handleScrollViewDidEnd:scrollView];
- }
- - (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
- {
- [self handleScrollViewDidEnd:scrollView];
- }
- #pragma mark - UIGestureRecognizerDelegate methods
- - (BOOL)gestureRecognizer:(UIGestureRecognizer *)recognizer shouldReceiveTouch:(UITouch *)touch
- {
- if ([touch.view isKindOfClass:[UIScrollView class]]) return YES;
- return NO;
- }
- #pragma mark - UIGestureRecognizer action methods
- - (void)decrementPageNumber
- {
- if ((maximumPage > minimumPage) && (currentPage != minimumPage))
- {
- CGPoint contentOffset = theScrollView.contentOffset;
- contentOffset.x -= theScrollView.bounds.size.width;
- [theScrollView setContentOffset:contentOffset animated:YES];
- }
- }
- - (void)incrementPageNumber
- {
- if ((maximumPage > minimumPage) && (currentPage != maximumPage))
- {
- CGPoint contentOffset = theScrollView.contentOffset;
- contentOffset.x += theScrollView.bounds.size.width;
- [theScrollView setContentOffset:contentOffset animated:YES];
- }
- }
- - (void)handleSingleTap:(UITapGestureRecognizer *)recognizer
- {
- if (recognizer.state == UIGestureRecognizerStateRecognized)
- {
-
- if ([self.delegate respondsToSelector:@selector(handleSingleTapReader)])
- [self.delegate handleSingleTapReader];
-
- CGRect viewRect = recognizer.view.bounds;
- CGPoint point = [recognizer locationInView:recognizer.view];
- CGRect areaRect = CGRectInset(viewRect, TAP_AREA_SIZE, 0.0f);
- if (CGRectContainsPoint(areaRect, point) == true)
- {
- NSNumber *key = [NSNumber numberWithInteger:currentPage];
- ReaderContentView *targetView = [contentViews objectForKey:key];
- id target = [targetView processSingleTap:recognizer];
- if (target != nil)
- {
- if ([target isKindOfClass:[NSURL class]])
- {
- NSURL *url = (NSURL *)target;
- if (url.scheme == nil)
- {
- NSString *www = url.absoluteString;
- if ([www hasPrefix:@"www"] == YES)
- {
- NSString *http = [[NSString alloc] initWithFormat:@"http://%@", www];
- url = [NSURL URLWithString:http];
- }
- }
- if ([[UIApplication sharedApplication] openURL:url] == NO)
- {
- #ifdef DEBUG
- NSLog(@"%s '%@'", __FUNCTION__, url);
- #endif
- }
- }
- else
- {
- if ([target isKindOfClass:[NSNumber class]])
- {
- NSInteger number = [target integerValue];
- [self showDocumentPage:number];
- }
- }
- }
- else
- {
- if ([lastHideTime timeIntervalSinceNow] < -0.75)
- {
- if ((mainToolbar.alpha < 1.0f) || (mainPagebar.alpha < 1.0f))
- {
- [mainToolbar showToolbar]; [mainPagebar showPagebar];
- }
- }
- }
- return;
- }
- CGRect nextPageRect = viewRect;
- nextPageRect.size.width = TAP_AREA_SIZE;
- nextPageRect.origin.x = (viewRect.size.width - TAP_AREA_SIZE);
- if (CGRectContainsPoint(nextPageRect, point) == true)
- {
- [self incrementPageNumber]; return;
- }
- CGRect prevPageRect = viewRect;
- prevPageRect.size.width = TAP_AREA_SIZE;
- if (CGRectContainsPoint(prevPageRect, point) == true)
- {
- [self decrementPageNumber]; return;
- }
- }
- }
- - (void)handleDoubleTap:(UITapGestureRecognizer *)recognizer
- {
- if (recognizer.state == UIGestureRecognizerStateRecognized)
- {
- CGRect viewRect = recognizer.view.bounds;
- CGPoint point = [recognizer locationInView:recognizer.view];
- CGRect zoomArea = CGRectInset(viewRect, TAP_AREA_SIZE, TAP_AREA_SIZE);
- if (CGRectContainsPoint(zoomArea, point) == true)
- {
- NSNumber *key = [NSNumber numberWithInteger:currentPage];
- ReaderContentView *targetView = [contentViews objectForKey:key];
- switch (recognizer.numberOfTouchesRequired)
- {
- case 1:
- {
- [targetView zoomIncrement:recognizer]; break;
- }
- case 2:
- {
- [targetView zoomDecrement:recognizer]; break;
- }
- }
- return;
- }
- CGRect nextPageRect = viewRect;
- nextPageRect.size.width = TAP_AREA_SIZE;
- nextPageRect.origin.x = (viewRect.size.width - TAP_AREA_SIZE);
- if (CGRectContainsPoint(nextPageRect, point) == true)
- {
- [self incrementPageNumber]; return;
- }
- CGRect prevPageRect = viewRect;
- prevPageRect.size.width = TAP_AREA_SIZE;
- if (CGRectContainsPoint(prevPageRect, point) == true)
- {
- [self decrementPageNumber]; return;
- }
- }
- }
- - (void)handleSwipeUpDown:(UITapGestureRecognizer *)recognizer
- {
-
- if ([self.delegate respondsToSelector:@selector(handleSwipeUpDown)])
- [self.delegate handleSwipeUpDown];
- }
- #pragma mark - ReaderContentViewDelegate methods
- - (void)contentView:(ReaderContentView *)contentView touchesBegan:(NSSet *)touches
- {
- if ((mainToolbar.alpha > 0.0f) || (mainPagebar.alpha > 0.0f))
- {
- if (touches.count == 1)
- {
- UITouch *touch = [touches anyObject];
- CGPoint point = [touch locationInView:self.view];
- CGRect areaRect = CGRectInset(self.view.bounds, TAP_AREA_SIZE, TAP_AREA_SIZE);
- if (CGRectContainsPoint(areaRect, point) == false) return;
- }
- [mainToolbar hideToolbar]; [mainPagebar hidePagebar];
- lastHideTime = [NSDate date];
- }
- }
- #pragma mark - ReaderMainToolbarDelegate methods
- - (void)tappedInToolbar:(ReaderMainToolbar *)toolbar doneButton:(UIButton *)button
- {
- #if (READER_STANDALONE == FALSE)
- [self closeDocument];
- #endif
- }
- - (void)tappedInToolbar:(ReaderMainToolbar *)toolbar thumbsButton:(UIButton *)button
- {
- #if (READER_ENABLE_THUMBS == TRUE)
-
- [[UIApplication sharedApplication] setStatusBarHidden:YES];
-
- if (printInteraction != nil) [printInteraction dismissAnimated:NO];
- ThumbsViewController *thumbsViewController = [[ThumbsViewController alloc] initWithReaderDocument:document];
- thumbsViewController.title = self.title; thumbsViewController.delegate = self;
- thumbsViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
- thumbsViewController.modalPresentationStyle = UIModalPresentationFullScreen;
- [self presentViewController:thumbsViewController animated:NO completion:NULL];
- #endif
- }
- - (void)tappedInToolbar:(ReaderMainToolbar *)toolbar exportButton:(UIButton *)button
- {
- if (printInteraction != nil) [printInteraction dismissAnimated:YES];
- NSURL *fileURL = document.fileURL;
- documentInteraction = [UIDocumentInteractionController interactionControllerWithURL:fileURL];
- documentInteraction.delegate = self;
- [documentInteraction presentOpenInMenuFromRect:button.bounds inView:button animated:YES];
- }
- - (void)tappedInToolbar:(ReaderMainToolbar *)toolbar printButton:(UIButton *)button
- {
- if ([UIPrintInteractionController isPrintingAvailable] == YES)
- {
- NSURL *fileURL = document.fileURL;
- if ([UIPrintInteractionController canPrintURL:fileURL] == YES)
- {
- printInteraction = [UIPrintInteractionController sharedPrintController];
- UIPrintInfo *printInfo = [UIPrintInfo printInfo];
- printInfo.duplex = UIPrintInfoDuplexLongEdge;
- printInfo.outputType = UIPrintInfoOutputGeneral;
- printInfo.jobName = document.fileName;
- printInteraction.printInfo = printInfo;
- printInteraction.printingItem = fileURL;
- printInteraction.showsPageRange = YES;
- if (userInterfaceIdiom == UIUserInterfaceIdiomPad)
- {
- [printInteraction presentFromRect:button.bounds inView:button animated:YES completionHandler:
- ^(UIPrintInteractionController *pic, BOOL completed, NSError *error)
- {
- #ifdef DEBUG
- if ((completed == NO) && (error != nil)) NSLog(@"%s %@", __FUNCTION__, error);
- #endif
- }
- ];
- }
- else
- {
- [printInteraction presentAnimated:YES completionHandler:
- ^(UIPrintInteractionController *pic, BOOL completed, NSError *error)
- {
- #ifdef DEBUG
- if ((completed == NO) && (error != nil)) NSLog(@"%s %@", __FUNCTION__, error);
- #endif
- }
- ];
- }
- }
- }
- }
- - (void)tappedInToolbar:(ReaderMainToolbar *)toolbar emailButton:(UIButton *)button
- {
- if ([MFMailComposeViewController canSendMail] == NO) return;
- if (printInteraction != nil) [printInteraction dismissAnimated:YES];
- unsigned long long fileSize = [document.fileSize unsignedLongLongValue];
- if (fileSize < 15728640ull)
- {
- NSURL *fileURL = document.fileURL; NSString *fileName = document.fileName;
- NSData *attachment = [NSData dataWithContentsOfURL:fileURL options:(NSDataReadingMapped|NSDataReadingUncached) error:nil];
- if (attachment != nil)
- {
- MFMailComposeViewController *mailComposer = [MFMailComposeViewController new];
- [mailComposer addAttachmentData:attachment mimeType:@"application/pdf" fileName:fileName];
- [mailComposer setSubject:fileName];
- mailComposer.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
- mailComposer.modalPresentationStyle = UIModalPresentationFormSheet;
- mailComposer.mailComposeDelegate = self;
- [self presentViewController:mailComposer animated:YES completion:NULL];
- }
- }
- }
- - (void)tappedInToolbar:(ReaderMainToolbar *)toolbar markButton:(UIButton *)button
- {
- #if (READER_BOOKMARKS == TRUE)
- if (printInteraction != nil) [printInteraction dismissAnimated:YES];
- if ([document.bookmarks containsIndex:currentPage])
- {
- [document.bookmarks removeIndex:currentPage]; [mainToolbar setBookmarkState:NO];
- }
- else
- {
- [document.bookmarks addIndex:currentPage]; [mainToolbar setBookmarkState:YES];
- }
- #endif
- }
- #pragma mark - MFMailComposeViewControllerDelegate methods
- - (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
- {
- #ifdef DEBUG
- if ((result == MFMailComposeResultFailed) && (error != NULL)) NSLog(@"%@", error);
- #endif
- [self dismissViewControllerAnimated:YES completion:NULL];
- }
- #pragma mark - UIDocumentInteractionControllerDelegate methods
- - (void)documentInteractionControllerDidDismissOpenInMenu:(UIDocumentInteractionController *)controller
- {
- documentInteraction = nil;
- }
- #pragma mark - ThumbsViewControllerDelegate methods
- - (void)thumbsViewController:(ThumbsViewController *)viewController gotoPage:(NSInteger)page
- {
- #if (READER_ENABLE_THUMBS == TRUE)
- [self showDocumentPage:page];
- #endif
- }
- - (void)dismissThumbsViewController:(ThumbsViewController *)viewController
- {
- #if (READER_ENABLE_THUMBS == TRUE)
- [self dismissViewControllerAnimated:NO completion:NULL];
- #endif
- }
- #pragma mark - ReaderMainPagebarDelegate methods
- - (void)pagebar:(ReaderMainPagebar *)pagebar gotoPage:(NSInteger)page
- {
- [self showDocumentPage:page];
- }
- #pragma mark - UIApplication notification methods
- - (void)applicationWillResign:(NSNotification *)notification
- {
- [document archiveDocumentProperties];
- if (userInterfaceIdiom == UIUserInterfaceIdiomPad) if (printInteraction != nil) [printInteraction dismissAnimated:NO];
- }
- @end
|