From 762f23ed7147116a8d33eaacabe44e8bd8b076be Mon Sep 17 00:00:00 2001 From: "sargamgayatri0803@gmail.com" Date: Sat, 21 Sep 2024 23:23:52 +0530 Subject: [PATCH] fix: retain search filter --- .../ichi2/anki/notetype/ManageNotetypes.kt | 30 +++++++++++++------ 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/notetype/ManageNotetypes.kt b/AnkiDroid/src/main/java/com/ichi2/anki/notetype/ManageNotetypes.kt index 7d8bfc0c4cba..5821523f651c 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/notetype/ManageNotetypes.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/notetype/ManageNotetypes.kt @@ -58,6 +58,9 @@ class ManageNotetypes : AnkiActivity() { private var currentNotetypes: List = emptyList() + // Store search query + private var searchQuery: String = "" + private val notetypesAdapter: NotetypesAdapter by lazy { NotetypesAdapter( this@ManageNotetypes, @@ -115,20 +118,29 @@ class ManageNotetypes : AnkiActivity() { } override fun onQueryTextChange(newText: String?): Boolean { - val filteredList = if (newText.isNullOrEmpty()) { - currentNotetypes - } else { - currentNotetypes.filter { - it.name.lowercase().contains(newText.lowercase()) - } - } - notetypesAdapter.submitList(filteredList) + // Update the search query + searchQuery = newText.orEmpty() + filterNoteTypes(searchQuery) return true } }) return true } + /** + * Filters and updates the note types list based on the query + */ + private fun filterNoteTypes(query: String) { + val filteredList = if (query.isEmpty()) { + currentNotetypes + } else { + currentNotetypes.filter { + it.name.lowercase().contains(query.lowercase()) + } + } + notetypesAdapter.submitList(filteredList) + } + @SuppressLint("CheckResult") private fun renameNotetype(manageNoteTypeUiModel: ManageNoteTypeUiModel) { launchCatchingTask { @@ -215,7 +227,7 @@ class ManageNotetypes : AnkiActivity() { currentNotetypes = updatedNotetypes - notetypesAdapter.submitList(updatedNotetypes) + filterNoteTypes(searchQuery) actionBar.subtitle = resources.getQuantityString( R.plurals.model_browser_types_available, updatedNotetypes.size,