feat: preference search in actionbar

This commit is contained in:
Andries Smit 2022-07-07 11:38:36 +02:00
parent 396bfc8304
commit e7bd9ad81e
4 changed files with 34 additions and 39 deletions

View file

@ -2,9 +2,9 @@ package info.nightscout.androidaps.activities
import android.content.Context import android.content.Context
import android.os.Bundle import android.os.Bundle
import android.text.Editable import android.view.Menu
import android.text.TextWatcher
import android.view.MenuItem import android.view.MenuItem
import androidx.appcompat.widget.SearchView
import androidx.preference.PreferenceFragmentCompat import androidx.preference.PreferenceFragmentCompat
import androidx.preference.PreferenceScreen import androidx.preference.PreferenceScreen
import info.nightscout.androidaps.R import info.nightscout.androidaps.R
@ -15,6 +15,7 @@ class PreferencesActivity : NoSplashAppCompatActivity(), PreferenceFragmentCompa
private var preferenceId = 0 private var preferenceId = 0
private var myPreferenceFragment: MyPreferenceFragment? = null private var myPreferenceFragment: MyPreferenceFragment? = null
private var searchView: SearchView? = null
private lateinit var binding: ActivityPreferencesBinding private lateinit var binding: ActivityPreferencesBinding
@ -24,15 +25,6 @@ class PreferencesActivity : NoSplashAppCompatActivity(), PreferenceFragmentCompa
binding = ActivityPreferencesBinding.inflate(layoutInflater) binding = ActivityPreferencesBinding.inflate(layoutInflater)
setContentView(binding.root) setContentView(binding.root)
binding.prefFilter.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
filterPreferences()
}
override fun afterTextChanged(s: Editable) {}
})
title = rh.gs(R.string.nav_preferences) title = rh.gs(R.string.nav_preferences)
supportActionBar?.setDisplayHomeAsUpEnabled(true) supportActionBar?.setDisplayHomeAsUpEnabled(true)
supportActionBar?.setDisplayShowHomeEnabled(true) supportActionBar?.setDisplayShowHomeEnabled(true)
@ -40,12 +32,29 @@ class PreferencesActivity : NoSplashAppCompatActivity(), PreferenceFragmentCompa
preferenceId = intent.getIntExtra("id", -1) preferenceId = intent.getIntExtra("id", -1)
myPreferenceFragment?.arguments = Bundle().also { myPreferenceFragment?.arguments = Bundle().also {
it.putInt("id", preferenceId) it.putInt("id", preferenceId)
it.putString("filter", binding.prefFilter.text.toString())
} }
if (savedInstanceState == null) if (savedInstanceState == null)
supportFragmentManager.beginTransaction().replace(R.id.frame_layout, myPreferenceFragment!!).commit() supportFragmentManager.beginTransaction().replace(R.id.frame_layout, myPreferenceFragment!!).commit()
} }
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.menu_preferences, menu)
val searchItem = menu.findItem(R.id.menu_search)
searchView = searchItem.actionView as SearchView
searchView?.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
override fun onQueryTextChange(newText: String): Boolean {
myPreferenceFragment?.setFilter(newText)
return false
}
override fun onQueryTextSubmit(query: String): Boolean {
return false
}
})
return super.onCreateOptionsMenu(menu)
}
override fun onPreferenceStartScreen(caller: PreferenceFragmentCompat, pref: PreferenceScreen): Boolean { override fun onPreferenceStartScreen(caller: PreferenceFragmentCompat, pref: PreferenceScreen): Boolean {
val fragment = MyPreferenceFragment() val fragment = MyPreferenceFragment()
fragment.arguments = Bundle().also { fragment.arguments = Bundle().also {
@ -60,10 +69,6 @@ class PreferencesActivity : NoSplashAppCompatActivity(), PreferenceFragmentCompa
super.attachBaseContext(LocaleHelper.wrap(newBase)) super.attachBaseContext(LocaleHelper.wrap(newBase))
} }
private fun filterPreferences() {
myPreferenceFragment?.setFilter(binding.prefFilter.text.toString())
}
override fun onOptionsItemSelected(item: MenuItem): Boolean { override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) { when (item.itemId) {
android.R.id.home -> { android.R.id.home -> {

View file

@ -5,28 +5,6 @@
android:layout_height="match_parent" android:layout_height="match_parent"
android:orientation="vertical"> android:orientation="vertical">
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/filter"
app:endIconMode="clear_text">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/pref_filter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:autofillHints="@string/filter"
android:gravity="start"
android:textStyle="bold"
android:inputType="text"
android:background="@color/transparent"/>
</com.google.android.material.textfield.TextInputLayout>
<ScrollView <ScrollView
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"

View file

@ -0,0 +1,11 @@
<?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/menu_search"
android:title="@string/search"
app:showAsAction="always"
app:actionViewClass="androidx.appcompat.widget.SearchView" />
</menu>

View file

@ -1242,6 +1242,7 @@
<string name="a11y_only_on_watch">only on watch</string> <string name="a11y_only_on_watch">only on watch</string>
<string name="a11y_only_on_phone">only on phone</string> <string name="a11y_only_on_phone">only on phone</string>
<string name="a11y_drag_and_drop_handle">drag and drop handle</string> <string name="a11y_drag_and_drop_handle">drag and drop handle</string>
<string name="search">Search</string>
<!-- Aidex Cgms --> <!-- Aidex Cgms -->
<string name="aidex">GlucoRx Aidex</string> <string name="aidex">GlucoRx Aidex</string>