Skip to content

Commit

Permalink
NF: Use com.ichi2.anki.Ease as much as possible
Browse files Browse the repository at this point in the history
This allo to remove else in `when` case, and generally improve
typing. Also, allows to remove some constant that were duplicate of
the enum entries.
  • Loading branch information
Arthur-Milchior committed Nov 14, 2024
1 parent d7b4118 commit 047acc3
Show file tree
Hide file tree
Showing 22 changed files with 243 additions and 245 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import android.content.ContentValues
import android.database.CursorWindow
import android.net.Uri
import anki.notetypes.StockNotetype
import com.ichi2.anki.AbstractFlashcardViewer
import com.ichi2.anki.CollectionManager
import com.ichi2.anki.Ease
import com.ichi2.anki.FlashCardsContract
import com.ichi2.anki.provider.pureAnswer
import com.ichi2.anki.testutil.DatabaseUtils.cursorFillWindow
Expand Down Expand Up @@ -1066,12 +1066,12 @@ class ContentProviderTest : InstrumentedTest() {
val reviewInfoUri = FlashCardsContract.ReviewInfo.CONTENT_URI
val noteId = card.nid
val cardOrd = card.ord
val earlyGraduatingEase = AbstractFlashcardViewer.EASE_4
val earlyGraduatingEase = Ease.EASY
val values = ContentValues().apply {
val timeTaken: Long = 5000 // 5 seconds
put(FlashCardsContract.ReviewInfo.NOTE_ID, noteId)
put(FlashCardsContract.ReviewInfo.CARD_ORD, cardOrd)
put(FlashCardsContract.ReviewInfo.EASE, earlyGraduatingEase)
put(FlashCardsContract.ReviewInfo.EASE, earlyGraduatingEase.value)
put(FlashCardsContract.ReviewInfo.TIME_TAKEN, timeTaken)
}
val updateCount = cr.update(reviewInfoUri, values, null, null)
Expand Down
69 changes: 30 additions & 39 deletions AnkiDroid/src/main/java/com/ichi2/anki/AbstractFlashcardViewer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ import com.ichi2.libanki.CardId
import com.ichi2.libanki.ChangeManager
import com.ichi2.libanki.Collection
import com.ichi2.libanki.Consts
import com.ichi2.libanki.Consts.ButtonType
import com.ichi2.libanki.DeckId
import com.ichi2.libanki.Decks
import com.ichi2.libanki.Sound.getAvTag
Expand Down Expand Up @@ -252,7 +251,7 @@ abstract class AbstractFlashcardViewer :
private val clipboard: ClipboardManager? = null
private var previousAnswerIndicator: PreviousAnswerIndicator? = null

private var currentEase = 0
private var currentEase: Ease? = null
private var initialFlipCardHeight = 0
private var buttonHeightSet = false

Expand Down Expand Up @@ -416,26 +415,26 @@ abstract class AbstractFlashcardViewer :
automaticAnswer.onSelectEase()
when (view.id) {
R.id.flashcard_layout_ease1 -> {
Timber.i("AbstractFlashcardViewer:: EASE_1 pressed")
answerCard(Consts.BUTTON_ONE)
Timber.i("AbstractFlashcardViewer:: Ease.ONE pressed")
answerCard(Ease.AGAIN)
}

R.id.flashcard_layout_ease2 -> {
Timber.i("AbstractFlashcardViewer:: EASE_2 pressed")
answerCard(Consts.BUTTON_TWO)
Timber.i("AbstractFlashcardViewer:: Ease.TWO pressed")
answerCard(Ease.HARD)
}

R.id.flashcard_layout_ease3 -> {
Timber.i("AbstractFlashcardViewer:: EASE_3 pressed")
answerCard(Consts.BUTTON_THREE)
Timber.i("AbstractFlashcardViewer:: Ease.THREE pressed")
answerCard(Ease.GOOD)
}

R.id.flashcard_layout_ease4 -> {
Timber.i("AbstractFlashcardViewer:: EASE_4 pressed")
answerCard(Consts.BUTTON_FOUR)
Timber.i("AbstractFlashcardViewer:: Ease.FOUR pressed")
answerCard(Ease.EASY)
}

else -> currentEase = 0
else -> currentEase = null
}
if (!hasBeenTouched) {
view.isPressed = false
Expand Down Expand Up @@ -856,7 +855,7 @@ abstract class AbstractFlashcardViewer :
}
}

