Skip to content

Commit

Permalink
Optimized search
Browse files Browse the repository at this point in the history
  • Loading branch information
2dust committed Aug 17, 2024
1 parent f040fa5 commit fee0a01
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
8 changes: 4 additions & 4 deletions V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ServersCache>()
val isRunning by lazy { MutableLiveData<Boolean>() }
val updateListAction by lazy { MutableLiveData<Int>() }
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions V2rayNG/app/src/main/res/menu/menu_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/search_view"
android:icon="@drawable/ic_description_24dp"
android:icon="@drawable/ic_outline_filter_alt_24"
android:title="@string/menu_item_search"
app:actionViewClass="androidx.appcompat.widget.SearchView"
app:showAsAction="always" />
app:showAsAction="always|collapseActionView"/>
<item
android:icon="@drawable/ic_add_24dp"
android:title="@string/menu_item_add_config"
Expand Down

0 comments on commit fee0a01

Please sign in to comment.