Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid folder disappears when expand root #67

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,10 @@ class FolderTreeView(
}

private fun onExpandClick(node: FolderNode) {
val actualNode = nodes.find { it.path == node.path }!!

if (actualNode.isExpanded)
removeChildrenCascade(actualNode)
else
insertChildren(actualNode)

setNodes(nodes)
when (val actualNode = nodes.find { it.path == node.path }!!) {
is DeviceNode -> handleCollapsibleParentNode(actualNode) { removeNodesExceptRoot(actualNode) }
else -> handleCollapsibleParentNode(actualNode) { removeChildrenCascade(actualNode) }
}
}

private fun insertChildren(parent: FolderNode) {
Expand All @@ -105,13 +101,30 @@ class FolderTreeView(
nodes.addAll(parentPos + 1, parent.children)
}

private fun removeNodesExceptRoot(parent: FolderNode) {
parent.isExpanded = false
nodes.removeIf {
it != parent
}
}

private inline fun handleCollapsibleParentNode(
actualNode: FolderNode,
removeNodes: (FolderNode) -> Unit
) {
if (actualNode.isExpanded)
removeNodes(actualNode)
else
insertChildren(actualNode)
setNodes(nodes)
}

private fun removeChildrenCascade(parent: FolderNode) {
parent.isExpanded = false
parent.children.forEach { child ->
removeChildrenCascade(child)
nodes
.find { it.path == child.path }
?.let { nodes.remove(it) }
nodes.removeIf {
it.path == child.path
}
}
}

Expand Down
Loading