Skip to content

Commit

Permalink
Improve footnotes handling (#571)
Browse files Browse the repository at this point in the history
  • Loading branch information
qnga authored Oct 22, 2024
1 parent cfa55e8 commit edd9991
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 34 deletions.
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

0 comments on commit edd9991

Please sign in to comment.