From 83a424a1726c23a96f197f28d7ad9340fbc4650b Mon Sep 17 00:00:00 2001 From: Christian Falch <875252+chrfalch@users.noreply.github.com> Date: Fri, 1 Sep 2023 18:05:45 +0200 Subject: [PATCH] Add custom `readingOrder` to `EpubNavigatorFragment` (#376) --- .../r2/navigator/epub/EpubNavigatorFactory.kt | 3 +++ .../navigator/epub/EpubNavigatorFragment.kt | 23 ++++++++++++------- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/readium/navigator/src/main/java/org/readium/r2/navigator/epub/EpubNavigatorFactory.kt b/readium/navigator/src/main/java/org/readium/r2/navigator/epub/EpubNavigatorFactory.kt index e02a03b13c..41714aec1c 100644 --- a/readium/navigator/src/main/java/org/readium/r2/navigator/epub/EpubNavigatorFactory.kt +++ b/readium/navigator/src/main/java/org/readium/r2/navigator/epub/EpubNavigatorFactory.kt @@ -7,6 +7,7 @@ package org.readium.r2.navigator.epub import org.readium.r2.shared.ExperimentalReadiumApi +import org.readium.r2.shared.publication.Link import org.readium.r2.shared.publication.Locator import org.readium.r2.shared.publication.Publication import org.readium.r2.shared.publication.epub.EpubLayout @@ -38,6 +39,7 @@ class EpubNavigatorFactory( fun createFragmentFactory( initialLocator: Locator?, + readingOrder: List? = null, initialPreferences: EpubPreferences = EpubPreferences(), listener: EpubNavigatorFragment.Listener? = null, paginationListener: EpubNavigatorFragment.PaginationListener? = null, @@ -47,6 +49,7 @@ class EpubNavigatorFactory( publication = publication, baseUrl = null, initialLocator = initialLocator, + readingOrder = readingOrder, initialPreferences = initialPreferences, listener = listener, paginationListener = paginationListener, diff --git a/readium/navigator/src/main/java/org/readium/r2/navigator/epub/EpubNavigatorFragment.kt b/readium/navigator/src/main/java/org/readium/r2/navigator/epub/EpubNavigatorFragment.kt index ffb45d513a..54b5e6b5fe 100644 --- a/readium/navigator/src/main/java/org/readium/r2/navigator/epub/EpubNavigatorFragment.kt +++ b/readium/navigator/src/main/java/org/readium/r2/navigator/epub/EpubNavigatorFragment.kt @@ -91,6 +91,7 @@ class EpubNavigatorFragment internal constructor( override val publication: Publication, private val baseUrl: String?, private val initialLocator: Locator?, + readingOrder: List?, private val initialPreferences: EpubPreferences, internal val listener: Listener?, internal val paginationListener: PaginationListener?, @@ -274,7 +275,12 @@ class EpubNavigatorFragment internal constructor( ) } - internal lateinit var positionsByReadingOrder: List> + private val readingOrder: List = readingOrder ?: publication.readingOrder + + private val positionsByReadingOrder: List> = + if (readingOrder != null) emptyList() + else runBlocking { publication.positionsByReadingOrder() } + internal lateinit var positions: List lateinit var resourcePager: R2ViewPager @@ -306,13 +312,12 @@ class EpubNavigatorFragment internal constructor( _binding = ActivityR2ViewpagerBinding.inflate(inflater, container, false) val view = binding.root - positionsByReadingOrder = runBlocking { publication.positionsByReadingOrder() } positions = positionsByReadingOrder.flatten() publicationIdentifier = publication.metadata.identifier ?: publication.metadata.title when (viewModel.layout) { EpubLayout.REFLOWABLE -> { - resourcesSingle = publication.readingOrder.mapIndexed { index, link -> + resourcesSingle = readingOrder.mapIndexed { index, link -> PageResource.EpubReflowable( link = link, url = viewModel.urlTo(link), @@ -329,7 +334,7 @@ class EpubNavigatorFragment internal constructor( var doublePageLeft: Link? = null var doublePageRight: Link? = null - for ((index, link) in publication.readingOrder.withIndex()) { + for ((index, link) in readingOrder.withIndex()) { val url = viewModel.urlTo(link) resourcesSingle.add(PageResource.EpubFxl(leftLink = link, leftUrl = url)) @@ -881,7 +886,7 @@ class EpubNavigatorFragment internal constructor( locatorToResourceAtIndex(resourcePager.currentItem + 1) private fun locatorToResourceAtIndex(index: Int): Locator? = - publication.readingOrder.getOrNull(index) + readingOrder.getOrNull(index) ?.let { publication.locatorFromLink(it) } private val r2PagerAdapter: R2PagerAdapter? @@ -919,7 +924,7 @@ class EpubNavigatorFragment internal constructor( override val currentLocator: StateFlow get() = _currentLocator private val _currentLocator = MutableStateFlow( initialLocator - ?: requireNotNull(publication.locatorFromLink(publication.readingOrder.first())) + ?: requireNotNull(publication.locatorFromLink(this.readingOrder.first())) ) /** @@ -930,7 +935,7 @@ class EpubNavigatorFragment internal constructor( override suspend fun firstVisibleElementLocator(): Locator? { if (!::resourcePager.isInitialized) return null - val resource = publication.readingOrder[resourcePager.currentItem] + val resource = readingOrder[resourcePager.currentItem] return currentReflowablePageFragment?.webView?.findFirstVisibleLocator() ?.copy( href = resource.href, @@ -1035,6 +1040,7 @@ class EpubNavigatorFragment internal constructor( * if you use a local HTTP server. * @param initialLocator The first location which should be visible when rendering the * publication. Can be used to restore the last reading location. + * @param readingOrder custom reading order * @param listener Optional listener to implement to observe events, such as user taps. * @param config Additional configuration. */ @@ -1042,6 +1048,7 @@ class EpubNavigatorFragment internal constructor( publication: Publication, baseUrl: String? = null, initialLocator: Locator? = null, + readingOrder: List? = null, listener: Listener? = null, paginationListener: PaginationListener? = null, config: Configuration = Configuration(), @@ -1049,7 +1056,7 @@ class EpubNavigatorFragment internal constructor( ): FragmentFactory = createFragmentFactory { EpubNavigatorFragment( - publication, baseUrl, initialLocator, initialPreferences, + publication, baseUrl, initialLocator, readingOrder, initialPreferences, listener, paginationListener, epubLayout = publication.metadata.presentation.layout ?: EpubLayout.REFLOWABLE, defaults = EpubDefaults(),