open fun answerCard(@ButtonType ease: Int) = preventSimultaneousExecutions(ANSWER_CARD) {
open fun answerCard(ease: Ease) = preventSimultaneousExecutions(ANSWER_CARD) {
launchCatchingTask {
if (inAnswer) {
return@launchCatchingTask
Expand All @@ -876,7 +875,7 @@ abstract class AbstractFlashcardViewer :
}
}

open suspend fun answerCardInner(@ButtonType ease: Int) {
open suspend fun answerCardInner(ease: Ease) {
// Legacy tests assume they can call answerCard() even outside of Reviewer
withCol {
sched.answerCard(currentCard!!, ease)
Expand All @@ -897,25 +896,25 @@ abstract class AbstractFlashcardViewer :
gestureDetector = GestureDetector(this, gestureDetectorImpl)
easeButtonsLayout = findViewById(R.id.ease_buttons)
easeButton1 = EaseButton(
EASE_1,
Ease.AGAIN,
findViewById(R.id.flashcard_layout_ease1),
findViewById(R.id.ease1),
findViewById(R.id.nextTime1)
).apply { setListeners(easeHandler) }
easeButton2 = EaseButton(
EASE_2,
Ease.HARD,
findViewById(R.id.flashcard_layout_ease2),
findViewById(R.id.ease2),
findViewById(R.id.nextTime2)
).apply { setListeners(easeHandler) }
easeButton3 = EaseButton(
EASE_3,
Ease.GOOD,
findViewById(R.id.flashcard_layout_ease3),
findViewById(R.id.ease3),
findViewById(R.id.nextTime3)
).apply { setListeners(easeHandler) }
easeButton4 = EaseButton(
EASE_4,
Ease.EASY,
findViewById(R.id.flashcard_layout_ease4),
findViewById(R.id.ease4),
findViewById(R.id.nextTime4)
Expand Down Expand Up @@ -1059,7 +1058,7 @@ abstract class AbstractFlashcardViewer :
}

/** If a card is displaying the question, flip it, otherwise answer it */
internal open fun flipOrAnswerCard(cardOrdinal: Int) {
internal open fun flipOrAnswerCard(cardOrdinal: Ease) {
if (!displayAnswer) {
displayCardAnswer()
return
Expand Down Expand Up @@ -1624,22 +1623,22 @@ abstract class AbstractFlashcardViewer :
}

ViewerCommand.FLIP_OR_ANSWER_EASE1 -> {
flipOrAnswerCard(EASE_1)
flipOrAnswerCard(Ease.AGAIN)
true
}

ViewerCommand.FLIP_OR_ANSWER_EASE2 -> {
flipOrAnswerCard(EASE_2)
flipOrAnswerCard(Ease.HARD)
true
}

ViewerCommand.FLIP_OR_ANSWER_EASE3 -> {
flipOrAnswerCard(EASE_3)
flipOrAnswerCard(Ease.GOOD)
true
}

ViewerCommand.FLIP_OR_ANSWER_EASE4 -> {
flipOrAnswerCard(EASE_4)
flipOrAnswerCard(Ease.EASY)
true
}

Expand Down Expand Up @@ -1793,13 +1792,13 @@ abstract class AbstractFlashcardViewer :
}
}

protected open fun performClickWithVisualFeedback(ease: Int) {
protected open fun performClickWithVisualFeedback(ease: Ease) {
// Delay could potentially be lower - testing with 20 left a visible "click"
when (ease) {
EASE_1 -> easeButton1!!.performClickWithVisualFeedback()
EASE_2 -> easeButton2!!.performClickWithVisualFeedback()
EASE_3 -> easeButton3!!.performClickWithVisualFeedback()
EASE_4 -> easeButton4!!.performClickWithVisualFeedback()
Ease.AGAIN -> easeButton1!!.performClickWithVisualFeedback()
Ease.HARD -> easeButton2!!.performClickWithVisualFeedback()
Ease.GOOD -> easeButton3!!.performClickWithVisualFeedback()
Ease.EASY -> easeButton4!!.performClickWithVisualFeedback()
}
}

Expand Down Expand Up @@ -2418,22 +2417,22 @@ abstract class AbstractFlashcardViewer :
}

WebViewSignalParserUtils.ANSWER_ORDINAL_1 -> {
flipOrAnswerCard(EASE_1)
flipOrAnswerCard(Ease.AGAIN)
return true
}

WebViewSignalParserUtils.ANSWER_ORDINAL_2 -> {
flipOrAnswerCard(EASE_2)
flipOrAnswerCard(Ease.HARD)
return true
}

WebViewSignalParserUtils.ANSWER_ORDINAL_3 -> {
flipOrAnswerCard(EASE_3)
flipOrAnswerCard(Ease.GOOD)
return true
}

WebViewSignalParserUtils.ANSWER_ORDINAL_4 -> {
flipOrAnswerCard(EASE_4)
flipOrAnswerCard(Ease.EASY)
return true
}

Expand Down Expand Up @@ -2639,14 +2638,6 @@ abstract class AbstractFlashcardViewer :
const val RESULT_NO_MORE_CARDS = 52
const val RESULT_ABORT_AND_SYNC = 53

/**
* Available options performed by other activities.
*/
const val EASE_1 = 1
const val EASE_2 = 2
const val EASE_3 = 3
const val EASE_4 = 4

/**
* Time to wait in milliseconds before resuming fullscreen mode
*
Expand Down
8 changes: 4 additions & 4 deletions AnkiDroid/src/main/java/com/ichi2/anki/AnkiDroidJsAPI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -318,19 +318,19 @@ open class AnkiDroidJsAPI(private val activity: AbstractFlashcardViewer) {
convertToByteArray(apiContract, true)
}
"answerEase1" -> {
activity.flipOrAnswerCard(AbstractFlashcardViewer.EASE_1)
activity.flipOrAnswerCard(Ease.AGAIN)
convertToByteArray(apiContract, true)
}
"answerEase2" -> {
activity.flipOrAnswerCard(AbstractFlashcardViewer.EASE_2)
activity.flipOrAnswerCard(Ease.HARD)
convertToByteArray(apiContract, true)
}
"answerEase3" -> {
activity.flipOrAnswerCard(AbstractFlashcardViewer.EASE_3)
activity.flipOrAnswerCard(Ease.GOOD)
convertToByteArray(apiContract, true)
}
"answerEase4" -> {
activity.flipOrAnswerCard(AbstractFlashcardViewer.EASE_4)
activity.flipOrAnswerCard(Ease.EASY)
convertToByteArray(apiContract, true)
}

Expand Down
11 changes: 11 additions & 0 deletions AnkiDroid/src/main/java/com/ichi2/anki/Ease.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,21 @@ package com.ichi2.anki

/**
* [value] should be kept in sync with the [com.ichi2.anki.api.Ease] enum.
*
* @param value The so called value of the button. For the sake of consistency with upstream and our API
* the buttons are numbered from 1 to 4.
*/
enum class Ease(val value: Int) {
AGAIN(1),
HARD(2),
GOOD(3),
EASY(4);

companion object {
/**
*The function associating to [value] the Ease whose value is [value].
* E.g. it sends 1 to [Again] and 4 to [Easy]
*/
fun fromValue(value: Int) = Ease.entries[value - 1]
}
}
4 changes: 2 additions & 2 deletions AnkiDroid/src/main/java/com/ichi2/anki/Reviewer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1064,7 +1064,7 @@ open class Reviewer :
queueState = state
}

override suspend fun answerCardInner(ease: Int) {
override suspend fun answerCardInner(ease: Ease) {
val state = queueState!!
Timber.d("answerCardInner: ${currentCard!!.id} $ease")
var wasLeech = false
Expand All @@ -1073,7 +1073,7 @@ open class Reviewer :
wasLeech = sched.stateIsLeech(state.states.again)
}
}.also {
if (ease == Consts.BUTTON_ONE && wasLeech) {
if (ease == Ease.AGAIN && wasLeech) {
state.topCard.load(getColUnsafe)
val leechMessage: String = if (state.topCard.queue < 0) {
resources.getString(R.string.leech_suspend_notification)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ import com.ichi2.anki.AnkiDroidApp
import com.ichi2.anki.BuildConfig
import com.ichi2.anki.CollectionManager
import com.ichi2.anki.CrashReportService
import com.ichi2.anki.Ease
import com.ichi2.anki.FlashCardsContract
import com.ichi2.anki.utils.ext.description
import com.ichi2.libanki.Card
import com.ichi2.libanki.Collection
import com.ichi2.libanki.Consts
import com.ichi2.libanki.Consts.ButtonType
import com.ichi2.libanki.Deck
import com.ichi2.libanki.DeckId
import com.ichi2.libanki.Decks
Expand Down Expand Up @@ -346,7 +346,7 @@ class CardContentProvider : ContentProvider() {
val buttonTexts = JSONArray()
var i = 0
while (i < buttonCount) {
buttonTexts.put(col.sched.nextIvlStr(currentCard, i + 1))
buttonTexts.put(col.sched.nextIvlStr(currentCard, Ease.fromValue(i + 1)))
i++
}
addReviewInfoToCursor(currentCard, buttonTexts, buttonCount, rv, col, columns)
Expand Down Expand Up @@ -590,15 +590,18 @@ class CardContentProvider : ContentProvider() {
val valueSet = values!!.valueSet()
var cardOrd = -1
var noteID: Long = -1
var ease = -1
var ease: Ease? = null
var timeTaken: Long = -1
var bury = -1
var suspend = -1
for ((key) in valueSet) {
when (key) {
FlashCardsContract.ReviewInfo.NOTE_ID -> noteID = values.getAsLong(key)
FlashCardsContract.ReviewInfo.CARD_ORD -> cardOrd = values.getAsInteger(key)
FlashCardsContract.ReviewInfo.EASE -> ease = values.getAsInteger(key)
FlashCardsContract.ReviewInfo.EASE -> {
// The value corresponds to
ease = Ease.fromValue(values.getAsInteger(key))
}
FlashCardsContract.ReviewInfo.TIME_TAKEN ->
timeTaken =
values.getAsLong(key)
Expand All @@ -619,7 +622,7 @@ class CardContentProvider : ContentProvider() {
// suspend card
buryOrSuspendCard(col, cardToAnswer, false)
} else {
answerCard(col, cardToAnswer, ease, timeTaken)
answerCard(col, cardToAnswer, ease!!, timeTaken)
}
updated++
} else {
Expand Down Expand Up @@ -1080,7 +1083,7 @@ class CardContentProvider : ContentProvider() {
}
}

private fun answerCard(col: Collection, cardToAnswer: Card?, @ButtonType ease: Int, timeTaken: Long) {
private fun answerCard(col: Collection, cardToAnswer: Card?, ease: Ease, timeTaken: Long) {
try {
if (cardToAnswer != null) {
if (timeTaken != -1L) {
Expand Down
9 changes: 6 additions & 3 deletions AnkiDroid/src/main/java/com/ichi2/anki/reviewer/EaseButton.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import android.widget.LinearLayout
import android.widget.TextView
import androidx.annotation.StringRes
import com.ichi2.anki.AbstractFlashcardViewer
import com.ichi2.anki.Ease
import com.ichi2.utils.KotlinCleanup

/**
* The UI of an ease button
Expand All @@ -31,7 +33,7 @@ import com.ichi2.anki.AbstractFlashcardViewer
* * [nextTime] is used by the API
* * [canPerformClick] is used to determine if the answer is being shown and the button isn't blocked
*/
class EaseButton(private val ease: Int, private val layout: LinearLayout, private val easeTextView: TextView, private val easeTimeView: TextView) {
class EaseButton(private val ease: Ease, private val layout: LinearLayout, private val easeTextView: TextView, private val easeTimeView: TextView) {

var height: Int
get() = layout.layoutParams.height
Expand Down Expand Up @@ -97,7 +99,8 @@ class EaseButton(private val ease: Int, private val layout: LinearLayout, privat
*
* @param currentEase The current ease of the card
*/
fun unblockBasedOnEase(currentEase: Int) {
@KotlinCleanup("Make the type non nullable.")
fun unblockBasedOnEase(currentEase: Ease?) {
if (this.ease == currentEase) {
layout.isClickable = true
} else {
Expand All @@ -110,7 +113,7 @@ class EaseButton(private val ease: Int, private val layout: LinearLayout, privat
*
* @param currentEase The current ease of the card
*/
fun blockBasedOnEase(currentEase: Int) {
fun blockBasedOnEase(currentEase: Ease) {
if (this.ease == currentEase) {
layout.isClickable = false
} else {
Expand Down
Loading

0 comments on commit 047acc3

Please sign in to comment.