Skip to content
This repository has been archived by the owner on Jul 29, 2022. It is now read-only.

Commit

Permalink
Rename positionList to positions
Browse files Browse the repository at this point in the history
  • Loading branch information
mickael-menu committed Feb 27, 2020
1 parent 9a47fe7 commit 76e3c65
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,10 @@ data class Locator(

companion object {

fun fromJSON(json: JSONObject?, warnings: WarningLogger<JsonWarning>? = null): Locator? {
fun fromJSON(json: JSONObject?): Locator? =
fromJSON(json, null)

internal fun fromJSON(json: JSONObject?, warnings: WarningLogger<JsonWarning>?): Locator? {
val href = json?.optNullableString("href")
val type = json?.optNullableString("type")
if (href == null || type == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import java.net.URL
*
* @param type The kind of publication it is ( Epub, Cbz, ... )
* @param version The version of the publication, if the type needs any.
* @param positionListFactory Factory used to build lazily the [positionList].
* @param positionsFactory Factory used to build lazily the [positions].
*/
@Parcelize
data class Publication(
Expand All @@ -50,7 +50,7 @@ data class Publication(
val resources: List<Link> = emptyList(),
val tableOfContents: List<Link> = emptyList(),
val otherCollections: List<PublicationCollection> = emptyList(),
val positionListFactory: @WriteWith<PositionListFactory.Parceler> PositionListFactory? = null,
val positionsFactory: @WriteWith<PositionListFactory.Parceler> PositionListFactory? = null,

// FIXME: To be refactored, with the TYPE and EXTENSION enums as well
var type: TYPE = TYPE.EPUB,
Expand All @@ -66,7 +66,7 @@ data class Publication(
) : JSONable, Parcelable {

/**
* Creates a [Publication]'s [positionList].
* Creates a [Publication]'s [positions].
*
* The parsers provide an implementation of this interface for each format, but a host app
* might want to use a custom factory to implement, for example, a caching mechanism or use a
Expand All @@ -79,16 +79,16 @@ data class Publication(
* Implementation of a [Parceler] to be used with [@Parcelize] to serialize a
* [PositionListFactory].
*
* Since we can't serialize a factory, we're loading eagerly the [positionList] to be
* Since we can't serialize a factory, we're loading eagerly the [positions] to be
* serialized. Upon deserialization, the positions will be wrapped in a static factory.
*
* This won't be needed anymore once we use [Fragment] instead of [Activity] in the
* navigator.
*/
object Parceler : kotlinx.android.parcel.Parceler<PositionListFactory?> {

private class StaticPositionListFactory(private val positionList: List<Locator>): PositionListFactory {
override fun create(): List<Locator> = positionList
private class StaticPositionListFactory(private val positions: List<Locator>): PositionListFactory {
override fun create(): List<Locator> = positions
}

override fun create(parcel: Parcel): PositionListFactory? =
Expand Down Expand Up @@ -141,16 +141,16 @@ data class Publication(
* List of all the positions in the publication.
*/
@IgnoredOnParcel
val positionList: List<Locator> by lazy {
positionListFactory?.create() ?: emptyList()
val positions: List<Locator> by lazy {
positionsFactory?.create() ?: emptyList()
}

/**
* List of all the positions in each resource, indexed by their [href].
*/
@IgnoredOnParcel
val positionListByResource: Map<String, List<Locator>> by lazy {
positionList.groupBy { it.href }
val positionsByResource: Map<String, List<Locator>> by lazy {
positions.groupBy { it.href }
}

/**
Expand Down Expand Up @@ -274,8 +274,8 @@ data class Publication(
* The provided closure will be used to build the [PositionListFactory], with [this] being the
* [Publication].
*/
fun copyWithPositionListFactory(createFactory: Publication.() -> PositionListFactory): Publication {
return run { copy(positionListFactory = createFactory()) }
fun copyWithPositionsFactory(createFactory: Publication.() -> PositionListFactory): Publication {
return run { copy(positionsFactory = createFactory()) }
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ data class DomRange(

companion object {

fun fromJSON(json: JSONObject?, warnings: WarningLogger<JsonWarning>? = null): Point? {
fun fromJSON(json: JSONObject?): Point? =
fromJSON(json, null)

internal fun fromJSON(json: JSONObject?, warnings: WarningLogger<JsonWarning>?): Point? {
val cssSelector = json?.optNullableString("cssSelector")
val textNodeIndex = json?.optPositiveInt("textNodeIndex")
if (cssSelector == null || textNodeIndex == null) {
Expand Down Expand Up @@ -104,7 +107,10 @@ data class DomRange(

companion object {

fun fromJSON(json: JSONObject?, warnings: WarningLogger<JsonWarning>? = null): DomRange? {
fun fromJSON(json: JSONObject?): DomRange? =
fromJSON(json, null)

internal fun fromJSON(json: JSONObject?, warnings: WarningLogger<JsonWarning>?): DomRange? {
val start = Point.fromJSON(json?.optJSONObject("start"))
if (start == null) {
warnings?.log(DomRange::class.java, "[start] is required", json)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class PublicationTest {
links: List<Link> = listOf(),
readingOrder: List<Link> = emptyList(),
resources: List<Link> = emptyList(),
positionListFactory: Publication.PositionListFactory? = null
positionsFactory: Publication.PositionListFactory? = null
) = Publication(
metadata = Metadata(
localizedTitle = LocalizedString(title),
Expand All @@ -35,7 +35,7 @@ class PublicationTest {
links = links,
readingOrder = readingOrder,
resources = resources,
positionListFactory = positionListFactory
positionsFactory = positionsFactory
)

@Test fun `parse minimal JSON`() {
Expand Down Expand Up @@ -261,23 +261,23 @@ class PublicationTest {
)
}

@Test fun `get the default empty {positionList}`() {
assertEquals(emptyList<Locator>(), createPublication().positionList)
@Test fun `get the default empty {positions}`() {
assertEquals(emptyList<Locator>(), createPublication().positions)
}

@Test fun `get the {positionList} computed from the {positionListFactory}`() {
@Test fun `get the {positions} computed from the {positionsFactory}`() {
assertEquals(
listOf(Locator(href = "locator", type = "")),
createPublication(
positionListFactory = object : Publication.PositionListFactory {
positionsFactory = object : Publication.PositionListFactory {
override fun create(): List<Locator> =
listOf(Locator(href = "locator", type = ""))
}
).positionList
).positions
)
}

@Test fun `get the {positionListByResource} computed from the {positionListFactory}`() {
@Test fun `get the {positionsByResource} computed from the {positionsFactory}`() {
assertEquals(
mapOf(
"res1" to listOf(
Expand All @@ -289,14 +289,14 @@ class PublicationTest {
)
),
createPublication(
positionListFactory = object : Publication.PositionListFactory {
positionsFactory = object : Publication.PositionListFactory {
override fun create(): List<Locator> = listOf(
Locator(href="res1", type = "text/html", title = "Loc A"),
Locator(href="res2", type = "text/html", title = "Loc B"),
Locator(href="res1", type = "text/html", title = "Loc B")
)
}
).positionListByResource
).positionsByResource
)
}

Expand Down

0 comments on commit 76e3c65

Please sign in to comment.