Koharu: Add mirror selection to preference page (#11588)

* Add mirror pref

* Koharu: Add more mirror sites

* Koharu: Raised extVersionCode

* Koharu: Implement index fallback

* Koharu: Remove preferences migration
This commit is contained in:
anewadventure 2025-11-22 05:24:43 +07:00 committed by Draff
parent fdc8e29671
commit 948ff10002
Signed by: Draff
GPG Key ID: E8A89F3211677653
2 changed files with 37 additions and 4 deletions

View File

@ -1,7 +1,7 @@
ext {
extName = 'SchaleNetwork'
extClass = '.KoharuFactory'
extVersionCode = 15
extVersionCode = 16
isNsfw = true
}

View File

@ -7,6 +7,7 @@ import android.os.Handler
import android.os.Looper
import android.webkit.WebView
import android.webkit.WebViewClient
import android.widget.Toast
import androidx.preference.EditTextPreference
import androidx.preference.ListPreference
import androidx.preference.PreferenceScreen
@ -57,9 +58,19 @@ class Koharu(
private val searchLang: String = "",
) : HttpSource(), ConfigurableSource {
private val preferences: SharedPreferences by getPreferencesLazy()
override val name = "SchaleNetwork"
override val baseUrl = "https://schale.network"
override val baseUrl: String
get() {
val preferenceValue = preferences.getString(PREF_MIRROR, MIRROR_PREF_DEFAULT) ?: MIRROR_PREF_DEFAULT
val mirror = preferenceValue.toIntOrNull()?.let { index ->
mirrors[index.coerceAtMost(mirrors.lastIndex)]
} ?: preferenceValue.takeIf { it in mirrors } ?: MIRROR_PREF_DEFAULT
return "https://$mirror"
}
override val id = if (lang == "en") 1484902275639232927 else super.id
@ -74,8 +85,6 @@ class Koharu(
private val shortenTitleRegex = Regex("""(\[[^]]*]|[({][^)}]*[)}])""")
private fun String.shortenTitle() = replace(shortenTitleRegex, "").trim()
private val preferences: SharedPreferences by getPreferencesLazy()
private fun quality() = preferences.getString(PREF_IMAGERES, "1280")!!
private fun remadd() = preferences.getBoolean(PREF_REM_ADD, false)
@ -457,6 +466,20 @@ class Koharu(
// Settings
override fun setupPreferenceScreen(screen: PreferenceScreen) {
ListPreference(screen.context).apply {
key = PREF_MIRROR
title = "Preferred Mirror"
entries = mirrors
entryValues = mirrors
setDefaultValue(MIRROR_PREF_DEFAULT)
summary = "%s"
setOnPreferenceChangeListener { _, _ ->
Toast.makeText(screen.context, "Restart the app to apply changes", Toast.LENGTH_LONG).show()
true
}
}.also(screen::addPreference)
ListPreference(screen.context).apply {
key = PREF_IMAGERES
title = "Image Resolution"
@ -488,6 +511,16 @@ class Koharu(
companion object {
const val PREFIX_ID_KEY_SEARCH = "id:"
private const val PREF_MIRROR = "pref_mirror"
private const val MIRROR_PREF_DEFAULT = "schale.network"
private val mirrors = arrayOf(
MIRROR_PREF_DEFAULT,
"anchira.to",
"gehenna.jp",
"niyaniya.moe",
"shupogaki.moe",
"hdoujin.net",
)
private const val PREF_IMAGERES = "pref_image_quality"
private const val PREF_REM_ADD = "pref_remove_additional"
private const val PREF_EXCLUDE_TAGS = "pref_exclude_tags"