Skip to content

Commit 1176cc9

Browse files
authored
Add API Navigator.Listener.onJumpToLocator() (#60)
1 parent aac14c9 commit 1176cc9

File tree

6 files changed

+30
-5
lines changed

6 files changed

+30
-5
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ All notable changes to this project will be documented in this file. Take a look
2424
return true
2525
}
2626
```
27+
* The new `Navigator.Listener.onJumpToLocator()` API is called every time the navigator jumps to an explicit location, which might break the linear reading progression.
28+
* For example, it is called when clicking on internal links or programmatically calling `Navigator.go()`, but not when turning pages.
29+
* You can use this callback to implement a navigation history by differentiating between continuous and discontinuous moves.
2730

2831
### Fixed
2932

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,20 @@ interface Navigator {
7272
*/
7373
fun goBackward(animated: Boolean = false, completion: () -> Unit = {}): Boolean
7474

75-
interface Listener
75+
interface Listener {
76+
77+
/**
78+
* Called when the navigator jumps to an explicit location, which might break the linear
79+
* reading progression.
80+
*
81+
* For example, it is called when clicking on internal links or programmatically calling
82+
* [go], but not when turning pages.
83+
*
84+
* You can use this callback to implement a navigation history by differentiating between
85+
* continuous and discontinuous moves.
86+
*/
87+
fun onJumpToLocator(locator: Locator) {}
88+
}
7689

7790
@Deprecated("Use [currentLocator.value] instead", ReplaceWith("currentLocator.value"))
7891
val currentLocation: Locator? get() = currentLocator.value
@@ -194,4 +207,6 @@ interface MediaNavigator : Navigator {
194207
* Seeks relatively from the current position in the current resource.
195208
*/
196209
fun seekRelative(offset: Duration)
210+
211+
interface Listener : Navigator.Listener
197212
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,8 @@ class EpubNavigatorFragment private constructor(
274274
internal var pendingLocator: Locator? = null
275275

276276
override fun go(locator: Locator, animated: Boolean, completion: () -> Unit): Boolean {
277+
listener?.onJumpToLocator(locator)
278+
277279
val href = locator.href
278280
// Remove anchor
279281
.substringBefore("#")

readium/navigator/src/main/java/org/readium/r2/navigator/image/ImageNavigatorFragment.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ class ImageNavigatorFragment private constructor(
157157
val resourceIndex = publication.readingOrder.indexOfFirstWithHref(locator.href)
158158
?: return false
159159

160+
listener?.onJumpToLocator(locator)
160161
currentPagerPosition = resourceIndex
161162
resourcePager.currentItem = currentPagerPosition
162163

readium/navigator/src/main/java/org/readium/r2/navigator/media/MediaSessionNavigator.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ package org.readium.r2.navigator.media
88

99
import android.media.session.PlaybackState
1010
import android.os.Bundle
11-
import android.os.Handler
12-
import android.os.Looper
1311
import android.support.v4.media.MediaMetadataCompat
1412
import android.support.v4.media.session.MediaControllerCompat
1513
import android.support.v4.media.session.MediaControllerCompat.TransportControls
@@ -43,9 +41,12 @@ private val skipBackwardInterval: Duration = Duration.seconds(30)
4341
class MediaSessionNavigator(
4442
override val publication: Publication,
4543
val publicationId: PublicationId,
46-
val controller: MediaControllerCompat
44+
val controller: MediaControllerCompat,
45+
var listener: Listener? = null
4746
) : MediaNavigator, CoroutineScope by MainScope() {
4847

48+
interface Listener: MediaNavigator.Listener
49+
4950
/**
5051
* Indicates whether the media session is loaded with a resource from this [publication]. This
5152
* is necessary because a single media session could be used to play multiple publications.
@@ -176,6 +177,8 @@ class MediaSessionNavigator(
176177
override fun go(locator: Locator, animated: Boolean, completion: () -> Unit): Boolean {
177178
if (!isActive) return false
178179

180+
listener?.onJumpToLocator(locator)
181+
179182
transportControls.playFromMediaId("$publicationId#${locator.href}", Bundle().apply {
180183
putParcelable("locator", locator)
181184
})

readium/navigator/src/main/java/org/readium/r2/navigator/pdf/PdfNavigatorFragment.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,13 +183,14 @@ class PdfNavigatorFragment internal constructor(
183183
private val _currentLocator = MutableStateFlow(initialLocator ?: publication.readingOrder.first().toLocator())
184184

185185
override fun go(locator: Locator, animated: Boolean, completion: () -> Unit): Boolean {
186+
listener?.onJumpToLocator(locator)
186187
// FIXME: `position` is relative to the full publication, which would cause an issue for a publication containing several PDFs resources. Only publications with a single PDF resource are supported at the moment, so we're fine.
187188
val pageNumber = locator.locations.page ?: locator.locations.position ?: 1
188189
return goToHref(locator.href, pageNumberToIndex(pageNumber), animated, completion)
189190
}
190191

191192
override fun go(link: Link, animated: Boolean, completion: () -> Unit): Boolean =
192-
goToHref(link.href, pageNumberToIndex(1), animated, completion)
193+
go(link.toLocator(), animated = animated, completion = completion)
193194

194195
override fun goForward(animated: Boolean, completion: () -> Unit): Boolean {
195196
val page = pageIndexToNumber(pdfView.currentPage)

0 commit comments

Comments
 (0)