From 2f08515455c0c9634b98e3fc24d91f4bf2a4ba90 Mon Sep 17 00:00:00 2001 From: arkon Date: Sun, 14 Mar 2021 17:03:43 -0400 Subject: [PATCH] Less janky enum iteration --- .../tachiyomi/ui/reader/setting/OrientationType.kt | 12 ++++++++---- .../tachiyomi/ui/reader/setting/ReadingModeType.kt | 7 +++---- .../eu/kanade/tachiyomi/util/lang/EnumExtensions.kt | 7 +++++++ 3 files changed, 18 insertions(+), 8 deletions(-) create mode 100644 app/src/main/java/eu/kanade/tachiyomi/util/lang/EnumExtensions.kt diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/setting/OrientationType.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/setting/OrientationType.kt index 8293712496..f5f79027af 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/setting/OrientationType.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/setting/OrientationType.kt @@ -6,7 +6,7 @@ import android.content.res.Resources import androidx.annotation.DrawableRes import androidx.annotation.StringRes import eu.kanade.tachiyomi.R -import kotlin.math.max +import eu.kanade.tachiyomi.util.lang.next enum class OrientationType(val prefValue: Int, val flag: Int, @StringRes val stringRes: Int, @DrawableRes val iconRes: Int) { FREE(1, ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED, R.string.rotation_free, R.drawable.ic_screen_rotation_24dp), @@ -31,9 +31,13 @@ enum class OrientationType(val prefValue: Int, val flag: Int, @StringRes val str } fun getNextOrientation(preference: Int, resources: Resources): OrientationType { - // There's only 4 options (1 to 4) - val newOrientation = max(1, (preference + 1) % 5) - return fromPreference(newOrientation, resources) + val current = if (preference == 2) { + // Avoid issue due to 2 types having the same prefValue + LOCKED_LANDSCAPE + } else { + fromPreference(preference, resources) + } + return current.next() } } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/setting/ReadingModeType.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/setting/ReadingModeType.kt index 945aff462f..3fd795fab8 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/setting/ReadingModeType.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/setting/ReadingModeType.kt @@ -2,7 +2,7 @@ package eu.kanade.tachiyomi.ui.reader.setting import androidx.annotation.StringRes import eu.kanade.tachiyomi.R -import kotlin.math.max +import eu.kanade.tachiyomi.util.lang.next enum class ReadingModeType(val prefValue: Int, @StringRes val stringRes: Int) { DEFAULT(0, R.string.default_viewer), @@ -17,9 +17,8 @@ enum class ReadingModeType(val prefValue: Int, @StringRes val stringRes: Int) { fun fromPreference(preference: Int): ReadingModeType = values().find { it.prefValue == preference } ?: DEFAULT fun getNextReadingMode(preference: Int): ReadingModeType { - // There's only 6 options (0 to 5) - val newReadingMode = max(0, (preference + 1) % 6) - return fromPreference(newReadingMode) + val current = fromPreference(preference) + return current.next() } } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/lang/EnumExtensions.kt b/app/src/main/java/eu/kanade/tachiyomi/util/lang/EnumExtensions.kt new file mode 100644 index 0000000000..4c9432303f --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/util/lang/EnumExtensions.kt @@ -0,0 +1,7 @@ +package eu.kanade.tachiyomi.util.lang + +inline fun > T.next(): T { + val values = enumValues() + val nextOrdinal = (ordinal + 1) % values.size + return values[nextOrdinal] +}