Update source preferences
This commit is contained in:
@@ -6,6 +6,7 @@ import android.os.Bundle
|
||||
import androidx.core.graphics.Insets
|
||||
import androidx.core.view.updatePadding
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.fragment.app.FragmentTransaction
|
||||
import androidx.fragment.app.commit
|
||||
import androidx.preference.Preference
|
||||
import androidx.preference.PreferenceFragmentCompat
|
||||
@@ -52,6 +53,7 @@ class SettingsActivity : BaseActivity<ActivitySettingsBinding>(),
|
||||
|
||||
private fun openFragment(fragment: Fragment) {
|
||||
supportFragmentManager.commit {
|
||||
setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
|
||||
replace(R.id.container, fragment)
|
||||
setReorderingAllowed(true)
|
||||
addToBackStack(null)
|
||||
|
||||
@@ -1,22 +1,25 @@
|
||||
package org.koitharu.kotatsu.settings
|
||||
|
||||
import android.os.Bundle
|
||||
import android.view.View
|
||||
import android.util.ArrayMap
|
||||
import android.view.inputmethod.EditorInfo
|
||||
import androidx.preference.EditTextPreference
|
||||
import androidx.preference.Preference
|
||||
import androidx.preference.PreferenceFragmentCompat
|
||||
import androidx.preference.TwoStatePreference
|
||||
import org.koitharu.kotatsu.R
|
||||
import org.koitharu.kotatsu.core.model.MangaSource
|
||||
import org.koitharu.kotatsu.core.parser.RemoteMangaRepository
|
||||
import org.koitharu.kotatsu.core.prefs.SourceSettings
|
||||
import org.koitharu.kotatsu.settings.utils.EditTextSummaryProvider
|
||||
import org.koitharu.kotatsu.settings.utils.EditTextBindListener
|
||||
import org.koitharu.kotatsu.settings.utils.EditTextDefaultSummaryProvider
|
||||
import org.koitharu.kotatsu.utils.ext.mangaRepositoryOf
|
||||
import org.koitharu.kotatsu.utils.ext.parcelableArgument
|
||||
import org.koitharu.kotatsu.utils.ext.withArgs
|
||||
|
||||
class SourceSettingsFragment : PreferenceFragmentCompat() {
|
||||
|
||||
private val source by lazy(LazyThreadSafetyMode.NONE) {
|
||||
requireArguments().getParcelable<MangaSource>(EXTRA_SOURCE)!!
|
||||
}
|
||||
private val source by parcelableArgument<MangaSource>(EXTRA_SOURCE)
|
||||
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
@@ -26,18 +29,39 @@ class SourceSettingsFragment : PreferenceFragmentCompat() {
|
||||
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
|
||||
preferenceManager.sharedPreferencesName = source.name
|
||||
val repo = mangaRepositoryOf(source) as? RemoteMangaRepository ?: return
|
||||
val keys = repo.onCreatePreferences()
|
||||
addPreferencesFromResource(R.xml.pref_source)
|
||||
for (i in 0 until preferenceScreen.preferenceCount) {
|
||||
val pref = preferenceScreen.getPreference(i)
|
||||
pref.isVisible = pref.key in keys
|
||||
val screen = preferenceScreen
|
||||
val prefsMap = ArrayMap<String, Any>(screen.preferenceCount)
|
||||
repo.onCreatePreferences(prefsMap)
|
||||
for (i in 0 until screen.preferenceCount) {
|
||||
val pref = screen.getPreference(i)
|
||||
val defValue = prefsMap[pref.key]
|
||||
pref.isVisible = defValue != null
|
||||
if (defValue != null) {
|
||||
initPreferenceWithDefaultValue(pref, defValue)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
findPreference<EditTextPreference>(SourceSettings.KEY_DOMAIN)?.summaryProvider =
|
||||
EditTextSummaryProvider(R.string._default)
|
||||
private fun initPreferenceWithDefaultValue(preference: Preference, defaultValue: Any) {
|
||||
when(preference) {
|
||||
is EditTextPreference -> {
|
||||
preference.summaryProvider = EditTextDefaultSummaryProvider(defaultValue.toString())
|
||||
when(preference.key) {
|
||||
SourceSettings.KEY_DOMAIN -> preference.setOnBindEditTextListener(
|
||||
EditTextBindListener(
|
||||
EditorInfo.TYPE_CLASS_TEXT or EditorInfo.TYPE_TEXT_VARIATION_URI,
|
||||
defaultValue.toString()
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
is TwoStatePreference -> {
|
||||
if (defaultValue is Boolean) {
|
||||
preference.isChecked = defaultValue
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
package org.koitharu.kotatsu.settings.utils
|
||||
|
||||
import android.widget.EditText
|
||||
import androidx.preference.EditTextPreference
|
||||
|
||||
class EditTextBindListener(
|
||||
private val inputType: Int,
|
||||
private val hint: String
|
||||
) : EditTextPreference.OnBindEditTextListener {
|
||||
|
||||
override fun onBindEditText(editText: EditText) {
|
||||
editText.inputType = inputType
|
||||
editText.hint = hint
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package org.koitharu.kotatsu.settings.utils
|
||||
|
||||
import androidx.preference.EditTextPreference
|
||||
import androidx.preference.Preference
|
||||
import org.koitharu.kotatsu.R
|
||||
|
||||
class EditTextDefaultSummaryProvider(
|
||||
private val defaultValue: String
|
||||
) : Preference.SummaryProvider<EditTextPreference> {
|
||||
|
||||
override fun provideSummary(preference: EditTextPreference): CharSequence {
|
||||
return if (preference.text.isNullOrEmpty()) {
|
||||
preference.context.getString(R.string.default_s, defaultValue)
|
||||
} else {
|
||||
preference.text
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user