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

Extend Navigator interface with shouldJumpToLink method #375

Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,17 @@ interface VisualNavigator : Navigator {
*/
fun onTap(point: PointF): Boolean = false

/**
* Called when a link to an internal resource was clicked in the navigator.
*
* You can use this callback to perform custom navigation like opening a new window
* or other operations.
*
* By returning false the navigator wont try to open the link itself and it is up
* to the calling app to decide how to display the link.
*/
fun onShouldJumpToLink(link: Link): Boolean { return true }

/**
* Called when the user starts dragging the content, but nothing handled the event
* internally.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ class EpubNavigatorFragment internal constructor(
requireActivity().application, publication,
baseUrl = baseUrl, config = this.config,
initialPreferences = initialPreferences,
listener = listener,
layout = epubLayout,
defaults = defaults
)
Expand Down Expand Up @@ -494,7 +495,7 @@ class EpubNavigatorFragment internal constructor(
is EpubNavigatorViewModel.Event.RunScript -> {
run(event.command)
}
is EpubNavigatorViewModel.Event.GoTo -> {
is EpubNavigatorViewModel.Event.OpenInternalLink -> {
go(event.target)
}
EpubNavigatorViewModel.Event.InvalidateViewPager -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ internal class EpubNavigatorViewModel(
val config: EpubNavigatorFragment.Configuration,
initialPreferences: EpubPreferences,
val layout: EpubLayout,
val listener: VisualNavigator.Listener?,
private val defaults: EpubDefaults,
baseUrl: String?,
private val server: WebViewServer?,
Expand All @@ -76,7 +77,7 @@ internal class EpubNavigatorViewModel(
}

sealed class Event {
data class GoTo(val target: Link) : Event()
data class OpenInternalLink(val target: Link) : Event()
data class OpenExternalLink(val url: Uri) : Event()
/** Refreshes all the resources in the view pager. */
object InvalidateViewPager : Event()
Expand Down Expand Up @@ -205,7 +206,9 @@ internal class EpubNavigatorViewModel(
val href = url.toString()
val link = internalLinkFromUrl(href)
if (link != null) {
_events.send(Event.GoTo(link))
if (listener?.onShouldJumpToLink(link) == true) {
_events.send(Event.OpenInternalLink(link))
}
} else {
_events.send(Event.OpenExternalLink(url))
}
Expand Down Expand Up @@ -384,12 +387,13 @@ internal class EpubNavigatorViewModel(
publication: Publication,
baseUrl: String?,
layout: EpubLayout,
listener: VisualNavigator.Listener?,
defaults: EpubDefaults,
config: EpubNavigatorFragment.Configuration,
initialPreferences: EpubPreferences
) = createViewModelFactory {
EpubNavigatorViewModel(
application, publication, config, initialPreferences, layout,
application, publication, config, initialPreferences, layout, listener,
defaults = defaults,
baseUrl = baseUrl,
server = if (baseUrl != null) null
Expand Down