Skip to content

Commit

Permalink
Add profile filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
2dust committed Aug 14, 2024
1 parent 9983ea2 commit 7f6a526
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
21 changes: 20 additions & 1 deletion V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import androidx.activity.result.contract.ActivityResultContracts
import androidx.activity.viewModels
import androidx.appcompat.app.ActionBarDrawerToggle
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.widget.SearchView
import androidx.core.content.ContextCompat
import androidx.core.view.GravityCompat
import androidx.core.view.isVisible
Expand Down Expand Up @@ -232,7 +233,25 @@ class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedList

override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.menu_main, menu)
return true

val searchItem = menu.findItem(R.id.search_view)
if (searchItem != null) {
val searchView = searchItem.actionView as SearchView
searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String?): Boolean {
mainViewModel.filterConfig(query.orEmpty())
return false
}

override fun onQueryTextChange(newText: String?): Boolean = false
})

searchView.setOnCloseListener {
mainViewModel.filterConfig("")
false
}
}
return super.onCreateOptionsMenu(menu)
}

override fun onOptionsItemSelected(item: MenuItem) = when (item.itemId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {

var serverList = MmkvManager.decodeServerList()
var subscriptionId: String = settingsStorage.decodeString(AppConfig.CACHE_SUBSCRIPTION_ID, "")?:""
//var keywordFilter: String = settingsStorage.decodeString(AppConfig.CACHE_KEYWORD_FILTER, "")?:""
var keywordFilter: String = settingsStorage.decodeString(AppConfig.CACHE_KEYWORD_FILTER, "")?:""
private set
val serversCache = mutableListOf<ServersCache>()
val isRunning by lazy { MutableLiveData<Boolean>() }
Expand Down Expand Up @@ -166,8 +166,9 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
continue
}

// if (keywordFilter.isEmpty() || config.remarks.contains(keywordFilter)) {
serversCache.add(ServersCache(guid, profile))
if (keywordFilter.isEmpty() || profile.remarks.contains(keywordFilter)) {
serversCache.add(ServersCache(guid, profile))
}
}
}

Expand Down Expand Up @@ -382,6 +383,12 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
}
}

fun filterConfig(keyword: String) {
keywordFilter = keyword
settingsStorage.encode(AppConfig.CACHE_KEYWORD_FILTER, keywordFilter)
reloadServerList()
}

private val mMsgReceiver = object : BroadcastReceiver() {
override fun onReceive(ctx: Context?, intent: Intent?) {
when (intent?.getIntExtra("key", 0)) {
Expand Down
6 changes: 6 additions & 0 deletions V2rayNG/app/src/main/res/menu/menu_main.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/search_view"
android:icon="@drawable/ic_description_24dp"
android:title="@string/menu_item_search"
app:actionViewClass="androidx.appcompat.widget.SearchView"
app:showAsAction="always" />
<item
android:icon="@drawable/ic_add_24dp"
android:title="@string/menu_item_add_config"
Expand Down

0 comments on commit 7f6a526

Please sign in to comment.