Skip to content

Commit e44c60b

Browse files
authored
Extend EPUB setting ranges (#329)
1 parent 97a31af commit e44c60b

File tree

9 files changed

+50
-38
lines changed

9 files changed

+50
-38
lines changed

readium/navigator/src/main/assets/_scripts/src/utils.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ window.addEventListener(
1919
"load",
2020
function () {
2121
const observer = new ResizeObserver(() => {
22-
appendVirtualColumnIfNeeded();
2322
onViewportWidthChanged();
2423
snapCurrentOffset();
2524
});
@@ -71,6 +70,8 @@ function onViewportWidthChanged() {
7170
"--RS__viewportWidth",
7271
"calc(" + width + "px / " + window.devicePixelRatio + ")"
7372
);
73+
74+
appendVirtualColumnIfNeeded();
7475
}
7576

7677
export function getColumnCountPerScreen() {

readium/navigator/src/main/assets/readium/scripts/readium-fixed.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

readium/navigator/src/main/assets/readium/scripts/readium-reflowable.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

readium/navigator/src/main/java/org/readium/r2/navigator/R2BasicWebView.kt

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ open class R2BasicWebView(context: Context, attrs: AttributeSet) : WebView(conte
8686
fun resourceAtUrl(url: String): Resource? = null
8787
}
8888

89-
lateinit var listener: Listener
89+
var listener: Listener? = null
9090
internal var preferences: SharedPreferences? = null
9191
internal var useLegacySettings: Boolean = false
9292

@@ -124,7 +124,7 @@ open class R2BasicWebView(context: Context, attrs: AttributeSet) : WebView(conte
124124
val pageWidth = computeHorizontalScrollExtent()
125125
val contentWidth = computeHorizontalScrollRange()
126126

127-
val isRtl = (listener.readingProgression == ReadingProgression.RTL)
127+
val isRtl = (listener?.readingProgression == ReadingProgression.RTL)
128128

129129
// For RTL, we need to add the equivalent of one page to the x position, otherwise the
130130
// progression will be one page off.
@@ -162,7 +162,7 @@ open class R2BasicWebView(context: Context, attrs: AttributeSet) : WebView(conte
162162

163163
override fun onScrollChanged(l: Int, t: Int, oldl: Int, oldt: Int) {
164164
super.onScrollChanged(l, t, oldl, oldt)
165-
listener.onProgressionChanged()
165+
listener?.onProgressionChanged()
166166
}
167167

168168
override fun onAttachedToWindow() {
@@ -180,6 +180,7 @@ open class R2BasicWebView(context: Context, attrs: AttributeSet) : WebView(conte
180180
@android.webkit.JavascriptInterface
181181
open fun scrollRight(animated: Boolean = false) {
182182
uiScope.launch {
183+
val listener = listener ?: return@launch
183184
listener.onScroll()
184185

185186
fun goRight() {
@@ -205,6 +206,7 @@ open class R2BasicWebView(context: Context, attrs: AttributeSet) : WebView(conte
205206
@android.webkit.JavascriptInterface
206207
open fun scrollLeft(animated: Boolean = false) {
207208
uiScope.launch {
209+
val listener = listener ?: return@launch
208210
listener.onScroll()
209211

210212
fun goLeft() {
@@ -270,7 +272,7 @@ open class R2BasicWebView(context: Context, attrs: AttributeSet) : WebView(conte
270272
true
271273
}
272274
else ->
273-
runBlocking(uiScope.coroutineContext) { listener.onTap(event.point) }
275+
runBlocking(uiScope.coroutineContext) { listener?.onTap(event.point) ?: false }
274276
}
275277
}
276278

@@ -289,7 +291,7 @@ open class R2BasicWebView(context: Context, attrs: AttributeSet) : WebView(conte
289291
return false
290292
}
291293

292-
return listener.onDecorationActivated(id, group, rect, click.point)
294+
return listener?.onDecorationActivated(id, group, rect, click.point) ?: false
293295
}
294296

295297
/** Produced by gestures.js */
@@ -336,7 +338,7 @@ open class R2BasicWebView(context: Context, attrs: AttributeSet) : WebView(conte
336338

337339
val aside = runBlocking {
338340
tryOrLog {
339-
listener.resourceAtUrl(absoluteUrl)
341+
listener?.resourceAtUrl(absoluteUrl)
340342
?.use { res ->
341343
res.readAsString()
342344
.map { Jsoup.parse(it) }
@@ -398,23 +400,23 @@ open class R2BasicWebView(context: Context, attrs: AttributeSet) : WebView(conte
398400
val event = DragEvent.fromJSON(eventJson)?.takeIf { it.isValid }
399401
?: return false
400402

401-
return runBlocking(uiScope.coroutineContext) { listener.onDragStart(event) }
403+
return runBlocking(uiScope.coroutineContext) { listener?.onDragStart(event) ?: false }
402404
}
403405

404406
@android.webkit.JavascriptInterface
405407
fun onDragMove(eventJson: String): Boolean {
406408
val event = DragEvent.fromJSON(eventJson)?.takeIf { it.isValid }
407409
?: return false
408410

409-
return runBlocking(uiScope.coroutineContext) { listener.onDragMove(event) }
411+
return runBlocking(uiScope.coroutineContext) { listener?.onDragMove(event) ?: false }
410412
}
411413

412414
@android.webkit.JavascriptInterface
413415
fun onDragEnd(eventJson: String): Boolean {
414416
val event = DragEvent.fromJSON(eventJson)?.takeIf { it.isValid }
415417
?: return false
416418

417-
return runBlocking(uiScope.coroutineContext) { listener.onDragEnd(event) }
419+
return runBlocking(uiScope.coroutineContext) { listener?.onDragEnd(event) ?: false }
418420
}
419421

420422
@android.webkit.JavascriptInterface
@@ -484,14 +486,14 @@ open class R2BasicWebView(context: Context, attrs: AttributeSet) : WebView(conte
484486
@android.webkit.JavascriptInterface
485487
fun highlightActivated(id: String) {
486488
uiScope.launch {
487-
listener.onHighlightActivated(id)
489+
listener?.onHighlightActivated(id)
488490
}
489491
}
490492

491493
@android.webkit.JavascriptInterface
492494
fun highlightAnnotationMarkActivated(id: String) {
493495
uiScope.launch {
494-
listener.onHighlightAnnotationMarkActivated(id)
496+
listener?.onHighlightAnnotationMarkActivated(id)
495497
}
496498
}
497499

@@ -587,7 +589,7 @@ open class R2BasicWebView(context: Context, attrs: AttributeSet) : WebView(conte
587589
internal fun shouldOverrideUrlLoading(request: WebResourceRequest): Boolean {
588590
if (resourceUrl == request.url?.toString()) return false
589591

590-
return listener.shouldOverrideUrlLoading(this, request)
592+
return listener?.shouldOverrideUrlLoading(this, request) ?: false
591593
}
592594

593595
internal fun shouldInterceptRequest(webView: WebView, request: WebResourceRequest): WebResourceResponse? {
@@ -598,7 +600,7 @@ open class R2BasicWebView(context: Context, attrs: AttributeSet) : WebView(conte
598600
}
599601
}
600602

601-
return listener.shouldInterceptRequest(webView, request)
603+
return listener?.shouldInterceptRequest(webView, request)
602604
}
603605

604606
// Text selection ActionMode overrides
@@ -608,7 +610,7 @@ open class R2BasicWebView(context: Context, attrs: AttributeSet) : WebView(conte
608610
// used by the web view.
609611

610612
override fun startActionMode(callback: ActionMode.Callback?): ActionMode? {
611-
val customCallback = listener.selectionActionModeCallback
613+
val customCallback = listener?.selectionActionModeCallback
612614
?: return super.startActionMode(callback)
613615

614616
val parent = parent ?: return null
@@ -628,7 +630,7 @@ open class R2BasicWebView(context: Context, attrs: AttributeSet) : WebView(conte
628630

629631
@RequiresApi(Build.VERSION_CODES.M)
630632
override fun startActionMode(callback: ActionMode.Callback?, type: Int): ActionMode? {
631-
val customCallback = listener.selectionActionModeCallback
633+
val customCallback = listener?.selectionActionModeCallback
632634
?: return super.startActionMode(callback, type)
633635

634636
val parent = parent ?: return null

readium/navigator/src/main/java/org/readium/r2/navigator/R2WebView.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class R2WebView(context: Context, attrs: AttributeSet) : R2BasicWebView(context,
4646
if (mCurItem < numPages - 1) {
4747
mCurItem++
4848
url?.let {
49-
listener.onPageChanged(mCurItem + 1, numPages, it)
49+
listener?.onPageChanged(mCurItem + 1, numPages, it)
5050
}
5151
}
5252
}
@@ -59,7 +59,7 @@ class R2WebView(context: Context, attrs: AttributeSet) : R2BasicWebView(context,
5959
if (mCurItem > 0) {
6060
mCurItem--
6161
url?.let {
62-
listener.onPageChanged(mCurItem + 1, numPages, it)
62+
listener?.onPageChanged(mCurItem + 1, numPages, it)
6363
}
6464
}
6565
}
@@ -354,7 +354,7 @@ class R2WebView(context: Context, attrs: AttributeSet) : R2BasicWebView(context,
354354

355355
if (post) {
356356
url?.let {
357-
listener.onPageChanged(item + 1, numPages, it)
357+
listener?.onPageChanged(item + 1, numPages, it)
358358
}
359359
}
360360
}

readium/navigator/src/main/java/org/readium/r2/navigator/epub/EpubNavigatorFragment.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,9 @@ class EpubNavigatorFragment internal constructor(
948948
}
949949

950950
private fun notifyCurrentLocation() {
951+
// Make sure viewLifecycleOwner is accessible.
952+
view ?: return
953+
951954
val navigator = this
952955
debounceLocationNotificationJob?.cancel()
953956
debounceLocationNotificationJob = viewLifecycleOwner.lifecycleScope.launch {

readium/navigator/src/main/java/org/readium/r2/navigator/epub/EpubPreferencesEditor.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ class EpubPreferencesEditor internal constructor(
107107
getEffectiveValue = { state.settings.fontSize },
108108
getIsEffective = { layout == EpubLayout.REFLOWABLE },
109109
updateValue = { value -> updateValues { it.copy(fontSize = value) } },
110-
supportedRange = 0.4..5.0,
110+
supportedRange = 0.1..5.0,
111111
progressionStrategy = DoubleIncrement(0.1),
112112
valueFormatter = percentFormatter(),
113113
)
@@ -240,7 +240,7 @@ class EpubPreferencesEditor internal constructor(
240240
getEffectiveValue = { state.settings.pageMargins },
241241
getIsEffective = { layout == EpubLayout.REFLOWABLE },
242242
updateValue = { value -> updateValues { it.copy(pageMargins = value) } },
243-
supportedRange = 0.5..4.0,
243+
supportedRange = 0.0..4.0,
244244
progressionStrategy = DoubleIncrement(0.3),
245245
valueFormatter = { it.format(5) },
246246
)

readium/navigator/src/main/java/org/readium/r2/navigator/pager/R2EpubPageFragment.kt

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -185,13 +185,13 @@ class R2EpubPageFragment : Fragment() {
185185
in topDecile..bottomDecile -> {
186186
if (!endReached) {
187187
endReached = true
188-
webView.listener.onPageEnded(endReached)
188+
webView.listener?.onPageEnded(endReached)
189189
}
190190
}
191191
else -> {
192192
if (endReached) {
193193
endReached = false
194-
webView.listener.onPageEnded(endReached)
194+
webView.listener?.onPageEnded(endReached)
195195
}
196196
}
197197
}
@@ -211,7 +211,7 @@ class R2EpubPageFragment : Fragment() {
211211
override fun onPageFinished(view: WebView?, url: String?) {
212212
super.onPageFinished(view, url)
213213

214-
webView.listener.onResourceLoaded(link, webView, url)
214+
webView.listener?.onResourceLoaded(link, webView, url)
215215

216216
// To make sure the page is properly laid out before jumping to the target locator,
217217
// we execute a dummy JavaScript and wait for the callback result.
@@ -241,7 +241,7 @@ class R2EpubPageFragment : Fragment() {
241241
// Forward a tap event when the web view is not ready to propagate the taps. This allows
242242
// to toggle a navigation UI while a page is loading, for example.
243243
binding.root.setOnClickListenerWithPoint { _, point ->
244-
webView.listener.onTap(point)
244+
webView.listener?.onTap(point)
245245
}
246246

247247
return containerView
@@ -260,6 +260,13 @@ class R2EpubPageFragment : Fragment() {
260260
}
261261
}
262262

263+
override fun onDestroyView() {
264+
webView?.listener = null
265+
_binding = null
266+
267+
super.onDestroyView()
268+
}
269+
263270
override fun onDetach() {
264271
super.onDetach()
265272

@@ -272,11 +279,6 @@ class R2EpubPageFragment : Fragment() {
272279
}
273280
}
274281

275-
override fun onDestroyView() {
276-
super.onDestroyView()
277-
_binding = null
278-
}
279-
280282
private fun setupPadding() {
281283
updatePadding()
282284

@@ -355,7 +357,7 @@ class R2EpubPageFragment : Fragment() {
355357
}
356358
.also { pendingLocator = null }
357359

358-
webView.listener.onPageLoaded()
360+
webView.listener?.onPageLoaded()
359361
}
360362
}
361363

@@ -369,7 +371,7 @@ class R2EpubPageFragment : Fragment() {
369371
val webView = requireNotNull(webView)
370372
val epubNavigator = requireNotNull(navigator)
371373
loadLocator(webView, epubNavigator.readingProgression, locator)
372-
webView.listener.onProgressionChanged()
374+
webView.listener?.onProgressionChanged()
373375
}
374376
}
375377

readium/navigator/src/main/java/org/readium/r2/navigator/pager/R2FXLPageFragment.kt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class R2FXLPageFragment : Fragment() {
3737
private val secondResourceUrl: String?
3838
get() = requireArguments().getString("secondUrl")
3939

40-
private var webViews = mutableListOf<WebView>()
40+
private var webViews = mutableListOf<R2BasicWebView>()
4141

4242
private var _doubleBinding: FragmentFxllayoutDoubleBinding? = null
4343
private val doubleBinding get() = _doubleBinding!!
@@ -74,7 +74,7 @@ class R2FXLPageFragment : Fragment() {
7474
r2FXLLayout.addOnDoubleTapListener(R2FXLOnDoubleTapListener(true))
7575
r2FXLLayout.addOnTapListener(object : R2FXLLayout.OnTapListener {
7676
override fun onTap(view: R2FXLLayout, info: R2FXLLayout.TapInfo): Boolean {
77-
return left.listener.onTap(PointF(info.x, info.y))
77+
return left.listener?.onTap(PointF(info.x, info.y)) ?: false
7878
}
7979
})
8080

@@ -94,7 +94,7 @@ class R2FXLPageFragment : Fragment() {
9494
r2FXLLayout.addOnDoubleTapListener(R2FXLOnDoubleTapListener(true))
9595
r2FXLLayout.addOnTapListener(object : R2FXLLayout.OnTapListener {
9696
override fun onTap(view: R2FXLLayout, info: R2FXLLayout.TapInfo): Boolean {
97-
return webview.listener.onTap(PointF(info.x, info.y))
97+
return webview.listener?.onTap(PointF(info.x, info.y)) ?: false
9898
}
9999
})
100100

@@ -115,9 +115,13 @@ class R2FXLPageFragment : Fragment() {
115115
}
116116

117117
override fun onDestroyView() {
118-
super.onDestroyView()
118+
for (webView in webViews) {
119+
webView.listener = null
120+
}
119121
_singleBinding = null
120122
_doubleBinding = null
123+
124+
super.onDestroyView()
121125
}
122126

123127
@SuppressLint("SetJavaScriptEnabled")

0 commit comments

Comments
 (0)