Skip to content

Commit f87dd95

Browse files
authored
Add VisualNavigator's shouldJumpToLink listener callback (#375)
1 parent 83a424a commit f87dd95

File tree

4 files changed

+26
-5
lines changed

4 files changed

+26
-5
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ All notable changes to this project will be documented in this file. Take a look
1616
* `WebSettings.textZoom` will work with more publications than `--USER__fontSize`, even the ones poorly authored. However the page width is not adjusted when changing the font size to keep the optimal line length.
1717
* Scroll mode: jumping between two EPUB resources with a horizontal swipe triggers the `Navigator.Listener.onJumpToLocator()` callback.
1818
* This can be used to allow the user to go back to their previous location if they swiped across chapters by mistake.
19+
* Support for non-linear EPUB resources with an opt-in in reading apps (contributed by @chrfalch in [#375](https://github.com/readium/kotlin-toolkit/pull/375) and [#376](https://github.com/readium/kotlin-toolkit/pull/376)).
20+
1. Override loading non-linear resources with `VisualNavigator.Listener.shouldJumpToLink()`.
21+
2. Present a new `EpubNavigatorFragment` by providing a custom `readingOrder` with only this resource to the constructor.
1922

2023
#### Streamer
2124

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,18 @@ interface VisualNavigator : Navigator {
136136
}
137137

138138
interface Listener : Navigator.Listener {
139+
140+
/**
141+
* Called when a link to an internal resource was clicked in the navigator.
142+
*
143+
* You can use this callback to perform custom navigation like opening a new window
144+
* or other operations.
145+
*
146+
* By returning false the navigator wont try to open the link itself and it is up
147+
* to the calling app to decide how to display the link.
148+
*/
149+
fun shouldJumpToLink(link: Link): Boolean { return true }
150+
139151
/**
140152
* Called when the user tapped the content, but nothing handled the event internally (eg.
141153
* by following an internal link).

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ class EpubNavigatorFragment internal constructor(
270270
requireActivity().application, publication,
271271
baseUrl = baseUrl, config = this.config,
272272
initialPreferences = initialPreferences,
273+
listener = listener,
273274
layout = epubLayout,
274275
defaults = defaults
275276
)
@@ -494,7 +495,7 @@ class EpubNavigatorFragment internal constructor(
494495
is EpubNavigatorViewModel.Event.RunScript -> {
495496
run(event.command)
496497
}
497-
is EpubNavigatorViewModel.Event.GoTo -> {
498+
is EpubNavigatorViewModel.Event.OpenInternalLink -> {
498499
go(event.target)
499500
}
500501
EpubNavigatorViewModel.Event.InvalidateViewPager -> {
@@ -1040,7 +1041,8 @@ class EpubNavigatorFragment internal constructor(
10401041
* if you use a local HTTP server.
10411042
* @param initialLocator The first location which should be visible when rendering the
10421043
* publication. Can be used to restore the last reading location.
1043-
* @param readingOrder custom reading order
1044+
* @param readingOrder Custom order of resources to display. Used for example to display a
1045+
* non-linear resource on its own.
10441046
* @param listener Optional listener to implement to observe events, such as user taps.
10451047
* @param config Additional configuration.
10461048
*/

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ internal class EpubNavigatorViewModel(
5252
val config: EpubNavigatorFragment.Configuration,
5353
initialPreferences: EpubPreferences,
5454
val layout: EpubLayout,
55+
val listener: VisualNavigator.Listener?,
5556
private val defaults: EpubDefaults,
5657
baseUrl: String?,
5758
private val server: WebViewServer?,
@@ -76,7 +77,7 @@ internal class EpubNavigatorViewModel(
7677
}
7778

7879
sealed class Event {
79-
data class GoTo(val target: Link) : Event()
80+
data class OpenInternalLink(val target: Link) : Event()
8081
data class OpenExternalLink(val url: Uri) : Event()
8182
/** Refreshes all the resources in the view pager. */
8283
object InvalidateViewPager : Event()
@@ -205,7 +206,9 @@ internal class EpubNavigatorViewModel(
205206
val href = url.toString()
206207
val link = internalLinkFromUrl(href)
207208
if (link != null) {
208-
_events.send(Event.GoTo(link))
209+
if (listener?.shouldJumpToLink(link) == true) {
210+
_events.send(Event.OpenInternalLink(link))
211+
}
209212
} else {
210213
_events.send(Event.OpenExternalLink(url))
211214
}
@@ -384,12 +387,13 @@ internal class EpubNavigatorViewModel(
384387
publication: Publication,
385388
baseUrl: String?,
386389
layout: EpubLayout,
390+
listener: VisualNavigator.Listener?,
387391
defaults: EpubDefaults,
388392
config: EpubNavigatorFragment.Configuration,
389393
initialPreferences: EpubPreferences
390394
) = createViewModelFactory {
391395
EpubNavigatorViewModel(
392-
application, publication, config, initialPreferences, layout,
396+
application, publication, config, initialPreferences, layout, listener,
393397
defaults = defaults,
394398
baseUrl = baseUrl,
395399
server = if (baseUrl != null) null

0 commit comments

Comments
 (0)