onlyVisibleItems repaired

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

View File

@ -15,7 +15,7 @@ android {
minSdkVersion 23
targetSdkVersion 29
versionCode 1
versionName "0.0.1-beta01"
versionName "0.0.1-beta02"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles 'consumer-rules.pro'
@ -35,7 +35,7 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.1.0'
implementation "androidx.recyclerview:recyclerview:1.1.0"
implementation "com.github.adriankuta:tree-structure:1.2.0"
implementation "com.github.adriankuta:tree-structure:1.2.3"
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'

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)
}
}