Skip to content

Commit 3c93a2d

Browse files
committed
Avoid blocking the main thread for too long
It can happen when there are a lot of selected items.
1 parent aea5b03 commit 3c93a2d

File tree

1 file changed

+33
-23
lines changed

1 file changed

+33
-23
lines changed

app/src/main/kotlin/org/fossify/filemanager/adapters/ItemsAdapter.kt

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,14 @@ import java.util.LinkedList
5757
import java.util.Locale
5858

5959
class ItemsAdapter(
60-
activity: SimpleActivity, var listItems: MutableList<ListItem>, val listener: ItemOperationsListener?, recyclerView: MyRecyclerView,
61-
val isPickMultipleIntent: Boolean, val swipeRefreshLayout: SwipeRefreshLayout?, canHaveIndividualViewType: Boolean = true, itemClick: (Any) -> Unit
60+
activity: SimpleActivity,
61+
var listItems: MutableList<ListItem>,
62+
private val listener: ItemOperationsListener?,
63+
recyclerView: MyRecyclerView,
64+
private val isPickMultipleIntent: Boolean,
65+
private val swipeRefreshLayout: SwipeRefreshLayout?,
66+
canHaveIndividualViewType: Boolean = true,
67+
itemClick: (Any) -> Unit,
6268
) : MyRecyclerViewAdapter(activity, recyclerView, itemClick), RecyclerViewFastScroller.OnPopupTextUpdate {
6369

6470
private lateinit var fileDrawable: Drawable
@@ -401,12 +407,12 @@ class ItemsAdapter(
401407
val firstFile = files[0]
402408
val source = firstFile.getParentPath()
403409
FilePickerDialog(
404-
activity,
405-
activity.getDefaultCopyDestinationPath(config.shouldShowHidden(), source),
406-
false,
407-
config.shouldShowHidden(),
408-
true,
409-
true,
410+
activity = activity,
411+
currPath = activity.getDefaultCopyDestinationPath(config.shouldShowHidden(), source),
412+
pickFile = false,
413+
showHidden = config.shouldShowHidden(),
414+
showFAB = true,
415+
canAddShowHiddenButton = true,
410416
showFavoritesButton = true
411417
) {
412418
config.lastCopyPath = it
@@ -749,28 +755,32 @@ class ItemsAdapter(
749755
return
750756
}
751757

752-
activity.handleSAFDialog(SAFPath) {
753-
if (!it) {
758+
activity.handleSAFDialog(SAFPath) { granted ->
759+
if (!granted) {
754760
return@handleSAFDialog
755761
}
756762

757763
val files = ArrayList<FileDirItem>(selectedKeys.size)
758764
val positions = ArrayList<Int>()
759-
selectedKeys.forEach {
760-
config.removeFavorite(getItemWithKey(it)?.path ?: "")
761-
val key = it
762-
val position = listItems.indexOfFirst { it.path.hashCode() == key }
763-
if (position != -1) {
764-
positions.add(position)
765-
files.add(listItems[position])
765+
766+
ensureBackgroundThread {
767+
selectedKeys.forEach { key ->
768+
config.removeFavorite(getItemWithKey(key)?.path ?: "")
769+
val position = listItems.indexOfFirst { it.path.hashCode() == key }
770+
if (position != -1) {
771+
positions.add(position)
772+
files.add(listItems[position])
773+
}
766774
}
767-
}
768775

769-
positions.sortDescending()
770-
removeSelectedItems(positions)
771-
listener?.deleteFiles(files)
772-
positions.forEach {
773-
listItems.removeAt(it)
776+
positions.sortDescending()
777+
activity.runOnUiThread {
778+
removeSelectedItems(positions)
779+
listener?.deleteFiles(files)
780+
positions.forEach {
781+
listItems.removeAt(it)
782+
}
783+
}
774784
}
775785
}
776786
}

0 commit comments

Comments
 (0)