Update source preferences
This commit is contained in:
@@ -5,6 +5,7 @@ import org.koitharu.kotatsu.core.model.MangaPage
|
|||||||
import org.koitharu.kotatsu.core.model.MangaSource
|
import org.koitharu.kotatsu.core.model.MangaSource
|
||||||
import org.koitharu.kotatsu.core.model.MangaTag
|
import org.koitharu.kotatsu.core.model.MangaTag
|
||||||
import org.koitharu.kotatsu.core.model.SortOrder
|
import org.koitharu.kotatsu.core.model.SortOrder
|
||||||
|
import org.koitharu.kotatsu.core.prefs.SourceSettings
|
||||||
|
|
||||||
abstract class RemoteMangaRepository(
|
abstract class RemoteMangaRepository(
|
||||||
protected val loaderContext: MangaLoaderContext
|
protected val loaderContext: MangaLoaderContext
|
||||||
@@ -24,7 +25,9 @@ abstract class RemoteMangaRepository(
|
|||||||
|
|
||||||
override suspend fun getTags(): Set<MangaTag> = emptySet()
|
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)
|
protected fun getDomain() = conf.getDomain(defaultDomain)
|
||||||
|
|
||||||
|
|||||||
@@ -1,11 +1,9 @@
|
|||||||
package org.koitharu.kotatsu.core.parser.site
|
package org.koitharu.kotatsu.core.parser.site
|
||||||
|
|
||||||
import androidx.collection.arraySetOf
|
|
||||||
import org.koitharu.kotatsu.base.domain.MangaLoaderContext
|
import org.koitharu.kotatsu.base.domain.MangaLoaderContext
|
||||||
import org.koitharu.kotatsu.core.exceptions.ParseException
|
import org.koitharu.kotatsu.core.exceptions.ParseException
|
||||||
import org.koitharu.kotatsu.core.model.*
|
import org.koitharu.kotatsu.core.model.*
|
||||||
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.utils.ext.*
|
import org.koitharu.kotatsu.utils.ext.*
|
||||||
import java.util.*
|
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?) =
|
private fun getSortKey(sortOrder: SortOrder?) =
|
||||||
when (sortOrder ?: sortOrders.minByOrNull { it.ordinal }) {
|
when (sortOrder ?: sortOrders.minByOrNull { it.ordinal }) {
|
||||||
SortOrder.ALPHABETICAL -> "catalog"
|
SortOrder.ALPHABETICAL -> "catalog"
|
||||||
|
|||||||
@@ -1,11 +1,9 @@
|
|||||||
package org.koitharu.kotatsu.core.parser.site
|
package org.koitharu.kotatsu.core.parser.site
|
||||||
|
|
||||||
import androidx.collection.arraySetOf
|
|
||||||
import org.koitharu.kotatsu.base.domain.MangaLoaderContext
|
import org.koitharu.kotatsu.base.domain.MangaLoaderContext
|
||||||
import org.koitharu.kotatsu.core.exceptions.ParseException
|
import org.koitharu.kotatsu.core.exceptions.ParseException
|
||||||
import org.koitharu.kotatsu.core.model.*
|
import org.koitharu.kotatsu.core.model.*
|
||||||
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.utils.ext.*
|
import org.koitharu.kotatsu.utils.ext.*
|
||||||
import java.util.*
|
import java.util.*
|
||||||
import kotlin.collections.ArrayList
|
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?) =
|
private fun getSortKey(sortOrder: SortOrder?) =
|
||||||
when (sortOrder) {
|
when (sortOrder) {
|
||||||
SortOrder.ALPHABETICAL -> "name"
|
SortOrder.ALPHABETICAL -> "name"
|
||||||
|
|||||||
@@ -173,8 +173,6 @@ abstract class GroupleRepository(loaderContext: MangaLoaderContext) :
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onCreatePreferences() = arraySetOf(SourceSettings.KEY_DOMAIN)
|
|
||||||
|
|
||||||
private fun getSortKey(sortOrder: SortOrder?) =
|
private fun getSortKey(sortOrder: SortOrder?) =
|
||||||
when (sortOrder ?: sortOrders.minByOrNull { it.ordinal }) {
|
when (sortOrder ?: sortOrders.minByOrNull { it.ordinal }) {
|
||||||
SortOrder.ALPHABETICAL -> "name"
|
SortOrder.ALPHABETICAL -> "name"
|
||||||
|
|||||||
@@ -72,8 +72,6 @@ open class MangaLibRepository(loaderContext: MangaLoaderContext) :
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onCreatePreferences() = arraySetOf(SourceSettings.KEY_DOMAIN)
|
|
||||||
|
|
||||||
override suspend fun getDetails(manga: Manga): Manga {
|
override suspend fun getDetails(manga: Manga): Manga {
|
||||||
val fullUrl = manga.url.withDomain()
|
val fullUrl = manga.url.withDomain()
|
||||||
val doc = loaderContext.httpGet("$fullUrl?section=info").parseHtml()
|
val doc = loaderContext.httpGet("$fullUrl?section=info").parseHtml()
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
package org.koitharu.kotatsu.core.parser.site
|
package org.koitharu.kotatsu.core.parser.site
|
||||||
|
|
||||||
import androidx.collection.arraySetOf
|
|
||||||
import org.intellij.lang.annotations.Language
|
import org.intellij.lang.annotations.Language
|
||||||
import org.koitharu.kotatsu.base.domain.MangaLoaderContext
|
import org.koitharu.kotatsu.base.domain.MangaLoaderContext
|
||||||
import org.koitharu.kotatsu.core.exceptions.ParseException
|
import org.koitharu.kotatsu.core.exceptions.ParseException
|
||||||
@@ -160,11 +159,10 @@ class MangaTownRepository(loaderContext: MangaLoaderContext) :
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun onCreatePreferences(map: MutableMap<String, Any>) {
|
||||||
override fun onCreatePreferences() = arraySetOf(
|
super.onCreatePreferences(map)
|
||||||
SourceSettings.KEY_DOMAIN,
|
map[SourceSettings.KEY_USE_SSL] = true
|
||||||
SourceSettings.KEY_USE_SSL
|
}
|
||||||
)
|
|
||||||
|
|
||||||
private fun String.parseTagKey() = split('/').findLast { TAG_REGEX matches it }
|
private fun String.parseTagKey() = split('/').findLast { TAG_REGEX matches it }
|
||||||
|
|
||||||
|
|||||||
@@ -159,8 +159,6 @@ class MangareadRepository(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onCreatePreferences() = arraySetOf(SourceSettings.KEY_DOMAIN)
|
|
||||||
|
|
||||||
private companion object {
|
private companion object {
|
||||||
|
|
||||||
private const val PAGE_SIZE = 12
|
private const val PAGE_SIZE = 12
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
package org.koitharu.kotatsu.core.parser.site
|
package org.koitharu.kotatsu.core.parser.site
|
||||||
|
|
||||||
import androidx.collection.arraySetOf
|
|
||||||
import org.json.JSONArray
|
import org.json.JSONArray
|
||||||
import org.json.JSONException
|
import org.json.JSONException
|
||||||
import org.json.JSONObject
|
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.exceptions.ParseException
|
||||||
import org.koitharu.kotatsu.core.model.*
|
import org.koitharu.kotatsu.core.model.*
|
||||||
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.utils.ext.*
|
import org.koitharu.kotatsu.utils.ext.*
|
||||||
import java.util.*
|
import java.util.*
|
||||||
import kotlin.collections.ArrayList
|
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) {
|
private fun getSortKey(order: SortOrder?) = when (order) {
|
||||||
SortOrder.UPDATED -> "-chapter_date"
|
SortOrder.UPDATED -> "-chapter_date"
|
||||||
SortOrder.POPULARITY -> "-rating"
|
SortOrder.POPULARITY -> "-rating"
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import android.os.Bundle
|
|||||||
import androidx.core.graphics.Insets
|
import androidx.core.graphics.Insets
|
||||||
import androidx.core.view.updatePadding
|
import androidx.core.view.updatePadding
|
||||||
import androidx.fragment.app.Fragment
|
import androidx.fragment.app.Fragment
|
||||||
|
import androidx.fragment.app.FragmentTransaction
|
||||||
import androidx.fragment.app.commit
|
import androidx.fragment.app.commit
|
||||||
import androidx.preference.Preference
|
import androidx.preference.Preference
|
||||||
import androidx.preference.PreferenceFragmentCompat
|
import androidx.preference.PreferenceFragmentCompat
|
||||||
@@ -52,6 +53,7 @@ class SettingsActivity : BaseActivity<ActivitySettingsBinding>(),
|
|||||||
|
|
||||||
private fun openFragment(fragment: Fragment) {
|
private fun openFragment(fragment: Fragment) {
|
||||||
supportFragmentManager.commit {
|
supportFragmentManager.commit {
|
||||||
|
setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
|
||||||
replace(R.id.container, fragment)
|
replace(R.id.container, fragment)
|
||||||
setReorderingAllowed(true)
|
setReorderingAllowed(true)
|
||||||
addToBackStack(null)
|
addToBackStack(null)
|
||||||
|
|||||||
@@ -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 {
|
||||||
|
|||||||
@@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -16,11 +16,13 @@ val Fragment.viewLifecycleScope
|
|||||||
get() = viewLifecycleOwner.lifecycle.coroutineScope
|
get() = viewLifecycleOwner.lifecycle.coroutineScope
|
||||||
|
|
||||||
@Suppress("NOTHING_TO_INLINE")
|
@Suppress("NOTHING_TO_INLINE")
|
||||||
inline fun <T : Parcelable> Fragment.parcelableArgument(name: String) =
|
inline fun <T : Parcelable> Fragment.parcelableArgument(name: String): Lazy<T> {
|
||||||
lazy<T>(LazyThreadSafetyMode.NONE) {
|
return lazy(LazyThreadSafetyMode.NONE) {
|
||||||
requireArguments().getParcelable(name)
|
requireNotNull(arguments?.getParcelable(name)) {
|
||||||
?: error("No argument $name passed in ${javaClass.simpleName}")
|
"No argument $name passed into ${javaClass.simpleName}"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Suppress("NOTHING_TO_INLINE")
|
@Suppress("NOTHING_TO_INLINE")
|
||||||
inline fun Fragment.stringArgument(name: String) = lazy(LazyThreadSafetyMode.NONE) {
|
inline fun Fragment.stringArgument(name: String) = lazy(LazyThreadSafetyMode.NONE) {
|
||||||
|
|||||||
@@ -199,4 +199,5 @@
|
|||||||
<string name="reverse">В обратном порядке</string>
|
<string name="reverse">В обратном порядке</string>
|
||||||
<string name="sign_in">Войти</string>
|
<string name="sign_in">Войти</string>
|
||||||
<string name="auth_required">Для просмотра этого контента требуется авторизация</string>
|
<string name="auth_required">Для просмотра этого контента требуется авторизация</string>
|
||||||
|
<string name="default_s">По умолчанию: %s</string>
|
||||||
</resources>
|
</resources>
|
||||||
@@ -201,4 +201,5 @@
|
|||||||
<string name="reverse">Reverse</string>
|
<string name="reverse">Reverse</string>
|
||||||
<string name="sign_in">Sign in</string>
|
<string name="sign_in">Sign in</string>
|
||||||
<string name="auth_required">You should authorize to view this content</string>
|
<string name="auth_required">You should authorize to view this content</string>
|
||||||
|
<string name="default_s">Default: %s</string>
|
||||||
</resources>
|
</resources>
|
||||||
Reference in New Issue
Block a user