Update source preferences

This commit is contained in:
Koitharu
2021-02-01 20:19:46 +02:00
parent 4aa1b58109
commit c25ee93ccb
15 changed files with 88 additions and 42 deletions

View File

@@ -5,6 +5,7 @@ import org.koitharu.kotatsu.core.model.MangaPage
import org.koitharu.kotatsu.core.model.MangaSource
import org.koitharu.kotatsu.core.model.MangaTag
import org.koitharu.kotatsu.core.model.SortOrder
import org.koitharu.kotatsu.core.prefs.SourceSettings
abstract class RemoteMangaRepository(
protected val loaderContext: MangaLoaderContext
@@ -24,7 +25,9 @@ abstract class RemoteMangaRepository(
override suspend fun getTags(): Set<MangaTag> = emptySet()
abstract fun onCreatePreferences(): Set<String>
open fun onCreatePreferences(map: MutableMap<String, Any>) {
map[SourceSettings.KEY_DOMAIN] = defaultDomain
}
protected fun getDomain() = conf.getDomain(defaultDomain)

View File

@@ -1,11 +1,9 @@
package org.koitharu.kotatsu.core.parser.site
import androidx.collection.arraySetOf
import org.koitharu.kotatsu.base.domain.MangaLoaderContext
import org.koitharu.kotatsu.core.exceptions.ParseException
import org.koitharu.kotatsu.core.model.*
import org.koitharu.kotatsu.core.parser.RemoteMangaRepository
import org.koitharu.kotatsu.core.prefs.SourceSettings
import org.koitharu.kotatsu.utils.ext.*
import java.util.*
@@ -136,8 +134,6 @@ abstract class ChanRepository(loaderContext: MangaLoaderContext) : RemoteMangaRe
}
}
override fun onCreatePreferences() = arraySetOf(SourceSettings.KEY_DOMAIN)
private fun getSortKey(sortOrder: SortOrder?) =
when (sortOrder ?: sortOrders.minByOrNull { it.ordinal }) {
SortOrder.ALPHABETICAL -> "catalog"

View File

@@ -1,11 +1,9 @@
package org.koitharu.kotatsu.core.parser.site
import androidx.collection.arraySetOf
import org.koitharu.kotatsu.base.domain.MangaLoaderContext
import org.koitharu.kotatsu.core.exceptions.ParseException
import org.koitharu.kotatsu.core.model.*
import org.koitharu.kotatsu.core.parser.RemoteMangaRepository
import org.koitharu.kotatsu.core.prefs.SourceSettings
import org.koitharu.kotatsu.utils.ext.*
import java.util.*
import kotlin.collections.ArrayList
@@ -127,8 +125,6 @@ class DesuMeRepository(loaderContext: MangaLoaderContext) : RemoteMangaRepositor
}
}
override fun onCreatePreferences() = arraySetOf(SourceSettings.KEY_DOMAIN)
private fun getSortKey(sortOrder: SortOrder?) =
when (sortOrder) {
SortOrder.ALPHABETICAL -> "name"

View File

@@ -173,8 +173,6 @@ abstract class GroupleRepository(loaderContext: MangaLoaderContext) :
}
}
override fun onCreatePreferences() = arraySetOf(SourceSettings.KEY_DOMAIN)
private fun getSortKey(sortOrder: SortOrder?) =
when (sortOrder ?: sortOrders.minByOrNull { it.ordinal }) {
SortOrder.ALPHABETICAL -> "name"

View File

@@ -72,8 +72,6 @@ open class MangaLibRepository(loaderContext: MangaLoaderContext) :
}
}
override fun onCreatePreferences() = arraySetOf(SourceSettings.KEY_DOMAIN)
override suspend fun getDetails(manga: Manga): Manga {
val fullUrl = manga.url.withDomain()
val doc = loaderContext.httpGet("$fullUrl?section=info").parseHtml()

View File

@@ -1,6 +1,5 @@
package org.koitharu.kotatsu.core.parser.site
import androidx.collection.arraySetOf
import org.intellij.lang.annotations.Language
import org.koitharu.kotatsu.base.domain.MangaLoaderContext
import org.koitharu.kotatsu.core.exceptions.ParseException
@@ -160,11 +159,10 @@ class MangaTownRepository(loaderContext: MangaLoaderContext) :
}
}
override fun onCreatePreferences() = arraySetOf(
SourceSettings.KEY_DOMAIN,
SourceSettings.KEY_USE_SSL
)
override fun onCreatePreferences(map: MutableMap<String, Any>) {
super.onCreatePreferences(map)
map[SourceSettings.KEY_USE_SSL] = true
}
private fun String.parseTagKey() = split('/').findLast { TAG_REGEX matches it }

View File

@@ -159,8 +159,6 @@ class MangareadRepository(
}
}
override fun onCreatePreferences() = arraySetOf(SourceSettings.KEY_DOMAIN)
private companion object {
private const val PAGE_SIZE = 12

View File

@@ -1,6 +1,5 @@
package org.koitharu.kotatsu.core.parser.site
import androidx.collection.arraySetOf
import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject
@@ -8,7 +7,6 @@ import org.koitharu.kotatsu.base.domain.MangaLoaderContext
import org.koitharu.kotatsu.core.exceptions.ParseException
import org.koitharu.kotatsu.core.model.*
import org.koitharu.kotatsu.core.parser.RemoteMangaRepository
import org.koitharu.kotatsu.core.prefs.SourceSettings
import org.koitharu.kotatsu.utils.ext.*
import java.util.*
import kotlin.collections.ArrayList
@@ -158,8 +156,6 @@ class RemangaRepository(loaderContext: MangaLoaderContext) : RemoteMangaReposito
}
}
override fun onCreatePreferences() = arraySetOf(SourceSettings.KEY_DOMAIN)
private fun getSortKey(order: SortOrder?) = when (order) {
SortOrder.UPDATED -> "-chapter_date"
SortOrder.POPULARITY -> "-rating"

View File

@@ -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)

View File

@@ -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 {

View File

@@ -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
}
}

View File

@@ -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
}
}
}

View File

@@ -16,11 +16,13 @@ val Fragment.viewLifecycleScope
get() = viewLifecycleOwner.lifecycle.coroutineScope
@Suppress("NOTHING_TO_INLINE")
inline fun <T : Parcelable> Fragment.parcelableArgument(name: String) =
lazy<T>(LazyThreadSafetyMode.NONE) {
requireArguments().getParcelable(name)
?: error("No argument $name passed in ${javaClass.simpleName}")
inline fun <T : Parcelable> Fragment.parcelableArgument(name: String): Lazy<T> {
return lazy(LazyThreadSafetyMode.NONE) {
requireNotNull(arguments?.getParcelable(name)) {
"No argument $name passed into ${javaClass.simpleName}"
}
}
}
@Suppress("NOTHING_TO_INLINE")
inline fun Fragment.stringArgument(name: String) = lazy(LazyThreadSafetyMode.NONE) {

View File

@@ -199,4 +199,5 @@
<string name="reverse">В обратном порядке</string>
<string name="sign_in">Войти</string>
<string name="auth_required">Для просмотра этого контента требуется авторизация</string>
<string name="default_s">По умолчанию: %s</string>
</resources>

View File

@@ -201,4 +201,5 @@
<string name="reverse">Reverse</string>
<string name="sign_in">Sign in</string>
<string name="auth_required">You should authorize to view this content</string>
<string name="default_s">Default: %s</string>
</resources>