Skip to content

Commit

Permalink
NF: Remove else -> when possible
Browse files Browse the repository at this point in the history
When `when` is used on an enum, the `else ->` can always be
transformed into a complete enumeration of the enum's entries.

This ensure that each time a new entry is added, there is a warning
and the developer must decide whether or not a change is needed. While
the `else` may hide some required change.

So, for the sake of future developement, I removed `else ->` when it
makes sense.

I should note that I kept some `else ->` when the `when` consider only
a very small part of all possible enum value, in order to reduce the
size of the code.
  • Loading branch information
Arthur-Milchior authored and mikehardy committed Dec 8, 2024
1 parent f023dea commit 6e4f8da
Show file tree
Hide file tree
Showing 16 changed files with 127 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import kotlinx.parcelize.Parcelize

object ActivityTransitionAnimation {
@Suppress("DEPRECATION", "deprecated in API34 for predictive back, must plumb through new open/close parameter")
fun slide(activity: Activity, direction: Direction?) {
fun slide(activity: Activity, direction: Direction) {
when (direction) {
Direction.START -> if (isRightToLeft(activity)) {
activity.overridePendingTransition(R.anim.slide_right_in, R.anim.slide_right_out)
Expand All @@ -32,8 +32,6 @@ object ActivityTransitionAnimation {
Direction.NONE -> activity.overridePendingTransition(R.anim.none, R.anim.none)
Direction.DEFAULT -> {
}
else -> {
}
}
}

Expand Down
27 changes: 24 additions & 3 deletions AnkiDroid/src/main/java/com/ichi2/anki/AbstractFlashcardViewer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1730,8 +1730,29 @@ abstract class AbstractFlashcardViewer :
loadUrlInViewer("javascript: showAllHints();")
true
}

else -> {
ViewerCommand.REDO,
ViewerCommand.MARK,
ViewerCommand.TOGGLE_FLAG_RED,
ViewerCommand.TOGGLE_FLAG_ORANGE,
ViewerCommand.TOGGLE_FLAG_GREEN,
ViewerCommand.TOGGLE_FLAG_BLUE,
ViewerCommand.TOGGLE_FLAG_PINK,
ViewerCommand.TOGGLE_FLAG_TURQUOISE,
ViewerCommand.TOGGLE_FLAG_PURPLE,
ViewerCommand.UNSET_FLAG,
ViewerCommand.CARD_INFO,
ViewerCommand.ADD_NOTE,
ViewerCommand.RESCHEDULE_NOTE,
ViewerCommand.TOGGLE_AUTO_ADVANCE,
ViewerCommand.USER_ACTION_1,
ViewerCommand.USER_ACTION_2,
ViewerCommand.USER_ACTION_3,
ViewerCommand.USER_ACTION_4,
ViewerCommand.USER_ACTION_5,
ViewerCommand.USER_ACTION_6,
ViewerCommand.USER_ACTION_7,
ViewerCommand.USER_ACTION_8,
ViewerCommand.USER_ACTION_9 -> {
Timber.w("Unknown command requested: %s", which)
false
}
Expand Down Expand Up @@ -2508,7 +2529,7 @@ abstract class AbstractFlashcardViewer :
is SoundOrVideoTag -> tag
is TTSTag -> tag
// not currently supported
else -> return
null -> return
}
cardMediaPlayer.playOneSound(avTag)
}
Expand Down
6 changes: 4 additions & 2 deletions AnkiDroid/src/main/java/com/ichi2/anki/CardBrowser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2176,7 +2176,7 @@ open class CardBrowser :
return Themes.getColorFromAttr(context, colorAttr)
}

fun getColumnHeaderText(key: CardBrowserColumn?): String? {
fun getColumnHeaderText(key: CardBrowserColumn): String? {
return when (key) {
CardBrowserColumn.SFLD -> card.note(col).sFld(col)
CardBrowserColumn.DECK -> col.decks.name(card.did)
Expand All @@ -2200,7 +2200,9 @@ open class CardBrowser :
updateSearchItemQA()
qa!!.second
}
else -> null
CardBrowserColumn.FSRS_DIFFICULTY,
CardBrowserColumn.FSRS_RETRIEVABILITY,
CardBrowserColumn.FSRS_STABILITY -> null
}
}

Expand Down
2 changes: 1 addition & 1 deletion AnkiDroid/src/main/java/com/ichi2/anki/DeckPicker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,7 @@ open class DeckPicker :
}

