Skip to content

Commit

Permalink
Update FileTree.kt
Browse files Browse the repository at this point in the history
  • Loading branch information
Sheikh-Abdul-Aziz authored Jul 31, 2024
1 parent 5d9e676 commit 2012cb6
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions filetree/src/main/kotlin/com/zyron/filetree/FileTree.kt
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
package com.zyron.filetree

import android.content.Context
import com.zyron.filetree.viewmodel.FileTreeNode
import kotlinx.coroutines.*
import kotlin.collections.*
import java.io.File
import java.nio.file.Files
import java.nio.file.Paths
import java.nio.file.*

interface FileTreeAdapterUpdateListener {
fun onFileTreeUpdated(startPosition: Int, itemCount: Int)
}

data class FileTreeNode(var file: File, var parent: FileTreeNode? = null, var level: Int = 0) {
var isExpanded: Boolean = false
var childrenStartIndex: Int = 0
var childrenEndIndex: Int = 0
var childrenLoaded: Boolean = false
}

class FileTree(private val context: Context, private val rootDirectory: String) {

private val nodes: MutableList<FileTreeNode> = mutableListOf()
Expand Down Expand Up @@ -47,6 +53,7 @@ class FileTree(private val context: Context, private val rootDirectory: String)
fun getExpandedNodes(): Set<FileTreeNode> = expandedNodes

private suspend fun addNode(node: FileTreeNode, parent: FileTreeNode? = null) {

node.parent = parent
nodes.add(node)
withContext(Dispatchers.Main) {
Expand All @@ -73,12 +80,8 @@ class FileTree(private val context: Context, private val rootDirectory: String)
node.childrenStartIndex = insertIndex
node.childrenEndIndex = insertIndex + children.size

val newNodes = children.mapIndexed { _, childFile ->
FileTreeNode(
file = childFile,
parent = node,
level = node.level + 1
)
val newNodes = children.map { childFile ->
FileTreeNode(file = childFile, parent = node, level = node.level + 1)
}

nodes.addAll(insertIndex, newNodes)
Expand All @@ -93,11 +96,8 @@ class FileTree(private val context: Context, private val rootDirectory: String)
}
}
}

private fun collectAllChildren(
node: FileTreeNode,
nodesToRemove: MutableList<FileTreeNode>
) {

private fun collectAllChildren(node: FileTreeNode, nodesToRemove: MutableList<FileTreeNode>) {
val children = nodes.filter { it.parent == node }
nodesToRemove.addAll(children)
children.forEach { childNode ->
Expand Down

0 comments on commit 2012cb6

Please sign in to comment.