forked from Simbul/baker
-
Notifications
You must be signed in to change notification settings - Fork 101
Expand file tree
/
Copy pathBKRIndexViewController.m
More file actions
259 lines (212 loc) · 8.96 KB
/
Copy pathBKRIndexViewController.m
File metadata and controls
259 lines (212 loc) · 8.96 KB
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
//
// IndexViewController.m
// Baker
//
// ==========================================================================================
//
// Copyright (c) 2010-2013, Davide Casali, Marco Colombo, Alessandro Morandi
// Copyright (c) 2014, Andrew Krowczyk, Cédric Mériau, Pieter Claerhout
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
// 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.
// Neither the name of the Baker Framework 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 "BKRIndexViewController.h"
#import "BKRBookViewController.h"
#import "UIScreen+BakerExtensions.h"
@implementation BKRIndexViewController
#pragma mark - Initialization
- (id)initWithBook:(BKRBook*)bakerBook fileName:(NSString*)name webViewDelegate:(UIViewController<UIWebViewDelegate>*)delegate {
self = [super init];
if (self) {
_book = bakerBook;
fileName = name;
webViewDelegate = delegate;
disabled = NO;
indexWidth = 0;
indexHeight = 0;
[self setPageSizeForOrientation:[UIApplication sharedApplication].statusBarOrientation];
}
return self;
}
#pragma mark - View lifecycle
- (void)loadView {
// Initialization to 1x1px is required to get sizeThatFits to work
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 1024, 1, 1)];
webView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
webView.delegate = self;
webView.backgroundColor = [UIColor clearColor];
webView.opaque = NO;
self.view = webView;
for (UIView *subView in webView.subviews) {
if ([subView isKindOfClass:[UIScrollView class]]) {
indexScrollView = (UIScrollView *)subView;
break;
}
}
[self loadContent];
}
- (void)setBounceForWebView:(UIWebView*)webView bounces:(BOOL)bounces {
indexScrollView.bounces = bounces;
}
- (void)setPageSizeForOrientation:(UIInterfaceOrientation)orientation {
pageWidth = [[UIScreen mainScreen] bkrWidthForOrientation:orientation];
pageHeight = [[UIScreen mainScreen] bkrHeightForOrientation:orientation];
NSLog(@"[IndexView] Set IndexView size to %dx%d", pageWidth, pageHeight);
}
- (void)setActualSize {
actualIndexWidth = MIN(indexWidth, pageWidth);
actualIndexHeight = MIN(indexHeight, pageHeight);
}
- (BOOL)isIndexViewHidden {
return ((BKRBookViewController*) webViewDelegate).barsHidden;
}
- (BOOL)isDisabled {
return disabled;
}
- (void)setIndexViewHidden:(BOOL)hidden withAnimation:(BOOL)animation {
CGRect frame;
if (hidden) {
if ([self stickToLeft]) {
frame = CGRectMake(-actualIndexWidth, [self trueY] + pageHeight - actualIndexHeight, actualIndexWidth, actualIndexHeight);
} else {
frame = CGRectMake(0, [self trueY] + pageHeight, actualIndexWidth, actualIndexHeight);
}
} else {
if ([self stickToLeft]) {
frame = CGRectMake(0, [self trueY] + pageHeight - actualIndexHeight, actualIndexWidth, actualIndexHeight);
} else {
frame = CGRectMake(0, [self trueY] + pageHeight - indexHeight, actualIndexWidth, actualIndexHeight);
}
}
if (animation) {
[UIView beginAnimations:@"slideIndexView" context:nil]; {
[UIView setAnimationDuration:0.3];
[self setViewFrame:frame];
}
[UIView commitAnimations];
} else {
[self setViewFrame:frame];
}
}
- (int)trueY {
// Sometimes the origin (0,0) is not where it should be: this horrible hack
// compensates for it, by exploiting the fact that the superview height is
// slightly smaller then the viewport height when the origin's y needs to be adjusted.
int height = self.view.superview.frame.size.height;
if (height == 320 || height == 375 || height == 414 || height == 480 || height == 568 || height == 667 || height == 736 || height == 768 || height == 1024) {
return 0;
} else {
return -20;
}
}
- (void)setViewFrame:(CGRect)frame {
self.view.frame = frame;
indexScrollView.contentSize = cachedContentSize;
}
- (void)fadeOut {
[UIView beginAnimations:@"fadeOutIndexView" context:nil]; {
[UIView setAnimationDuration:0.0];
self.view.alpha = 0.0;
}
[UIView commitAnimations];
}
- (void)fadeIn {
[UIView beginAnimations:@"fadeInIndexView" context:nil]; {
[UIView setAnimationDuration:0.2];
self.view.alpha = 1.0;
}
[UIView commitAnimations];
}
- (void)willRotate {
[self fadeOut];
}
- (void)rotateFromOrientation:(UIInterfaceOrientation)fromInterfaceOrientation toOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
BOOL hidden = [self isIndexViewHidden]; // cache hidden status before setting page size
[self setPageSizeForOrientation:toInterfaceOrientation];
[self setActualSize];
[self setIndexViewHidden:hidden withAnimation:NO];
[self fadeIn];
}
- (void)adjustIndexView {
[self setPageSizeForOrientation:[UIApplication sharedApplication].statusBarOrientation];
[self setActualSize];
[self setIndexViewHidden:self.isIndexViewHidden withAnimation:NO];
}
- (void)loadContent {
NSString* path = self.indexPath;
UIWebView *webView = (UIWebView*)self.view;
webView.mediaPlaybackRequiresUserAction = ![self.book.bakerMediaAutoplay boolValue];
[self setBounceForWebView:webView bounces:[self.book.bakerIndexBounce boolValue]];
//NSLog(@"[IndexView] Path to index view is %@", path);
if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
disabled = NO;
[(UIWebView *)self.view loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]]];
} else {
NSLog(@"[IndexView] Index HTML not found at %@", path);
disabled = YES;
}
}
- (void)webViewDidFinishLoad:(UIWebView*)webView {
id width = self.book.bakerIndexWidth;
id height = self.book.bakerIndexHeight;
if (width != nil) {
indexWidth = (int)[width integerValue];
} else {
indexWidth = [self sizeFromContentOf:webView].width;
}
if (height != nil) {
indexHeight = (int)[height integerValue];
} else {
indexHeight = [self sizeFromContentOf:webView].height;
}
cachedContentSize = indexScrollView.contentSize;
if (cachedContentSize.width < indexWidth) {
cachedContentSize = CGSizeMake(indexWidth, indexHeight);
}
[self setActualSize];
NSLog(@"[IndexView] Set size for IndexView to %dx%d (constrained from %dx%d)", actualIndexWidth, actualIndexHeight, indexWidth, indexHeight);
// After the first load, point the delegate to the main view controller
webView.delegate = webViewDelegate;
[self setIndexViewHidden:[self isIndexViewHidden] withAnimation:NO];
}
- (BOOL)stickToLeft {
return (actualIndexHeight > actualIndexWidth);
}
- (CGSize)sizeFromContentOf:(UIWebView*)webView {
/*
// Setting the frame to 1x1 is required to get meaningful results from sizeThatFits when
// the orientation of the is anything but Portrait.
// See: http://stackoverflow.com/questions/3936041/how-to-determine-the-content-size-of-a-uiwebview/3937599#3937599
CGRect frame = view.frame;
frame.size.width = 1;
frame.size.height = 1;
view.frame = frame;
return [view sizeThatFits:CGSizeZero];
*/
CGFloat contentWidth = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollWidth"] floatValue];
CGFloat contentHeight = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight"] floatValue];
return CGSizeMake(contentWidth, contentHeight);
}
- (NSString*)indexPath {
return [self.book.path stringByAppendingPathComponent:fileName];
}
@end