|
|
|
@ -1,22 +1,25 @@
|
|
|
|
package org.koitharu.kotatsu.settings
|
|
|
|
package org.koitharu.kotatsu.settings
|
|
|
|
|
|
|
|
|
|
|
|
import android.os.Bundle
|
|
|
|
import android.os.Bundle
|
|
|
|
import android.view.View
|
|
|
|
import android.util.ArrayMap
|
|
|
|
|
|
|
|
import android.view.inputmethod.EditorInfo
|
|
|
|
import androidx.preference.EditTextPreference
|
|
|
|
import androidx.preference.EditTextPreference
|
|
|
|
|
|
|
|
import androidx.preference.Preference
|
|
|
|
import androidx.preference.PreferenceFragmentCompat
|
|
|
|
import androidx.preference.PreferenceFragmentCompat
|
|
|
|
|
|
|
|
import androidx.preference.TwoStatePreference
|
|
|
|
import org.koitharu.kotatsu.R
|
|
|
|
import org.koitharu.kotatsu.R
|
|
|
|
import org.koitharu.kotatsu.core.model.MangaSource
|
|
|
|
import org.koitharu.kotatsu.core.model.MangaSource
|
|
|
|
import org.koitharu.kotatsu.core.parser.RemoteMangaRepository
|
|
|
|
import org.koitharu.kotatsu.core.parser.RemoteMangaRepository
|
|
|
|
import org.koitharu.kotatsu.core.prefs.SourceSettings
|
|
|
|
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.mangaRepositoryOf
|
|
|
|
|
|
|
|
import org.koitharu.kotatsu.utils.ext.parcelableArgument
|
|
|
|
import org.koitharu.kotatsu.utils.ext.withArgs
|
|
|
|
import org.koitharu.kotatsu.utils.ext.withArgs
|
|
|
|
|
|
|
|
|
|
|
|
class SourceSettingsFragment : PreferenceFragmentCompat() {
|
|
|
|
class SourceSettingsFragment : PreferenceFragmentCompat() {
|
|
|
|
|
|
|
|
|
|
|
|
private val source by lazy(LazyThreadSafetyMode.NONE) {
|
|
|
|
private val source by parcelableArgument<MangaSource>(EXTRA_SOURCE)
|
|
|
|
requireArguments().getParcelable<MangaSource>(EXTRA_SOURCE)!!
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
override fun onResume() {
|
|
|
|
override fun onResume() {
|
|
|
|
super.onResume()
|
|
|
|
super.onResume()
|
|
|
|
@ -26,18 +29,39 @@ class SourceSettingsFragment : PreferenceFragmentCompat() {
|
|
|
|
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
|
|
|
|
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
|
|
|
|
preferenceManager.sharedPreferencesName = source.name
|
|
|
|
preferenceManager.sharedPreferencesName = source.name
|
|
|
|
val repo = mangaRepositoryOf(source) as? RemoteMangaRepository ?: return
|
|
|
|
val repo = mangaRepositoryOf(source) as? RemoteMangaRepository ?: return
|
|
|
|
val keys = repo.onCreatePreferences()
|
|
|
|
|
|
|
|
addPreferencesFromResource(R.xml.pref_source)
|
|
|
|
addPreferencesFromResource(R.xml.pref_source)
|
|
|
|
for (i in 0 until preferenceScreen.preferenceCount) {
|
|
|
|
val screen = preferenceScreen
|
|
|
|
val pref = preferenceScreen.getPreference(i)
|
|
|
|
val prefsMap = ArrayMap<String, Any>(screen.preferenceCount)
|
|
|
|
pref.isVisible = pref.key in keys
|
|
|
|
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?) {
|
|
|
|
private fun initPreferenceWithDefaultValue(preference: Preference, defaultValue: Any) {
|
|
|
|
super.onViewCreated(view, savedInstanceState)
|
|
|
|
when(preference) {
|
|
|
|
findPreference<EditTextPreference>(SourceSettings.KEY_DOMAIN)?.summaryProvider =
|
|
|
|
is EditTextPreference -> {
|
|
|
|
EditTextSummaryProvider(R.string._default)
|
|
|
|
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 {
|
|
|
|
companion object {
|
|
|
|
|