From 279026e33bc688a5e82b419819d1ea052d4c1273 Mon Sep 17 00:00:00 2001 From: Maxim Molochkov <48491463+klaseca@users.noreply.github.com> Date: Sun, 9 Oct 2022 21:14:09 +0400 Subject: [PATCH] [RU] Henchan - Move url definition to preferences (#13783) --- .../multichan/henchan/src/HenChan.kt | 91 ++++++++++++++++--- .../multisrc/multichan/ChanGenerator.kt | 2 +- 2 files changed, 81 insertions(+), 12 deletions(-) diff --git a/multisrc/overrides/multichan/henchan/src/HenChan.kt b/multisrc/overrides/multichan/henchan/src/HenChan.kt index 88319551e..3dd65239b 100644 --- a/multisrc/overrides/multichan/henchan/src/HenChan.kt +++ b/multisrc/overrides/multichan/henchan/src/HenChan.kt @@ -1,9 +1,14 @@ package eu.kanade.tachiyomi.extension.ru.henchan import android.annotation.SuppressLint +import android.app.Application +import android.content.SharedPreferences +import android.widget.Toast +import androidx.preference.EditTextPreference import eu.kanade.tachiyomi.multisrc.multichan.MultiChan import eu.kanade.tachiyomi.network.GET import eu.kanade.tachiyomi.network.asObservable +import eu.kanade.tachiyomi.source.ConfigurableSource import eu.kanade.tachiyomi.source.model.Filter import eu.kanade.tachiyomi.source.model.FilterList import eu.kanade.tachiyomi.source.model.Page @@ -16,15 +21,25 @@ import okhttp3.Response import org.jsoup.nodes.Document import org.jsoup.nodes.Element import rx.Observable +import uy.kohesive.injekt.Injekt +import uy.kohesive.injekt.api.get import java.net.URL import java.text.SimpleDateFormat import java.util.Date import java.util.Locale -class HenChan : MultiChan("HenChan", "https://hchan.live", "ru") { +class HenChan : MultiChan("HenChan", "http://y.hchan.live", "ru"), ConfigurableSource { override val id: Long = 5504588601186153612 + private val preferences: SharedPreferences by lazy { + Injekt.get().getSharedPreferences("source_$id", 0x0000) + } + + private val domain = preferences.getString(DOMAIN_TITLE, DOMAIN_DEFAULT)!! + + override val baseUrl = domain + override fun searchMangaRequest(page: Int, query: String, filters: FilterList): Request { val url = if (query.isNotEmpty()) { @@ -76,8 +91,17 @@ class HenChan : MultiChan("HenChan", "https://hchan.live", "ru") { val isExHenManga = this.contains("/manganew_thumbs_blur/") val regex = "(?<=/)manganew_thumbs\\w*?(?=/)".toRegex(RegexOption.IGNORE_CASE) return this.replace(regex, "showfull_retina/manga") - .replace("_".plus(URL(baseUrl).host), "_hentaichan.ru") // domain-related replacing for very old mangas - .plus(if (isExHenManga) { "#" } else { "" }) // # for later so we know what type manga is it + .replace( + "_".plus(URL(baseUrl).host), + "_hentaichan.ru" + ) // domain-related replacing for very old mangas + .plus( + if (isExHenManga) { + "#" + } else { + "" + } + ) // # for later so we know what type manga is it } override fun popularMangaFromElement(element: Element): SManga { @@ -154,7 +178,8 @@ class HenChan : MultiChan("HenChan", "https://hchan.live", "ru") { .replace("\\\"", "\"") .replace("\\'", "'") chap.chapter_number = 1F - chap.date_upload = Date().time // setting to current date because of a sorting in the "Recent updates" section + chap.date_upload = + Date().time // setting to current date because of a sorting in the "Recent updates" section return listOf(chap) } @@ -190,8 +215,11 @@ class HenChan : MultiChan("HenChan", "https://hchan.live", "ru") { chapter.setUrlWithoutDomain(element.select("h2 a").attr("href")) val chapterName = element.select("h2 a").attr("title") chapter.name = chapterName - chapter.chapter_number = "(глава\\s|часть\\s)([0-9]+\\.?[0-9]*)".toRegex(RegexOption.IGNORE_CASE).find(chapterName)?.groupValues?.get(2)?.toFloat() ?: -1F - chapter.date_upload = Date().time // setting to current date because of a sorting in the "Recent updates" section + chapter.chapter_number = + "(глава\\s|часть\\s)([0-9]+\\.?[0-9]*)".toRegex(RegexOption.IGNORE_CASE) + .find(chapterName)?.groupValues?.get(2)?.toFloat() ?: -1F + chapter.date_upload = + Date().time // setting to current date because of a sorting in the "Recent updates" section return chapter } @@ -217,19 +245,35 @@ class HenChan : MultiChan("HenChan", "https://hchan.live", "ru") { return pageUrls.mapIndexed { i, url -> Page(i, "", url) } } - private class Genre(val id: String, @SuppressLint("DefaultLocale") name: String = id.replace('_', ' ').capitalize()) : Filter.TriState(name) + private class Genre( + val id: String, + @SuppressLint("DefaultLocale") name: String = id.replace('_', ' ').capitalize() + ) : Filter.TriState(name) + private class GenreList(genres: List) : Filter.Group("Тэги", genres) private class OrderBy : UriPartFilter( "Сортировка", arrayOf("Дата", "Популярность", "Алфавит"), arrayOf("&n=dateasc" to "", "&n=favasc" to "&n=favdesc", "&n=abcdesc" to "&n=abcasc"), - arrayOf("manga/new&n=dateasc" to "manga/new", "manga/new&n=favasc" to "mostfavorites&sort=manga", "manga/new&n=abcdesc" to "manga/new&n=abcasc") + arrayOf( + "manga/new&n=dateasc" to "manga/new", + "manga/new&n=favasc" to "mostfavorites&sort=manga", + "manga/new&n=abcdesc" to "manga/new&n=abcasc" + ) ) - private open class UriPartFilter(displayName: String, sortNames: Array, val withGenres: Array>, val withoutGenres: Array>) : + private open class UriPartFilter( + displayName: String, + sortNames: Array, + val withGenres: Array>, + val withoutGenres: Array> + ) : Filter.Sort(displayName, sortNames, Selection(1, false)) { - fun toUriPartWithGenres() = if (state!!.ascending) withGenres[state!!.index].first else withGenres[state!!.index].second - fun toUriPartWithoutGenres() = if (state!!.ascending) withoutGenres[state!!.index].first else withoutGenres[state!!.index].second + fun toUriPartWithGenres() = + if (state!!.ascending) withGenres[state!!.index].first else withGenres[state!!.index].second + + fun toUriPartWithoutGenres() = + if (state!!.ascending) withoutGenres[state!!.index].first else withoutGenres[state!!.index].second } override fun getFilterList() = FilterList( @@ -415,4 +459,29 @@ class HenChan : MultiChan("HenChan", "https://hchan.live", "ru") { Genre("яндере"), Genre("яой") ) + + override fun setupPreferenceScreen(screen: androidx.preference.PreferenceScreen) { + EditTextPreference(screen.context).apply { + key = DOMAIN_TITLE + this.title = DOMAIN_TITLE + summary = domain + this.setDefaultValue(DOMAIN_DEFAULT) + dialogTitle = DOMAIN_TITLE + setOnPreferenceChangeListener { _, newValue -> + try { + val res = preferences.edit().putString(DOMAIN_TITLE, newValue as String).commit() + Toast.makeText(screen.context, "Для смены домена необходимо перезапустить приложение с полной остановкой.", Toast.LENGTH_LONG).show() + res + } catch (e: Exception) { + e.printStackTrace() + false + } + } + }.let(screen::addPreference) + } + + companion object { + private const val DOMAIN_TITLE = "Домен" + private const val DOMAIN_DEFAULT = "http://y.hchan.live" + } } diff --git a/multisrc/src/main/java/eu/kanade/tachiyomi/multisrc/multichan/ChanGenerator.kt b/multisrc/src/main/java/eu/kanade/tachiyomi/multisrc/multichan/ChanGenerator.kt index 40387647b..856430e2d 100644 --- a/multisrc/src/main/java/eu/kanade/tachiyomi/multisrc/multichan/ChanGenerator.kt +++ b/multisrc/src/main/java/eu/kanade/tachiyomi/multisrc/multichan/ChanGenerator.kt @@ -13,7 +13,7 @@ class ChanGenerator : ThemeSourceGenerator { override val sources = listOf( SingleLang("MangaChan", "https://manga-chan.me", "ru", overrideVersionCode = 14), - SingleLang("HenChan", "https://hchan.live", "ru", isNsfw = true, overrideVersionCode = 36), + SingleLang("HenChan", "http://y.hchan.live", "ru", isNsfw = true, overrideVersionCode = 37), SingleLang("YaoiChan", "https://yaoi-chan.me", "ru", isNsfw = true, overrideVersionCode = 4) )