From 6c8d768508142c8a6a672b94311c8e9257f15735 Mon Sep 17 00:00:00 2001 From: Robert Jaros Date: Mon, 21 Jan 2019 12:41:10 +0100 Subject: Removed mapping function from DataContainer class. Add sorter and sorterType functions instead. --- .../pl/treksoft/kvision/data/DataContainer.kt | 55 ++++++++++++++++------ 1 file changed, 41 insertions(+), 14 deletions(-) (limited to 'src/main/kotlin/pl') diff --git a/src/main/kotlin/pl/treksoft/kvision/data/DataContainer.kt b/src/main/kotlin/pl/treksoft/kvision/data/DataContainer.kt index d8b74c54..bb3eaa83 100644 --- a/src/main/kotlin/pl/treksoft/kvision/data/DataContainer.kt +++ b/src/main/kotlin/pl/treksoft/kvision/data/DataContainer.kt @@ -28,6 +28,14 @@ import pl.treksoft.kvision.core.Container import pl.treksoft.kvision.core.Widget import pl.treksoft.kvision.panel.VPanel +/** + * Sorter types. + */ +enum class SorterType { + ASC, + DESC +} + /** * A container class with support for observable data model. * @@ -37,15 +45,17 @@ import pl.treksoft.kvision.panel.VPanel * @param model data model of type *ObservableList* * @param factory a function which creates component C from data model at given index * @param filter a filtering function - * @param mapping a mapping function + * @param sorter a sorting function + * @param sorterType a sorting type selection function * @param container internal container (defaults to [VPanel]) * @param init an initializer extension function */ class DataContainer( private val model: ObservableList, - private val factory: (Int, M) -> C, - private val filter: ((Int, M) -> Boolean)? = null, - private val mapping: ((List>) -> List>)? = null, + private val factory: (M, Int, ObservableList) -> C, + private val filter: ((M) -> Boolean)? = null, + private val sorter: ((M) -> Comparable<*>?)? = null, + private val sorterType: () -> SorterType = { SorterType.ASC }, private val container: Container = VPanel(), init: (DataContainer.() -> Unit)? = null ) : @@ -106,13 +116,29 @@ class DataContainer( } singleRender { container.removeAll() - val indexed = model.mapIndexed { index, m -> index to m } - val mapped = mapping?.invoke(indexed) ?: indexed - val children = if (filter != null) { - mapped.filter { p -> filter.invoke(p.first, p.second) } + val indexed = model.mapIndexed { index, m -> m to index } + val sorted = if (sorter != null) { + when (sorterType()) { + SorterType.ASC -> + indexed.sortedBy { + @Suppress("UNCHECKED_CAST") + sorter.invoke(it.first) as Comparable? + } + SorterType.DESC -> + indexed.sortedByDescending { + @Suppress("UNCHECKED_CAST") + sorter.invoke(it.first) as Comparable? + } + } + } else { + indexed + } + val filtered = if (filter != null) { + sorted.filter { filter.invoke(it.first) } } else { - mapped - }.map { p -> factory(p.first, p.second) } + sorted + } + val children = filtered.map { p -> factory(p.first, p.second, model) } container.addAll(children) } onUpdateHandler?.invoke() @@ -145,13 +171,14 @@ class DataContainer( */ fun Container.dataContainer( model: ObservableList, - factory: (Int, M) -> C, - filter: ((Int, M) -> Boolean)? = null, - mapping: ((List>) -> List>)? = null, + factory: (M, Int, ObservableList) -> C, + filter: ((M) -> Boolean)? = null, + sorter: ((M) -> Comparable<*>?)? = null, + sorterType: () -> SorterType = { SorterType.ASC }, container: Container = VPanel(), init: (DataContainer.() -> Unit)? = null ): DataContainer { - val dataContainer = DataContainer(model, factory, filter, mapping, container, init) + val dataContainer = DataContainer(model, factory, filter, sorter, sorterType, container, init) this.add(dataContainer) return dataContainer } -- cgit