Skip to content

Commit

Permalink
refactor: Fix more codefactor issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Claudemirovsky committed Oct 25, 2023
1 parent bbd08fe commit 9bb31fc
Show file tree
Hide file tree
Showing 10 changed files with 144 additions and 200 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ object JsUnpacker {
val count = args[3].toInt()
val origList = args[4].split("|")

val replaceMap = (0..(count - 1)).map {
val replaceMap = (0..count - 1).map {
val key = convert(base, it)
key to try { origList[it] } catch (e: Exception) { key }
}.toMap()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -663,60 +663,59 @@ class Jellyfin(private val suffix: String) : ConfigurableAnimeSource, AnimeHttpS
}

private fun medialibPreference(screen: PreferenceScreen) =
(
object : MediaLibPreference(screen.context) {
override fun reload() {
this.apply {
key = JFConstants.MEDIALIB_KEY
title = JFConstants.MEDIALIB_TITLE
summary = "%s"

Thread {
try {
val mediaLibsResponse = client.newCall(
GET("$baseUrl/Users/$userId/Items?api_key=$apiKey"),
).execute()
val mediaJson = mediaLibsResponse.body.let { json.decodeFromString<ItemsResponse>(it.string()) }?.Items

val entriesArray = mutableListOf<String>()
val entriesValueArray = mutableListOf<String>()

if (mediaJson != null) {
for (media in mediaJson) {
entriesArray.add(media.Name)
entriesValueArray.add(media.Id)
}
object : MediaLibPreference(screen.context) {
override fun reload() {
this.apply {
key = JFConstants.MEDIALIB_KEY
title = JFConstants.MEDIALIB_TITLE
summary = "%s"

Thread {
try {
val mediaLibsResponse = client.newCall(
GET("$baseUrl/Users/$userId/Items?api_key=$apiKey"),
).execute()
val mediaJson = mediaLibsResponse.body.let { json.decodeFromString<ItemsResponse>(it.string()) }?.Items

val entriesArray = mutableListOf<String>()
val entriesValueArray = mutableListOf<String>()

if (mediaJson != null) {
for (media in mediaJson) {
entriesArray.add(media.Name)
entriesValueArray.add(media.Id)
}

entries = entriesArray.toTypedArray()
entryValues = entriesValueArray.toTypedArray()
} catch (ex: Exception) {
entries = emptyArray()
entryValues = emptyArray()
}
}.start()

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

entries = entriesArray.toTypedArray()
entryValues = entriesValueArray.toTypedArray()
} catch (ex: Exception) {
entries = emptyArray()
entryValues = emptyArray()
}
}.start()

setOnPreferenceChangeListener { _, newValue ->
val selected = newValue as String
val index = findIndexOfValue(selected)
val entry = entryValues[index] as String
parentId = entry
preferences.edit().putString(key, entry).commit()
}
}
}
).apply { reload() }
}.apply { reload() }

private fun getSummary(isPassword: Boolean, value: String, placeholder: String) = when {
isPassword && value.isNotEmpty() || !isPassword && value.isEmpty() -> placeholder
else -> value
}

private fun PreferenceScreen.editTextPreference(key: String, title: String, default: String, value: String, isPassword: Boolean = false, placeholder: String, mediaLibPref: MediaLibPreference): EditTextPreference {
return EditTextPreference(context).apply {
this.key = key
this.title = title
summary = if ((isPassword && value.isNotEmpty()) || (!isPassword && value.isEmpty())) {
placeholder
} else {
value
}
summary = getSummary(isPassword, value, placeholder)
this.setDefaultValue(default)
dialogTitle = title

Expand All @@ -732,11 +731,7 @@ class Jellyfin(private val suffix: String) : ConfigurableAnimeSource, AnimeHttpS
try {
val newValueString = newValue as String
val res = preferences.edit().putString(key, newValueString).commit()
summary = if ((isPassword && newValueString.isNotEmpty()) || (!isPassword && newValueString.isEmpty())) {
placeholder
} else {
newValueString
}
summary = getSummary(isPassword, newValueString, placeholder)
val loginRes = login(true, context)
if (loginRes == true) {
mediaLibPref.reload()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ class Yomiroll : ConfigurableAnimeSource, AnimeHttpSource() {
}
) +
(
if ((series_metadata?.audio_locales?.size ?: 0) > 1 ||
if (series_metadata?.audio_locales?.size ?: 0 > 1 ||
movie_metadata?.is_dubbed == true
) {
" Dub"
Expand Down Expand Up @@ -501,33 +501,31 @@ class Yomiroll : ConfigurableAnimeSource, AnimeHttpSource() {
}

private fun localSubsPreference(screen: PreferenceScreen) =
(
object : LocalSubsPreference(screen.context) {
override fun reload() {
this.apply {
key = PREF_USE_LOCAL_TOKEN_KEY
title = PREF_USE_LOCAL_TOKEN_TITLE
summary = runBlocking {
withContext(Dispatchers.IO) { getTokenDetail() }
}
setDefaultValue(false)
setOnPreferenceChangeListener { _, newValue ->
val new = newValue as Boolean
preferences.edit().putBoolean(key, new).commit().also {
Thread {
summary = runBlocking {
withContext(Dispatchers.IO) { getTokenDetail(true) }
}
}.start()
}
object : LocalSubsPreference(screen.context) {
override fun reload() {
this.apply {
key = PREF_USE_LOCAL_TOKEN_KEY
title = PREF_USE_LOCAL_TOKEN_TITLE
summary = runBlocking {
withContext(Dispatchers.IO) { getTokenDetail() }
}
setDefaultValue(false)
setOnPreferenceChangeListener { _, newValue ->
val new = newValue as Boolean
preferences.edit().putBoolean(key, new).commit().also {
Thread {
summary = runBlocking {
withContext(Dispatchers.IO) { getTokenDetail(true) }
}
}.start()
}
}
}
}
).apply { reload() }
}.apply { reload() }

// From Dopebox
private fun <A, B> Iterable<A>.parallelMap(f: suspend (A) -> B): List<B> =
private inline fun <A, B> Iterable<A>.parallelMap(crossinline f: suspend (A) -> B): List<B> =
runBlocking {
map { async(Dispatchers.Default) { f(it) } }.awaitAll()
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ class Okanime : ConfigurableAnimeSource, ParsedAnimeHttpSource() {
private fun extractVideosFromUrl(url: String, quality: String, selection: Set<String>): List<Video> {
return runCatching {
when {
("https://doo" in url && "/e/" in url) && selection.contains("Dood") -> {
"https://doo" in url && "/e/" in url && selection.contains("Dood") -> {
doodExtractor.videoFromUrl(url, "DoodStream - $quality")
?.let(::listOf)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class MoflixStream : ConfigurableAnimeSource, AnimeHttpSource() {
val pagination = response.parseAs<PopularPaginationDto>().pagination

val animeList = pagination.data.parseItems()
val hasNextPage = pagination.current_page < (pagination.next_page ?: 1)
val hasNextPage = pagination.current_page < pagination.next_page ?: 1
return AnimesPage(animeList, hasNextPage)
}

Expand Down

This file was deleted.

Loading

0 comments on commit 9bb31fc

Please sign in to comment.