Skip to content

Commit

Permalink
Update FileTreeAdapter.kt
Browse files Browse the repository at this point in the history
  • Loading branch information
Sheikh-Abdul-Aziz authored Jul 31, 2024
1 parent 25c2c1a commit 1eca844
Showing 1 changed file with 34 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import android.content.Context
import android.util.*
import android.view.*
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.*
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.zyron.filetree.R
import com.zyron.filetree.FileTree
import com.zyron.filetree.FileTreeNode
import com.zyron.filetree.FileTreeAdapterUpdateListener
import com.zyron.filetree.viewholder.FileTreeViewHolder
import com.zyron.filetree.viewmodel.*
import com.zyron.filetree.viewmodel.FileTreeNodeDiffCallback
import com.zyron.filetree.provider.FileTreeIconProvider
import kotlinx.coroutines.*
import com.zyron.filetree.viewholder.FileTreeViewHolder
import java.io.File
import java.nio.file.Files

Expand All @@ -22,13 +23,9 @@ interface FileTreeClickListener {
fun onFolderLongClick(folder: File): Boolean
}

class FileTreeAdapter(
private val context: Context,
private val fileTree: FileTree,
private val fileTreeIconProvider: FileTreeIconProvider,
private val listener: FileTreeClickListener? = null
) : RecyclerView.Adapter<FileTreeViewHolder>(), FileTreeAdapterUpdateListener {
class FileTreeAdapter(private val context: Context, private val fileTree: FileTree, private val fileTreeIconProvider: FileTreeIconProvider, private val listener: FileTreeClickListener? = null) : RecyclerView.Adapter<FileTreeViewHolder>(), FileTreeAdapterUpdateListener {

private var selectedItemPosition: Int = RecyclerView.NO_POSITION
private var nodes: MutableList<FileTreeNode> = fileTree.getNodes().toMutableList()

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): FileTreeViewHolder {
Expand All @@ -38,55 +35,64 @@ class FileTreeAdapter(

override fun onBindViewHolder(holder: FileTreeViewHolder, position: Int) {
val node = nodes[position]

val indentationDp = 16 * node.level
val indentationDp = 12 * node.level
val indentationPx = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, indentationDp.toFloat(), context.resources.displayMetrics).toInt()

val isRtl = context.resources.configuration.layoutDirection == View.LAYOUT_DIRECTION_RTL

val layoutParams = holder.itemView.layoutParams as ViewGroup.MarginLayoutParams
if (isRtl) {
layoutParams.rightMargin = indentationPx
layoutParams.leftMargin = 0

} else {
layoutParams.leftMargin = indentationPx
layoutParams.rightMargin = 0

}
holder.itemView.layoutParams = layoutParams

holder.itemView.setPadding(4, 4, 4, 4)
holder.chevronIconView.setPadding(0, 0, 0, 0)
holder.fileIconView.setPadding(0, 0, 0, 0)
val chevronIcon = if (node.isExpanded) {
fileTreeIconProvider.getChevronCollapseIcon()
} else {
fileTreeIconProvider.getChevronExpandIcon()
}

holder.itemView.setPadding(4, 8, 4, 8)
holder.chevronIconView.setPadding(4, 0, 2, 0)
holder.fileIconView.setPadding(2, 0, 4, 0)
holder.fileNameView.setPadding(6, 7, 7, 6)

holder.itemView.setBackgroundResource(R.drawable.item_background)

holder.itemView.isSelected = position == selectedItemPosition && node.isExpanded && Files.isDirectory(node.file.toPath())

if (Files.isDirectory(node.file.toPath())) {
holder.chevronIconView.setImageDrawable(ContextCompat.getDrawable(context, chevronIcon))
holder.chevronIconView.visibility = View.VISIBLE
holder.fileIconView.setImageDrawable(ContextCompat.getDrawable(context, fileTreeIconProvider.getFolderIcon()))
holder.fileNameView.text = node.file.name

val chevronIcon = if (node.isExpanded) {
fileTreeIconProvider.getChevronCollapseIcon()
} else {
fileTreeIconProvider.getChevronExpandIcon()
}

holder.chevronIconView.setImageDrawable(ContextCompat.getDrawable(context, chevronIcon))

holder.itemView.setOnClickListener {
val previousPosition = selectedItemPosition
if (node.isExpanded) {
fileTree.collapseNode(node)
} else {
fileTree.expandNode(node)
}
notifyItemChanged(holder.bindingAdapterPosition)
selectedItemPosition = holder.bindingAdapterPosition
notifyItemChanged(selectedItemPosition)
notifyItemChanged(previousPosition)
}

holder.itemView.setOnLongClickListener {
listener?.onFolderLongClick(node.file) ?: false
}

} else if (node.file.isFile) {
holder.chevronIconView.visibility = View.GONE
holder.chevronIconView.setImageDrawable(ContextCompat.getDrawable(context, chevronIcon))
holder.chevronIconView.visibility = View.INVISIBLE
holder.fileIconView.setImageDrawable(ContextCompat.getDrawable(context, fileTreeIconProvider.getIconForFile(node.file)))
holder.fileNameView.text = node.file.name

Expand Down Expand Up @@ -114,7 +120,6 @@ class FileTreeAdapter(
nodes.addAll(newNodes)
diffResult.dispatchUpdatesTo(this)
}


override fun getItemCount(): Int {
return nodes.size
Expand Down

0 comments on commit 1eca844

Please sign in to comment.