kotlin-toolkit icon indicating copy to clipboard operation
kotlin-toolkit copied to clipboard

Locator never updated due to State.Ready not being set in EpubNavigatorFragment

Open Yahoo-Mike opened this issue 1 year ago • 2 comments

Describe the bug

The reading progress observer (navigator.currentLocator) is only triggered once (on onResume()), and never again during user navigation — even though the EPUB UI works and scrolling pages works.

I followed the official guide on observing progress.

What I noticed

  • The fragment is initialized and rendered correctly
  • my callback saveReadingProgression() is called once at startup
  • After that, page changes in the UI do not emit updated Locators
  • In debugging, I noticed this test always returns true due to state != State.Ready

Root cause?

It seems like state is never being set to State.Ready. The condition to transition state in onPageLoaded() never passes this test:

val href = link.url()
if (state is State.Initializing || (state as? State.Loading)?.initialResourceHref?.isEquivalent(href) == true) {
    state = State.Ready
}

At runtime: state = State.Loading("file:///data/user/0/my.package/files/book.epub") href = relative URI (e.g., "OEBPS/9059500800945941315_6130-h-2.htm.html") initialResourceHref = absolute URI ("file:///data/user/0/my.package/files/book.epub")

So isEquivalent() always returns false, and the state never becomes Ready.

Extra info:

I tried using PaginationListener, but onPageChanged() also never fired due to the same issue

How to reproduce?

I followed the official guide on observing progress.

class EpubReaderFragment :  Fragment() {
...
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
...
        if ( childFragmentManager.findFragmentByTag(tag) == null )  {

            val navigatorFactory: EpubNavigatorFactory = initData.navigatorFactory

            childFragmentManager.fragmentFactory =
                navigatorFactory.createFragmentFactory(
                    initialLocator = initData.currentLocation,
                    listener = this
                )
            
            childFragmentManager.commitNow {
                add(R.id.fragment_reader_container, EpubNavigatorFragment::class.java, null, tag)
            }
        }
        navigator = childFragmentManager.findFragmentByTag(tag) as? EpubNavigatorFragment

        // watch for user turning the page
        navigator?.let {
            viewLifecycleOwner.lifecycleScope.launch {
                viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
                    navigator!!.currentLocator
                        .onEach { viewModel.saveReadingProgression(it) }
                        .launchIn(this)
                }
            }
        }
...

Readium version

3.1.1

Android API version

min=28 target=35

Additional context

Questions

  • Am I missing a step in configuring the navigator to ensure this state transition?
  • Should href and initialResourceHref be normalized or resolved to comparable forms?
  • Is this a bug in the state handling logic?

Yahoo-Mike avatar May 23 '25 06:05 Yahoo-Mike

Hello, URI ("file:///data/user/0/my.package/files/book.epub") is definitely not a resource href. You might pass a wrong initial locator. Could you please check that? On the Readium side, when the initial locator is wrong and we don't go there, we should completely ignore it.

qnga avatar May 23 '25 06:05 qnga

You are absolutely right. Thank you for pointing that out.

I was passing a value of Locator(myAbsoluteUrl, MediaType.EPUB) to initialLocator. I incorrectly assumed that was an empty Locator.

When I set initialLocator to null, the program flow works as expected. State.Ready is set and currentLocator emits.

I will close this report.

Yahoo-Mike avatar May 23 '25 21:05 Yahoo-Mike