NewToki/ManaToki: migrate preferences (#13560)

* NewToki/ManaToki: migrate preferences

* migrate on start
This commit is contained in:
stevenyomi 2022-09-24 20:17:03 +08:00 committed by GitHub
parent d571a309f5
commit a5187d9148
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 28 additions and 22 deletions

View File

@ -5,7 +5,7 @@ ext {
extName = 'NewToki / ManaToki' extName = 'NewToki / ManaToki'
pkgNameSuffix = 'ko.newtoki' pkgNameSuffix = 'ko.newtoki'
extClass = '.TokiFactory' extClass = '.TokiFactory'
extVersionCode = 24 extVersionCode = 25
isNsfw = true isNsfw = true
} }

View File

@ -12,14 +12,12 @@ import org.jsoup.nodes.Element
* ManaToki is too big to support in a Factory File., So split into separate file. * ManaToki is too big to support in a Factory File., So split into separate file.
*/ */
object ManaToki : NewToki("ManaToki", "comic") { object ManaToki : NewToki("ManaToki", "comic", manaTokiPreferences) {
// / ! DO NOT CHANGE THIS ! Only the site name changed from newtoki. // / ! DO NOT CHANGE THIS ! Only the site name changed from newtoki.
override val id = MANATOKI_ID override val id = MANATOKI_ID
override val baseUrl get() = "https://$MANATOKI_PREFIX$domainNumber.net" override val baseUrl get() = "https://$MANATOKI_PREFIX$domainNumber.net"
override val preferences = manaTokiPreferences
private val chapterRegex by lazy { Regex(""" [ \d,~.-]+화$""") } private val chapterRegex by lazy { Regex(""" [ \d,~.-]+화$""") }
fun latestUpdatesElementParse(element: Element): SManga { fun latestUpdatesElementParse(element: Element): SManga {

View File

@ -14,7 +14,6 @@ import eu.kanade.tachiyomi.source.model.SManga
import eu.kanade.tachiyomi.source.online.ParsedHttpSource import eu.kanade.tachiyomi.source.online.ParsedHttpSource
import eu.kanade.tachiyomi.util.asJsoup import eu.kanade.tachiyomi.util.asJsoup
import okhttp3.HttpUrl.Companion.toHttpUrl import okhttp3.HttpUrl.Companion.toHttpUrl
import okhttp3.OkHttpClient
import okhttp3.Response import okhttp3.Response
import org.jsoup.Jsoup import org.jsoup.Jsoup
import org.jsoup.nodes.Document import org.jsoup.nodes.Document
@ -30,18 +29,17 @@ import java.util.concurrent.TimeUnit
* *
* Based on https://github.com/gnuboard/gnuboard5 * Based on https://github.com/gnuboard/gnuboard5
**/ **/
abstract class NewToki(override val name: String, private val boardName: String) : ConfigurableSource, ParsedHttpSource() { abstract class NewToki(
override val name: String,
private val boardName: String,
private val preferences: SharedPreferences,
) : ParsedHttpSource(), ConfigurableSource {
override val lang: String = "ko" override val lang: String = "ko"
override val supportsLatest = true override val supportsLatest = true
override val client: OkHttpClient by lazy { override val client by lazy { buildClient(withRateLimit = false) }
buildClient(withRateLimit = false) private val rateLimitedClient by lazy { buildClient(withRateLimit = true) }
}
protected val rateLimitedClient: OkHttpClient by lazy {
buildClient(withRateLimit = true)
}
private fun buildClient(withRateLimit: Boolean) = private fun buildClient(withRateLimit: Boolean) =
network.cloudflareClient.newBuilder() network.cloudflareClient.newBuilder()
@ -260,10 +258,6 @@ abstract class NewToki(override val name: String, private val boardName: String)
// We are able to get the image URL directly from the page list // We are able to get the image URL directly from the page list
override fun imageUrlParse(document: Document) = throw UnsupportedOperationException("This method should not be called!") override fun imageUrlParse(document: Document) = throw UnsupportedOperationException("This method should not be called!")
override fun getFilterList() = FilterList()
abstract val preferences: SharedPreferences
override fun setupPreferenceScreen(screen: androidx.preference.PreferenceScreen) { override fun setupPreferenceScreen(screen: androidx.preference.PreferenceScreen) {
getPreferencesInternal(screen.context).map(screen::addPreference) getPreferencesInternal(screen.context).map(screen::addPreference)
} }

View File

@ -6,14 +6,12 @@ import eu.kanade.tachiyomi.source.model.FilterList
import okhttp3.HttpUrl.Companion.toHttpUrl import okhttp3.HttpUrl.Companion.toHttpUrl
import okhttp3.Request import okhttp3.Request
object NewTokiWebtoon : NewToki("NewToki", "webtoon") { object NewTokiWebtoon : NewToki("NewToki", "webtoon", newTokiPreferences) {
// / ! DO NOT CHANGE THIS ! Prevent to treating as a new site // / ! DO NOT CHANGE THIS ! Prevent to treating as a new site
override val id = NEWTOKI_ID override val id = NEWTOKI_ID
override val baseUrl get() = "https://$NEWTOKI_PREFIX$domainNumber.com" override val baseUrl get() = "https://$NEWTOKI_PREFIX$domainNumber.com"
override val preferences = newTokiPreferences
override fun searchMangaRequest(page: Int, query: String, filters: FilterList): Request { override fun searchMangaRequest(page: Int, query: String, filters: FilterList): Request {
val url = ("$baseUrl/webtoon" + (if (page > 1) "/p$page" else "")).toHttpUrl().newBuilder() val url = ("$baseUrl/webtoon" + (if (page > 1) "/p$page" else "")).toHttpUrl().newBuilder()
filters.forEach { filter -> filters.forEach { filter ->

View File

@ -14,8 +14,8 @@ const val NEWTOKI_ID = 1977818283770282459L // "NewToki (Webtoon)/ko/1"
const val MANATOKI_PREFIX = "manatoki" const val MANATOKI_PREFIX = "manatoki"
const val NEWTOKI_PREFIX = "newtoki" const val NEWTOKI_PREFIX = "newtoki"
val manaTokiPreferences = getSharedPreferences(MANATOKI_ID) val manaTokiPreferences = getSharedPreferences(MANATOKI_ID).migrate()
val newTokiPreferences = getSharedPreferences(NEWTOKI_ID) val newTokiPreferences = getSharedPreferences(NEWTOKI_ID).migrate()
fun getPreferencesInternal(context: Context) = arrayOf( fun getPreferencesInternal(context: Context) = arrayOf(
@ -54,6 +54,22 @@ var SharedPreferences.domainNumber: String
val SharedPreferences.rateLimitPeriod: Int val SharedPreferences.rateLimitPeriod: Int
get() = getString(RATE_LIMIT_PERIOD_PREF, RATE_LIMIT_PERIOD_DEFAULT)!!.toInt().coerceIn(1, RATE_LIMIT_PERIOD_MAX) get() = getString(RATE_LIMIT_PERIOD_PREF, RATE_LIMIT_PERIOD_DEFAULT)!!.toInt().coerceIn(1, RATE_LIMIT_PERIOD_MAX)
private fun SharedPreferences.migrate(): SharedPreferences {
if ("Override BaseUrl" !in this) return this // already migrated
val editor = edit().clear() // clear all legacy preferences listed below
val oldValue = try { // this was a long
getLong(RATE_LIMIT_PERIOD_PREF, -1).toInt()
} catch (_: ClassCastException) {
-1
}
if (oldValue != -1) { // convert to string
val newValue = oldValue.coerceIn(1, RATE_LIMIT_PERIOD_MAX)
editor.putString(RATE_LIMIT_PERIOD_PREF, newValue.toString())
}
editor.apply()
return this
}
/** /**
* Don't use the following legacy keys: * Don't use the following legacy keys:
* - "Override BaseUrl" * - "Override BaseUrl"