Skip to content

Commit

Permalink
Simplify partial parsing for entities
Browse files Browse the repository at this point in the history
  • Loading branch information
seadowg committed Oct 15, 2024
1 parent 9c2a5ca commit 5beb718
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 28 deletions.
2 changes: 1 addition & 1 deletion buildSrc/src/main/java/dependencies/Dependencies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ object Dependencies {
const val opencsv = "com.opencsv:opencsv:5.9"
const val javarosa_online = "org.getodk:javarosa:5.0.0-SNAPSHOT-30ef2a9"
const val javarosa_local = "org.getodk:javarosa:local"
const val javarosa = javarosa_online
const val javarosa = javarosa_local
const val karumi_dexter = "com.karumi:dexter:6.2.3"
const val zxing_android_embedded = "com.journeyapps:zxing-android-embedded:4.3.0"
const val dagger = "com.google.dagger:dagger:${Versions.dagger}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class LocalEntitiesInstanceAdapter(private val entitiesRepository: EntitiesRepos

0.until(count).map {
if (it == 0) {
convertToElement(first, true)
convertToElement(first)
} else {
TreeElement("item", it, true)
}
Expand All @@ -33,7 +33,7 @@ class LocalEntitiesInstanceAdapter(private val entitiesRepository: EntitiesRepos
}
} else {
entitiesRepository.getEntities(instanceId).map { entity ->
convertToElement(entity, false)
convertToElement(entity)
}
}
}
Expand All @@ -47,7 +47,7 @@ class LocalEntitiesInstanceAdapter(private val entitiesRepository: EntitiesRepos
)

if (entity != null) {
listOf(convertToElement(entity, false))
listOf(convertToElement(entity))
} else {
emptyList()
}
Expand Down Expand Up @@ -76,7 +76,7 @@ class LocalEntitiesInstanceAdapter(private val entitiesRepository: EntitiesRepos
value
)

entities.map { convertToElement(it, false) }
entities.map { convertToElement(it) }
}
}
}
Expand All @@ -86,31 +86,29 @@ class LocalEntitiesInstanceAdapter(private val entitiesRepository: EntitiesRepos
filter: (Entity.Saved) -> Boolean
): List<TreeElement> {
val entities = entitiesRepository.getEntities(list)
return entities.filter(filter).map { convertToElement(it, false) }
return entities.filter(filter).map { convertToElement(it) }
}

private fun convertToElement(entity: Entity.Saved, partial: Boolean): TreeElement {
private fun convertToElement(entity: Entity.Saved): TreeElement {
val name = TreeElement(EntityItemElement.ID)
val label = TreeElement(EntityItemElement.LABEL)
val version = TreeElement(EntityItemElement.VERSION)
val trunkVersion = TreeElement(EntityItemElement.TRUNK_VERSION)
val branchId = TreeElement(EntityItemElement.BRANCH_ID)

if (!partial) {
name.value = StringData(entity.id)
version.value = StringData(entity.version.toString())
branchId.value = StringData(entity.branchId)
name.value = StringData(entity.id)
version.value = StringData(entity.version.toString())
branchId.value = StringData(entity.branchId)

if (entity.label != null) {
label.value = StringData(entity.label)
}
if (entity.label != null) {
label.value = StringData(entity.label)
}

if (entity.trunkVersion != null) {
trunkVersion.value = StringData(entity.trunkVersion.toString())
}
if (entity.trunkVersion != null) {
trunkVersion.value = StringData(entity.trunkVersion.toString())
}

val item = TreeElement("item", entity.index, partial)
val item = TreeElement("item", entity.index, false)
item.addChild(name)
item.addChild(label)
item.addChild(version)
Expand All @@ -119,11 +117,7 @@ class LocalEntitiesInstanceAdapter(private val entitiesRepository: EntitiesRepos

entity.properties.forEach { property ->
val propertyElement = TreeElement(property.first)

if (!partial) {
propertyElement.value = StringData(property.second)
}

propertyElement.value = StringData(property.second)
item.addChild(propertyElement)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class LocalEntitiesInstanceProviderTest {
}

@Test
fun `partial parse returns elements without values for first item and just item for others`() {
fun `partial parse returns the full first item and just item for others`() {
val entity = arrayOf(
Entity.New(
"1",
Expand All @@ -133,11 +133,9 @@ class LocalEntitiesInstanceProviderTest {
assertThat(instance.numChildren, equalTo(2))

val item1 = instance.getChildAt(0)!!
assertThat(item1.isPartial, equalTo(true))
assertThat(item1.isPartial, equalTo(false))
assertThat(item1.numChildren, equalTo(6))
0.until(item1.numChildren).forEach {
assertThat(item1.getChildAt(it).value?.value, equalTo(null))
}
assertThat(item1.getFirstChild("name")!!.value!!.value, equalTo("1"))

val item2 = instance.getChildAt(1)!!
assertThat(item2.isPartial, equalTo(true))
Expand Down

0 comments on commit 5beb718

Please sign in to comment.