Skip to content

Commit

Permalink
NF: CardType is a enum class
Browse files Browse the repository at this point in the history
Imporves typing
  • Loading branch information
Arthur-Milchior committed Dec 12, 2024
1 parent d36795a commit 496b2df
Show file tree
Hide file tree
Showing 15 changed files with 120 additions and 132 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1104,7 +1104,7 @@ class ContentProviderTest : InstrumentedTest() {
val cardId = card!!.id

// the card starts out being new
assertEquals("card is initial new", Consts.CARD_TYPE_NEW, card.queue)
assertEquals("card is initial new", 0, card.queue)
val cr = contentResolver
val reviewInfoUri = FlashCardsContract.ReviewInfo.CONTENT_URI
val noteId = card.nid
Expand Down Expand Up @@ -1134,7 +1134,7 @@ class ContentProviderTest : InstrumentedTest() {

// lookup the card after update, ensure it's not new anymore
val cardAfterReview = col.getCard(cardId)
assertEquals("card is now type rev", Card.TYPE_REV, cardAfterReview.queue)
assertEquals("card is now type rev", Consts.QUEUE_TYPE_REV, cardAfterReview.queue)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ abstract class InstrumentedTest {
@DuplicatedCode("This is copied from RobolectricTest. This will be refactored into a shared library later")
protected fun Card.moveToReviewQueue() {
this.queue = Consts.QUEUE_TYPE_REV
this.type = Consts.CARD_TYPE_REV
this.type = CardType.REV
this.due = 0
col.updateCard(this, true)
}
Expand Down
2 changes: 1 addition & 1 deletion AnkiDroid/src/main/java/com/ichi2/anki/AnkiDroidJsAPI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ open class AnkiDroidJsAPI(
"cardMod" -> convertToByteArray(apiContract, currentCard.mod)
"cardId" -> convertToByteArray(apiContract, currentCard.id)
"cardNid" -> convertToByteArray(apiContract, currentCard.nid)
"cardType" -> convertToByteArray(apiContract, currentCard.type)
"cardType" -> convertToByteArray(apiContract, currentCard.type.code)
"cardDid" -> convertToByteArray(apiContract, currentCard.did)
"cardLeft" -> convertToByteArray(apiContract, currentCard.left)
"cardODid" -> convertToByteArray(apiContract, currentCard.oDid)
Expand Down
41 changes: 19 additions & 22 deletions AnkiDroid/src/main/java/com/ichi2/anki/CardBrowser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ import com.ichi2.annotations.NeedsTest
import com.ichi2.async.renderBrowserQA
import com.ichi2.libanki.Card
import com.ichi2.libanki.CardId
import com.ichi2.libanki.CardType
import com.ichi2.libanki.ChangeManager
import com.ichi2.libanki.Collection
import com.ichi2.libanki.Consts
Expand Down Expand Up @@ -2309,7 +2310,7 @@ open class CardBrowser :
}

private fun getEaseForCards(): String =
if (card.type == Consts.CARD_TYPE_NEW) {
if (card.type == CardType.NEW) {
AnkiDroidApp.instance.getString(R.string.card_browser_interval_new_card)
} else {
"${card.factor / 10}%"
Expand All @@ -2327,9 +2328,10 @@ open class CardBrowser :

private fun queryIntervalForCards(): String =
when (card.type) {
Consts.CARD_TYPE_NEW -> AnkiDroidApp.instance.getString(R.string.card_browser_interval_new_card)
Consts.CARD_TYPE_LRN -> AnkiDroidApp.instance.getString(R.string.card_browser_interval_learning_card)
else -> roundedTimeSpanUnformatted(AnkiDroidApp.instance, card.ivl * SECONDS_PER_DAY)
CardType.NEW -> AnkiDroidApp.instance.getString(R.string.card_browser_interval_new_card)
CardType.LRN -> AnkiDroidApp.instance.getString(R.string.card_browser_interval_learning_card)
CardType.REV,
CardType.RELEARNING -> roundedTimeSpanUnformatted(AnkiDroidApp.instance, card.ivl * SECONDS_PER_DAY)
}

private fun queryAvgIntervalForNotes(): String {
Expand Down Expand Up @@ -2600,24 +2602,19 @@ open class CardBrowser :
): String {
val date: Long
val due = card.due
date =
if (card.isInDynamicDeck) {
return AnkiDroidApp.appResources.getString(R.string.card_browser_due_filtered_card)
} else if (card.queue == Consts.QUEUE_TYPE_LRN) {
due.toLong()
} else if (card.queue == Consts.QUEUE_TYPE_NEW || card.type == Consts.CARD_TYPE_NEW) {
return due.toString()
} else if (card.queue == Consts.QUEUE_TYPE_REV ||
card.queue == Consts.QUEUE_TYPE_DAY_LEARN_RELEARN ||
card.type == Consts.CARD_TYPE_REV &&
card.queue < 0
) {
val time = TimeManager.time.intTime()
val nbDaySinceCreation = due - col.sched.today
time + nbDaySinceCreation * SECONDS_PER_DAY
} else {
return ""
}
date = if (card.isInDynamicDeck) {
return AnkiDroidApp.appResources.getString(R.string.card_browser_due_filtered_card)
} else if (card.queue == Consts.QUEUE_TYPE_LRN) {
due.toLong()
} else if (card.queue == Consts.QUEUE_TYPE_NEW || card.type == CardType.NEW) {
return due.toString()
} else if (card.queue == Consts.QUEUE_TYPE_REV || card.queue == Consts.QUEUE_TYPE_DAY_LEARN_RELEARN || card.type == CardType.REV && card.queue < 0) {
val time = TimeManager.time.intTime()
val nbDaySinceCreation = due - col.sched.today
time + nbDaySinceCreation * SECONDS_PER_DAY
} else {
return ""
}
return LanguageUtil.getShortDateFormatFromS(date)
} // In Anki Desktop, a card with oDue <> 0 && oDid == 0 is not marked as dynamic.
}
Expand Down
17 changes: 5 additions & 12 deletions AnkiDroid/src/main/java/com/ichi2/anki/servicelayer/NoteService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import com.ichi2.anki.multimediacard.fields.MediaClipField
import com.ichi2.anki.multimediacard.fields.TextField
import com.ichi2.anki.multimediacard.impl.MultimediaEditableNote
import com.ichi2.libanki.Card
import com.ichi2.libanki.CardType
import com.ichi2.libanki.Collection
import com.ichi2.libanki.Consts
import com.ichi2.libanki.Note
Expand Down Expand Up @@ -230,12 +231,8 @@ object NoteService {
* returns the average ease of all the non-new cards in the note,
* or if all the cards in the note are new, returns null
*/
fun avgEase(
col: Collection,
note: Note,
): Int? {
val nonNewCards = note.cards(col).filter { it.type != Consts.CARD_TYPE_NEW }

fun avgEase(col: Collection, note: Note): Int? {
val nonNewCards = note.cards(col).filter { it.type != CardType.NEW }
return nonNewCards.average { it.factor }?.let { it / 10 }?.toInt()
}

Expand All @@ -254,12 +251,8 @@ object NoteService {
* Returns the average interval of all the non-new and non-learning cards in the note,
* or if all the cards in the note are new or learning, returns null
*/
fun avgInterval(
col: Collection,
note: Note,
): Int? {
val nonNewOrLearningCards = note.cards(col).filter { it.type != Consts.CARD_TYPE_NEW && it.type != Consts.CARD_TYPE_LRN }

fun avgInterval(col: Collection, note: Note): Int? {
val nonNewOrLearningCards = note.cards(col).filter { it.type != CardType.NEW && it.type != CardType.LRN }
return nonNewOrLearningCards.average { it.ivl }?.toInt()
}

Expand Down
15 changes: 5 additions & 10 deletions AnkiDroid/src/main/java/com/ichi2/libanki/Card.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import anki.cards.FsrsMemoryState
import com.ichi2.anki.Flag
import com.ichi2.anki.utils.ext.ifZero
import com.ichi2.libanki.Consts.CardQueue
import com.ichi2.libanki.Consts.CardType
import com.ichi2.libanki.CardType
import com.ichi2.libanki.TemplateManager.TemplateRenderContext.TemplateRenderOutput
import com.ichi2.libanki.utils.LibAnkiAlias
import com.ichi2.libanki.utils.NotInLibAnki
Expand Down Expand Up @@ -74,9 +74,7 @@ open class Card : Cloneable {
var mod: Long = 0
private var usn = 0

@get:CardType
@CardType
var type = 0
var type: CardType = CardType.NEW

@get:CardQueue
@CardQueue
Expand Down Expand Up @@ -129,7 +127,7 @@ open class Card : Cloneable {
ord = card.templateIdx
mod = card.mtimeSecs
usn = card.usn
type = card.ctype
type = CardType.fromCode(card.ctype)
queue = card.queue
due = card.due
ivl = card.interval
Expand All @@ -149,13 +147,12 @@ open class Card : Cloneable {
@LibAnkiAlias("_to_backend_card")
fun toBackendCard(): anki.cards.Card {
val builder =
anki.cards.Card
.newBuilder()
anki.cards.Card.newBuilder()
.setId(id)
.setNoteId(nid)
.setDeckId(did)
.setTemplateIdx(ord)
.setCtype(type)
.setCtype(type.code)
.setQueue(queue)
.setDue(due)
.setInterval(ivl)
Expand Down Expand Up @@ -449,8 +446,6 @@ open class Card : Cloneable {
}

companion object {
const val TYPE_REV = 2

// A list of class members to skip in the toString() representation
val SKIP_PRINT: Set<String> =
HashSet(
Expand Down
20 changes: 10 additions & 10 deletions AnkiDroid/src/main/java/com/ichi2/libanki/Consts.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ package com.ichi2.libanki
import androidx.annotation.IntDef
import kotlin.annotation.Retention



// Card types
enum class CardType(val code: Int) {
NEW(0), LRN(1), REV(2), RELEARNING(3);
companion object {
fun fromCode(code: Int) = CardType.entries.first{it.code == code}
}
}

object Consts {
// Queue types
const val QUEUE_TYPE_MANUALLY_BURIED = -3
Expand All @@ -42,16 +52,6 @@ object Consts {
)
annotation class CardQueue

// Card types
const val CARD_TYPE_NEW = 0
const val CARD_TYPE_LRN = 1
const val CARD_TYPE_REV = 2
const val CARD_TYPE_RELEARNING = 3

@Retention(AnnotationRetention.SOURCE)
@IntDef(CARD_TYPE_NEW, CARD_TYPE_LRN, CARD_TYPE_REV, CARD_TYPE_RELEARNING)
annotation class CardType

// dynamic deck order
const val DYN_OLDEST = 0
const val DYN_RANDOM = 1
Expand Down
27 changes: 13 additions & 14 deletions AnkiDroid/src/main/java/com/ichi2/libanki/sched/Scheduler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import com.ichi2.anki.Ease
import com.ichi2.anki.utils.SECONDS_PER_DAY
import com.ichi2.libanki.Card
import com.ichi2.libanki.CardId
import com.ichi2.libanki.CardType
import com.ichi2.libanki.Collection
import com.ichi2.libanki.Consts
import com.ichi2.libanki.DeckConfig
Expand Down Expand Up @@ -575,20 +576,18 @@ open class Scheduler(
.db
.query(
"select " +
"avg(case when type = " + Consts.CARD_TYPE_NEW +
" then case when ease > 1 then 1.0 else 0.0 end else null end) as newRate, avg(case when type = " +
Consts.CARD_TYPE_NEW +
" then time else null end) as newTime, " +
"avg(case when type in (" + Consts.CARD_TYPE_LRN + ", " + Consts.CARD_TYPE_RELEARNING +
") then case when ease > 1 then 1.0 else 0.0 end else null end) as revRate, avg(case when type in (" +
Consts.CARD_TYPE_LRN +
", " +
Consts.CARD_TYPE_RELEARNING +
") then time else null end) as revTime, " +
"avg(case when type = " + Consts.CARD_TYPE_REV +
" then case when ease > 1 then 1.0 else 0.0 end else null end) as relrnRate, avg(case when type = " +
Consts.CARD_TYPE_REV +
" then time else null end) as relrnTime " +
"avg(case when type = " + CardType.NEW.code +
" then case when ease > 1 then 1.0 else 0.0 end else null end) as newRate, avg(case when type = " +
CardType.NEW.code + " then time else null end) as newTime, " +
"avg(case when type in (" + CardType.LRN.code + ", " + CardType.RELEARNING.code +
") then case when ease > 1 then 1.0 else 0.0 end else null end) as revRate, avg(case when type in (" +
CardType.LRN.code +
", " +
CardType.RELEARNING.code +
") then time else null end) as revTime, " +
"avg(case when type = " + CardType.REV.code +
" then case when ease > 1 then 1.0 else 0.0 end else null end) as relrnRate, avg(case when type = " +
CardType.REV.code + " then time else null end) as relrnTime " +
"from revlog where id > " +
"?",
(col.sched.dayCutoff - (10 * SECONDS_PER_DAY)) * 1000,
Expand Down
10 changes: 5 additions & 5 deletions AnkiDroid/src/test/java/com/ichi2/anki/AnkiDroidJsAPITest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ package com.ichi2.anki
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.ichi2.anki.AnkiDroidJsAPI.Companion.SUCCESS_KEY
import com.ichi2.anki.AnkiDroidJsAPI.Companion.VALUE_KEY
import com.ichi2.libanki.Consts
import com.ichi2.libanki.CardType
import com.ichi2.libanki.utils.TimeManager
import com.ichi2.utils.BASIC_MODEL_NAME
import net.ankiweb.rsdroid.withoutUnicodeIsolation
Expand Down Expand Up @@ -109,7 +109,7 @@ class AnkiDroidJsAPITest : RobolectricTest() {
// Card Type
assertThat(
getDataFromRequest("cardType", jsapi),
equalTo(formatApiResult(currentCard.type)),
equalTo(formatApiResult(currentCard.type.code)),
)
// Card ODue
assertThat(
Expand Down Expand Up @@ -389,7 +389,7 @@ class AnkiDroidJsAPITest : RobolectricTest() {
val c = n.firstCard()

// Make card review with 28L due and 280% ease
c.type = Consts.CARD_TYPE_REV
c.type = CardType.REV
c.due = 28
c.factor = 2800
c.ivl = 8
Expand All @@ -398,7 +398,7 @@ class AnkiDroidJsAPITest : RobolectricTest() {
assertEquals(28, c.due, "Card due before reset")
assertEquals(8, c.ivl, "Card interval before reset")
assertEquals(2800, c.factor, "Card ease before reset")
assertEquals(Consts.CARD_TYPE_REV, c.type, "Card type before reset")
assertEquals(CardType.REV, c.type, "Card type before reset")

val reviewer: Reviewer = startReviewer()
waitForAsyncTasksToComplete()
Expand All @@ -414,7 +414,7 @@ class AnkiDroidJsAPITest : RobolectricTest() {
val cardAfterReset = col.getCard(reviewer.currentCard!!.id)
assertEquals(2, cardAfterReset.due, "Card due after reset")
assertEquals(0, cardAfterReset.ivl, "Card interval after reset")
assertEquals(Consts.CARD_TYPE_NEW, cardAfterReset.type, "Card type after reset")
assertEquals(CardType.NEW, cardAfterReset.type, "Card type after reset")
}

companion object {
Expand Down
11 changes: 6 additions & 5 deletions AnkiDroid/src/test/java/com/ichi2/anki/CardBrowserTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import com.ichi2.anki.utils.ext.getCurrentDialogFragment
import com.ichi2.anki.utils.ext.showDialogFragment
import com.ichi2.libanki.BrowserConfig
import com.ichi2.libanki.CardId
import com.ichi2.libanki.CardType
import com.ichi2.libanki.Consts
import com.ichi2.libanki.Note
import com.ichi2.libanki.NotetypeJson
Expand Down Expand Up @@ -560,7 +561,7 @@ class CardBrowserTest : RobolectricTest() {
addNoteUsingBasicModel("Hello", "World").firstCard().update {
due = 5
queue = Consts.QUEUE_TYPE_REV
type = Consts.CARD_TYPE_REV
type = CardType.REV
}
val cal = Calendar.getInstance()
cal.add(Calendar.DATE, 5)
Expand Down Expand Up @@ -1147,7 +1148,7 @@ class CardBrowserTest : RobolectricTest() {
cal.add(Calendar.DATE, 27)

// Not filtered
c.type = Consts.CARD_TYPE_NEW
c.type = CardType.NEW
c.due = 27
c.queue = Consts.QUEUE_TYPE_MANUALLY_BURIED
Assert.assertEquals("27", nextDue(col, c))
Expand All @@ -1165,7 +1166,7 @@ class CardBrowserTest : RobolectricTest() {
c.queue = Consts.QUEUE_TYPE_PREVIEW
Assert.assertEquals("27", nextDue(col, c))
Assert.assertEquals("27", dueString(col, c))
c.type = Consts.CARD_TYPE_LRN
c.type = CardType.LRN
c.due = id
c.queue = Consts.QUEUE_TYPE_MANUALLY_BURIED
Assert.assertEquals("", nextDue(col, c))
Expand All @@ -1182,7 +1183,7 @@ class CardBrowserTest : RobolectricTest() {
c.queue = Consts.QUEUE_TYPE_PREVIEW
Assert.assertEquals("", nextDue(col, c))
Assert.assertEquals("", dueString(col, c))
c.type = Consts.CARD_TYPE_REV
c.type = CardType.REV

val cal2 = Calendar.getInstance()
cal2.add(Calendar.DATE, 20)
Expand All @@ -1203,7 +1204,7 @@ class CardBrowserTest : RobolectricTest() {
c.queue = Consts.QUEUE_TYPE_PREVIEW
Assert.assertEquals("", nextDue(col, c))
Assert.assertEquals("", dueString(col, c))
c.type = Consts.CARD_TYPE_RELEARNING
c.type = CardType.RELEARNING
c.due = id
c.queue = Consts.QUEUE_TYPE_MANUALLY_BURIED
Assert.assertEquals("", nextDue(col, c))
Expand Down
3 changes: 2 additions & 1 deletion AnkiDroid/src/test/java/com/ichi2/anki/ReviewerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import com.ichi2.anki.preferences.PreferenceTestUtils
import com.ichi2.anki.preferences.sharedPrefs
import com.ichi2.anki.reviewer.ActionButtonStatus
import com.ichi2.libanki.Card
import com.ichi2.libanki.CardType
import com.ichi2.libanki.Consts
import com.ichi2.libanki.NotetypeJson
import com.ichi2.libanki.Notetypes
Expand Down Expand Up @@ -508,7 +509,7 @@ class ReviewerTest : RobolectricTest() {
private fun moveToReviewQueue(reviewCard: Card) {
reviewCard.update {
queue = Consts.QUEUE_TYPE_REV
type = Consts.CARD_TYPE_REV
type = CardType.REV
due = 0
}
}
Expand Down
Loading

0 comments on commit 496b2df

Please sign in to comment.