Skip to content
This repository was archived by the owner on May 7, 2025. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions Classes/BookmarkListController.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#import "EPubViewController.h"
#import "RDContainer.h"
#import "RDPackage.h"
#import "ReaderViewController.h"


@interface BookmarkListController()
Expand Down Expand Up @@ -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];
}


Expand Down
31 changes: 29 additions & 2 deletions Classes/EPubViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,30 @@
@class RDPackage;
@class RDSpineItem;

@class EPubViewController;

@protocol EPubViewControllerDelegate <NSObject>

- (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;
- (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;
Expand All @@ -36,6 +55,9 @@
@private UIWebView *m_webView;
}

@property (nonatomic, assign) id<EPubViewControllerDelegate> delegate;


- (id)
initWithContainer:(RDContainer *)container
package:(RDPackage *)package;
Expand All @@ -56,4 +78,9 @@
spineItem:(RDSpineItem *)spineItem
cfi:(NSString *)cfi;


- (void)openNextPage;
- (void)openPrevPage;
- (NSDictionary*)bookmarkDict;

@end
217 changes: 39 additions & 178 deletions Classes/EPubViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

#import "EPubViewController.h"
#import "Bookmark.h"
#import "BookmarkDatabase.h"
#import "EPubURLProtocolBridge.h"
#import "HTMLUtil.h"
#import "PackageResourceServer.h"
Expand All @@ -17,66 +16,24 @@
#import "RDPackage.h"
#import "RDPackageResource.h"
#import "RDSpineItem.h"
#import "Constants.h"


@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;
}
}


Expand Down Expand Up @@ -241,53 +198,30 @@ - (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 {
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;
}


- (void)onClickNext {
- (void)openNextPage {
[m_webView stringByEvaluatingJavaScriptFromString:@"ReadiumSDK.reader.openPageNext()"];
}


- (void)onClickPrev {
- (void)openPrevPage {
[m_webView stringByEvaluatingJavaScriptFromString:@"ReadiumSDK.reader.openPagePrev()"];
}

Expand Down Expand Up @@ -353,94 +287,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;
}
Expand All @@ -449,18 +295,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];
}
}


Expand Down Expand Up @@ -515,7 +376,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];
}
}

Expand Down
Loading