Skip to content

Commit

Permalink
Add custom readingOrder to EpubNavigatorFragment (#376)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrfalch authored Sep 1, 2023
1 parent af78d31 commit 83a424a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -38,6 +39,7 @@ class EpubNavigatorFactory(

fun createFragmentFactory(
initialLocator: Locator?,
readingOrder: List<Link>? = null,
initialPreferences: EpubPreferences = EpubPreferences(),
listener: EpubNavigatorFragment.Listener? = null,
paginationListener: EpubNavigatorFragment.PaginationListener? = null,
Expand All @@ -47,6 +49,7 @@ class EpubNavigatorFactory(
publication = publication,
baseUrl = null,
initialLocator = initialLocator,
readingOrder = readingOrder,
initialPreferences = initialPreferences,
listener = listener,
paginationListener = paginationListener,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ class EpubNavigatorFragment internal constructor(
override val publication: Publication,
private val baseUrl: String?,
private val initialLocator: Locator?,
readingOrder: List<Link>?,
private val initialPreferences: EpubPreferences,
internal val listener: Listener?,
internal val paginationListener: PaginationListener?,
Expand Down Expand Up @@ -274,7 +275,12 @@ class EpubNavigatorFragment internal constructor(
)
}

internal lateinit var positionsByReadingOrder: List<List<Locator>>
private val readingOrder: List<Link> = readingOrder ?: publication.readingOrder

private val positionsByReadingOrder: List<List<Locator>> =
if (readingOrder != null) emptyList()
else runBlocking { publication.positionsByReadingOrder() }

internal lateinit var positions: List<Locator>
lateinit var resourcePager: R2ViewPager

Expand Down Expand Up @@ -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),
Expand All @@ -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))

Expand Down Expand Up @@ -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?
Expand Down Expand Up @@ -919,7 +924,7 @@ class EpubNavigatorFragment internal constructor(
override val currentLocator: StateFlow<Locator> get() = _currentLocator
private val _currentLocator = MutableStateFlow(
initialLocator
?: requireNotNull(publication.locatorFromLink(publication.readingOrder.first()))
?: requireNotNull(publication.locatorFromLink(this.readingOrder.first()))
)

/**
Expand All @@ -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,
Expand Down Expand Up @@ -1035,21 +1040,23 @@ 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.
*/
fun createFactory(
publication: Publication,
baseUrl: String? = null,
initialLocator: Locator? = null,
readingOrder: List<Link>? = null,
listener: Listener? = null,
paginationListener: PaginationListener? = null,
config: Configuration = Configuration(),
initialPreferences: EpubPreferences = EpubPreferences()
): FragmentFactory =
createFragmentFactory {
EpubNavigatorFragment(
publication, baseUrl, initialLocator, initialPreferences,
publication, baseUrl, initialLocator, readingOrder, initialPreferences,
listener, paginationListener,
epubLayout = publication.metadata.presentation.layout ?: EpubLayout.REFLOWABLE,
defaults = EpubDefaults(),
Expand Down

0 comments on commit 83a424a

Please sign in to comment.