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.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()
}
}
}

View File

@ -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()
}
}
}

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]
}