readium / r2-testapp-swift

BSD 3-Clause "New" or "Revised" License
146 stars 38 forks source link

How to get the current table of contents item? #341

Closed Supalet closed 3 years ago

mickael-menu commented 4 years ago

If by chapter you mean from the table of contents, then it's not possible at the moment.

However, if you mean the current resource, then:

let totalResources = publication.readingOrder.count

func navigator(_ navigator: Navigator, locationDidChange locator: Locator) {
    let currentResourceIndex = (publication.readingOrder. firstIndex { $0.href == locator.href } ?? 0) + 1
}
Supalet commented 4 years ago

thank @mickael-menu. I would like to ask 1 more question. Can the library get all page numbers in the chapter and page numbers in the currently open chapter?

mickael-menu commented 4 years ago

Yes, we call these "positions" in our architecture, see https://github.com/readium/architecture/tree/master/models/locators/positions

You can use it like that:

// Total number of positions in the publication
let totalPositions = publication.positionList.count

// Total number of positions in one chapter
let totalPositionsInResource = publication.positionListByResource[href].count

func navigator(_ navigator: Navigator, locationDidChange locator: Locator) {
    // Current position in the visible resource (starting from 1)
    locator.locations.position
}

(Note: positionList is named positions on the develop branch)

Supalet commented 4 years ago

IMG_0102 I use locator.locations.position this last page book but i get prosition 19 not 25.

mickael-menu commented 4 years ago

That's normal, we can have more (or less) positions per "screen page", so the last page in this case contains 6 positions, and we report the first position in it (19).

Unfortunately, we don't have any reliable way to report real screen page numbers for reflowable EPUBs, because it depends on the size of the screen, user settings, font size, etc. We could calculate it for the current chapter, but not for the whole publication, so that would be pointless.