Skip to content

Commit fefb3cb

Browse files
authored
Fix notifying onPageChanged() when the publication misses positions (#107)
1 parent d24f1d2 commit fefb3cb

File tree

2 files changed

+14
-24
lines changed

2 files changed

+14
-24
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,8 @@ class R2WebView(context: Context, attrs: AttributeSet) : R2BasicWebView(context,
300300
internal fun updateCurrentItem() {
301301
val clientWidth = getClientWidth()
302302
if (!scrollMode && !mIsBeingDragged && clientWidth != null) {
303-
mCurItem = scrollX / clientWidth
303+
// Sometimes scrollX is not exactly a multiple of clientWidth, so we need to round the result.
304+
mCurItem = (scrollX.toDouble() / clientWidth.toDouble()).roundToInt()
304305
}
305306
}
306307

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

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -436,22 +436,6 @@ class EpubNavigatorFragment private constructor(
436436

437437
override fun onPageChanged(pageIndex: Int, totalPages: Int, url: String) {
438438
r2Activity?.onPageChanged(pageIndex = pageIndex, totalPages = totalPages, url = url)
439-
if(paginationListener != null) {
440-
// Find current locator
441-
val resource = publication.readingOrder[resourcePager.currentItem]
442-
val progression = currentFragment?.webView?.progression?.coerceIn(0.0, 1.0) ?: 0.0
443-
val positions = publication.positionsByResource[resource.href]?.takeIf { it.isNotEmpty() }
444-
?: return
445-
446-
val positionIndex = ceil(progression * (positions.size - 1)).toInt()
447-
if (!positions.indices.contains(positionIndex)) {
448-
return
449-
}
450-
451-
val locator = positions[positionIndex].copyWithLocations(progression = progression)
452-
// Pageindex is actually the page number so to get a zero based index we subtract one.
453-
paginationListener.onPageChanged(pageIndex - 1, totalPages, locator)
454-
}
455439
}
456440

457441
override fun onPageEnded(end: Boolean) {
@@ -702,16 +686,17 @@ class EpubNavigatorFragment private constructor(
702686
debounceLocationNotificationJob = launch {
703687
delay(100L)
704688

705-
if (pendingLocator != null) {
689+
val webView = currentFragment?.webView
690+
if (pendingLocator != null || webView == null) {
706691
return@launch
707692
}
708693

709694
// The transition has stabilized, so we can ask the web view to refresh its current
710695
// item to reflect the current scroll position.
711-
currentFragment?.webView?.updateCurrentItem()
696+
webView.updateCurrentItem()
712697

713698
val resource = publication.readingOrder[resourcePager.currentItem]
714-
val progression = currentFragment?.webView?.progression?.coerceIn(0.0, 1.0) ?: 0.0
699+
val progression = webView.progression.coerceIn(0.0, 1.0)
715700
val positions = publication.positionsByResource[resource.href]?.takeIf { it.isNotEmpty() }
716701
?: return@launch
717702

@@ -724,12 +709,16 @@ class EpubNavigatorFragment private constructor(
724709
.copy(title = tableOfContentsTitleByHref[resource.href])
725710
.copyWithLocations(progression = progression)
726711

727-
if (locator == _currentLocator.value) {
728-
return@launch
729-
}
730-
731712
_currentLocator.value = locator
713+
714+
// Deprecated notifications
715+
732716
navigatorDelegate?.locationDidChange(navigator = navigator, locator = locator)
717+
paginationListener?.onPageChanged(
718+
pageIndex = webView.mCurItem,
719+
totalPages = webView.numPages,
720+
locator = locator
721+
)
733722
}
734723
}
735724

0 commit comments

Comments
 (0)