diff --git a/collect_app/src/main/java/org/odk/collect/android/entities/JsonFileEntitiesRepository.kt b/collect_app/src/main/java/org/odk/collect/android/entities/JsonFileEntitiesRepository.kt index cabc9e356e9..2fa6cec40ff 100644 --- a/collect_app/src/main/java/org/odk/collect/android/entities/JsonFileEntitiesRepository.kt +++ b/collect_app/src/main/java/org/odk/collect/android/entities/JsonFileEntitiesRepository.kt @@ -16,12 +16,21 @@ class JsonFileEntitiesRepository(directory: File) : EntitiesRepository { } override fun getEntities(list: String): List { - return readEntities().filter { it.list == list } + return readEntities().filter { it.list == list }.mapIndexed { index, entity -> + Entity.Saved( + entity.list, + entity.id, + entity.label, + entity.version, + entity.properties, + entity.state, + index + ) + } } override fun save(vararg entities: Entity) { val storedEntities = readEntities() - var maxIndex = storedEntities.maxOfOrNull { it.index } ?: -1 entities.forEach { entity -> val existing = storedEntities.find { it.id == entity.id } @@ -34,26 +43,24 @@ class JsonFileEntitiesRepository(directory: File) : EntitiesRepository { storedEntities.remove(existing) storedEntities.add( - Entity.Saved( + Entity.New( entity.list, entity.id, entity.label ?: existing.label, version = entity.version, properties = mergeProperties(existing, entity), - state = state, - existing.index + state = state ) ) } else { storedEntities.add( - Entity.Saved( + Entity.New( entity.list, entity.id, entity.label, entity.version, entity.properties, - entity.state, - ++maxIndex + entity.state ) ) } @@ -79,15 +86,10 @@ class JsonFileEntitiesRepository(directory: File) : EntitiesRepository { override fun delete(id: String) { val existing = readEntities() existing.removeIf { it.id == id } - - val withAdjustedIndex = existing.mapIndexed { index, entity -> - entity.copy(index = index) - } - - writeEntities(withAdjustedIndex) + writeEntities(existing) } - private fun writeEntities(entities: List) { + private fun writeEntities(entities: List) { val map = mutableMapOf>() entities.forEach { map.getOrPut(it.list) { mutableListOf() }.add(it.toJson()) @@ -96,7 +98,7 @@ class JsonFileEntitiesRepository(directory: File) : EntitiesRepository { writeJson(map) } - private fun readEntities(): MutableList { + private fun readEntities(): MutableList { return readJson().entries.flatMap { (list, entities) -> entities.map { it.toEntity(list) } }.toMutableList() @@ -158,36 +160,33 @@ class JsonFileEntitiesRepository(directory: File) : EntitiesRepository { val label: String?, val version: Int, val properties: Map, - val offline: Boolean, - val index: Int + val offline: Boolean ) - private fun JsonEntity.toEntity(list: String): Entity.Saved { + private fun JsonEntity.toEntity(list: String): Entity.New { val state = if (this.offline) { Entity.State.OFFLINE } else { Entity.State.ONLINE } - return Entity.Saved( + return Entity.New( list, this.id, this.label, this.version, this.properties.entries.map { Pair(it.key, it.value) }, - state, - this.index + state ) } - private fun Entity.Saved.toJson(): JsonEntity { + private fun Entity.New.toJson(): JsonEntity { return JsonEntity( this.id, this.label, this.version, this.properties.toMap(), - this.state == Entity.State.OFFLINE, - this.index + this.state == Entity.State.OFFLINE ) } } diff --git a/entities/src/main/java/org/odk/collect/entities/storage/InMemEntitiesRepository.kt b/entities/src/main/java/org/odk/collect/entities/storage/InMemEntitiesRepository.kt index 17046abd132..572bd3d77ed 100644 --- a/entities/src/main/java/org/odk/collect/entities/storage/InMemEntitiesRepository.kt +++ b/entities/src/main/java/org/odk/collect/entities/storage/InMemEntitiesRepository.kt @@ -1,18 +1,26 @@ package org.odk.collect.entities.storage -class InMemEntitiesRepository(startingIndex: Int = 0) : EntitiesRepository { +class InMemEntitiesRepository : EntitiesRepository { private val lists = mutableSetOf() - private val entities = mutableListOf() - - private var indexCounter = startingIndex + private val entities = mutableListOf() override fun getLists(): Set { return lists } override fun getEntities(list: String): List { - return entities.filter { it.list == list } + return entities.filter { it.list == list }.mapIndexed { index, entity -> + Entity.Saved( + entity.list, + entity.id, + entity.label, + entity.version, + entity.properties, + entity.state, + index + ) + } } override fun clear() { @@ -26,14 +34,6 @@ class InMemEntitiesRepository(startingIndex: Int = 0) : EntitiesRepository { override fun delete(id: String) { entities.removeIf { it.id == id } - - indexCounter-- - val withAdjustedIndex = entities.mapIndexed { index, entity -> - entity.copy(index = index) - } - - entities.clear() - entities.addAll(withAdjustedIndex) } override fun save(vararg entities: Entity) { @@ -49,26 +49,24 @@ class InMemEntitiesRepository(startingIndex: Int = 0) : EntitiesRepository { this.entities.remove(existing) this.entities.add( - Entity.Saved( + Entity.New( entity.list, entity.id, entity.label ?: existing.label, version = entity.version, properties = mergeProperties(existing, entity), - state = state, - existing.index + state = state ) ) } else { this.entities.add( - Entity.Saved( + Entity.New( entity.list, entity.id, entity.label, entity.version, entity.properties, - entity.state, - indexCounter++ + entity.state ) ) } diff --git a/entities/src/test/java/org/odk/collect/entities/javarosa/LocalEntitiesFileInstanceParserTest.kt b/entities/src/test/java/org/odk/collect/entities/javarosa/LocalEntitiesFileInstanceParserTest.kt index a716b6ce760..35ef9d5654a 100644 --- a/entities/src/test/java/org/odk/collect/entities/javarosa/LocalEntitiesFileInstanceParserTest.kt +++ b/entities/src/test/java/org/odk/collect/entities/javarosa/LocalEntitiesFileInstanceParserTest.kt @@ -86,13 +86,13 @@ class LocalEntitiesFileInstanceParserTest { "Shiv Roy", version = 1 ) - val repository = InMemEntitiesRepository(11) + val repository = InMemEntitiesRepository() repository.save(entity) val parser = LocalEntitiesFileInstanceParser { repository } val instance = parser.parse("people", "people.csv", true) val item = instance.getChildAt(0)!! - assertThat(item.multiplicity, equalTo(11)) + assertThat(item.multiplicity, equalTo(0)) } }