From 32eb20ce7a1f8e61657e95c83bf6f2cef1f38314 Mon Sep 17 00:00:00 2001 From: Claudemirovsky <63046606+Claudemirovsky@users.noreply.github.com> Date: Wed, 21 Feb 2024 06:17:48 -0300 Subject: [PATCH] chore: Reduce a bit of overhead (#11) * chore(json-extensions): Remove unnecessary use {} blocks * chore(coroutines-extensions): Replace runCatching with try-catch * fix(ConfigurableAnimeSource): Remove crash-inducing exposed function --- .../animesource/ConfigurableAnimeSource.kt | 29 ++++--------------- .../tachiyomi/util/CoroutinesExtensions.kt | 9 ++++-- .../kanade/tachiyomi/util/JsonExtensions.kt | 10 +++---- 3 files changed, 16 insertions(+), 32 deletions(-) diff --git a/library/src/main/java/eu/kanade/tachiyomi/animesource/ConfigurableAnimeSource.kt b/library/src/main/java/eu/kanade/tachiyomi/animesource/ConfigurableAnimeSource.kt index ff030e6..634aa79 100644 --- a/library/src/main/java/eu/kanade/tachiyomi/animesource/ConfigurableAnimeSource.kt +++ b/library/src/main/java/eu/kanade/tachiyomi/animesource/ConfigurableAnimeSource.kt @@ -1,49 +1,32 @@ package eu.kanade.tachiyomi.animesource -import android.content.SharedPreferences import androidx.preference.PreferenceScreen /** * A interface to add user preferences to the source. */ interface ConfigurableAnimeSource { - - /** - * Gets instance of [SharedPreferences] scoped to the specific source. - * - * @since extensions-lib 14 - */ - fun getSourcePreferences(): SharedPreferences = throw Exception("stub!") - /** * Implementations must override this method to add the user preferences. - * + * * You can use some stubbed inheritors of [androidx.preference.Preference] here. - * + * * **Common usage example:** * ``` * // ============================== Settings ============================== * override fun setupPreferenceScreen(screen: PreferenceScreen) { - * val videoQualityPref = ListPreference(screen.context).apply { + * ListPreference(screen.context).apply { * key = PREF_QUALITY_KEY // String, like "pref_quality" - * title = PREF_QUALITY_TITLE // String, like "Preferred quality:" + * title = PREF_QUALITY_TITLE // String, like "Preferred quality:" * entries = PREF_QUALITY_ENTRIES // Array, like arrayOf("240p", "720p"...) * // Another Array. Can be different from the property above, as long it have the same size * // and equivalent values per index. - * entryValues = PREF_QUALITY_VALUES + * 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() - * } - * } - * screen.addPreference(videoQualityPref) + * }.also(screen::addPreference) * } * ``` */ fun setupPreferenceScreen(screen: PreferenceScreen) - } diff --git a/library/src/main/java/eu/kanade/tachiyomi/util/CoroutinesExtensions.kt b/library/src/main/java/eu/kanade/tachiyomi/util/CoroutinesExtensions.kt index 3dc12e5..7b8fdfa 100644 --- a/library/src/main/java/eu/kanade/tachiyomi/util/CoroutinesExtensions.kt +++ b/library/src/main/java/eu/kanade/tachiyomi/util/CoroutinesExtensions.kt @@ -62,7 +62,7 @@ inline fun Iterable.parallelFlatMapBlocking(crossinline f: suspend (A) /** * Parallel implementation of [Iterable.flatMap], but running - * the transformation function inside a [runCatching] block. + * the transformation function inside a try-catch block. * * @since extensions-lib 14 */ @@ -70,14 +70,17 @@ suspend inline fun Iterable.parallelCatchingFlatMap(crossinline f: sus withContext(Dispatchers.IO) { map { async { - runCatching { f(it) }.onFailure(Throwable::printStackTrace).getOrElse { emptyList() } + try { f(it) } catch (e: Throwable) { + e.printStackTrace() + emptyList() + } } }.awaitAll().flatten() } /** * Thread-blocking parallel implementation of [Iterable.flatMap], but running - * the transformation function inside a [runCatching] block. + * the transformation function inside a try-catch block. * * @since extensions-lib 14 */ diff --git a/library/src/main/java/eu/kanade/tachiyomi/util/JsonExtensions.kt b/library/src/main/java/eu/kanade/tachiyomi/util/JsonExtensions.kt index fe2af3a..fc7aa82 100644 --- a/library/src/main/java/eu/kanade/tachiyomi/util/JsonExtensions.kt +++ b/library/src/main/java/eu/kanade/tachiyomi/util/JsonExtensions.kt @@ -16,7 +16,7 @@ import uy.kohesive.injekt.api.get * @since extensions-lib 14 */ inline fun Response.parseAs(transform: (String) -> String): T { - val responseBody = use { transform(it.body.string()) } + val responseBody = transform(body.string()) return Injekt.get().decodeFromString(responseBody) } @@ -30,10 +30,8 @@ inline fun Response.parseAs(transform: (String) -> String): T { * @since extensions-lib 14 */ @ExperimentalSerializationApi -inline fun Response.parseAs(): T = use { res -> - res.body.source().use { - Injekt.get().decodeFromBufferedSource(serializer(), it) - } +inline fun Response.parseAs(): T = body.source().use { + Injekt.get().decodeFromBufferedSource(serializer(), it) } /** @@ -46,7 +44,7 @@ inline fun String.parseAs(transform: (String) -> String): T = Injekt.get().decodeFromString(transform(this)) /** - * Parses and serializes the String as the type . + * Parses and serializes the Json String as the type . * * @since extensions-lib 14 */