mirror of
				https://github.com/AdrianKuta/Expandable-RecyclerView.git
				synced 2025-10-31 08:53:41 +01:00 
			
		
		
		
	onlyVisibleItems repaired
This commit is contained in:
		| @@ -15,7 +15,7 @@ android { | |||||||
|         minSdkVersion 23 |         minSdkVersion 23 | ||||||
|         targetSdkVersion 29 |         targetSdkVersion 29 | ||||||
|         versionCode 1 |         versionCode 1 | ||||||
|         versionName "0.0.1-beta01" |         versionName "0.0.1-beta02" | ||||||
|  |  | ||||||
|         testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" |         testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" | ||||||
|         consumerProguardFiles 'consumer-rules.pro' |         consumerProguardFiles 'consumer-rules.pro' | ||||||
| @@ -35,7 +35,7 @@ dependencies { | |||||||
|     implementation 'androidx.appcompat:appcompat:1.1.0' |     implementation 'androidx.appcompat:appcompat:1.1.0' | ||||||
|     implementation 'androidx.core:core-ktx:1.1.0' |     implementation 'androidx.core:core-ktx:1.1.0' | ||||||
|     implementation "androidx.recyclerview:recyclerview: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' |     testImplementation 'junit:junit:4.12' | ||||||
|     androidTestImplementation 'androidx.test.ext:junit:1.1.1' |     androidTestImplementation 'androidx.test.ext:junit:1.1.1' | ||||||
|   | |||||||
| @@ -7,11 +7,12 @@ class ExpandableTreeNode<T>(value: T) : TreeNode<T>(value) { | |||||||
|  |  | ||||||
|     var expanded: Boolean = true |     var expanded: Boolean = true | ||||||
|  |  | ||||||
|     override fun child(child: T, childDeclaration: ChildDeclaration<T>?) { |     override fun child(value: T, childDeclaration: ChildDeclaration<T>?) : ExpandableTreeNode<T> { | ||||||
|         val newChild = ExpandableTreeNode(child) |         val newChild = ExpandableTreeNode(value) | ||||||
|         if (childDeclaration != null) |         if (childDeclaration != null) | ||||||
|             newChild.childDeclaration() |             newChild.childDeclaration() | ||||||
|         addChild(newChild) |         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. |      * @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. |         //Visible if parent of node is expanded. | ||||||
|         return map { it as ExpandableTreeNode } |         return map { it as ExpandableTreeNode } | ||||||
|             .filter { allAncestorsAreExpanded(it) } |             .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. |      * @return `True` if parent, and all parent's ancestors are expanded. | ||||||
|      */ |      */ | ||||||
|     private fun allAncestorsAreExpanded(node: ExpandableTreeNode<T>): Boolean { |     private fun allAncestorsAreExpanded(node: ExpandableTreeNode<T>): Boolean { | ||||||
|         var ancestor = parent as? ExpandableTreeNode |         var ancestor = node.parent as? ExpandableTreeNode | ||||||
|         var ancestorsAreExpanded = isAncestorExpanded(ancestor) |         var ancestorsAreExpanded = isAncestorExpanded(ancestor) | ||||||
|  |  | ||||||
|         while (ancestorsAreExpanded && ancestor != null) { |         while (ancestorsAreExpanded && ancestor != null) { | ||||||
|   | |||||||
| @@ -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) | ||||||
|  |     } | ||||||
|  | } | ||||||
		Reference in New Issue
	
	Block a user