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

Improve footnotes handling #571

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ import org.readium.r2.shared.util.Url
import org.readium.r2.shared.util.data.decodeString
import org.readium.r2.shared.util.flatMap
import org.readium.r2.shared.util.resource.Resource
import org.readium.r2.shared.util.toUrl
import org.readium.r2.shared.util.use
import timber.log.Timber

Expand Down Expand Up @@ -89,7 +88,7 @@ internal open class R2BasicWebView(context: Context, attrs: AttributeSet) : WebV
fun shouldInterceptRequest(webView: WebView, request: WebResourceRequest): WebResourceResponse? = null

@InternalReadiumApi
fun shouldFollowFootnoteLink(url: AbsoluteUrl, context: HyperlinkNavigator.FootnoteContext): Boolean
fun onFootnoteLinkActivated(url: AbsoluteUrl, context: HyperlinkNavigator.FootnoteContext)

@InternalReadiumApi
fun resourceAtUrl(url: Url): Resource? = null
Expand Down Expand Up @@ -131,12 +130,6 @@ internal open class R2BasicWebView(context: Context, attrs: AttributeSet) : WebV

private val uiScope = CoroutineScope(Dispatchers.Main)

/*
* Url already handled by listener.shouldFollowFootnoteLink,
* Tries to ignore the matching shouldOverrideUrlLoading call.
*/
private var urlNotToOverrideLoading: AbsoluteUrl? = null

init {
setWebContentsDebuggingEnabled(BuildConfig.DEBUG)
}
Expand Down Expand Up @@ -382,14 +375,10 @@ internal open class R2BasicWebView(context: Context, attrs: AttributeSet) : WebV
noteContent = safe
)

val shouldFollowLink = listener?.shouldFollowFootnoteLink(absoluteUrl, context) ?: true

if (shouldFollowLink) {
urlNotToOverrideLoading = absoluteUrl
}
listener?.onFootnoteLinkActivated(absoluteUrl, context)

// Consume event if the link should not be followed.
return !shouldFollowLink
// Consume the event to prevent the Webview from loading the link.
return true
}

@android.webkit.JavascriptInterface
Expand Down Expand Up @@ -584,15 +573,7 @@ internal open class R2BasicWebView(context: Context, attrs: AttributeSet) : WebV
}

internal fun shouldOverrideUrlLoading(request: WebResourceRequest): Boolean {
val requestUrl = request.url.toUrl() ?: return false

// FIXME: I doubt this can work well. hasGesture considers itself unreliable.
return if (urlNotToOverrideLoading?.isEquivalent(requestUrl) == true && request.hasGesture()) {
urlNotToOverrideLoading = null
false
} else {
listener?.shouldOverrideUrlLoading(this, request) ?: false
}
return listener?.shouldOverrideUrlLoading(this, request) ?: false
}

internal fun shouldInterceptRequest(webView: WebView, request: WebResourceRequest): WebResourceResponse? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -859,11 +859,12 @@ public class EpubNavigatorFragment internal constructor(
return true
}

override fun shouldFollowFootnoteLink(
override fun onFootnoteLinkActivated(
url: AbsoluteUrl,
context: HyperlinkNavigator.FootnoteContext
): Boolean =
viewModel.shouldFollowFootnoteLink(url, context)
) {
viewModel.navigateToUrl(url, context)
}

override fun shouldInterceptRequest(webView: WebView, request: WebResourceRequest): WebResourceResponse? =
viewModel.shouldInterceptRequest(request)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,22 +173,20 @@ internal class EpubNavigatorViewModel(
/**
* Intercepts and handles web view navigation to [url].
*/
fun navigateToUrl(url: AbsoluteUrl) = viewModelScope.launch {
fun navigateToUrl(
url: AbsoluteUrl,
context: HyperlinkNavigator.LinkContext? = null
) = viewModelScope.launch {
val link = internalLinkFromUrl(url)
if (link != null) {
if (listener == null || listener.shouldFollowInternalLink(link, null)) {
if (listener == null || listener.shouldFollowInternalLink(link, context)) {
_events.send(Event.OpenInternalLink(link))
}
} else {
listener?.onExternalLinkActivated(url)
}
}

fun shouldFollowFootnoteLink(url: AbsoluteUrl, context: HyperlinkNavigator.FootnoteContext): Boolean {
val link = internalLinkFromUrl(url) ?: return true
return listener?.shouldFollowInternalLink(link, context) ?: true
}

/**
* Gets the publication [Link] targeted by the given [url].
*/
Expand Down
Loading