From 5655c24a123e6b9557766186790cd4f7e6bfe011 Mon Sep 17 00:00:00 2001 From: Robert Jaros Date: Sun, 19 Jan 2020 22:49:15 +0100 Subject: Add an "optional" receiver for factory function in DataContainer component (#128) --- .../kotlin/pl/treksoft/kvision/data/DataContainer.kt | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) (limited to 'kvision-modules/kvision-datacontainer/src/main/kotlin/pl') diff --git a/kvision-modules/kvision-datacontainer/src/main/kotlin/pl/treksoft/kvision/data/DataContainer.kt b/kvision-modules/kvision-datacontainer/src/main/kotlin/pl/treksoft/kvision/data/DataContainer.kt index 84b30afa..6acd72ad 100644 --- a/kvision-modules/kvision-datacontainer/src/main/kotlin/pl/treksoft/kvision/data/DataContainer.kt +++ b/kvision-modules/kvision-datacontainer/src/main/kotlin/pl/treksoft/kvision/data/DataContainer.kt @@ -54,7 +54,7 @@ enum class SorterType { */ class DataContainer( private val model: MutableList, - private val factory: (M, Int, MutableList) -> C, + private val factory: Container.(M, Int, MutableList) -> C, private val container: CONT, private val containerAdd: (CONT.(C, M) -> Unit)? = null, private val filter: ((M) -> Boolean)? = null, @@ -144,13 +144,15 @@ class DataContainer( } else { sorted } - val children = filtered.map { p -> p.first to factory(p.first, p.second, model) } - if (containerAdd != null) { - children.forEach { child -> - containerAdd.invoke(container, child.second, child.first) + val children = filtered.map { p -> p.first to container.factory(p.first, p.second, model) } + if (container.getChildren().isEmpty() && children.isNotEmpty()) { + if (containerAdd != null) { + children.forEach { child -> + containerAdd.invoke(container, child.second, child.first) + } + } else { + container.addAll(children.map { it.second }) } - } else { - container.addAll(children.map { it.second }) } } onUpdateHandler?.invoke() @@ -183,7 +185,7 @@ class DataContainer( */ fun Container.dataContainer( model: MutableList, - factory: (M, Int, MutableList) -> C, + factory: Container.(M, Int, MutableList) -> C, container: CONT, containerAdd: (CONT.(C, M) -> Unit)? = null, filter: ((M) -> Boolean)? = null, @@ -203,7 +205,7 @@ fun Container.dataContainer( */ fun Container.dataContainer( model: MutableList, - factory: (M, Int, MutableList) -> C, + factory: Container.(M, Int, MutableList) -> C, containerAdd: (VPanel.(C, M) -> Unit)? = null, filter: ((M) -> Boolean)? = null, sorter: ((M) -> Comparable<*>?)? = null, -- cgit