tornadofx icon indicating copy to clipboard operation
tornadofx copied to clipboard

SortedFilteredList does not set predicate field with initialPredicate

Open sedsystemstrottier opened this issue 5 years ago • 5 comments

If the initialPredicate constructor parameter of SortedFilteredList is set, the predicate field does not get set. This means that refilter() does not work as expected.

fun test() {
    val list = SortedFilteredList(listOf(1).toObservable(), { myPredicate(it) })
    list.refilter()
}

fun myPredicate(value: Int) {
    // Expect this to be called twice, but is only called once
    return value > 3
}

sedsystemstrottier avatar Feb 12 '20 18:02 sedsystemstrottier

but this example does not understand anything, you call the function - therefore, it must be called when it calls the lambda (predicate), and in this case the order is not important

SchweinchenFuntik avatar Feb 12 '20 18:02 SchweinchenFuntik

listOf(1).toObservable() == observableListOf(1)

SchweinchenFuntik avatar Feb 12 '20 18:02 SchweinchenFuntik

work

class V : View() {
    val items = observableListOf(1, 2, 10)
    val list = SortedFilteredList(items, { print("Sorter"); myPredicate(it) }).apply {
        refilter()
    }

//    val filter = FilteredList(items, Predicate { print("Filter"); myPredicate(it) })

    override val root = vbox {

        listview(list) {
            cellFormat { text = it.toString() }
        }

//        listview(filter) {
//            cellFormat { text = it.toString() }
//        }
    }


    private var callPredicate = 0
    fun myPredicate(value: Int): Boolean {
        // Expect this to be called twice, but is only called once
        println("call: ${callPredicate++}")
        return value > 3
    }
}```

SchweinchenFuntik avatar Feb 12 '20 18:02 SchweinchenFuntik

I apologize, did not immediately understand what was needed, but the repeated filter did not invoke. @edvin

SchweinchenFuntik avatar Feb 12 '20 18:02 SchweinchenFuntik

I think, if I understand this, that I came to the same situation when first trying to use SortedFilteredList with a TableView.

It seemed kind of strange having to define the same predicate twice, once for the initial conditions, then once to have it filter the table when some value changes (using filterWhen), when I would have guessed that simply calling refilter() would filter the table again with the initially supplied predicate.

xipgroc avatar Apr 03 '20 15:04 xipgroc