Skip to content

Commit edd9991

Browse files
authored
Improve footnotes handling (#571)
1 parent cfa55e8 commit edd9991

File tree

3 files changed

+14
-34
lines changed

3 files changed

+14
-34
lines changed

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

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ import org.readium.r2.shared.util.Url
4747
import org.readium.r2.shared.util.data.decodeString
4848
import org.readium.r2.shared.util.flatMap
4949
import org.readium.r2.shared.util.resource.Resource
50-
import org.readium.r2.shared.util.toUrl
5150
import org.readium.r2.shared.util.use
5251
import timber.log.Timber
5352

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

9190
@InternalReadiumApi
92-
fun shouldFollowFootnoteLink(url: AbsoluteUrl, context: HyperlinkNavigator.FootnoteContext): Boolean
91+
fun onFootnoteLinkActivated(url: AbsoluteUrl, context: HyperlinkNavigator.FootnoteContext)
9392

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

132131
private val uiScope = CoroutineScope(Dispatchers.Main)
133132

134-
/*
135-
* Url already handled by listener.shouldFollowFootnoteLink,
136-
* Tries to ignore the matching shouldOverrideUrlLoading call.
137-
*/
138-
private var urlNotToOverrideLoading: AbsoluteUrl? = null
139-
140133
init {
141134
setWebContentsDebuggingEnabled(BuildConfig.DEBUG)
142135
}
@@ -382,14 +375,10 @@ internal open class R2BasicWebView(context: Context, attrs: AttributeSet) : WebV
382375
noteContent = safe
383376
)
384377

385-
val shouldFollowLink = listener?.shouldFollowFootnoteLink(absoluteUrl, context) ?: true
386-
387-
if (shouldFollowLink) {
388-
urlNotToOverrideLoading = absoluteUrl
389-
}
378+
listener?.onFootnoteLinkActivated(absoluteUrl, context)
390379

391-
// Consume event if the link should not be followed.
392-
return !shouldFollowLink
380+
// Consume the event to prevent the Webview from loading the link.
381+
return true
393382
}
394383

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

586575
internal fun shouldOverrideUrlLoading(request: WebResourceRequest): Boolean {
587-
val requestUrl = request.url.toUrl() ?: return false
588-
589-
// FIXME: I doubt this can work well. hasGesture considers itself unreliable.
590-
return if (urlNotToOverrideLoading?.isEquivalent(requestUrl) == true && request.hasGesture()) {
591-
urlNotToOverrideLoading = null
592-
false
593-
} else {
594-
listener?.shouldOverrideUrlLoading(this, request) ?: false
595-
}
576+
return listener?.shouldOverrideUrlLoading(this, request) ?: false
596577
}
597578

598579
internal fun shouldInterceptRequest(webView: WebView, request: WebResourceRequest): WebResourceResponse? {

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -859,11 +859,12 @@ public class EpubNavigatorFragment internal constructor(
859859
return true
860860
}
861861

862-
override fun shouldFollowFootnoteLink(
862+
override fun onFootnoteLinkActivated(
863863
url: AbsoluteUrl,
864864
context: HyperlinkNavigator.FootnoteContext
865-
): Boolean =
866-
viewModel.shouldFollowFootnoteLink(url, context)
865+
) {
866+
viewModel.navigateToUrl(url, context)
867+
}
867868

868869
override fun shouldInterceptRequest(webView: WebView, request: WebResourceRequest): WebResourceResponse? =
869870
viewModel.shouldInterceptRequest(request)

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -173,22 +173,20 @@ internal class EpubNavigatorViewModel(
173173
/**
174174
* Intercepts and handles web view navigation to [url].
175175
*/
176-
fun navigateToUrl(url: AbsoluteUrl) = viewModelScope.launch {
176+
fun navigateToUrl(
177+
url: AbsoluteUrl,
178+
context: HyperlinkNavigator.LinkContext? = null
179+
) = viewModelScope.launch {
177180
val link = internalLinkFromUrl(url)
178181
if (link != null) {
179-
if (listener == null || listener.shouldFollowInternalLink(link, null)) {
182+
if (listener == null || listener.shouldFollowInternalLink(link, context)) {
180183
_events.send(Event.OpenInternalLink(link))
181184
}
182185
} else {
183186
listener?.onExternalLinkActivated(url)
184187
}
185188
}
186189

187-
fun shouldFollowFootnoteLink(url: AbsoluteUrl, context: HyperlinkNavigator.FootnoteContext): Boolean {
188-
val link = internalLinkFromUrl(url) ?: return true
189-
return listener?.shouldFollowInternalLink(link, context) ?: true
190-
}
191-
192190
/**
193191
* Gets the publication [Link] targeted by the given [url].
194192
*/

0 commit comments

Comments
 (0)