update folder children count asynchronously

This commit is contained in:
tibbi 2021-04-19 19:13:46 +02:00
parent 260da456c1
commit f4388d7d8b
2 changed files with 23 additions and 2 deletions

View file

@ -719,6 +719,13 @@ class ItemsAdapter(activity: SimpleActivity, var listItems: MutableList<ListItem
notifyDataSetChanged() notifyDataSetChanged()
} }
fun updateChildCount(path: String, count: Int) {
val position = getItemKeyPosition(path.hashCode())
val item = listItems.getOrNull(position) ?: return
item.children = count
notifyItemChanged(position)
}
fun isASectionTitle(position: Int) = listItems.getOrNull(position)?.isSectionTitle ?: false fun isASectionTitle(position: Int) = listItems.getOrNull(position)?.isSectionTitle ?: false
override fun onViewRecycled(holder: ViewHolder) { override fun onViewRecycled(holder: ViewHolder) {

View file

@ -241,13 +241,27 @@ class ItemsFragment : Fragment(), ItemOperationsListener, Breadcrumbs.Breadcrumb
val lastModifieds = context!!.getFolderLastModifieds(path) val lastModifieds = context!!.getFolderLastModifieds(path)
for (file in files) { for (file in files) {
val fileDirItem = getFileDirItemFromFile(file, isSortingBySize, lastModifieds, getProperChildCount) val fileDirItem = getFileDirItemFromFile(file, isSortingBySize, lastModifieds, false)
if (fileDirItem != null) { if (fileDirItem != null) {
items.add(fileDirItem) items.add(fileDirItem)
} }
} }
// send out the initial item list asap, get proper child count asynchronously as it can be slow
callback(path, items) callback(path, items)
if (getProperChildCount) {
items.filter { it.mIsDirectory }.forEach {
if (context != null) {
val childrenCount = it.getDirectChildrenCount(context!!, showHidden)
if (childrenCount != 0) {
activity?.runOnUiThread {
getRecyclerAdapter()?.updateChildCount(it.mPath, childrenCount)
}
}
}
}
}
} }
private fun getFileDirItemFromFile(file: File, isSortingBySize: Boolean, lastModifieds: HashMap<String, Long>, getProperChildCount: Boolean): ListItem? { private fun getFileDirItemFromFile(file: File, isSortingBySize: Boolean, lastModifieds: HashMap<String, Long>, getProperChildCount: Boolean): ListItem? {
@ -451,7 +465,7 @@ class ItemsFragment : Fragment(), ItemOperationsListener, Breadcrumbs.Breadcrumb
private fun getRecyclerAdapter() = mView.items_list.adapter as? ItemsAdapter private fun getRecyclerAdapter() = mView.items_list.adapter as? ItemsAdapter
fun setupLayoutManager() { private fun setupLayoutManager() {
if (context!!.config.getFolderViewType(currentPath) == VIEW_TYPE_GRID) { if (context!!.config.getFolderViewType(currentPath) == VIEW_TYPE_GRID) {
currentViewType = VIEW_TYPE_GRID currentViewType = VIEW_TYPE_GRID
setupGridLayoutManager() setupGridLayoutManager()