-
Notifications
You must be signed in to change notification settings - Fork 116
Deprecate Link.toLocator() in favor of Publication.locatorFromLink()
#83
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -327,7 +327,8 @@ class EpubNavigatorFragment private constructor( | |
| } | ||
|
|
||
| override fun go(link: Link, animated: Boolean, completion: () -> Unit): Boolean { | ||
| return go(link.toLocator(), animated, completion) | ||
| val locator = publication.locatorFromLink(link) ?: return false | ||
| return go(locator, animated, completion) | ||
| } | ||
|
|
||
| private fun run(commands: List<RunScriptCommand>) { | ||
|
|
@@ -638,7 +639,9 @@ class EpubNavigatorFragment private constructor( | |
| get() = publication.metadata.effectiveReadingProgression | ||
|
|
||
| override val currentLocator: StateFlow<Locator> get() = _currentLocator | ||
| private val _currentLocator = MutableStateFlow(initialLocator ?: publication.readingOrder.first().toLocator()) | ||
| private val _currentLocator = MutableStateFlow(initialLocator | ||
| ?: requireNotNull(publication.locatorFromLink(publication.readingOrder.first())) | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A link in the reading order must have a |
||
| ) | ||
|
|
||
| /** | ||
| * While scrolling we receive a lot of new current locations, so we use a coroutine job to | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -64,6 +64,38 @@ data class Manifest( | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Finds the first [Link] with the given HREF in the manifest's links. | ||
| * | ||
| * Searches through (in order) [readingOrder], [resources] and [links] recursively following | ||
| * alternate and children links. | ||
| * | ||
| * If there's no match, try again after removing any query parameter and anchor from the | ||
| * given [href]. | ||
| */ | ||
| fun linkWithHref(href: String): Link? { | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I moved this from |
||
| fun List<Link>.deepLinkWithHref(href: String): Link? { | ||
| for (l in this) { | ||
| if (l.href == href) | ||
| return l | ||
| else { | ||
| l.alternates.deepLinkWithHref(href)?.let { return it } | ||
| l.children.deepLinkWithHref(href)?.let { return it } | ||
| } | ||
| } | ||
| return null | ||
| } | ||
|
|
||
| fun find(href: String): Link? { | ||
| return readingOrder.deepLinkWithHref(href) | ||
| ?: resources.deepLinkWithHref(href) | ||
| ?: links.deepLinkWithHref(href) | ||
| } | ||
|
|
||
| return find(href) | ||
| ?: find(href.takeWhile { it !in "#?" }) | ||
| } | ||
|
|
||
| /** | ||
| * Finds the first [Link] with the given relation in the manifest's links. | ||
| */ | ||
|
|
@@ -78,6 +110,29 @@ data class Manifest( | |
| fun linksWithRel(rel: String): List<Link> = | ||
| (readingOrder + resources + links).filterByRel(rel) | ||
|
|
||
| /** | ||
| * Creates a new [Locator] object from a [Link] to a resource of this manifest. | ||
| * | ||
| * Returns null if the resource is not found in this manifest. | ||
| */ | ||
| fun locatorFromLink(link: Link): Locator? { | ||
| val components = link.href.split("#", limit = 2) | ||
| val href = components.firstOrNull() ?: link.href | ||
| val resourceLink = linkWithHref(href) ?: return null | ||
| val type = resourceLink.type ?: return null | ||
| val fragment = components.getOrNull(1) | ||
|
|
||
| return Locator( | ||
| href = href, | ||
| type = type, | ||
| title = resourceLink.title ?: link.title, | ||
| locations = Locator.Locations( | ||
| fragments = listOfNotNull(fragment), | ||
| progression = if (fragment == null) 0.0 else null | ||
| ) | ||
| ) | ||
| } | ||
|
|
||
| /** | ||
| * Serializes a [Publication] to its RWPM JSON representation. | ||
| */ | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.