From fee0a016d8b356da58aaccd03e86c6072da534a2 Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Sat, 17 Aug 2024 14:58:48 +0800 Subject: [PATCH] Optimized search --- .../src/main/kotlin/com/v2ray/ang/ui/MainActivity.kt | 8 ++++---- .../kotlin/com/v2ray/ang/viewmodel/MainViewModel.kt | 11 +++++++---- V2rayNG/app/src/main/res/menu/menu_main.xml | 4 ++-- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/MainActivity.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/MainActivity.kt index f6af32804..8a78473d8 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/MainActivity.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/MainActivity.kt @@ -236,12 +236,12 @@ class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedList if (searchItem != null) { val searchView = searchItem.actionView as SearchView searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener { - override fun onQueryTextSubmit(query: String?): Boolean { - mainViewModel.filterConfig(query.orEmpty()) + override fun onQueryTextSubmit(query: String?): Boolean = false + + override fun onQueryTextChange(newText: String?): Boolean { + mainViewModel.filterConfig(newText.orEmpty()) return false } - - override fun onQueryTextChange(newText: String?): Boolean = false }) searchView.setOnCloseListener { diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/viewmodel/MainViewModel.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/viewmodel/MainViewModel.kt index e24017598..43c3a9c38 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/viewmodel/MainViewModel.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/viewmodel/MainViewModel.kt @@ -46,7 +46,7 @@ class MainViewModel(application: Application) : AndroidViewModel(application) { var subscriptionId: String = MmkvManager.settingsStorage.decodeString(AppConfig.CACHE_SUBSCRIPTION_ID, "").orEmpty() //var keywordFilter: String = MmkvManager.settingsStorage.decodeString(AppConfig.CACHE_KEYWORD_FILTER, "")?:"" - private var keywordFilter = "" + var keywordFilter = "" val serversCache = mutableListOf() val isRunning by lazy { MutableLiveData() } val updateListAction by lazy { MutableLiveData() } @@ -170,7 +170,7 @@ class MainViewModel(application: Application) : AndroidViewModel(application) { fun exportAllServer(): Int { val serverListCopy = - if (subscriptionId.isNullOrEmpty()) { + if (subscriptionId.isNullOrEmpty() && keywordFilter.isNullOrEmpty()) { serverList } else { serversCache.map { it.guid }.toList() @@ -295,7 +295,7 @@ class MainViewModel(application: Application) : AndroidViewModel(application) { } fun removeAllServer() { - if (subscriptionId.isNullOrEmpty()) { + if (subscriptionId.isNullOrEmpty() && keywordFilter.isNullOrEmpty()) { MmkvManager.removeAllServer() } else { val serversCopy = serversCache.toList() @@ -306,7 +306,7 @@ class MainViewModel(application: Application) : AndroidViewModel(application) { } fun removeInvalidServer() { - if (subscriptionId.isNullOrEmpty()) { + if (subscriptionId.isNullOrEmpty() && keywordFilter.isNullOrEmpty()) { MmkvManager.removeInvalidServer("") } else { val serversCopy = serversCache.toList() @@ -348,6 +348,9 @@ class MainViewModel(application: Application) : AndroidViewModel(application) { } fun filterConfig(keyword: String) { + if (keyword == keywordFilter) { + return + } keywordFilter = keyword MmkvManager.settingsStorage.encode(AppConfig.CACHE_KEYWORD_FILTER, keywordFilter) reloadServerList() diff --git a/V2rayNG/app/src/main/res/menu/menu_main.xml b/V2rayNG/app/src/main/res/menu/menu_main.xml index 8edaf8a10..5bcba5e7e 100644 --- a/V2rayNG/app/src/main/res/menu/menu_main.xml +++ b/V2rayNG/app/src/main/res/menu/menu_main.xml @@ -3,10 +3,10 @@ xmlns:app="http://schemas.android.com/apk/res-auto"> + app:showAsAction="always|collapseActionView"/>