From 01e03ec0f089d99b21c556723f3c9a383b540221 Mon Sep 17 00:00:00 2001 From: FooIbar <118464521+FooIbar@users.noreply.github.com> Date: Tue, 15 Aug 2023 22:25:35 +0800 Subject: [PATCH] Support search in DownloadsScene --- .../ehviewer/download/DownloadManager.kt | 13 ++-- .../hippo/ehviewer/ui/scene/DownloadsScene.kt | 60 ++++++++++----- .../hippo/ehviewer/ui/scene/FavoritesScene.kt | 3 +- .../hippo/ehviewer/ui/scene/SearchBarScene.kt | 75 +++++++++++-------- .../com/hippo/ehviewer/util/StringUtils.kt | 2 + app/src/main/res/menu/scene_download.xml | 17 ++--- app/src/main/res/values-de/strings.xml | 2 +- app/src/main/res/values-es/strings.xml | 2 +- app/src/main/res/values-fr/strings.xml | 2 +- app/src/main/res/values-ja/strings.xml | 2 +- app/src/main/res/values-ko/strings.xml | 2 +- app/src/main/res/values-th/strings.xml | 2 +- app/src/main/res/values-zh-rCN/strings.xml | 2 +- app/src/main/res/values-zh-rHK/strings.xml | 2 +- app/src/main/res/values-zh-rTW/strings.xml | 2 +- app/src/main/res/values/strings.xml | 2 +- 16 files changed, 111 insertions(+), 79 deletions(-) diff --git a/app/src/main/java/com/hippo/ehviewer/download/DownloadManager.kt b/app/src/main/java/com/hippo/ehviewer/download/DownloadManager.kt index c45b7a4c9..3b7cbc86f 100644 --- a/app/src/main/java/com/hippo/ehviewer/download/DownloadManager.kt +++ b/app/src/main/java/com/hippo/ehviewer/download/DownloadManager.kt @@ -503,7 +503,7 @@ object DownloadManager : OnSpiderListener { ensureDownload() } - fun moveDownload(fromPosition: Int, toPosition: Int): List { + private fun moveDownload(fromPosition: Int, toPosition: Int): List { val info = allInfoList.removeAt(fromPosition) allInfoList.add(toPosition, info) val range = if (fromPosition < toPosition) fromPosition..toPosition else toPosition..fromPosition @@ -515,12 +515,11 @@ object DownloadManager : OnSpiderListener { return list } - fun moveDownload(label: String?, fromPosition: Int, toPosition: Int) = - getInfoListForLabel(label)!!.let { - val absFromPosition = allInfoList.indexOf(it[fromPosition]) - val absToPosition = allInfoList.indexOf(it[toPosition]) - moveDownload(absFromPosition, absToPosition) - } + fun moveDownload(fromItem: DownloadInfo, toItem: DownloadInfo): List { + val fromPosition = allInfoList.indexOf(fromItem) + val toPosition = allInfoList.indexOf(toItem) + return moveDownload(fromPosition, toPosition) + } suspend fun resetAllReadingProgress() = coroutineScope { allInfoList.forEach { info -> diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/DownloadsScene.kt b/app/src/main/java/com/hippo/ehviewer/ui/scene/DownloadsScene.kt index 205afa326..4059e4ea6 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/DownloadsScene.kt +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/DownloadsScene.kt @@ -102,6 +102,7 @@ import com.hippo.ehviewer.ui.tools.CropDefaults import com.hippo.ehviewer.ui.tools.DialogState import com.hippo.ehviewer.util.FileUtils import com.hippo.ehviewer.util.LongList +import com.hippo.ehviewer.util.containsIgnoreCase import com.hippo.ehviewer.util.sendTo import com.hippo.unifile.UniFile import eu.kanade.tachiyomi.util.lang.launchIO @@ -116,12 +117,13 @@ import com.hippo.ehviewer.download.DownloadManager as downloadManager @SuppressLint("RtlHardcoded") class DownloadsScene : - BaseToolbarScene(), + SearchBarScene(), DownloadInfoListener, OnClickFabListener { /*--------------- Whole life cycle ---------------*/ + private var mKeyword: String? = null private var mLabel: String? = null private var mList: List? = null @@ -141,13 +143,16 @@ class DownloadsScene : private val dialogState = DialogState() + override val fabLayout get() = binding.fabLayout + private fun initLabels() { context ?: return val listLabel = downloadManager.labelList - mLabels = ArrayList(listLabel.size + LABEL_OFFSET) // Add "All" and "Default" label names - mLabels.add(getString(R.string.download_all)) - mLabels.add(getString(R.string.default_download_label_name)) + mLabels = arrayListOf( + getString(R.string.download_all), + getString(R.string.default_download_label_name), + ) listLabel.forEach { mLabels.add(it.label) } @@ -207,10 +212,14 @@ class DownloadsScene : else -> DownloadManager.getLabelDownloadInfoList(mLabel) ?: DownloadManager.allInfoList.also { mLabel = null } } - mList = if (mType != -1) { - list.filter { it.state == mType } - } else { - list + mList = list.filter { + (mType == -1 || it.state == mType) && + mKeyword?.let { keyword -> + it.galleryInfo.run { + title.containsIgnoreCase(keyword) || titleJpn.containsIgnoreCase(keyword) || + uploader.containsIgnoreCase(keyword) + } + } ?: true } if (mAdapter != null) { mAdapter!!.notifyDataSetChanged() @@ -219,12 +228,9 @@ class DownloadsScene : } private fun updateTitle() { - setTitle( - getString( - R.string.scene_download_title, - if (mLabel != null) mLabel else getString(R.string.download_all), - ), - ) + val title = getString(R.string.scene_download_title, mLabel ?: getString(R.string.download_all)) + setSearchBarHint(title) + setEditTextHint(getString(R.string.search_bar_hint, title)) } private fun onInit() { @@ -325,10 +331,11 @@ class DownloadsScene : if (fromPosition == toPosition) { return false } - val list = when (mLabel) { - null -> DownloadManager.moveDownload(fromPosition, toPosition) - getString(R.string.default_download_label_name) -> DownloadManager.moveDownload(null, fromPosition, toPosition) - else -> DownloadManager.moveDownload(mLabel, fromPosition, toPosition) + val fromItem = mList!![fromPosition] + val toItem = mList!![toPosition] + val list = DownloadManager.moveDownload(fromItem, toItem) + if (mType != -1 || mKeyword != null) { + mList = mList!!.sortedByDescending { it.position } } mAdapter!!.notifyItemMoved(fromPosition, toPosition) lifecycleScope.launchIO { @@ -360,13 +367,28 @@ class DownloadsScene : addAboveSnackView(fabLayout) updateView() } + setOnApplySearch { + mKeyword = it.takeUnless { it.isEmpty() } + updateForLabel() + updateView() + } return binding.root } + override fun onSearchViewExpanded() { + super.onSearchViewExpanded() + if (tracker.isInCustomChoice) tracker.clearSelection() + showSearchFab(true) + } + + override fun onSearchViewHidden() { + super.onSearchViewHidden() + hideSearchFab(true) + } + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) updateTitle() - setNavigationIcon(R.drawable.ic_baseline_menu_24) } override fun onDestroyView() { diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/FavoritesScene.kt b/app/src/main/java/com/hippo/ehviewer/ui/scene/FavoritesScene.kt index 357883457..4c93dfaff 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/FavoritesScene.kt +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/FavoritesScene.kt @@ -203,7 +203,7 @@ class FavoritesScene : SearchBarScene() { } else { setSearchBarHint(getString(R.string.favorites_title_2, favCatName, keyword)) } - setEditTextHint(getString(R.string.favorites_search_bar_hint, favCatName)) + setEditTextHint(getString(R.string.search_bar_hint, favCatName)) Settings.recentFavCat = urlBuilder.favCat } @@ -422,7 +422,6 @@ class FavoritesScene : SearchBarScene() { } }) itemTouchHelper.attachToRecyclerView(binding.recyclerView) - allowEmptySearch = false return binding.root } diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/SearchBarScene.kt b/app/src/main/java/com/hippo/ehviewer/ui/scene/SearchBarScene.kt index 1de4690aa..d1113dada 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/SearchBarScene.kt +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/SearchBarScene.kt @@ -164,52 +164,68 @@ abstract class SearchBarScene : BaseScene(), ToolBarScene { privLockModeStart = null } - fun selectSearchFab(animation: Boolean) { - _binding ?: return - mShowActionFab = false + fun showSearchFab(animation: Boolean, delay: Long = 0) { if (animation) { - val fab: View? = fabLayout.primaryFab - val delay: Long - if (View.INVISIBLE == fab!!.visibility) { - delay = 0L - } else { - delay = ANIMATE_TIME - fabLayout.setExpanded(expanded = false, animation = true) - fab.animate().scaleX(0.0f).scaleY(0.0f).setListener(mActionFabAnimatorListener) - .setDuration(ANIMATE_TIME).setStartDelay(0L) - .setInterpolator(AnimationUtils.SLOW_FAST_INTERPOLATOR).start() - } mSearchFab.visibility = View.VISIBLE mSearchFab.rotation = -45.0f mSearchFab.animate().scaleX(1.0f).scaleY(1.0f).rotation(0.0f).setListener(null) .setDuration(ANIMATE_TIME).setStartDelay(delay) .setInterpolator(AnimationUtils.FAST_SLOW_INTERPOLATOR).start() } else { - fabLayout.setExpanded(expanded = false, animation = false) - val fab = fabLayout.primaryFab!! - fab.visibility = View.INVISIBLE - fab.scaleX = 0.0f - fab.scaleY = 0.0f mSearchFab.visibility = View.VISIBLE mSearchFab.scaleX = 1.0f mSearchFab.scaleY = 1.0f } } - fun selectActionFab(animation: Boolean) { - _binding ?: return - mShowActionFab = true - if (animation) { - val delay: Long - if (View.INVISIBLE == mSearchFab.visibility) { - delay = 0L - } else { - delay = ANIMATE_TIME + fun hideSearchFab(animation: Boolean): Long { + return if (View.INVISIBLE == mSearchFab.visibility) { + 0L + } else { + if (animation) { mSearchFab.animate().scaleX(0.0f).scaleY(0.0f) .setListener(mSearchFabAnimatorListener) .setDuration(ANIMATE_TIME).setStartDelay(0L) .setInterpolator(AnimationUtils.SLOW_FAST_INTERPOLATOR).start() + } else { + mSearchFab.visibility = View.INVISIBLE + mSearchFab.scaleX = 0.0f + mSearchFab.scaleY = 0.0f } + ANIMATE_TIME + } + } + + fun selectSearchFab(animation: Boolean) { + _binding ?: return + mShowActionFab = false + val delay = if (animation) { + val fab = fabLayout.primaryFab!! + if (View.INVISIBLE == fab.visibility) { + 0L + } else { + fabLayout.setExpanded(expanded = false, animation = true) + fab.animate().scaleX(0.0f).scaleY(0.0f).setListener(mActionFabAnimatorListener) + .setDuration(ANIMATE_TIME).setStartDelay(0L) + .setInterpolator(AnimationUtils.SLOW_FAST_INTERPOLATOR).start() + ANIMATE_TIME + } + } else { + fabLayout.setExpanded(expanded = false, animation = false) + val fab = fabLayout.primaryFab!! + fab.visibility = View.INVISIBLE + fab.scaleX = 0.0f + fab.scaleY = 0.0f + 0L + } + showSearchFab(animation, delay) + } + + fun selectActionFab(animation: Boolean) { + _binding ?: return + mShowActionFab = true + val delay = hideSearchFab(animation) + if (animation) { val fab = fabLayout.primaryFab!! fab.visibility = View.VISIBLE fab.rotation = -45.0f @@ -222,9 +238,6 @@ abstract class SearchBarScene : BaseScene(), ToolBarScene { fab.visibility = View.VISIBLE fab.scaleX = 1.0f fab.scaleY = 1.0f - mSearchFab.visibility = View.INVISIBLE - mSearchFab.scaleX = 0.0f - mSearchFab.scaleY = 0.0f } } diff --git a/app/src/main/java/com/hippo/ehviewer/util/StringUtils.kt b/app/src/main/java/com/hippo/ehviewer/util/StringUtils.kt index 9e4b9a739..bfa54d224 100644 --- a/app/src/main/java/com/hippo/ehviewer/util/StringUtils.kt +++ b/app/src/main/java/com/hippo/ehviewer/util/StringUtils.kt @@ -21,3 +21,5 @@ fun String.toLongOrDefault(defaultValue: Long): Long { fun String.toFloatOrDefault(defaultValue: Float): Float { return toFloatOrNull() ?: defaultValue } + +fun String?.containsIgnoreCase(other: String) = this?.contains(other, true) ?: false diff --git a/app/src/main/res/menu/scene_download.xml b/app/src/main/res/menu/scene_download.xml index 406b369e2..54034a95a 100644 --- a/app/src/main/res/menu/scene_download.xml +++ b/app/src/main/res/menu/scene_download.xml @@ -17,29 +17,26 @@ - - + + + app:showAsAction="never" /> + app:showAsAction="never" /> Protokolldatei wurde auf %s gespeichert. Cloud-Favoriten Lokale Favoriten - %s durchsuchen + %s durchsuchen %s %1$s - %2$s Aus Favoriten löschen diff --git a/app/src/main/res/values-es/strings.xml b/app/src/main/res/values-es/strings.xml index 6f91868f8..138bfa307 100644 --- a/app/src/main/res/values-es/strings.xml +++ b/app/src/main/res/values-es/strings.xml @@ -354,7 +354,7 @@ Favoritos en la nube Favoritos locales - Buscar %s + Buscar %s %s %1$s - %2$s Eliminar de favoritos diff --git a/app/src/main/res/values-fr/strings.xml b/app/src/main/res/values-fr/strings.xml index e04c66fab..f9e67ab24 100755 --- a/app/src/main/res/values-fr/strings.xml +++ b/app/src/main/res/values-fr/strings.xml @@ -393,7 +393,7 @@ Favoris de cloud Favoris locales - Chercher dans %s + Chercher dans %s %s %1$s - %2$s Effacer de favoris diff --git a/app/src/main/res/values-ja/strings.xml b/app/src/main/res/values-ja/strings.xml index 3ab693179..95ff0ab01 100644 --- a/app/src/main/res/values-ja/strings.xml +++ b/app/src/main/res/values-ja/strings.xml @@ -390,7 +390,7 @@ Cloudのお気に入り ローカルのお気に入り - %sを検索 + %sを検索 %s %1$s - %2$s お気に入りから削除 diff --git a/app/src/main/res/values-ko/strings.xml b/app/src/main/res/values-ko/strings.xml index 70bacb526..3fb553d57 100755 --- a/app/src/main/res/values-ko/strings.xml +++ b/app/src/main/res/values-ko/strings.xml @@ -364,7 +364,7 @@ 클라우드 즐겨찾기 로컬 즐겨찾기 - %s 검색 + %s 검색 %s %1$s - %2$s 즐겨찾기에서 삭제 diff --git a/app/src/main/res/values-th/strings.xml b/app/src/main/res/values-th/strings.xml index 72c9cedb6..c2d7e2382 100644 --- a/app/src/main/res/values-th/strings.xml +++ b/app/src/main/res/values-th/strings.xml @@ -379,7 +379,7 @@ รายการโปรดบนเว็บ รายการโปรดบนเครื่อง - ค้นหา %s + ค้นหา %s %s %1$s - %2$s ลบออกจากรายการโปรด diff --git a/app/src/main/res/values-zh-rCN/strings.xml b/app/src/main/res/values-zh-rCN/strings.xml index ca30eaeee..46b92593f 100644 --- a/app/src/main/res/values-zh-rCN/strings.xml +++ b/app/src/main/res/values-zh-rCN/strings.xml @@ -409,7 +409,7 @@ 云端收藏 本地收藏 - 搜索 %s + 搜索 %s %s %1$s - %2$s 删除收藏 diff --git a/app/src/main/res/values-zh-rHK/strings.xml b/app/src/main/res/values-zh-rHK/strings.xml index d68327dbd..8b68f2a7a 100755 --- a/app/src/main/res/values-zh-rHK/strings.xml +++ b/app/src/main/res/values-zh-rHK/strings.xml @@ -419,7 +419,7 @@ 雲端收藏 本地收藏 - 搜索 %s + 搜索 %s %s %1$s - %2$s 刪除收藏 diff --git a/app/src/main/res/values-zh-rTW/strings.xml b/app/src/main/res/values-zh-rTW/strings.xml index d9d2c9f7b..d0e7f596d 100644 --- a/app/src/main/res/values-zh-rTW/strings.xml +++ b/app/src/main/res/values-zh-rTW/strings.xml @@ -400,7 +400,7 @@ 雲端收藏 本機收藏 - 搜尋 %s + 搜尋 %s %s %1$s - %2$s 移除收藏 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 4acfd051c..8eee01959 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -467,7 +467,7 @@ Cloud Favorites Local Favorites - Search %s + Search %s %s %1$s - %2$s Delete from favorites