From 73588feb06d92d43c2e9bebf29fe888b5b9fdb03 Mon Sep 17 00:00:00 2001 From: Horis <8674809+821938089@users.noreply.github.com> Date: Sun, 1 Sep 2024 11:44:10 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/ui/main/explore/ExploreAdapter.kt | 7 ++++--- .../legado/app/utils/CollectionExtensions.kt | 21 ++++++++++++++++--- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/main/explore/ExploreAdapter.kt b/app/src/main/java/io/legado/app/ui/main/explore/ExploreAdapter.kt index 9dbba95a8..e84b6a943 100644 --- a/app/src/main/java/io/legado/app/ui/main/explore/ExploreAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/main/explore/ExploreAdapter.kt @@ -23,6 +23,7 @@ import io.legado.app.ui.widget.dialog.TextDialog import io.legado.app.utils.activity import io.legado.app.utils.dpToPx import io.legado.app.utils.gone +import io.legado.app.utils.removeLastElement import io.legado.app.utils.showDialogFragment import io.legado.app.utils.startActivity import io.legado.app.utils.visible @@ -118,9 +119,7 @@ class ExploreAdapter(context: Context, val callBack: CallBack) : return if (recycler.isEmpty()) { ItemFilletTextBinding.inflate(inflater, flexbox, false).root } else { - recycler.last().also { - recycler.removeLast() - } as TextView + recycler.removeLastElement() as TextView } } @@ -174,11 +173,13 @@ class ExploreAdapter(context: Context, val callBack: CallBack) : putExtra("type", "bookSource") putExtra("key", source.bookSourceUrl) } + R.id.menu_refresh -> Coroutine.async(callBack.scope) { source.clearExploreKindsCache() }.onSuccess { notifyItemChanged(position) } + R.id.menu_del -> callBack.deleteSource(source) } true diff --git a/app/src/main/java/io/legado/app/utils/CollectionExtensions.kt b/app/src/main/java/io/legado/app/utils/CollectionExtensions.kt index 0dfb7a8c6..f64df9239 100644 --- a/app/src/main/java/io/legado/app/utils/CollectionExtensions.kt +++ b/app/src/main/java/io/legado/app/utils/CollectionExtensions.kt @@ -14,9 +14,16 @@ inline fun List.fastBinarySearch( comparison: (T) -> Int ): Int { when { - fromIndex > toIndex -> throw IllegalArgumentException("fromIndex ($fromIndex) is greater than toIndex ($toIndex).") - fromIndex < 0 -> throw IndexOutOfBoundsException("fromIndex ($fromIndex) is less than zero.") - toIndex > size -> throw IndexOutOfBoundsException("toIndex ($toIndex) is greater than size ($size).") + fromIndex > toIndex -> + throw IllegalArgumentException( + "fromIndex ($fromIndex) is greater than toIndex ($toIndex)." + ) + + fromIndex < 0 -> + throw IndexOutOfBoundsException("fromIndex ($fromIndex) is less than zero.") + + toIndex > size -> + throw IndexOutOfBoundsException("toIndex ($toIndex) is greater than size ($size).") } var low = fromIndex @@ -43,3 +50,11 @@ inline fun > List.fastBinarySearchBy( toIndex: Int = size, crossinline selector: (T) -> K? ): Int = fastBinarySearch(fromIndex, toIndex) { compareValues(selector(it), key) } + +fun MutableList.removeLastElement(): T { + return if (isEmpty()) { + throw NoSuchElementException("List is empty.") + } else { + removeAt(lastIndex) + } +}