LANraragi: Switch to SourceFactory (#11436)

- Maintain current ID for first instance
- Refactor preferences
- Consolidate constants
- Implementations to compile only
This commit is contained in:
RePod 2022-04-12 14:38:33 -04:00 committed by GitHub
parent cbb9470b3b
commit c89949e63f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 79 additions and 75 deletions

View File

@ -5,13 +5,12 @@ apply plugin: 'kotlinx-serialization'
ext { ext {
extName = 'LANraragi' extName = 'LANraragi'
pkgNameSuffix = 'all.lanraragi' pkgNameSuffix = 'all.lanraragi'
extClass = '.LANraragi' extClass = '.LANraragiFactory'
extVersionCode = 10 extVersionCode = 11
} }
dependencies { dependencies {
implementation 'io.reactivex:rxandroid:1.2.1' compileOnly libs.bundles.reactivex
implementation 'io.reactivex:rxjava:1.3.8'
} }
apply from: "$rootDir/common.gradle" apply from: "$rootDir/common.gradle"

View File

@ -3,7 +3,9 @@ package eu.kanade.tachiyomi.extension.all.lanraragi
import android.app.Application import android.app.Application
import android.content.SharedPreferences import android.content.SharedPreferences
import android.net.Uri import android.net.Uri
import android.text.InputType
import android.util.Base64 import android.util.Base64
import android.widget.Toast
import eu.kanade.tachiyomi.network.GET import eu.kanade.tachiyomi.network.GET
import eu.kanade.tachiyomi.network.asObservableSuccess import eu.kanade.tachiyomi.network.asObservableSuccess
import eu.kanade.tachiyomi.source.ConfigurableSource import eu.kanade.tachiyomi.source.ConfigurableSource
@ -31,22 +33,20 @@ import rx.schedulers.Schedulers
import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get import uy.kohesive.injekt.api.get
import java.io.IOException import java.io.IOException
import java.security.MessageDigest
class LANraragi : ConfigurableSource, HttpSource() { open class LANraragi(private val suffix: String = "") : ConfigurableSource, HttpSource() {
override val baseUrl: String override val baseUrl by lazy { getPrefBaseUrl() }
get() = preferences.getString("hostname", "http://127.0.0.1:3000")!!
override val lang = "all" override val lang = "all"
override val name = "LANraragi" override val name by lazy { "LANraragi (${getPrefCustomLabel()})" }
override val supportsLatest = true override val supportsLatest = true
private val apiKey: String private val apiKey by lazy { getPrefAPIKey() }
get() = preferences.getString("apiKey", "")!!
private val latestNamespacePref: String private val latestNamespacePref by lazy { getPrefLatestNS() }
get() = preferences.getString("latestNamespacePref", DEFAULT_SORT_BY_NS)!!
private val json by lazy { Injekt.get<Json>() } private val json by lazy { Injekt.get<Json>() }
@ -141,7 +141,7 @@ class LANraragi : ConfigurableSource, HttpSource() {
override fun latestUpdatesRequest(page: Int): Request { override fun latestUpdatesRequest(page: Int): Request {
val filters = mutableListOf<Filter<*>>() val filters = mutableListOf<Filter<*>>()
val prefNewOnly = preferences.getBoolean("latestNewOnly", false) val prefNewOnly = preferences.getBoolean(NEW_ONLY_KEY, false)
if (prefNewOnly) filters.add(NewArchivesOnly(true)) if (prefNewOnly) filters.add(NewArchivesOnly(true))
@ -263,86 +263,72 @@ class LANraragi : ConfigurableSource, HttpSource() {
private var categories = emptyList<Category>() private var categories = emptyList<Category>()
// Preferences // Preferences
override val id by lazy {
// Retain previous ID for first entry
val key = "lanraragi" + (if (suffix == "1") "" else "_$suffix") + "/all/$versionId"
val bytes = MessageDigest.getInstance("MD5").digest(key.toByteArray())
(0..7).map { bytes[it].toLong() and 0xff shl 8 * (7 - it) }.reduce(Long::or) and Long.MAX_VALUE
}
private val preferences: SharedPreferences by lazy { private val preferences: SharedPreferences by lazy {
Injekt.get<Application>().getSharedPreferences("source_$id", 0x0000) Injekt.get<Application>().getSharedPreferences("source_$id", 0x0000)
} }
private fun getPrefBaseUrl(): String = preferences.getString(HOSTNAME_KEY, HOSTNAME_DEFAULT)!!
private fun getPrefAPIKey(): String = preferences.getString(APIKEY_KEY, "")!!
private fun getPrefLatestNS(): String = preferences.getString(SORT_BY_NS_KEY, SORT_BY_NS_DEFAULT)!!
private fun getPrefCustomLabel(): String = preferences.getString(CUSTOM_LABEL_KEY, suffix)!!.ifBlank { suffix }
override fun setupPreferenceScreen(screen: androidx.preference.PreferenceScreen) { override fun setupPreferenceScreen(screen: androidx.preference.PreferenceScreen) {
val hostnamePref = androidx.preference.EditTextPreference(screen.context).apply {
key = "Hostname"
title = "Hostname"
text = baseUrl
summary = baseUrl
dialogTitle = "Hostname"
setOnPreferenceChangeListener { _, newValue ->
var hostname = newValue as String
if (!hostname.startsWith("http://") && !hostname.startsWith("https://")) {
hostname = "http://$hostname"
}
this.apply {
text = hostname
summary = hostname
}
preferences.edit().putString("hostname", hostname).commit()
}
}
val apiKeyPref = androidx.preference.EditTextPreference(screen.context).apply {
key = "API Key"
title = "API Key"
text = apiKey
summary = "Required if No-Fun Mode is enabled."
dialogTitle = "API Key"
setOnPreferenceChangeListener { _, newValue ->
val apiKey = newValue as String
this.apply {
text = apiKey
summary = "Required if No-Fun Mode is enabled."
}
preferences.edit().putString("apiKey", newValue).commit()
}
}
val latestNewOnlyPref = androidx.preference.CheckBoxPreference(screen.context).apply { val latestNewOnlyPref = androidx.preference.CheckBoxPreference(screen.context).apply {
key = "latestNewOnly" key = NEW_ONLY_KEY
title = "Latest - New Only" title = "Latest - New Only"
setDefaultValue(true) setDefaultValue(true)
setOnPreferenceChangeListener { _, newValue -> setOnPreferenceChangeListener { _, newValue ->
val checkValue = newValue as Boolean preferences.edit().putBoolean(this.key, newValue as Boolean).commit()
preferences.edit().putBoolean("latestNewOnly", checkValue).commit()
} }
} }
val latestNamespacePref = androidx.preference.EditTextPreference(screen.context).apply { screen.addPreference(screen.editTextPreference(HOSTNAME_KEY, "Hostname", HOSTNAME_DEFAULT, baseUrl, refreshSummary = true))
key = "latestNamespacePref" screen.addPreference(screen.editTextPreference(APIKEY_KEY, "API Key", "", "Required if No-Fun Mode is enabled.", true))
title = "Latest - Sort by Namespace" screen.addPreference(screen.editTextPreference(CUSTOM_LABEL_KEY, "Custom Label", "", "Show the given label for the source instead of the default."))
text = latestNamespacePref screen.addPreference(latestNewOnlyPref)
summary = "Sort by the given namespace for Latest, such as date_added." screen.addPreference(screen.editTextPreference(SORT_BY_NS_KEY, "Latest - Sort by Namespace", SORT_BY_NS_DEFAULT, "Sort by the given namespace for Latest, such as date_added."))
dialogTitle = "Latest - Sort by Namespace" }
setDefaultValue(DEFAULT_SORT_BY_NS)
private fun androidx.preference.PreferenceScreen.editTextPreference(key: String, title: String, default: String, summary: String, isPassword: Boolean = false, refreshSummary: Boolean = false): androidx.preference.EditTextPreference {
return androidx.preference.EditTextPreference(context).apply {
this.key = key
this.title = title
this.summary = summary
this.setDefaultValue(default)
if (isPassword) {
setOnBindEditTextListener {
it.inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD
}
}
setOnPreferenceChangeListener { _, newValue -> setOnPreferenceChangeListener { _, newValue ->
val latestNamespacePref = newValue as String try {
val newString = newValue.toString()
val res = preferences.edit().putString(this.key, newString).commit()
if (refreshSummary) {
this.apply { this.apply {
text = latestNamespacePref this.summary = newValue as String
}
preferences.edit().putString("latestNamespacePref", newValue).commit()
} }
} }
screen.addPreference(hostnamePref) Toast.makeText(context, "Restart Tachiyomi to apply new setting.", Toast.LENGTH_LONG).show()
screen.addPreference(apiKeyPref) res
screen.addPreference(latestNewOnlyPref) } catch (e: Exception) {
screen.addPreference(latestNamespacePref) e.printStackTrace()
false
}
}
}
} }
// Helper // Helper
@ -464,6 +450,12 @@ class LANraragi : ConfigurableSource, HttpSource() {
} }
companion object { companion object {
private const val DEFAULT_SORT_BY_NS = "date_added" private const val HOSTNAME_DEFAULT = "http://127.0.0.1:3000"
private const val HOSTNAME_KEY = "hostname"
private const val APIKEY_KEY = "apiKey"
private const val CUSTOM_LABEL_KEY = "customLabel"
private const val NEW_ONLY_KEY = "latestNewOnly"
private const val SORT_BY_NS_DEFAULT = "date_added"
private const val SORT_BY_NS_KEY = "latestNamespacePref"
} }
} }

View File

@ -0,0 +1,13 @@
package eu.kanade.tachiyomi.extension.all.lanraragi
import eu.kanade.tachiyomi.source.Source
import eu.kanade.tachiyomi.source.SourceFactory
class LANraragiFactory : SourceFactory {
override fun createSources(): List<Source> =
listOf(
LANraragi("1"),
LANraragi("2"),
LANraragi("3")
)
}