Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix applying preferences while recreating EPUB fragments #507

Merged
merged 3 commits into from
May 3, 2024
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ All notable changes to this project will be documented in this file. Take a look
#### Navigator

* [#325](https://github.com/readium/kotlin-toolkit/issues/325) Top EPUB selections no longer break when dragging the selection handles.
* Fixed applying preferences while the EPUB navigator fragment is being recreated.


## [3.0.0-alpha.2]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,7 @@ public class EpubNavigatorFragment internal constructor(
public suspend fun evaluateJavascript(script: String): String? {
val page = currentReflowablePageFragment ?: return null
page.awaitLoaded()
val webView = page.webView ?: return null
return webView.runJavaScriptSuspend(script)
return page.runJavaScriptSuspend(script)
}

private val viewModel: EpubNavigatorViewModel by viewModels {
Expand Down Expand Up @@ -665,17 +664,17 @@ public class EpubNavigatorFragment internal constructor(
private fun run(command: RunScriptCommand) {
when (command.scope) {
RunScriptCommand.Scope.CurrentResource -> {
currentReflowablePageFragment?.webView
currentReflowablePageFragment
?.runJavaScript(command.script)
}
RunScriptCommand.Scope.LoadedResources -> {
r2PagerAdapter?.mFragments?.forEach { _, fragment ->
(fragment as? R2EpubPageFragment)?.webView
(fragment as? R2EpubPageFragment)
?.runJavaScript(command.script)
}
}
is RunScriptCommand.Scope.Resource -> {
loadedFragmentForHref(command.scope.href)?.webView
loadedFragmentForHref(command.scope.href)
?.runJavaScript(command.script)
}
is RunScriptCommand.Scope.WebView -> {
Expand Down Expand Up @@ -713,9 +712,9 @@ public class EpubNavigatorFragment internal constructor(
// SelectableNavigator

override suspend fun currentSelection(): Selection? {
val webView = currentReflowablePageFragment?.webView ?: return null
val fragment = currentReflowablePageFragment ?: return null
val json =
webView.runJavaScriptSuspend("readium.getCurrentSelection();")
fragment.runJavaScriptSuspend("readium.getCurrentSelection();")
.takeIf { it != "null" }
?.let { tryOrLog { JSONObject(it) } }
?: return null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ package org.readium.r2.navigator.pager

import android.annotation.SuppressLint
import android.graphics.PointF
import android.os.Build
import android.os.Bundle
import android.util.DisplayMetrics
import android.view.*
Expand All @@ -30,6 +29,8 @@ import androidx.lifecycle.repeatOnLifecycle
import androidx.webkit.WebViewClientCompat
import androidx.webkit.WebViewCompat
import androidx.webkit.WebViewFeature
import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine
import kotlin.math.roundToInt
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -213,6 +214,8 @@ internal class R2EpubPageFragment : Fragment() {
override fun onPageFinished(view: WebView?, url: String?) {
super.onPageFinished(view, url)

onPageFinished()

link?.let {
webView.listener?.onResourceLoaded(webView, it)
}
Expand Down Expand Up @@ -249,6 +252,26 @@ internal class R2EpubPageFragment : Fragment() {
return containerView
}

private var isPageFinished = false
private val pendingPageFinished = mutableListOf<() -> Unit>()

/**
* Will run the given [action] when the content of the [WebView] is loaded.
*/
fun whenPageFinished(action: () -> Unit) {
if (isPageFinished) {
action()
} else {
pendingPageFinished.add(action)
}
}

private fun onPageFinished() {
isPageFinished = true
pendingPageFinished.forEach { it() }
pendingPageFinished.clear()
}

/**
* Will run the given [action] when the content of the [WebView] is fully laid out.
*/
Expand Down Expand Up @@ -448,6 +471,18 @@ internal class R2EpubPageFragment : Fragment() {
}
}

fun runJavaScript(script: String, callback: ((String) -> Unit)? = null) {
whenPageFinished {
requireNotNull(webView).runJavaScript(script, callback)
}
}

suspend fun runJavaScriptSuspend(javascript: String): String = suspendCoroutine { cont ->
runJavaScript(javascript) { result ->
cont.resume(result)
}
}

companion object {
private const val textZoomBundleKey = "org.readium.textZoom"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public class OPDS1Parser {
ReplaceWith("parse(jsonData, url.toUrl()!!)"),
DeprecationLevel.ERROR
)
public fun parse(jsonData: ByteArray, url: URL): ParseData =
public fun parse(xmlData: ByteArray, url: URL): ParseData =
throw NotImplementedError()

private fun parseFeed(root: ElementNode, url: Url): Feed {
Expand Down
Loading