Skip to content

Commit f4388d7

Browse files
committed
update folder children count asynchronously
1 parent 260da45 commit f4388d7

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

app/src/main/kotlin/com/simplemobiletools/filemanager/pro/adapters/ItemsAdapter.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,13 @@ class ItemsAdapter(activity: SimpleActivity, var listItems: MutableList<ListItem
719719
notifyDataSetChanged()
720720
}
721721

722+
fun updateChildCount(path: String, count: Int) {
723+
val position = getItemKeyPosition(path.hashCode())
724+
val item = listItems.getOrNull(position) ?: return
725+
item.children = count
726+
notifyItemChanged(position)
727+
}
728+
722729
fun isASectionTitle(position: Int) = listItems.getOrNull(position)?.isSectionTitle ?: false
723730

724731
override fun onViewRecycled(holder: ViewHolder) {

app/src/main/kotlin/com/simplemobiletools/filemanager/pro/fragments/ItemsFragment.kt

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,13 +241,27 @@ class ItemsFragment : Fragment(), ItemOperationsListener, Breadcrumbs.Breadcrumb
241241
val lastModifieds = context!!.getFolderLastModifieds(path)
242242

243243
for (file in files) {
244-
val fileDirItem = getFileDirItemFromFile(file, isSortingBySize, lastModifieds, getProperChildCount)
244+
val fileDirItem = getFileDirItemFromFile(file, isSortingBySize, lastModifieds, false)
245245
if (fileDirItem != null) {
246246
items.add(fileDirItem)
247247
}
248248
}
249249

250+
// send out the initial item list asap, get proper child count asynchronously as it can be slow
250251
callback(path, items)
252+
253+
if (getProperChildCount) {
254+
items.filter { it.mIsDirectory }.forEach {
255+
if (context != null) {
256+
val childrenCount = it.getDirectChildrenCount(context!!, showHidden)
257+
if (childrenCount != 0) {
258+
activity?.runOnUiThread {
259+
getRecyclerAdapter()?.updateChildCount(it.mPath, childrenCount)
260+
}
261+
}
262+
}
263+
}
264+
}
251265
}
252266

253267
private fun getFileDirItemFromFile(file: File, isSortingBySize: Boolean, lastModifieds: HashMap<String, Long>, getProperChildCount: Boolean): ListItem? {
@@ -451,7 +465,7 @@ class ItemsFragment : Fragment(), ItemOperationsListener, Breadcrumbs.Breadcrumb
451465

452466
private fun getRecyclerAdapter() = mView.items_list.adapter as? ItemsAdapter
453467

454-
fun setupLayoutManager() {
468+
private fun setupLayoutManager() {
455469
if (context!!.config.getFolderViewType(currentPath) == VIEW_TYPE_GRID) {
456470
currentViewType = VIEW_TYPE_GRID
457471
setupGridLayoutManager()

0 commit comments

Comments
 (0)