From 3c93a2d2dab5f15d85e79e1be282ca641b4cd0ed Mon Sep 17 00:00:00 2001 From: Naveen Date: Fri, 15 Mar 2024 05:16:50 +0530 Subject: [PATCH] Avoid blocking the main thread for too long It can happen when there are a lot of selected items. --- .../filemanager/adapters/ItemsAdapter.kt | 58 +++++++++++-------- 1 file changed, 34 insertions(+), 24 deletions(-) diff --git a/app/src/main/kotlin/org/fossify/filemanager/adapters/ItemsAdapter.kt b/app/src/main/kotlin/org/fossify/filemanager/adapters/ItemsAdapter.kt index d7816784..82f6a33f 100644 --- a/app/src/main/kotlin/org/fossify/filemanager/adapters/ItemsAdapter.kt +++ b/app/src/main/kotlin/org/fossify/filemanager/adapters/ItemsAdapter.kt @@ -57,8 +57,14 @@ import java.util.LinkedList import java.util.Locale class ItemsAdapter( - activity: SimpleActivity, var listItems: MutableList, val listener: ItemOperationsListener?, recyclerView: MyRecyclerView, - val isPickMultipleIntent: Boolean, val swipeRefreshLayout: SwipeRefreshLayout?, canHaveIndividualViewType: Boolean = true, itemClick: (Any) -> Unit + activity: SimpleActivity, + var listItems: MutableList, + private val listener: ItemOperationsListener?, + recyclerView: MyRecyclerView, + private val isPickMultipleIntent: Boolean, + private val swipeRefreshLayout: SwipeRefreshLayout?, + canHaveIndividualViewType: Boolean = true, + itemClick: (Any) -> Unit, ) : MyRecyclerViewAdapter(activity, recyclerView, itemClick), RecyclerViewFastScroller.OnPopupTextUpdate { private lateinit var fileDrawable: Drawable @@ -401,12 +407,12 @@ class ItemsAdapter( val firstFile = files[0] val source = firstFile.getParentPath() FilePickerDialog( - activity, - activity.getDefaultCopyDestinationPath(config.shouldShowHidden(), source), - false, - config.shouldShowHidden(), - true, - true, + activity = activity, + currPath = activity.getDefaultCopyDestinationPath(config.shouldShowHidden(), source), + pickFile = false, + showHidden = config.shouldShowHidden(), + showFAB = true, + canAddShowHiddenButton = true, showFavoritesButton = true ) { config.lastCopyPath = it @@ -749,28 +755,32 @@ class ItemsAdapter( return } - activity.handleSAFDialog(SAFPath) { - if (!it) { + activity.handleSAFDialog(SAFPath) { granted -> + if (!granted) { return@handleSAFDialog } val files = ArrayList(selectedKeys.size) val positions = ArrayList() - selectedKeys.forEach { - config.removeFavorite(getItemWithKey(it)?.path ?: "") - val key = it - val position = listItems.indexOfFirst { it.path.hashCode() == key } - if (position != -1) { - positions.add(position) - files.add(listItems[position]) - } - } - positions.sortDescending() - removeSelectedItems(positions) - listener?.deleteFiles(files) - positions.forEach { - listItems.removeAt(it) + ensureBackgroundThread { + selectedKeys.forEach { key -> + config.removeFavorite(getItemWithKey(key)?.path ?: "") + val position = listItems.indexOfFirst { it.path.hashCode() == key } + if (position != -1) { + positions.add(position) + files.add(listItems[position]) + } + } + + positions.sortDescending() + activity.runOnUiThread { + removeSelectedItems(positions) + listener?.deleteFiles(files) + positions.forEach { + listItems.removeAt(it) + } + } } } }