Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add search tool for profiles #2682

Merged
merged 1 commit into from
Apr 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion mobile/src/main/java/com/github/shadowsocks/ProfilesFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import android.widget.ImageView
import android.widget.TextView
import androidx.activity.result.ActivityResultLauncher
import androidx.appcompat.widget.PopupMenu
import androidx.appcompat.widget.SearchView
import androidx.appcompat.widget.Toolbar
import androidx.appcompat.widget.TooltipCompat
import androidx.core.os.bundleOf
Expand Down Expand Up @@ -64,7 +65,7 @@ import com.google.zxing.WriterException
import timber.log.Timber
import java.nio.charset.StandardCharsets

class ProfilesFragment : ToolbarFragment(), Toolbar.OnMenuItemClickListener {
class ProfilesFragment : ToolbarFragment(), Toolbar.OnMenuItemClickListener, SearchView.OnQueryTextListener {
companion object {
/**
* used for callback from stateChanged from MainActivity
Expand Down Expand Up @@ -237,6 +238,17 @@ class ProfilesFragment : ToolbarFragment(), Toolbar.OnMenuItemClickListener {
notifyItemInserted(pos)
}

fun filter(name: String) {
val active = ProfileManager.getActiveProfiles()?.toMutableList() ?: mutableListOf()
profiles.clear()
active.forEach {
if (it.name?.contains(name, true) ?: false || it.host.contains(name, true)) {
profiles.add(it)
}
}
notifyDataSetChanged()
}

fun move(from: Int, to: Int) {
undoManager.flush()
val first = profiles[from]
Expand Down Expand Up @@ -322,6 +334,13 @@ class ProfilesFragment : ToolbarFragment(), Toolbar.OnMenuItemClickListener {
startActivity(Intent(context, ProfileConfigActivity::class.java).putExtra(Action.EXTRA_PROFILE_ID, profile.id))
}

override fun onQueryTextChange(query: String): Boolean {
profilesAdapter.filter(query)
return false
}

override fun onQueryTextSubmit(query: String): Boolean = false

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? =
inflater.inflate(R.layout.layout_list, container, false)

Expand All @@ -331,6 +350,10 @@ class ProfilesFragment : ToolbarFragment(), Toolbar.OnMenuItemClickListener {
toolbar.setTitle(R.string.profiles)
toolbar.inflateMenu(R.menu.profile_manager_menu)
toolbar.setOnMenuItemClickListener(this)
val searchView = toolbar.findViewById<SearchView>(R.id.action_search)
searchView.setOnQueryTextListener(this)
searchView.setQueryHint(getString(android.R.string.search_go))

ProfileManager.ensureNotEmpty()
profilesList = view.findViewById(R.id.list)
ViewCompat.setOnApplyWindowInsetsListener(profilesList, MainListListener)
Expand Down
4 changes: 4 additions & 0 deletions mobile/src/main/res/menu/profile_manager_menu.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,8 @@
app:alphabeticModifiers="CTRL|SHIFT"/>
</menu>
</item>
<item android:id="@+id/action_search"
android:title="@android:string/search_go"
app:actionViewClass="androidx.appcompat.widget.SearchView"
app:showAsAction="always"/>
</menu>