diff --git a/src/es/catharsisworld/build.gradle b/src/es/catharsisworld/build.gradle index 4a99a3dc0..6b52cdd2b 100644 --- a/src/es/catharsisworld/build.gradle +++ b/src/es/catharsisworld/build.gradle @@ -3,7 +3,7 @@ ext { extClass = '.CatharsisWorld' themePkg = 'madara' baseUrl = 'https://catharsisworld.dig-it.info' - overrideVersionCode = 7 + overrideVersionCode = 8 isNsfw = true } diff --git a/src/es/catharsisworld/src/eu/kanade/tachiyomi/extension/es/catharsisworld/CatharsisWorld.kt b/src/es/catharsisworld/src/eu/kanade/tachiyomi/extension/es/catharsisworld/CatharsisWorld.kt index b22447d6b..e104d22bd 100644 --- a/src/es/catharsisworld/src/eu/kanade/tachiyomi/extension/es/catharsisworld/CatharsisWorld.kt +++ b/src/es/catharsisworld/src/eu/kanade/tachiyomi/extension/es/catharsisworld/CatharsisWorld.kt @@ -1,26 +1,50 @@ package eu.kanade.tachiyomi.extension.es.catharsisworld +import android.content.SharedPreferences +import android.widget.Toast +import androidx.preference.EditTextPreference +import androidx.preference.PreferenceScreen import eu.kanade.tachiyomi.multisrc.madara.Madara import eu.kanade.tachiyomi.network.interceptor.rateLimitHost +import eu.kanade.tachiyomi.source.ConfigurableSource import eu.kanade.tachiyomi.source.model.SChapter import eu.kanade.tachiyomi.source.model.SManga +import keiyoushi.utils.getPreferences import okhttp3.HttpUrl.Companion.toHttpUrl import org.jsoup.nodes.Element -class CatharsisWorld : Madara( - "Catharsis World", - "https://catharsisworld.dig-it.info", - "es", -) { +class CatharsisWorld : + Madara( + "Catharsis World", + "https://catharsisworld.dig-it.info", + "es", + ), + ConfigurableSource { + + override val baseUrl get() = preferences.prefBaseUrl + override val versionId = 2 override val mangaSubString = "serie" override val useLoadMoreRequest = LoadMoreStrategy.Always - override val client = super.client.newBuilder() - .rateLimitHost(baseUrl.toHttpUrl(), 3, 1) - .build() + override val client by lazy { + super.client.newBuilder() + .rateLimitHost(baseUrl.toHttpUrl(), 3, 1) + .build() + } + + private val preferences = getPreferences { + this.getString(DEFAULT_BASE_URL_PREF, null).let { domain -> + if (domain != super.baseUrl) { + this.edit() + .putString(BASE_URL_PREF, super.baseUrl) + .putString(DEFAULT_BASE_URL_PREF, super.baseUrl) + .apply() + } + } + } override fun popularMangaSelector() = "div.latest-poster" @@ -57,4 +81,43 @@ class CatharsisWorld : Madara( private fun Element.imageFromStyle(): String { return this.attr("style").substringAfter("url(").substringBefore(")") } + + override fun setupPreferenceScreen(screen: PreferenceScreen) { + EditTextPreference(screen.context).apply { + key = BASE_URL_PREF + title = "Editar URL de la fuente" + summary = "Para uso temporal, si la extensión se actualiza se perderá el cambio." + dialogTitle = "Editar URL de la fuente" + dialogMessage = "URL por defecto:\n${super.baseUrl}" + setDefaultValue(super.baseUrl) + setOnPreferenceChangeListener { _, newVal -> + val url = newVal as String + try { + val httpurl = url.toHttpUrl() + preferences.prefBaseUrl = "https://${httpurl.host}" + } catch (_: Throwable) { + Toast.makeText(screen.context, "Invalid Url", Toast.LENGTH_LONG).show() + } + false + } + }.also { screen.addPreference(it) } + } + + private var _cachedBaseUrl: String? = null + private var SharedPreferences.prefBaseUrl: String + get() { + if (_cachedBaseUrl == null) { + _cachedBaseUrl = getString(BASE_URL_PREF, super.baseUrl)!! + } + return _cachedBaseUrl!! + } + set(value) { + _cachedBaseUrl = value + edit().putString(BASE_URL_PREF, value).apply() + } + + companion object { + private const val BASE_URL_PREF = "overrideBaseUrl" + private const val DEFAULT_BASE_URL_PREF = "defaultBaseUrl" + } }