@VisibleForTesting
fun handleStartupFailure(failure: StartupFailure?) {
fun handleStartupFailure(failure: StartupFailure) {
when (failure) {
is SDCardNotMounted -> {
Timber.i("SD card not mounted")
Expand Down
2 changes: 1 addition & 1 deletion AnkiDroid/src/main/java/com/ichi2/anki/Reviewer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ open class Reviewer :
return when (fullscreenMode) {
FullScreenMode.BUTTONS_ONLY -> R.layout.reviewer_fullscreen
FullScreenMode.FULLSCREEN_ALL_GONE -> R.layout.reviewer_fullscreen_noanswers
else -> R.layout.reviewer
FullScreenMode.BUTTONS_AND_MENU -> R.layout.reviewer
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,6 @@ class CardMediaPlayer : Closeable {
soundErrorListener?.onTtsError(it, isAutomaticPlayback)
}
}
else -> Timber.w("unknown audio: ${tag.javaClass}")
}
ensureActive()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,11 @@ class GestureProcessor(private val processor: ViewerCommand.CommandProcessor?) {
}

private fun execute(gesture: Gesture?): Boolean? {
val command = mapGestureToCommand(gesture) ?: return false
val command = gesture?.let { mapGestureToCommand(it) } ?: return false
return processor?.executeCommand(command, gesture)
}

private fun mapGestureToCommand(gesture: Gesture?): ViewerCommand? {
private fun mapGestureToCommand(gesture: Gesture): ViewerCommand? {
return when (gesture) {
Gesture.SWIPE_UP -> gestureSwipeUp
Gesture.SWIPE_DOWN -> gestureSwipeDown
Expand All @@ -129,7 +129,6 @@ class GestureProcessor(private val processor: ViewerCommand.CommandProcessor?) {
Gesture.DOUBLE_TAP -> gestureDoubleTap
Gesture.LONG_TAP -> gestureLongclick
Gesture.SHAKE -> gestureShake
else -> null
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,28 @@ enum class ViewerCommand(val resourceId: Int) {
SHOW_HINT -> listOf(keyCode(KeyEvent.KEYCODE_H, CardSide.BOTH))
SHOW_ALL_HINTS -> listOf(keyCode(KeyEvent.KEYCODE_G, CardSide.BOTH))
ADD_NOTE -> listOf(keyCode(KeyEvent.KEYCODE_A, CardSide.BOTH))
else -> emptyList()
SHOW_ANSWER,
DELETE,
EXIT,
UNSET_FLAG,
PAGE_UP,
PAGE_DOWN,
TAG,
CARD_INFO,
ABORT_AND_SYNC,
TOGGLE_WHITEBOARD,
CLEAR_WHITEBOARD,
CHANGE_WHITEBOARD_PEN_COLOR,
RESCHEDULE_NOTE,
USER_ACTION_1,
USER_ACTION_2,
USER_ACTION_3,
USER_ACTION_4,
USER_ACTION_5,
USER_ACTION_6,
USER_ACTION_7,
USER_ACTION_8,
USER_ACTION_9 -> emptyList()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ class DatabaseErrorDialog : AsyncDialogFragment() {
?: res().getString(R.string.card_browser_unknown_deck_name)
res().getString(R.string.directory_inaccessible_after_uninstall_summary, directory)
}
else -> requireArguments().getString("dialogMessage")
DIALOG_ERROR_HANDLING -> requireArguments().getString("dialogMessage")
}
private val title: String
get() = when (requireDialogType()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,12 @@ class CustomStudyDialog(private val collection: Collection, private val customSt
)
requireActivity().showDialogFragment(dialogFragment)
}
else -> {
STUDY_NEW,
STUDY_REV,
STUDY_FORGOT,
STUDY_AHEAD,
STUDY_RANDOM,
STUDY_PREVIEW -> {
// User asked for a standard custom study option
val d = CustomStudyDialog(collection, customStudyListener)
.withArguments(
Expand Down Expand Up @@ -337,7 +342,12 @@ class CustomStudyDialog(private val collection: Collection, private val customSt
return when (getOption()) {
STUDY_NEW -> res.getString(R.string.custom_study_new_total_new, collection.sched.totalNewForCurrentDeck())
STUDY_REV -> res.getString(R.string.custom_study_rev_total_rev, collection.sched.totalRevForCurrentDeck())
else -> ""
STUDY_FORGOT,
STUDY_AHEAD,
STUDY_RANDOM,
STUDY_PREVIEW,
STUDY_TAGS,
null -> ""
}
}
private val text2: String
Expand All @@ -350,7 +360,8 @@ class CustomStudyDialog(private val collection: Collection, private val customSt
STUDY_AHEAD -> res.getString(R.string.custom_study_ahead)
STUDY_RANDOM -> res.getString(R.string.custom_study_random)
STUDY_PREVIEW -> res.getString(R.string.custom_study_preview)
else -> ""
STUDY_TAGS,
null -> ""
}
}
private val defaultValue: String
Expand All @@ -363,7 +374,8 @@ class CustomStudyDialog(private val collection: Collection, private val customSt
STUDY_AHEAD -> prefs.getInt("aheadDays", 1).toString()
STUDY_RANDOM -> prefs.getInt("randomCards", 100).toString()
STUDY_PREVIEW -> prefs.getInt("previewDays", 1).toString()
else -> ""
STUDY_TAGS,
null -> ""
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ class MediaPlayer :
when (state) {
IDLE, INITIALIZED, PREPARED, STARTED, PAUSED, STOPPED, PLAYBACK_COMPLETE, ERROR ->
state = IDLE
else -> throw IllegalStateException("Invalid MediaPlayerState $state")
PREPARING,
END -> throw IllegalStateException("Invalid MediaPlayerState $state")
}
}

Expand All @@ -139,55 +140,94 @@ class MediaPlayer :
super.prepareAsync()
when (state) {
INITIALIZED, STOPPED -> state = PREPARING
else -> throw IllegalStateException("Invalid MediaPlayerState $state")
ERROR,
IDLE,
PREPARING,
PREPARED,
STARTED,
PAUSED,
PLAYBACK_COMPLETE,
END -> throw IllegalStateException("Invalid MediaPlayerState $state")
}
}

override fun prepare() {
super.prepare()
when (state) {
INITIALIZED, STOPPED -> state = PREPARED
else -> throw IllegalStateException("Invalid MediaPlayerState $state")
ERROR,
IDLE,
PREPARING,
PREPARED,
STARTED,
PAUSED,
PLAYBACK_COMPLETE,
END -> throw IllegalStateException("Invalid MediaPlayerState $state")
}
}

override fun seekTo(msec: Int) {
super.seekTo(msec)
when (state) {
PREPARED, STARTED, PAUSED, PLAYBACK_COMPLETE -> {}
else -> throw IllegalStateException("Invalid MediaPlayerState $state")
ERROR,
IDLE,
INITIALIZED,
PREPARING,
STOPPED,
END -> throw IllegalStateException("Invalid MediaPlayerState $state")
}
}

override fun seekTo(msec: Long, mode: Int) {
super.seekTo(msec, mode)
when (state) {
PREPARED, STARTED, PAUSED, PLAYBACK_COMPLETE -> {}
else -> throw IllegalStateException("Invalid MediaPlayerState $state")
ERROR,
IDLE,
INITIALIZED,
PREPARING,
STOPPED,
END -> throw IllegalStateException("Invalid MediaPlayerState $state")
}
}

override fun stop() {
super.stop()
when (state) {
PREPARED, STARTED, STOPPED, PAUSED, PLAYBACK_COMPLETE -> state = STOPPED
else -> throw IllegalStateException("Invalid MediaPlayerState $state")
ERROR,
IDLE,
INITIALIZED,
PREPARING,
END -> throw IllegalStateException("Invalid MediaPlayerState $state")
}
}

override fun start() {
super.start()
when (state) {
PREPARED, STARTED, PAUSED, PLAYBACK_COMPLETE -> state = STARTED
else -> throw IllegalStateException("Invalid MediaPlayerState $state")
ERROR,
IDLE,
INITIALIZED,
PREPARING,
STOPPED,
END -> throw IllegalStateException("Invalid MediaPlayerState $state")
}
}

override fun pause() {
super.pause()
when (state) {
STARTED, PAUSED, PLAYBACK_COMPLETE -> state = PAUSED
else -> throw IllegalStateException("Invalid MediaPlayerState $state")
ERROR,
IDLE,
INITIALIZED,
PREPARING,
PREPARED,
STOPPED,
END -> throw IllegalStateException("Invalid MediaPlayerState $state")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ enum class AutomaticAnswerAction(private val configValue: Int) {
ANSWER_AGAIN -> AGAIN.toViewerCommand()
ANSWER_HARD -> HARD.toViewerCommand()
ANSWER_GOOD -> GOOD.toViewerCommand()
else -> null
SHOW_REMINDER -> null
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class MappableBinding(val binding: Binding, val screen: Screen) {
val formatString = when (side) {
CardSide.QUESTION -> context.getString(R.string.display_binding_card_side_question)
CardSide.ANSWER -> context.getString(R.string.display_binding_card_side_answer)
else -> context.getString(R.string.display_binding_card_side_both) // intentionally no prefix
CardSide.BOTH -> context.getString(R.string.display_binding_card_side_both) // intentionally no prefix
}
return String.format(formatString, binding.toDisplayString(context))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,7 @@ object NoteService {
}
when (field.type) {
EFieldType.AUDIO_RECORDING, EFieldType.MEDIA_CLIP, EFieldType.IMAGE -> field.mediaPath = outFile.absolutePath
else -> {
}
EFieldType.TEXT -> {}
}
}
} catch (e: IOException) {
Expand Down
6 changes: 3 additions & 3 deletions AnkiDroid/src/main/java/com/ichi2/libanki/Sound.kt
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,14 @@ data class SoundOrVideoTag(val filename: String) : AvTag() {
}
}

/** In python, this is a union of [TTSTag] and [SoundOrVideoTag] */
open class AvTag

/**
* [Regex] used to identify the markers for sound files
*/
val SOUND_RE = Pattern.compile("\\[sound:([^\\[\\]]*)]").toRegex()

/** In python, this is a union of [TTSTag] and [SoundOrVideoTag] */
sealed class AvTag

fun stripAvRefs(text: String, replacement: String = "") = AvRef.REGEX.replace(text, replacement)

// not in libAnki
Expand Down
2 changes: 1 addition & 1 deletion AnkiDroid/src/main/java/com/ichi2/ui/CheckBoxTriStates.kt
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ class CheckBoxTriStates : AppCompatCheckBox {
val btnDrawable: Int = when (_state) {
State.UNCHECKED -> R.drawable.ic_baseline_check_box_outline_blank_24_inset
State.CHECKED -> R.drawable.ic_baseline_check_box_24_inset
else -> R.drawable.ic_baseline_indeterminate_check_box_24_inset
State.INDETERMINATE -> R.drawable.ic_baseline_indeterminate_check_box_24_inset
}
setButtonDrawable(btnDrawable)
}
Expand Down

0 comments on commit 6e4f8da

Please sign in to comment.