AndroidAPS/app/src/main/java/info/nightscout/androidaps/activities/PreferencesActivity.kt

64 lines
2.6 KiB
Kotlin
Raw Normal View History

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
2021-01-22 14:04:32 +01:00
import info.nightscout.androidaps.databinding.ActivityPreferencesBinding
2020-05-23 15:39:05 +02:00
import info.nightscout.androidaps.utils.locale.LocaleHelper
class PreferencesActivity : NoSplashAppCompatActivity(), PreferenceFragmentCompat.OnPreferenceStartScreenCallback {
2021-01-22 14:04:32 +01:00
private var preferenceId = 0
private var myPreferenceFragment: MyPreferenceFragment? = null
private lateinit var binding: ActivityPreferencesBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
2021-01-22 14:04:32 +01:00
binding = ActivityPreferencesBinding.inflate(layoutInflater)
setContentView(binding.root)
2021-01-22 14:04:32 +01:00
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) {}
})
2021-11-02 09:53:16 +01:00
title = rh.gs(R.string.nav_preferences)
supportActionBar?.setDisplayHomeAsUpEnabled(true)
supportActionBar?.setDisplayShowHomeEnabled(true)
myPreferenceFragment = MyPreferenceFragment()
preferenceId = intent.getIntExtra("id", -1)
myPreferenceFragment?.arguments = Bundle().also {
it.putInt("id", preferenceId)
2021-01-22 14:04:32 +01:00
it.putString("filter", binding.prefFilter.text.toString())
}
if (savedInstanceState == null)
supportFragmentManager.beginTransaction().replace(R.id.frame_layout, myPreferenceFragment!!).commit()
}
override fun onPreferenceStartScreen(caller: PreferenceFragmentCompat, pref: PreferenceScreen): Boolean {
val fragment = MyPreferenceFragment()
fragment.arguments = Bundle().also {
it.putString(PreferenceFragmentCompat.ARG_PREFERENCE_ROOT, pref.key)
it.putInt("id", preferenceId)
}
supportFragmentManager.beginTransaction().replace(R.id.frame_layout, fragment, pref.key).addToBackStack(pref.key).commit()
return true
}
override fun attachBaseContext(newBase: Context) {
super.attachBaseContext(LocaleHelper.wrap(newBase))
}
private fun filterPreferences() {
2021-01-22 14:04:32 +01:00
myPreferenceFragment?.setFilter(binding.prefFilter.text.toString())
}
}