From eaba35c0247740f1e2835c2b6c2603a8ee002d09 Mon Sep 17 00:00:00 2001 From: Brian Quinion Date: Tue, 24 Nov 2020 00:50:09 +0000 Subject: [PATCH] Add a text filter to the top of preferences --- .../activities/MyPreferenceFragment.kt | 41 +++++++++++++++++- .../activities/PreferencesActivity.kt | 29 +++++++++++-- .../main/res/layout/activity_preferences.xml | 43 +++++++++++++++++++ app/src/main/res/values/strings.xml | 2 + 4 files changed, 110 insertions(+), 5 deletions(-) create mode 100644 app/src/main/res/layout/activity_preferences.xml diff --git a/app/src/main/java/info/nightscout/androidaps/activities/MyPreferenceFragment.kt b/app/src/main/java/info/nightscout/androidaps/activities/MyPreferenceFragment.kt index 705c377afa..235447a2fd 100644 --- a/app/src/main/java/info/nightscout/androidaps/activities/MyPreferenceFragment.kt +++ b/app/src/main/java/info/nightscout/androidaps/activities/MyPreferenceFragment.kt @@ -61,6 +61,7 @@ import javax.inject.Inject class MyPreferenceFragment : PreferenceFragmentCompat(), OnSharedPreferenceChangeListener, HasAndroidInjector { private var pluginId = -1 + private var filter = "" @Inject lateinit var rxBus: RxBusWrapper @Inject lateinit var resourceHelper: ResourceHelper @@ -115,11 +116,13 @@ class MyPreferenceFragment : PreferenceFragmentCompat(), OnSharedPreferenceChang override fun setArguments(args: Bundle?) { super.setArguments(args) pluginId = args?.getInt("id") ?: -1 + filter = args?.getString("filter") ?: "" } override fun onSaveInstanceState(outState: Bundle) { super.onSaveInstanceState(outState) outState.putInt("id", pluginId) + outState.putString("filter", filter) } override fun onDestroy() { @@ -148,7 +151,10 @@ class MyPreferenceFragment : PreferenceFragmentCompat(), OnSharedPreferenceChang override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { (savedInstanceState ?: arguments)?.let { bundle -> if (bundle.containsKey("id")) { - pluginId = bundle.getInt("id") + pluginId = bundle.getInt("id") ?: -1 + } + if (bundle.containsKey("filter")) { + filter = bundle.getString("filter") ?: "" } } if (pluginId != -1) { @@ -191,6 +197,7 @@ class MyPreferenceFragment : PreferenceFragmentCompat(), OnSharedPreferenceChang } initSummary(preferenceScreen, pluginId != -1) preprocessPreferences() + if (filter != "") updateFilterVisibility(filter, preferenceScreen) } override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences, key: String) { @@ -288,6 +295,33 @@ class MyPreferenceFragment : PreferenceFragmentCompat(), OnSharedPreferenceChang } } + private fun updateFilterVisibility(filter: String, p: Preference): Boolean { + + var visible = false + + if (p is PreferenceGroup) { + for (i in 0 until p.preferenceCount) { + visible = updateFilterVisibility(filter, p.getPreference(i)) || visible + } + if (visible && p is PreferenceCategory) { + p.initialExpandedChildrenCount = Int.MAX_VALUE + } + } else { + if (p.key != null) { + visible = visible || p.key.contains(filter, true) + } + if (p.title != null) { + visible = visible || p.title.contains(filter, true) + } + if (p.summary != null) { + visible = visible || p.summary.contains(filter, true) + } + } + + p.isVisible = visible + return visible + } + private fun updatePrefSummary(pref: Preference?) { if (pref is ListPreference) { pref.setSummary(pref.entry) @@ -391,4 +425,9 @@ class MyPreferenceFragment : PreferenceFragmentCompat(), OnSharedPreferenceChang } return super.onPreferenceTreeClick(preference) } + + public fun setFilter(filter: String) { + this.filter = filter + updateFilterVisibility(filter, preferenceScreen) + } } \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/activities/PreferencesActivity.kt b/app/src/main/java/info/nightscout/androidaps/activities/PreferencesActivity.kt index ea90f4b250..8d83c1f771 100644 --- a/app/src/main/java/info/nightscout/androidaps/activities/PreferencesActivity.kt +++ b/app/src/main/java/info/nightscout/androidaps/activities/PreferencesActivity.kt @@ -2,11 +2,15 @@ package info.nightscout.androidaps.activities import android.content.Context import android.os.Bundle +import android.text.Editable +import android.text.TextWatcher import androidx.preference.PreferenceFragmentCompat import androidx.preference.PreferenceScreen import info.nightscout.androidaps.R import info.nightscout.androidaps.utils.locale.LocaleHelper import info.nightscout.androidaps.utils.resources.ResourceHelper +import kotlinx.android.synthetic.main.activity_preferences.* +import kotlinx.android.synthetic.main.local_insight_status_item.view.* import javax.inject.Inject class PreferencesActivity : NoSplashAppCompatActivity(), PreferenceFragmentCompat.OnPreferenceStartScreenCallback { @@ -14,18 +18,31 @@ class PreferencesActivity : NoSplashAppCompatActivity(), PreferenceFragmentCompa @Inject lateinit var resourceHelper: ResourceHelper var preferenceId = 0 + var myPreferenceFragment: MyPreferenceFragment? = null + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - setContentView(R.layout.activity_single_fragment) + setContentView(R.layout.activity_preferences) + + pref_filter.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 = resourceHelper.gs(R.string.nav_preferences) supportActionBar?.setDisplayHomeAsUpEnabled(true) supportActionBar?.setDisplayShowHomeEnabled(true) - val myPreferenceFragment = MyPreferenceFragment() + myPreferenceFragment = MyPreferenceFragment() preferenceId = intent.getIntExtra("id", -1) val args = Bundle() args.putInt("id", preferenceId) - myPreferenceFragment.arguments = args - supportFragmentManager.beginTransaction().replace(R.id.frame_layout, myPreferenceFragment).commit() + args.putString("filter", pref_filter.text.toString()) + myPreferenceFragment?.arguments = args + supportFragmentManager.beginTransaction().replace(R.id.frame_layout, myPreferenceFragment!!).commit() } override fun onPreferenceStartScreen(caller: PreferenceFragmentCompat, pref: PreferenceScreen): Boolean { @@ -44,4 +61,8 @@ class PreferencesActivity : NoSplashAppCompatActivity(), PreferenceFragmentCompa override fun attachBaseContext(newBase: Context) { super.attachBaseContext(LocaleHelper.wrap(newBase)) } + + private fun filterPreferences() { + myPreferenceFragment?.setFilter(pref_filter.text.toString()) + } } \ No newline at end of file diff --git a/app/src/main/res/layout/activity_preferences.xml b/app/src/main/res/layout/activity_preferences.xml new file mode 100644 index 0000000000..c5c51650d6 --- /dev/null +++ b/app/src/main/res/layout/activity_preferences.xml @@ -0,0 +1,43 @@ + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 72f49373eb..e40850f3ec 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1466,4 +1466,6 @@ Uploaded Data The following data will be uploaded to your Open Humans account: Glucose values, boluses, carbs, careportal events (except notes), extended boluses, profile switches, total daily doses, temporary basals, temp targets, preferences, application version, device model and screen dimensions. Secret or private information such as your Nightscout URL or API secret will not be uploaded. RileyLink status: + Filter +