-
Notifications
You must be signed in to change notification settings - Fork 47
/
EPubViewController.m
865 lines (686 loc) · 26.2 KB
/
EPubViewController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
//
// EPubViewController.m
// SDKLauncher-iOS
//
// Created by Shane Meyer on 6/5/13.
// Copyright (c) 2014 Readium Foundation and/or its licensees. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
// 1. Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation and/or
// other materials provided with the distribution.
// 3. Neither the name of the organization nor the names of its contributors may be
// used to endorse or promote products derived from this software without specific
// prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
// IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
// OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
// OF THE POSSIBILITY OF SUCH DAMAGE.
#import "EPubViewController.h"
#import "Bookmark.h"
#import "BookmarkDatabase.h"
#import "EPubSettings.h"
#import "EPubSettingsController.h"
#import "RDContainer.h"
#import "RDNavigationElement.h"
#import "RDPackage.h"
#import "RDPackageResourceServer.h"
#import "RDSpineItem.h"
#import <WebKit/WebKit.h>
@interface EPubViewController () <
RDPackageResourceServerDelegate,
UIAlertViewDelegate,
UIPopoverControllerDelegate,
UIWebViewDelegate,
WKScriptMessageHandler>
{
@private UIAlertView *m_alertAddBookmark;
@private RDContainer *m_container;
@private BOOL m_currentPageCanGoLeft;
@private BOOL m_currentPageCanGoRight;
@private BOOL m_currentPageIsFixedLayout;
@private NSArray* m_currentPageOpenPagesArray;
@private BOOL m_currentPageProgressionIsLTR;
@private int m_currentPageSpineItemCount;
@private NSString *m_initialCFI;
@private BOOL m_moIsPlaying;
@private RDNavigationElement *m_navElement;
@private RDPackage *m_package;
@private UIPopoverController *m_popover;
@private RDPackageResourceServer *m_resourceServer;
@private RDSpineItem *m_spineItem;
@private __weak UIWebView *m_webViewUI;
@private __weak WKWebView *m_webViewWK;
}
@end
@implementation EPubViewController
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
m_alertAddBookmark = nil;
if (buttonIndex == 1) {
UITextField *textField = [alertView textFieldAtIndex:0];
NSString *title = [textField.text stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceAndNewlineCharacterSet]];
[self executeJavaScript:@"ReadiumSDK.reader.bookmarkCurrentPage()"
completionHandler:^(id response, NSError *error)
{
NSString *s = response;
if (error != nil || s == nil || ![s isKindOfClass:[NSString class]] || s.length == 0) {
return;
}
NSData *data = [s dataUsingEncoding:NSUTF8StringEncoding];
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];
if (bookmark == nil) {
NSLog(@"The bookmark is nil!");
}
else {
[[BookmarkDatabase shared] addBookmark:bookmark];
}
}];
}
}
- (void)cleanUp {
if (m_webViewWK != nil) {
[m_webViewWK.configuration.userContentController removeScriptMessageHandlerForName:@"readium"];
}
[[NSNotificationCenter defaultCenter] removeObserver:self];
m_moIsPlaying = NO;
if (m_alertAddBookmark != nil) {
m_alertAddBookmark.delegate = nil;
[m_alertAddBookmark dismissWithClickedButtonIndex:999 animated:NO];
m_alertAddBookmark = nil;
}
if (m_popover != nil) {
[m_popover dismissPopoverAnimated:NO];
m_popover = nil;
}
}
- (BOOL)commonInit {
// Load the special payloads. This is optional (the payloads can be nil), in which case
// MathJax and annotations.css functionality will be disabled.
NSBundle *bundle = [NSBundle mainBundle];
NSString *path = [bundle pathForResource:@"annotations" ofType:@"css"];
NSData *payloadAnnotations = (path == nil) ? nil : [[NSData alloc] initWithContentsOfFile:path];
path = [bundle pathForResource:@"MathJax" ofType:@"js" inDirectory:@"mathjax"];
NSData *payloadMathJax = (path == nil) ? nil : [[NSData alloc] initWithContentsOfFile:path];
m_resourceServer = [[RDPackageResourceServer alloc]
initWithDelegate:self
package:m_package
specialPayloadAnnotationsCSS:payloadAnnotations
specialPayloadMathJaxJS:payloadMathJax];
if (m_resourceServer == nil) {
return NO;
}
// Configure the package's root URL. Rather than "localhost", "127.0.0.1" is specified in the
// following URL to work around an issue introduced in iOS 7.0. When an iOS 7 device is offline
// (Wi-Fi off, or airplane mode on), audio and video fails to be served by UIWebView / QuickTime,
// even though being offline is irrelevant for an embedded HTTP server. Daniel suggested trying
// 127.0.0.1 in case the underlying issue was host name resolution, and it works.
m_package.rootURL = [NSString stringWithFormat:@"http://127.0.0.1:%d/", m_resourceServer.port];
// Observe application background/foreground notifications
// HTTP server becomes unreachable after the application has become inactive
// so we need to stop and restart it whenever it happens
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleAppWillResignActiveNotification:) name:UIApplicationWillResignActiveNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleAppWillEnterForegroundNotification:) name:UIApplicationWillEnterForegroundNotification object:nil];
[self updateNavigationItems];
return YES;
}
- (void)
executeJavaScript:(NSString *)javaScript
completionHandler:(void (^)(id response, NSError *error))completionHandler
{
if (m_webViewUI != nil) {
NSString *response = [m_webViewUI stringByEvaluatingJavaScriptFromString:javaScript];
if (completionHandler != nil) {
completionHandler(response, nil);
}
}
else if (m_webViewWK != nil) {
[m_webViewWK evaluateJavaScript:javaScript completionHandler:^(id response, NSError *error) {
if (error != nil) {
NSLog(@"%@", error);
}
if (completionHandler != nil) {
if ([NSThread isMainThread]) {
completionHandler(response, error);
}
else {
dispatch_async(dispatch_get_main_queue(), ^{
completionHandler(response, error);
});
}
}
}];
}
else if (completionHandler != nil) {
completionHandler(nil, nil);
}
}
- (void)handleMediaOverlayStatusDidChange:(NSString *)payload {
NSData *data = [payload dataUsingEncoding:NSUTF8StringEncoding];
NSError *error = nil;
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
if (error != nil || dict == nil || ![dict isKindOfClass:[NSDictionary class]]) {
NSLog(@"The mediaOverlayStatusDidChange payload is invalid! (%@, %@)", error, dict);
}
else {
NSNumber *n = dict[@"isPlaying"];
if (n != nil && [n isKindOfClass:[NSNumber class]]) {
m_moIsPlaying = n.boolValue;
[self updateToolbar];
}
}
}
- (void)handlePageDidChange:(NSString *)payload {
NSData *data = [payload dataUsingEncoding:NSUTF8StringEncoding];
NSError *error = nil;
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
if (error != nil || dict == nil || ![dict isKindOfClass:[NSDictionary class]]) {
NSLog(@"The pageDidChange payload is invalid! (%@, %@)", error, dict);
}
else {
NSNumber *n = dict[@"canGoLeft_"];
m_currentPageCanGoLeft = [n isKindOfClass:[NSNumber class]] && n.boolValue;
n = dict[@"canGoRight_"];
m_currentPageCanGoRight = [n isKindOfClass:[NSNumber class]] && n.boolValue;
n = dict[@"isRightToLeft"];
m_currentPageProgressionIsLTR = [n isKindOfClass:[NSNumber class]] && !n.boolValue;
n = dict[@"isFixedLayout"];
m_currentPageIsFixedLayout = [n isKindOfClass:[NSNumber class]] && n.boolValue;
n = dict[@"spineItemCount"];
m_currentPageSpineItemCount = [n isKindOfClass:[NSNumber class]] ? n.intValue : 0;
NSArray *array = dict[@"openPages"];
m_currentPageOpenPagesArray = [array isKindOfClass:[NSArray class]] ? array : nil;
if (m_webViewUI != nil) {
m_webViewUI.hidden = NO;
}
else if (m_webViewWK != nil) {
m_webViewWK.hidden = NO;
}
[self updateToolbar];
}
}
- (void)handleReaderDidInitialize {
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
dict[@"package"] = m_package.dictionary;
dict[@"settings"] = [EPubSettings shared].dictionary;
NSDictionary *pageDict = nil;
if (m_spineItem == nil) {
}
else if (m_initialCFI != nil && m_initialCFI.length > 0) {
pageDict = @{
@"idref" : m_spineItem.idref,
@"elementCfi" : m_initialCFI
};
}
else if (m_navElement.content != nil && m_navElement.content.length > 0) {
pageDict = @{
@"contentRefUrl" : m_navElement.content,
@"sourceFileHref" : (m_navElement.sourceHref == nil ?
@"" : m_navElement.sourceHref)
};
}
else {
pageDict = @{
@"idref" : m_spineItem.idref
};
}
if (pageDict != nil) {
dict[@"openPageRequest"] = pageDict;
}
NSData *data = [NSJSONSerialization dataWithJSONObject:dict options:0 error:nil];
if (data != nil) {
NSString *arg = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
arg = [NSString stringWithFormat:@"ReadiumSDK.reader.openBook(%@)", arg];
[self executeJavaScript:arg completionHandler:nil];
}
}
- (instancetype)
initWithContainer:(RDContainer *)container
package:(RDPackage *)package
{
return [self initWithContainer:container package:package spineItem:nil cfi:nil];
}
- (instancetype)
initWithContainer:(RDContainer *)container
package:(RDPackage *)package
bookmark:(Bookmark *)bookmark
{
RDSpineItem *spineItem = nil;
for (RDSpineItem *currSpineItem in package.spineItems) {
if ([currSpineItem.idref isEqualToString:bookmark.idref]) {
spineItem = currSpineItem;
break;
}
}
return [self
initWithContainer:container
package:package
spineItem:spineItem
cfi:bookmark.cfi];
}
- (instancetype)
initWithContainer:(RDContainer *)container
package:(RDPackage *)package
navElement:(RDNavigationElement *)navElement
{
if (container == nil || package == nil) {
return nil;
}
RDSpineItem *spineItem = nil;
if (package.spineItems.count > 0) {
spineItem = [package.spineItems objectAtIndex:0];
}
if (spineItem == nil) {
return nil;
}
if (self = [super initWithTitle:package.title navBarHidden:NO]) {
m_container = container;
m_navElement = navElement;
m_package = package;
m_spineItem = spineItem;
if (![self commonInit]) {
return nil;
}
}
return self;
}
- (instancetype)
initWithContainer:(RDContainer *)container
package:(RDPackage *)package
spineItem:(RDSpineItem *)spineItem
cfi:(NSString *)cfi
{
if (container == nil || package == nil) {
return nil;
}
if (spineItem == nil && package.spineItems.count > 0) {
spineItem = [package.spineItems objectAtIndex:0];
}
if (spineItem == nil) {
return nil;
}
if (self = [super initWithTitle:package.title navBarHidden:NO]) {
m_container = container;
m_initialCFI = cfi;
m_package = package;
m_spineItem = spineItem;
if (![self commonInit]) {
return nil;
}
}
return self;
}
- (void)loadView {
self.view = [[UIView alloc] init];
self.view.backgroundColor = [UIColor whiteColor];
// Notifications
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:@selector(onEPubSettingsDidChange:)
name:kSDKLauncherEPubSettingsDidChange object:nil];
// Create the web view. The choice of web view type is based on the existence of the WKWebView
// class, but this could be decided some other way.
// The "no optimize" RequireJS option means that the entire "readium-shared-js" folder must be copied in to the OSX app bundle's "scripts" folder! (including "node_modules" subfolder, which is populated when invoking the "npm run prepare" build command) There is therefore some significant filesystem / size overhead, but the benefits are significant too: no need for the WebView to fetch sourcemaps, and to attempt to un-mangle the obfuscated Javascript during debugging.
// However, the recommended development-time pattern is to invoke "npm run build" in order to refresh the "build-output" folder, with the RJS_UGLY environment variable set to "false" or "no". This way, the RequireJS single/multiple bundle(s) will be in readable uncompressed form.
//NSString* readerFileName = @"reader_RequireJS-no-optimize.html";
//NSString* readerFileName = @"reader_RequireJS-multiple-bundles.html";
NSString* readerFileName = @"reader_RequireJS-single-bundle.html";
if ([WKWebView class] != nil) {
WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
config.allowsInlineMediaPlayback = YES;
config.mediaPlaybackRequiresUserAction = NO;
// Configure a "readium" message handler, which is used by host_app_feedback.js.
WKUserContentController *contentController = [[WKUserContentController alloc] init];
[contentController addScriptMessageHandler:self name:@"readium"];
config.userContentController = contentController;
WKWebView *webView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:config];
m_webViewWK = webView;
webView.hidden = YES;
webView.scrollView.bounces = NO;
[self.view addSubview:webView];
// RDPackageResourceConnection looks at corePaths and corePrefixes in the following
// query string to determine what core resources it should provide responses for. Since
// WKWebView can't handle file URLs, the web server must provide these resources.
NSString *url = [NSString stringWithFormat:
@"%@%@?"
@"corePaths=readium-shared-js_all.js,readium-shared-js_all.js.map,epubReadingSystem.js,host_app_feedback.js,sdk.css&"
@"corePrefixes=readium-shared-js",
m_package.rootURL,
readerFileName];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
}
else {
UIWebView *webView = [[UIWebView alloc] init];
m_webViewUI = webView;
webView.delegate = self;
webView.hidden = YES;
webView.scalesPageToFit = YES;
webView.scrollView.bounces = NO;
webView.allowsInlineMediaPlayback = YES;
webView.mediaPlaybackRequiresUserAction = NO;
[self.view addSubview:webView];
NSURL *url = [[NSBundle mainBundle] URLForResource:readerFileName withExtension:nil];
[webView loadRequest:[NSURLRequest requestWithURL: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];
}
}
- (void)onClickMONext {
[self executeJavaScript:@"ReadiumSDK.reader.nextMediaOverlay()" completionHandler:nil];
}
- (void)onClickMOPause {
[self executeJavaScript:@"ReadiumSDK.reader.toggleMediaOverlay()" completionHandler:nil];
}
- (void)onClickMOPlay {
[self executeJavaScript:@"ReadiumSDK.reader.toggleMediaOverlay()" completionHandler:nil];
}
- (void)onClickMOPrev {
[self executeJavaScript:@"ReadiumSDK.reader.previousMediaOverlay()" completionHandler:nil];
}
- (void)onClickNext {
[self executeJavaScript:@"ReadiumSDK.reader.openPageNext()" completionHandler:nil];
}
- (void)onClickPrev {
[self executeJavaScript:@"ReadiumSDK.reader.openPagePrev()" completionHandler:nil];
}
- (void)onClickSettings {
EPubSettingsController *c = [[EPubSettingsController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:c];
if (IS_IPAD) {
if (m_popover == nil) {
m_popover = [[UIPopoverController alloc] initWithContentViewController:nav];
m_popover.delegate = self;
[m_popover presentPopoverFromBarButtonItem:self.navigationItem.rightBarButtonItem
permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
}
else {
[self presentViewController:nav animated:YES completion:nil];
}
}
- (void)onEPubSettingsDidChange:(NSNotification *)notification {
[self passSettingsToJavaScript];
}
- (void)
packageResourceServer:(RDPackageResourceServer *)packageResourceServer
executeJavaScript:(NSString *)javaScript
{
if ([NSThread isMainThread]) {
[self executeJavaScript:javaScript completionHandler:nil];
}
else {
dispatch_async(dispatch_get_main_queue(), ^{
[self executeJavaScript:javaScript completionHandler:nil];
});
}
}
- (void)passSettingsToJavaScript {
NSData *data = [NSJSONSerialization dataWithJSONObject:[EPubSettings shared].dictionary
options:0 error:nil];
if (data != nil) {
NSString *s = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
if (s != nil && s.length > 0) {
s = [NSString stringWithFormat:@"ReadiumSDK.reader.updateSettings(%@)", s];
[self executeJavaScript:s completionHandler:nil];
}
}
}
- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController {
m_popover = nil;
}
- (void)updateNavigationItems {
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAction
target:self
action:@selector(onClickSettings)];
}
- (void)updateToolbar {
if ((m_webViewUI != nil && m_webViewUI.hidden) || (m_webViewWK != nil && m_webViewWK.hidden)) {
self.toolbarItems = nil;
return;
}
NSMutableArray *items = [NSMutableArray arrayWithCapacity:8];
UIBarButtonItem *itemFixed = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
target:nil
action:nil];
itemFixed.width = 12;
static NSString *arrowL = @"\u2190";
static NSString *arrowR = @"\u2192";
UIBarButtonItem *itemNext = [[UIBarButtonItem alloc]
initWithTitle:m_currentPageProgressionIsLTR ? arrowR : arrowL
style:UIBarButtonItemStylePlain
target:self
action:@selector(onClickNext)];
UIBarButtonItem *itemPrev = [[UIBarButtonItem alloc]
initWithTitle:m_currentPageProgressionIsLTR ? arrowL : arrowR
style:UIBarButtonItemStylePlain
target:self
action:@selector(onClickPrev)];
if (m_currentPageProgressionIsLTR) {
[items addObject:itemPrev];
[items addObject:itemFixed];
[items addObject:itemNext];
}
else {
[items addObject:itemNext];
[items addObject:itemFixed];
[items addObject:itemPrev];
}
[items addObject:itemFixed];
UILabel *label = [[UILabel alloc] init];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont systemFontOfSize:16];
label.textColor = [UIColor blackColor];
BOOL canGoNext = m_currentPageProgressionIsLTR ? m_currentPageCanGoRight : m_currentPageCanGoLeft;
BOOL canGoPrevious = m_currentPageProgressionIsLTR ? m_currentPageCanGoLeft : m_currentPageCanGoRight;
itemNext.enabled = canGoNext;
itemPrev.enabled = canGoPrevious;
if (m_currentPageOpenPagesArray == nil || [m_currentPageOpenPagesArray count] <= 0) {
label.text = @"";
}
else {
NSMutableArray *pageNumbers = [NSMutableArray array];
for (NSDictionary *pageDict in m_currentPageOpenPagesArray) {
NSNumber *spineItemIndex = [pageDict valueForKey:@"spineItemIndex"];
NSNumber *spineItemPageIndex = [pageDict valueForKey:@"spineItemPageIndex"];
int pageIndex = m_currentPageIsFixedLayout ? spineItemIndex.intValue : spineItemPageIndex.intValue;
[pageNumbers addObject: [NSNumber numberWithInt:pageIndex + 1]];
}
NSString* currentPages = [NSString stringWithFormat:@"%@", [pageNumbers componentsJoinedByString:@"-"]];
int pageCount = 0;
if ([m_currentPageOpenPagesArray count] > 0)
{
NSDictionary *firstOpenPageDict = [m_currentPageOpenPagesArray objectAtIndex:0];
NSNumber *number = [firstOpenPageDict valueForKey:@"spineItemPageCount"];
pageCount = m_currentPageIsFixedLayout ? m_currentPageSpineItemCount: number.intValue;
}
NSString* totalPages = [NSString stringWithFormat:@"%d", pageCount];
label.text = LocStr(@"PAGE_X_OF_Y", [currentPages UTF8String], [totalPages UTF8String], m_currentPageIsFixedLayout?[@"FXL" UTF8String]:[@"reflow" UTF8String]);
}
[label sizeToFit];
[items addObject:[[UIBarButtonItem alloc] initWithCustomView:label]];
[items addObject:[[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil]
];
[self executeJavaScript:@"ReadiumSDK.reader.isMediaOverlayAvailable()"
completionHandler:^(id response, NSError *error)
{
if (error == nil && response != nil && (
([response isKindOfClass:[NSNumber class]] && ((NSNumber *)response).boolValue)
||
([response isKindOfClass:[NSString class]] && [((NSString *)response) isEqualToString:@"true"])
))
{
[items addObject:[[UIBarButtonItem alloc]
initWithTitle:@"<"
style:UIBarButtonItemStylePlain
target:self
action:@selector(onClickMOPrev)]
];
if (m_moIsPlaying) {
[items addObject:[[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemPause
target:self
action:@selector(onClickMOPause)]
];
}
else {
[items addObject:[[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemPlay
target:self
action:@selector(onClickMOPlay)]
];
}
[items addObject:[[UIBarButtonItem alloc]
initWithTitle:@">"
style:UIBarButtonItemStylePlain
target:self
action:@selector(onClickMONext)]
];
[items addObject:itemFixed];
}
[items addObject:[[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:@selector(onClickAddBookmark)]
];
self.toolbarItems = items;
}];
}
- (void)
userContentController:(WKUserContentController *)userContentController
didReceiveScriptMessage:(WKScriptMessage *)message
{
if (![NSThread isMainThread]) {
NSLog(@"A script message unexpectedly arrived on a non-main thread!");
}
NSArray *body = message.body;
if (message.name == nil ||
![message.name isEqualToString:@"readium"] ||
body == nil ||
![body isKindOfClass:[NSArray class]] ||
body.count == 0 ||
![body[0] isKindOfClass:[NSString class]])
{
NSLog(@"Invalid script message! (%@, %@)", message.name, message.body);
return;
}
NSString *messageName = body[0];
if ([messageName isEqualToString:@"mediaOverlayStatusDidChange"]) {
if (body.count < 2 || ![body[1] isKindOfClass:[NSString class]]) {
NSLog(@"The mediaOverlayStatusDidChange payload is invalid!");
}
else {
[self handleMediaOverlayStatusDidChange:body[1]];
}
}
else if ([messageName isEqualToString:@"pageDidChange"]) {
if (body.count < 2 || ![body[1] isKindOfClass:[NSString class]]) {
NSLog(@"The pageDidChange payload is invalid!");
}
else {
[self handlePageDidChange:body[1]];
}
}
else if ([messageName isEqualToString:@"readerDidInitialize"]) {
[self handleReaderDidInitialize];
}
}
- (void)viewDidLayoutSubviews {
CGSize size = self.view.bounds.size;
if (m_webViewUI != nil) {
m_webViewUI.frame = self.view.bounds;
}
else if (m_webViewWK != nil) {
self.automaticallyAdjustsScrollViewInsets = NO;
CGFloat y0 = self.topLayoutGuide.length;
CGFloat y1 = size.height - self.bottomLayoutGuide.length;
m_webViewWK.frame = CGRectMake(0, y0, size.width, y1 - y0);
m_webViewWK.scrollView.contentInset = UIEdgeInsetsZero;
m_webViewWK.scrollView.scrollIndicatorInsets = UIEdgeInsetsZero;
}
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if (self.navigationController != nil) {
[self.navigationController setToolbarHidden:NO animated:YES];
}
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
if (self.navigationController != nil) {
[self.navigationController setToolbarHidden:YES animated:YES];
}
}
- (BOOL)
webView:(UIWebView *)webView
shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType
{
BOOL shouldLoad = YES;
NSString *url = request.URL.absoluteString;
NSString *s = @"epubobjc:";
// When opening the web inspector from Safari (on desktop OSX), the Javascript sourcemaps are requested and fetched automatically based on the location of their source file counterpart. In other words, no need for intercepting requests below (or via NSURLProtocol), unlike the OSX ReadiumSDK launcher app which requires building custom URL responses containing the sourcemap payload. This needs testing with WKWebView though (right now this works fine with UIWebView because local resources are fetched from the file:// app bundle.
if ([url hasSuffix:@".map"]) {
NSLog(@"%@", [NSString stringWithFormat:@"WEBVIEW-REQUESTED SOURCEMAP: %@", url]);
}
if ([url hasPrefix:s]) {
url = [url substringFromIndex:s.length];
shouldLoad = NO;
s = @"mediaOverlayStatusDidChange?q=";
if ([url hasPrefix:s]) {
s = [url substringFromIndex:s.length];
s = [s stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[self handleMediaOverlayStatusDidChange:s];
}
else {
s = @"pageDidChange?q=";
if ([url hasPrefix:s]) {
s = [url substringFromIndex:s.length];
s = [s stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[self handlePageDidChange:s];
}
else if ([url isEqualToString:@"readerDidInitialize"]) {
[self handleReaderDidInitialize];
}
}
}
return shouldLoad;
}
- (void)handleAppWillResignActiveNotification:(NSNotification *)notification {
[m_resourceServer stopHTTPServer];
}
- (void)handleAppWillEnterForegroundNotification:(NSNotification *)notification {
[m_resourceServer startHTTPServer];
}
@end