Skip to content

Commit

Permalink
Add search tool for profiles
Browse files Browse the repository at this point in the history
  • Loading branch information
madeye committed Feb 25, 2021
1 parent 49f3891 commit fb8649e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
27 changes: 26 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,15 @@ 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 {
return false
}

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

Expand All @@ -331,6 +352,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
3 changes: 3 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,7 @@
app:alphabeticModifiers="CTRL|SHIFT"/>
</menu>
</item>
<item android:id="@+id/action_search"
app:actionViewClass="androidx.appcompat.widget.SearchView"
app:showAsAction="always"/>
</menu>

0 comments on commit fb8649e

Please sign in to comment.