onlyVisibleItems repaired

This commit is contained in:
2020-01-21 20:58:35 +01:00
parent 4380ddf9a8
commit e019238c2c
3 changed files with 31 additions and 6 deletions

View File

@ -7,11 +7,12 @@ class ExpandableTreeNode<T>(value: T) : TreeNode<T>(value) {
var expanded: Boolean = true
override fun child(child: T, childDeclaration: ChildDeclaration<T>?) {
val newChild = ExpandableTreeNode(child)
override fun child(value: T, childDeclaration: ChildDeclaration<T>?) : ExpandableTreeNode<T> {
val newChild = ExpandableTreeNode(value)
if (childDeclaration != null)
newChild.childDeclaration()
addChild(newChild)
return newChild
}
/**
@ -53,7 +54,7 @@ class ExpandableTreeNode<T>(value: T) : TreeNode<T>(value) {
/**
* @return List of nodes which parent or higher ancestor aren't collapsed.
*/
private fun onlyVisibleItems(): List<ExpandableTreeNode<T>> {
fun onlyVisibleItems(): List<ExpandableTreeNode<T>> {
//Visible if parent of node is expanded.
return map { it as ExpandableTreeNode }
.filter { allAncestorsAreExpanded(it) }
@ -64,7 +65,7 @@ class ExpandableTreeNode<T>(value: T) : TreeNode<T>(value) {
* @return `True` if parent, and all parent's ancestors are expanded.
*/
private fun allAncestorsAreExpanded(node: ExpandableTreeNode<T>): Boolean {
var ancestor = parent as? ExpandableTreeNode
var ancestor = node.parent as? ExpandableTreeNode
var ancestorsAreExpanded = isAncestorExpanded(ancestor)
while (ancestorsAreExpanded && ancestor != null) {

View File

@ -0,0 +1,24 @@
package com.github.adriankuta.expandable_recyclerview
import org.junit.Assert
import org.junit.Test
class ExpandableTreeNodeTest {
@Test
fun getVisibleNode() {
//given
val root = expandableTree("Root") {
child("Level 1") {
child("Level 2") {
child("Level 3") {
child("Level 4")
}
}
}
}
root.expanded = false
Assert.assertEquals("Root", root.onlyVisibleItems().first().value)
}
}