From f6d9083867eee7f9ecc94373d4d6c4844658c771 Mon Sep 17 00:00:00 2001 From: vincent daubry Date: Mon, 8 Jul 2013 19:08:09 +0200 Subject: [PATCH 1/2] Separate book rendering from reader look and feel, so that we can integrate Readium more easily in another app --- Classes/EPubViewController.h | 27 ++++ Classes/EPubViewController.m | 147 ++++--------------- Classes/ReaderViewController.h | 32 +++++ Classes/ReaderViewController.m | 165 ++++++++++++++++++++++ Classes/SpineItemListController.m | 7 +- SDKLauncher-iOS.xcodeproj/project.pbxproj | 10 ++ 6 files changed, 264 insertions(+), 124 deletions(-) create mode 100644 Classes/ReaderViewController.h create mode 100644 Classes/ReaderViewController.m diff --git a/Classes/EPubViewController.h b/Classes/EPubViewController.h index 2f1914c..577be52 100644 --- a/Classes/EPubViewController.h +++ b/Classes/EPubViewController.h @@ -15,6 +15,25 @@ @class RDPackage; @class RDSpineItem; +@class EPubViewController; + +@protocol EPubViewControllerDelegate + +- (void) epubViewController:(EPubViewController*)spineItemController + didReachEndOfSpineItem:(RDSpineItem *)spineItem; + +- (void) epubViewController:(EPubViewController*)spineItemController + didReachBeginingOfSpineItem:(RDSpineItem *)spineItem; + +- (void) epubViewController:(EPubViewController*)spineItemController + didDisplayPage:(int)pageIndex + totalPage:(int)pageCount + inItem:(RDSpineItem *)spineItem + atItemIndex:(int)spineItemIndex; + + +@end + @interface EPubViewController : BaseViewController < UIAlertViewDelegate, UIWebViewDelegate> @@ -36,6 +55,9 @@ @private UIWebView *m_webView; } +@property (nonatomic, assign) id delegate; + + - (id) initWithContainer:(RDContainer *)container package:(RDPackage *)package; @@ -56,4 +78,9 @@ spineItem:(RDSpineItem *)spineItem cfi:(NSString *)cfi; + +- (void)openNextPage; +- (void)openPrevPage; +- (void)addBookmark; + @end diff --git a/Classes/EPubViewController.m b/Classes/EPubViewController.m index 78f8985..2995bd5 100644 --- a/Classes/EPubViewController.m +++ b/Classes/EPubViewController.m @@ -17,6 +17,7 @@ #import "RDPackage.h" #import "RDPackageResource.h" #import "RDSpineItem.h" +#import "Constants.h" @interface EPubViewController() @@ -241,32 +242,9 @@ - (NSString *)htmlFromData:(NSData *)data { } -- (void)loadView { - self.view = [[[UIView alloc] init] autorelease]; - self.view.backgroundColor = [UIColor whiteColor]; - [[NSNotificationCenter defaultCenter] - addObserver:self - selector:@selector(onProtocolBridgeNeedsResponse:) - name:kSDKLauncherEPubURLProtocolBridgeNeedsResponse - object:nil]; - m_webView = [[[UIWebView alloc] init] autorelease]; - m_webView.delegate = self; - m_webView.hidden = YES; - m_webView.scrollView.bounces = NO; - [self.view addSubview:m_webView]; - - NSString *url = [NSString stringWithFormat:@"%@://%@/%@", - kSDKLauncherWebViewSDKProtocol, - m_package.packageUUID, - m_spineItem.baseHref]; - - [m_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]]; -} - - -- (void)onClickAddBookmark { +- (void)addBookmark { if (m_alertAddBookmark == nil) { m_alertAddBookmark = [[UIAlertView alloc] initWithTitle:LocStr(@"ADD_BOOKMARK_PROMPT_TITLE") @@ -282,12 +260,12 @@ - (void)onClickAddBookmark { } -- (void)onClickNext { +- (void)openNextPage { [m_webView stringByEvaluatingJavaScriptFromString:@"ReadiumSDK.reader.openPageNext()"]; } -- (void)onClickPrev { +- (void)openPrevPage { [m_webView stringByEvaluatingJavaScriptFromString:@"ReadiumSDK.reader.openPagePrev()"]; } @@ -353,94 +331,6 @@ - (void)onProtocolBridgeNeedsResponse:(NSNotification *)notification { } -- (void)updateToolbar { - if (m_webView.hidden) { - self.toolbarItems = nil; - return; - } - - UIBarButtonItem *itemFixed = [[[UIBarButtonItem alloc] - initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace - target:nil - action:nil] autorelease]; - itemFixed.width = 12; - - UIBarButtonItem *itemFlex = [[[UIBarButtonItem alloc] - initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace - target:nil - action:nil] autorelease]; - - static NSString *arrowL = @"\u2190"; - static NSString *arrowR = @"\u2192"; - - UIBarButtonItem *itemNext = [[[UIBarButtonItem alloc] - initWithTitle:m_currentPageProgressionIsLTR ? arrowR : arrowL - style:UIBarButtonItemStylePlain - target:self - action:@selector(onClickNext)] autorelease]; - - UIBarButtonItem *itemPrev = [[[UIBarButtonItem alloc] - initWithTitle:m_currentPageProgressionIsLTR ? arrowL : arrowR - style:UIBarButtonItemStylePlain - target:self - action:@selector(onClickPrev)] autorelease]; - - UIBarButtonItem *itemAddBookmark = [[[UIBarButtonItem alloc] - initWithBarButtonSystemItem:UIBarButtonSystemItemAdd - target:self - action:@selector(onClickAddBookmark)] autorelease]; - - UILabel *label = [[[UILabel alloc] init] autorelease]; - label.backgroundColor = [UIColor clearColor]; - label.font = [UIFont boldSystemFontOfSize:16]; - label.shadowColor = [UIColor colorWithWhite:0 alpha:0.5]; - label.shadowOffset = CGSizeMake(0, -1); - label.textColor = [UIColor whiteColor]; - - if (m_currentPageCount == 0) { - label.text = @""; - itemNext.enabled = NO; - itemPrev.enabled = NO; - } - else { - label.text = LocStr(@"PAGE_X_OF_Y", m_currentPageIndex + 1, m_currentPageCount); - - itemNext.enabled = !( - (m_currentSpineItemIndex + 1 == m_package.spineItems.count) && - (m_currentPageIndex + m_currentOpenPageCount + 1 >= m_currentPageCount) - ); - - itemPrev.enabled = !(m_currentSpineItemIndex == 0 && m_currentPageIndex == 0); - } - - [label sizeToFit]; - - UIBarButtonItem *itemLabel = [[[UIBarButtonItem alloc] - initWithCustomView:label] autorelease]; - - if (m_currentPageProgressionIsLTR) { - self.toolbarItems = @[ - itemPrev, - itemFixed, - itemNext, - itemFixed, - itemLabel, - itemFlex, - itemAddBookmark ]; - } - else { - self.toolbarItems = @[ - itemNext, - itemFixed, - itemPrev, - itemFixed, - itemLabel, - itemFlex, - itemAddBookmark ]; - } -} - - - (void)viewDidLayoutSubviews { m_webView.frame = self.view.bounds; } @@ -449,18 +339,33 @@ - (void)viewDidLayoutSubviews { - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; - if (self.navigationController != nil) { - [self.navigationController setToolbarHidden:NO animated:YES]; - } + self.view = [[[UIView alloc] init] autorelease]; + self.view.backgroundColor = [UIColor whiteColor]; + + [[NSNotificationCenter defaultCenter] + addObserver:self + selector:@selector(onProtocolBridgeNeedsResponse:) + name:kSDKLauncherEPubURLProtocolBridgeNeedsResponse + object:nil]; + + m_webView = [[[UIWebView alloc] init] autorelease]; + m_webView.delegate = self; + m_webView.hidden = YES; + m_webView.scrollView.bounces = NO; + [self.view addSubview:m_webView]; + + NSString *url = [NSString stringWithFormat:@"%@://%@/%@", + kSDKLauncherWebViewSDKProtocol, + m_package.packageUUID, + m_spineItem.baseHref]; + + [m_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]]; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; - if (self.navigationController != nil) { - [self.navigationController setToolbarHidden:YES animated:YES]; - } } @@ -515,7 +420,7 @@ - (void)viewWillDisappear:(BOOL)animated { } m_webView.hidden = NO; - [self updateToolbar]; + [self.delegate epubViewController:self didDisplayPage:m_currentPageIndex totalPage:m_currentPageCount inItem:m_spineItem atItemIndex:m_currentSpineItemIndex]; } } diff --git a/Classes/ReaderViewController.h b/Classes/ReaderViewController.h new file mode 100644 index 0000000..75ba4d1 --- /dev/null +++ b/Classes/ReaderViewController.h @@ -0,0 +1,32 @@ +// +// ReaderViewController.h +// SDKLauncher-iOS +// +// Created by Vincent Daubry on 28/06/13. +// Copyright (c) 2013 The Readium Foundation. All rights reserved. +// + +#import +#import "EPubViewController.h" + +@class RDContainer; +@class RDNavigationElement; +@class RDPackage; +@class RDSpineItem; + + +@interface ReaderViewController : UIViewController { + @private RDContainer *m_container; + @private RDNavigationElement *m_navElement; + @private RDPackage *m_package; + @private RDSpineItem *m_spineItem; + @private EPubViewController *m_epubViewController; +} + +- (id) +initWithContainer:(RDContainer *)container +package:(RDPackage *)package +spineItem:(RDSpineItem *)spineItem; + + +@end diff --git a/Classes/ReaderViewController.m b/Classes/ReaderViewController.m new file mode 100644 index 0000000..2f1dcb3 --- /dev/null +++ b/Classes/ReaderViewController.m @@ -0,0 +1,165 @@ +// +// ReaderViewController.m +// SDKLauncher-iOS +// +// Created by Vincent Daubry on 28/06/13. +// Copyright (c) 2013 The Readium Foundation. All rights reserved. +// + +#import "ReaderViewController.h" +#import "EPubViewController.h" +#import "RDPackage.h" + +@implementation ReaderViewController + + +#pragma mark - ViewController life cycle + + +- (id) +initWithContainer:(RDContainer *)container +package:(RDPackage *)package +spineItem:(RDSpineItem *)spineItem +{ + self = [super init]; + if (self) { + m_container = [container retain]; + m_package = [package retain]; + m_spineItem = [spineItem retain]; + } + return self; +} + + + +- (void)viewDidLoad +{ + [super viewDidLoad]; + [self loadEpub]; +} + + +#pragma mark - Load epub controller + + +- (void) loadEpub { + m_epubViewController = [[EPubViewController alloc] initWithContainer:m_container + package:m_package + spineItem:m_spineItem + cfi:nil]; + m_epubViewController.delegate = self; + [self addChildViewController:m_epubViewController]; + m_epubViewController.view.frame = self.view.bounds; + [self.view addSubview:m_epubViewController.view]; + [m_epubViewController didMoveToParentViewController:self]; + [m_epubViewController release]; +} + + +#pragma mark - Reader Interface + + +- (void)updateToolbarWithPageCount:(int)pageCount currentPageIndex:(int)pageIndex inItemIndex:(int)itemIndex { + + [self.navigationController setToolbarHidden:NO animated:YES]; + + UIBarButtonItem *itemFixed = [[[UIBarButtonItem alloc] + initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace + target:nil + action:nil] autorelease]; + itemFixed.width = 12; + + UIBarButtonItem *itemFlex = [[[UIBarButtonItem alloc] + initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace + target:nil + action:nil] autorelease]; + + static NSString *arrowL = @"\u2190"; + static NSString *arrowR = @"\u2192"; + + UIBarButtonItem *itemNext = [[[UIBarButtonItem alloc] + initWithTitle:arrowR + style:UIBarButtonItemStylePlain + target:self + action:@selector(onClickNext)] autorelease]; + + UIBarButtonItem *itemPrev = [[[UIBarButtonItem alloc] + initWithTitle:arrowL + style:UIBarButtonItemStylePlain + target:self + action:@selector(onClickPrev)] autorelease]; + + UIBarButtonItem *itemAddBookmark = [[[UIBarButtonItem alloc] + initWithBarButtonSystemItem:UIBarButtonSystemItemAdd + target:self + action:@selector(onClickAddBookmark)] autorelease]; + + UILabel *label = [[[UILabel alloc] init] autorelease]; + label.backgroundColor = [UIColor clearColor]; + label.font = [UIFont boldSystemFontOfSize:16]; + label.shadowColor = [UIColor colorWithWhite:0 alpha:0.5]; + label.shadowOffset = CGSizeMake(0, -1); + label.textColor = [UIColor whiteColor]; + + if (pageCount == 0) { + label.text = @""; + itemNext.enabled = NO; + itemPrev.enabled = NO; + } + else { + label.text = [NSString stringWithFormat:@"PAGE %d OF %d", pageIndex + 1, pageCount]; + + itemNext.enabled = (pageIndex < pageCount && itemIndex < m_package.spineItems.count); + + itemPrev.enabled = !(m_spineItem == 0 && pageIndex == 0); + } + + [label sizeToFit]; + + UIBarButtonItem *itemLabel = [[[UIBarButtonItem alloc] + initWithCustomView:label] autorelease]; + + self.toolbarItems = @[ + itemPrev, + itemFixed, + itemNext, + itemFixed, + itemLabel, + itemFlex, + itemAddBookmark ]; +} + + +- (void)onClickNext { + [m_epubViewController openNextPage]; +} + + +- (void)onClickPrev { + [m_epubViewController openPrevPage]; +} + + +- (void)onClickAddBookmark { + [m_epubViewController addBookmark]; +} + + + +#pragma mark - EpubViewControllerDelegate + +- (void) epubViewController:(EPubViewController*)spineItemController didReachEndOfSpineItem:(RDSpineItem *)spineItem { + +} + +- (void) epubViewController:(EPubViewController*)spineItemController didReachBeginingOfSpineItem:(RDSpineItem *)spineItem { + +} + +- (void) epubViewController:(EPubViewController*)spineItemController didDisplayPage:(int)pageIndex totalPage:(int)pageCount inItem:(RDSpineItem *)spineItem atItemIndex:(int)spineItemIndex{ + [self updateToolbarWithPageCount:pageCount currentPageIndex:pageIndex inItemIndex:spineItemIndex]; +} + + + +@end diff --git a/Classes/SpineItemListController.m b/Classes/SpineItemListController.m index 1bb2ca8..722b71e 100644 --- a/Classes/SpineItemListController.m +++ b/Classes/SpineItemListController.m @@ -7,6 +7,7 @@ // #import "SpineItemListController.h" +#import "ReaderViewController.h" #import "EPubViewController.h" #import "RDContainer.h" #import "RDPackage.h" @@ -72,11 +73,11 @@ - (void)loadView { didSelectRowAtIndexPath:(NSIndexPath *)indexPath { RDSpineItem *spineItem = [m_package.spineItems objectAtIndex:indexPath.row]; - EPubViewController *c = [[[EPubViewController alloc] + ReaderViewController *c = [[[ReaderViewController alloc] initWithContainer:m_container package:m_package - spineItem:spineItem - cfi:nil] autorelease]; + spineItem:spineItem] autorelease]; + [self.navigationController pushViewController:c animated:YES]; } diff --git a/SDKLauncher-iOS.xcodeproj/project.pbxproj b/SDKLauncher-iOS.xcodeproj/project.pbxproj index a0bf7c5..171b96b 100644 --- a/SDKLauncher-iOS.xcodeproj/project.pbxproj +++ b/SDKLauncher-iOS.xcodeproj/project.pbxproj @@ -7,6 +7,8 @@ objects = { /* Begin PBXBuildFile section */ + 282A8A891788336700E7E579 /* L.epub in Resources */ = {isa = PBXBuildFile; fileRef = 282A8A881788336700E7E579 /* L.epub */; }; + 28487F8D177DEF0900B53BB7 /* ReaderViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 28487F8C177DEF0900B53BB7 /* ReaderViewController.m */; }; 3403516516CE93F2009E3B88 /* reader.css in Resources */ = {isa = PBXBuildFile; fileRef = 3403516116CE93F2009E3B88 /* reader.css */; }; 3403516616CE93F2009E3B88 /* reader.html in Resources */ = {isa = PBXBuildFile; fileRef = 3403516216CE93F2009E3B88 /* reader.html */; }; 3403516D16CE9FF2009E3B88 /* BundleURLProtocol.m in Sources */ = {isa = PBXBuildFile; fileRef = 3403516C16CE9FF2009E3B88 /* BundleURLProtocol.m */; }; @@ -99,6 +101,9 @@ /* End PBXContainerItemProxy section */ /* Begin PBXFileReference section */ + 282A8A881788336700E7E579 /* L.epub */ = {isa = PBXFileReference; lastKnownFileType = file; path = L.epub; sourceTree = ""; }; + 28487F8B177DEF0900B53BB7 /* ReaderViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ReaderViewController.h; sourceTree = ""; }; + 28487F8C177DEF0900B53BB7 /* ReaderViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ReaderViewController.m; sourceTree = ""; }; 3403516116CE93F2009E3B88 /* reader.css */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.css; path = reader.css; sourceTree = ""; }; 3403516216CE93F2009E3B88 /* reader.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = reader.html; sourceTree = ""; }; 3403516B16CE9FF2009E3B88 /* BundleURLProtocol.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BundleURLProtocol.h; sourceTree = ""; }; @@ -220,6 +225,7 @@ 340535B716BC728800D4A802 /* EPub */ = { isa = PBXGroup; children = ( + 282A8A881788336700E7E579 /* L.epub */, 34B56B5D1723158E00A5A4B8 /* childrens-literature-20130206.epub */, 340535B816BC729200D4A802 /* moby-dick-20120118.epub */, 340535BA16BC793700D4A802 /* wasteland-20120118.epub */, @@ -308,6 +314,8 @@ 34B56B4E1722E54A00A5A4B8 /* Settings.m */, 341652A616C2F9E700AFDB8B /* SpineItemListController.h */, 341652A716C2F9E700AFDB8B /* SpineItemListController.m */, + 28487F8B177DEF0900B53BB7 /* ReaderViewController.h */, + 28487F8C177DEF0900B53BB7 /* ReaderViewController.m */, ); name = Main; sourceTree = ""; @@ -521,6 +529,7 @@ 3403516616CE93F2009E3B88 /* reader.html in Resources */, 340535BB16BC793700D4A802 /* wasteland-20120118.epub in Resources */, 34B53528170B3E60000D384F /* JavaScript in Resources */, + 282A8A891788336700E7E579 /* L.epub in Resources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -560,6 +569,7 @@ 34B56B4F1722E54A00A5A4B8 /* Settings.m in Sources */, 341652A816C2F9E700AFDB8B /* SpineItemListController.m in Sources */, 3411DF03175FACC000FCE6D9 /* EPubViewController.m in Sources */, + 28487F8D177DEF0900B53BB7 /* ReaderViewController.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; From 682a0346c528ff909b75d4c8f26b4b34650d3ffe Mon Sep 17 00:00:00 2001 From: vincent daubry Date: Mon, 8 Jul 2013 21:12:57 +0200 Subject: [PATCH 2/2] extract the bookmark persistence from the book rendering --- Classes/BookmarkListController.m | 17 ++-- Classes/EPubViewController.h | 6 +- Classes/EPubViewController.m | 72 +++------------ Classes/ReaderViewController.h | 6 ++ Classes/ReaderViewController.m | 102 ++++++++++++++++++++-- SDKLauncher-iOS.xcodeproj/project.pbxproj | 4 - 6 files changed, 129 insertions(+), 78 deletions(-) diff --git a/Classes/BookmarkListController.m b/Classes/BookmarkListController.m index 0f75602..0c431cc 100644 --- a/Classes/BookmarkListController.m +++ b/Classes/BookmarkListController.m @@ -12,6 +12,7 @@ #import "EPubViewController.h" #import "RDContainer.h" #import "RDPackage.h" +#import "ReaderViewController.h" @interface BookmarkListController() @@ -123,14 +124,18 @@ - (void)onClickEdit { { Bookmark *bookmark = [m_bookmarks objectAtIndex:indexPath.row]; - EPubViewController *c = [[[EPubViewController alloc] + /*EPubViewController *c = [[[EPubViewController alloc] initWithContainer:m_container package:m_package - bookmark:bookmark] autorelease]; - - if (c != nil) { - [self.navigationController pushViewController:c animated:YES]; - } + bookmark:bookmark] autorelease];*/ + + ReaderViewController *c = [[ReaderViewController alloc] + initWithContainer:m_container + package:m_package + bookmark:bookmark]; + + [self.navigationController pushViewController:c animated:YES]; + [c release]; } diff --git a/Classes/EPubViewController.h b/Classes/EPubViewController.h index 577be52..3ea1f1f 100644 --- a/Classes/EPubViewController.h +++ b/Classes/EPubViewController.h @@ -30,15 +30,15 @@ totalPage:(int)pageCount inItem:(RDSpineItem *)spineItem atItemIndex:(int)spineItemIndex; +- (void) epubViewController:(EPubViewController*)spineItemController + shouldSaveBookMark:(Bookmark*)bookMark; @end @interface EPubViewController : BaseViewController < - UIAlertViewDelegate, UIWebViewDelegate> { - @private UIAlertView *m_alertAddBookmark; @private RDContainer *m_container; @private int m_currentOpenPageCount; @private int m_currentPageCount; @@ -81,6 +81,6 @@ - (void)openNextPage; - (void)openPrevPage; -- (void)addBookmark; +- (NSDictionary*)bookmarkDict; @end diff --git a/Classes/EPubViewController.m b/Classes/EPubViewController.m index 2995bd5..b2bd5b1 100644 --- a/Classes/EPubViewController.m +++ b/Classes/EPubViewController.m @@ -8,7 +8,6 @@ #import "EPubViewController.h" #import "Bookmark.h" -#import "BookmarkDatabase.h" #import "EPubURLProtocolBridge.h" #import "HTMLUtil.h" #import "PackageResourceServer.h" @@ -23,61 +22,18 @@ @interface EPubViewController() - (NSString *)htmlFromData:(NSData *)data; -- (void)updateToolbar; @end @implementation EPubViewController - -- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { - [m_alertAddBookmark autorelease]; - m_alertAddBookmark = nil; - - if (buttonIndex == 1) { - UITextField *textField = [alertView textFieldAtIndex:0]; - - NSString *title = [textField.text stringByTrimmingCharactersInSet: - [NSCharacterSet whitespaceAndNewlineCharacterSet]]; - - NSString *response = [m_webView stringByEvaluatingJavaScriptFromString: - @"ReadiumSDK.reader.bookmarkCurrentPage()"]; - - if (response != nil && response.length > 0) { - NSData *data = [response dataUsingEncoding:NSUTF8StringEncoding]; - NSError *error; - - NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data - options:0 error:&error]; - - Bookmark *bookmark = [[[Bookmark alloc] - initWithCFI:[dict objectForKey:@"contentCFI"] - containerPath:m_container.path - idref:[dict objectForKey:@"idref"] - title:title] autorelease]; - - if (bookmark == nil) { - NSLog(@"The bookmark is nil!"); - } - else { - [[BookmarkDatabase shared] addBookmark:bookmark]; - } - } - } -} +@synthesize delegate; - (void)cleanUp { [[NSNotificationCenter defaultCenter] removeObserver:self]; m_webView = nil; - - if (m_alertAddBookmark != nil) { - m_alertAddBookmark.delegate = nil; - [m_alertAddBookmark dismissWithClickedButtonIndex:999 animated:NO]; - [m_alertAddBookmark release]; - m_alertAddBookmark = nil; - } } @@ -244,19 +200,19 @@ - (NSString *)htmlFromData:(NSData *)data { -- (void)addBookmark { - if (m_alertAddBookmark == nil) { - m_alertAddBookmark = [[UIAlertView alloc] - initWithTitle:LocStr(@"ADD_BOOKMARK_PROMPT_TITLE") - message:nil - delegate:self - cancelButtonTitle:LocStr(@"GENERIC_CANCEL") - otherButtonTitles:LocStr(@"GENERIC_OK"), nil]; - m_alertAddBookmark.alertViewStyle = UIAlertViewStylePlainTextInput; - UITextField *textField = [m_alertAddBookmark textFieldAtIndex:0]; - textField.placeholder = LocStr(@"ADD_BOOKMARK_PROMPT_PLACEHOLDER"); - [m_alertAddBookmark show]; - } +- (NSDictionary*)bookmarkDict { + NSString *response = [m_webView stringByEvaluatingJavaScriptFromString: + @"ReadiumSDK.reader.bookmarkCurrentPage()"]; + NSDictionary *dict = nil; + + if (response != nil && response.length > 0) { + NSData *data = [response dataUsingEncoding:NSUTF8StringEncoding]; + NSError *error; + + dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error]; + } + + return dict; } diff --git a/Classes/ReaderViewController.h b/Classes/ReaderViewController.h index 75ba4d1..50d2406 100644 --- a/Classes/ReaderViewController.h +++ b/Classes/ReaderViewController.h @@ -13,6 +13,7 @@ @class RDNavigationElement; @class RDPackage; @class RDSpineItem; +@class Bookmark; @interface ReaderViewController : UIViewController { @@ -21,6 +22,7 @@ @private RDPackage *m_package; @private RDSpineItem *m_spineItem; @private EPubViewController *m_epubViewController; + @private Bookmark *m_bookmark; } - (id) @@ -28,5 +30,9 @@ initWithContainer:(RDContainer *)container package:(RDPackage *)package spineItem:(RDSpineItem *)spineItem; +- (id) +initWithContainer:(RDContainer *)container +package:(RDPackage *)package +bookmark:(Bookmark *)bookmark; @end diff --git a/Classes/ReaderViewController.m b/Classes/ReaderViewController.m index 2f1dcb3..a5f7784 100644 --- a/Classes/ReaderViewController.m +++ b/Classes/ReaderViewController.m @@ -9,6 +9,11 @@ #import "ReaderViewController.h" #import "EPubViewController.h" #import "RDPackage.h" +#import "RDContainer.h" +#import "RDSpineItem.h" +#import "Bookmark.h" +#import "BookmarkDatabase.h" + @implementation ReaderViewController @@ -31,22 +36,48 @@ @implementation ReaderViewController } +- (id) +initWithContainer:(RDContainer *)container +package:(RDPackage *)package +bookmark:(Bookmark *)bookmark +{ + self = [super init]; + if (self) { + m_container = [container retain]; + m_package = [package retain]; + m_bookmark = [bookmark retain]; + } + return self; + +} + - (void)viewDidLoad { [super viewDidLoad]; - [self loadEpub]; + + NSString *cfi = nil; + if(m_bookmark) { + m_spineItem = [self spineItemFromBookmark]; + cfi = m_bookmark.cfi; + } + + + + [self loadEpubAtCfi:cfi]; } #pragma mark - Load epub controller -- (void) loadEpub { - m_epubViewController = [[EPubViewController alloc] initWithContainer:m_container - package:m_package - spineItem:m_spineItem - cfi:nil]; +- (void) loadEpubAtCfi:(NSString *)cfi { + m_epubViewController = [[EPubViewController alloc] + initWithContainer:m_container + package:m_package + spineItem:m_spineItem + cfi:cfi]; + m_epubViewController.delegate = self; [self addChildViewController:m_epubViewController]; m_epubViewController.view.frame = self.view.bounds; @@ -130,6 +161,10 @@ - (void)updateToolbarWithPageCount:(int)pageCount currentPageIndex:(int)pageInde } + + +#pragma mark - Toolbar actions + - (void)onClickNext { [m_epubViewController openNextPage]; } @@ -141,11 +176,64 @@ - (void)onClickPrev { - (void)onClickAddBookmark { - [m_epubViewController addBookmark]; + UIAlertView *alertAddBookmark = [[UIAlertView alloc] + initWithTitle:LocStr(@"ADD_BOOKMARK_PROMPT_TITLE") + message:nil + delegate:self + cancelButtonTitle:LocStr(@"GENERIC_CANCEL") + otherButtonTitles:LocStr(@"GENERIC_OK"), nil]; + alertAddBookmark.alertViewStyle = UIAlertViewStylePlainTextInput; + UITextField *textField = [alertAddBookmark textFieldAtIndex:0]; + textField.placeholder = LocStr(@"ADD_BOOKMARK_PROMPT_PLACEHOLDER"); + [alertAddBookmark show]; +} + + + +#pragma mark - Bookmark managment + + +- (RDSpineItem*) spineItemFromBookmark { + RDSpineItem *spineItem = nil; + + for (RDSpineItem *currSpineItem in m_package.spineItems) { + if ([currSpineItem.idref isEqualToString:m_bookmark.idref]) { + spineItem = currSpineItem; + break; + } + } + + return spineItem; +} + +- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { + + if (buttonIndex == 1) { + UITextField *textField = [alertView textFieldAtIndex:0]; + + NSString *title = [textField.text stringByTrimmingCharactersInSet: + [NSCharacterSet whitespaceAndNewlineCharacterSet]]; + + NSDictionary *bookmarkDict = [m_epubViewController bookmarkDict]; + + Bookmark *bookmark = [[[Bookmark alloc] + initWithCFI:[bookmarkDict objectForKey:@"contentCFI"] + containerPath:m_container.path + idref:[bookmarkDict objectForKey:@"idref"] + title:title] autorelease]; + + if (bookmark == nil) { + NSLog(@"The bookmark is nil!"); + } + else { + [[BookmarkDatabase shared] addBookmark:bookmark]; + } + } } + #pragma mark - EpubViewControllerDelegate - (void) epubViewController:(EPubViewController*)spineItemController didReachEndOfSpineItem:(RDSpineItem *)spineItem { diff --git a/SDKLauncher-iOS.xcodeproj/project.pbxproj b/SDKLauncher-iOS.xcodeproj/project.pbxproj index 171b96b..d8bb0eb 100644 --- a/SDKLauncher-iOS.xcodeproj/project.pbxproj +++ b/SDKLauncher-iOS.xcodeproj/project.pbxproj @@ -7,7 +7,6 @@ objects = { /* Begin PBXBuildFile section */ - 282A8A891788336700E7E579 /* L.epub in Resources */ = {isa = PBXBuildFile; fileRef = 282A8A881788336700E7E579 /* L.epub */; }; 28487F8D177DEF0900B53BB7 /* ReaderViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 28487F8C177DEF0900B53BB7 /* ReaderViewController.m */; }; 3403516516CE93F2009E3B88 /* reader.css in Resources */ = {isa = PBXBuildFile; fileRef = 3403516116CE93F2009E3B88 /* reader.css */; }; 3403516616CE93F2009E3B88 /* reader.html in Resources */ = {isa = PBXBuildFile; fileRef = 3403516216CE93F2009E3B88 /* reader.html */; }; @@ -101,7 +100,6 @@ /* End PBXContainerItemProxy section */ /* Begin PBXFileReference section */ - 282A8A881788336700E7E579 /* L.epub */ = {isa = PBXFileReference; lastKnownFileType = file; path = L.epub; sourceTree = ""; }; 28487F8B177DEF0900B53BB7 /* ReaderViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ReaderViewController.h; sourceTree = ""; }; 28487F8C177DEF0900B53BB7 /* ReaderViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ReaderViewController.m; sourceTree = ""; }; 3403516116CE93F2009E3B88 /* reader.css */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.css; path = reader.css; sourceTree = ""; }; @@ -225,7 +223,6 @@ 340535B716BC728800D4A802 /* EPub */ = { isa = PBXGroup; children = ( - 282A8A881788336700E7E579 /* L.epub */, 34B56B5D1723158E00A5A4B8 /* childrens-literature-20130206.epub */, 340535B816BC729200D4A802 /* moby-dick-20120118.epub */, 340535BA16BC793700D4A802 /* wasteland-20120118.epub */, @@ -529,7 +526,6 @@ 3403516616CE93F2009E3B88 /* reader.html in Resources */, 340535BB16BC793700D4A802 /* wasteland-20120118.epub in Resources */, 34B53528170B3E60000D384F /* JavaScript in Resources */, - 282A8A891788336700E7E579 /* L.epub in Resources */, ); runOnlyForDeploymentPostprocessing = 0; };