Less janky enum iteration

This commit is contained in:
arkon 2021-03-14 17:03:43 -04:00
parent 7f450e185d
commit 2f08515455
3 changed files with 18 additions and 8 deletions

View file

@ -6,7 +6,7 @@ import android.content.res.Resources
import androidx.annotation.DrawableRes import androidx.annotation.DrawableRes
import androidx.annotation.StringRes import androidx.annotation.StringRes
import eu.kanade.tachiyomi.R 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) { 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), 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 { fun getNextOrientation(preference: Int, resources: Resources): OrientationType {
// There's only 4 options (1 to 4) val current = if (preference == 2) {
val newOrientation = max(1, (preference + 1) % 5) // Avoid issue due to 2 types having the same prefValue
return fromPreference(newOrientation, resources) LOCKED_LANDSCAPE
} else {
fromPreference(preference, resources)
}
return current.next()
} }
} }
} }

View file

@ -2,7 +2,7 @@ package eu.kanade.tachiyomi.ui.reader.setting
import androidx.annotation.StringRes import androidx.annotation.StringRes
import eu.kanade.tachiyomi.R 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) { enum class ReadingModeType(val prefValue: Int, @StringRes val stringRes: Int) {
DEFAULT(0, R.string.default_viewer), 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 fromPreference(preference: Int): ReadingModeType = values().find { it.prefValue == preference } ?: DEFAULT
fun getNextReadingMode(preference: Int): ReadingModeType { fun getNextReadingMode(preference: Int): ReadingModeType {
// There's only 6 options (0 to 5) val current = fromPreference(preference)
val newReadingMode = max(0, (preference + 1) % 6) return current.next()
return fromPreference(newReadingMode)
} }
} }
} }

View file

@ -0,0 +1,7 @@
package eu.kanade.tachiyomi.util.lang
inline fun <reified T : Enum<T>> T.next(): T {
val values = enumValues<T>()
val nextOrdinal = (ordinal + 1) % values.size
return values[nextOrdinal]
}