Skip to content

Commit

Permalink
feat: Implement quality preference
Browse files Browse the repository at this point in the history
  • Loading branch information
Claudemirovsky committed Oct 9, 2023
1 parent 7f49861 commit cf81016
Showing 1 changed file with 44 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package eu.kanade.tachiyomi.animeextension.tr.hdfilmcehennemi

import android.app.Application
import androidx.preference.ListPreference
import androidx.preference.PreferenceScreen
import eu.kanade.tachiyomi.animeextension.tr.hdfilmcehennemi.extractors.RapidrameExtractor
import eu.kanade.tachiyomi.animeextension.tr.hdfilmcehennemi.extractors.VidmolyExtractor
import eu.kanade.tachiyomi.animesource.ConfigurableAnimeSource
import eu.kanade.tachiyomi.animesource.model.AnimeFilterList
import eu.kanade.tachiyomi.animesource.model.AnimesPage
import eu.kanade.tachiyomi.animesource.model.SAnime
Expand All @@ -25,9 +29,11 @@ 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 uy.kohesive.injekt.injectLazy

class HDFilmCehennemi : ParsedAnimeHttpSource() {
class HDFilmCehennemi : ConfigurableAnimeSource, ParsedAnimeHttpSource() {

override val name = "HDFilmCehennemi"

Expand All @@ -45,6 +51,10 @@ class HDFilmCehennemi : ParsedAnimeHttpSource() {

private val json: Json by injectLazy()

private val preferences by lazy {
Injekt.get<Application>().getSharedPreferences("source_$id", 0x0000)
}

// ============================== Popular ===============================
override fun popularAnimeRequest(page: Int) = GET("$baseUrl/en-cok-begenilen-filmleri-izle/page/$page/")

Expand Down Expand Up @@ -198,6 +208,25 @@ class HDFilmCehennemi : ParsedAnimeHttpSource() {
throw UnsupportedOperationException("Not used.")
}

// ============================== Settings ==============================
override fun setupPreferenceScreen(screen: PreferenceScreen) {
ListPreference(screen.context).apply {
key = PREF_QUALITY_KEY
title = PREF_QUALITY_TITLE
entries = PREF_QUALITY_ENTRIES
entryValues = PREF_QUALITY_VALUES
setDefaultValue(PREF_QUALITY_DEFAULT)
summary = "%s"

setOnPreferenceChangeListener { _, newValue ->
val selected = newValue as String
val index = findIndexOfValue(selected)
val entry = entryValues[index] as String
preferences.edit().putString(key, entry).commit()
}
}.also(screen::addPreference)
}

// ============================= Utilities ==============================
private inline fun <reified T> Response.parseAs(): T {
return use { it.body.string() }.let(json::decodeFromString)
Expand All @@ -208,7 +237,21 @@ class HDFilmCehennemi : ParsedAnimeHttpSource() {
map { async(Dispatchers.Default) { f(it) } }.awaitAll()
}

override fun List<Video>.sort(): List<Video> {
val quality = preferences.getString(PREF_QUALITY_KEY, PREF_QUALITY_DEFAULT)!!

return sortedWith(
compareBy { it.quality.contains(quality) },
).reversed()
}

companion object {
const val PREFIX_SEARCH = "id:"

private const val PREF_QUALITY_KEY = "pref_quality_key"
private const val PREF_QUALITY_TITLE = "Preferred quality"
private const val PREF_QUALITY_DEFAULT = "720p"
private val PREF_QUALITY_ENTRIES = arrayOf("360p", "480p", "720p", "1080p")
private val PREF_QUALITY_VALUES = PREF_QUALITY_ENTRIES
}
}

0 comments on commit cf81016

Please sign in to comment.