Skip to content

Commit

Permalink
NF: QueueType becomes an enum
Browse files Browse the repository at this point in the history
This improve the typing.
  • Loading branch information
Arthur-Milchior committed Dec 14, 2024
1 parent 59a27ac commit 141681c
Show file tree
Hide file tree
Showing 18 changed files with 239 additions and 220 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import com.ichi2.anki.testutil.DatabaseUtils.cursorFillWindow
import com.ichi2.anki.testutil.GrantStoragePermission.storagePermission
import com.ichi2.anki.testutil.grantPermissions
import com.ichi2.libanki.Card
import com.ichi2.libanki.Consts
import com.ichi2.libanki.Decks
import com.ichi2.libanki.Note
import com.ichi2.libanki.NotetypeJson
Expand Down Expand Up @@ -1104,7 +1103,7 @@ class ContentProviderTest : InstrumentedTest() {
val cardId = card!!.id

// the card starts out being new
assertEquals("card is initial new", 0, card.queue)
assertEquals("card is initial new", QueueType.NEW, card.queue)
val cr = contentResolver
val reviewInfoUri = FlashCardsContract.ReviewInfo.CONTENT_URI
val noteId = card.nid
Expand Down Expand Up @@ -1134,7 +1133,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", Consts.QUEUE_TYPE_REV, cardAfterReview.queue)
assertEquals("card is now type rev", QueueType.REV, cardAfterReview.queue)
}

/**
Expand All @@ -1149,7 +1148,7 @@ class ContentProviderTest : InstrumentedTest() {
// verify that the card is not already user-buried
assertNotEquals(
"Card is not user-buried before test",
Consts.QUEUE_TYPE_SIBLING_BURIED,
QueueType.SIBLING_BURIED,
card!!.queue,
)

Expand All @@ -1175,10 +1174,10 @@ class ContentProviderTest : InstrumentedTest() {
// verify that it did get buried
// -----------------------------
val cardAfterUpdate = col.getCard(cardId)
// QUEUE_TYPE_MANUALLY_BURIED was also used for SIBLING_BURIED in sched v1
// QueueType.MANUALLY_BURIED was also used for SIBLING_BURIED in sched v1
assertEquals(
"Card is user-buried",
Consts.QUEUE_TYPE_MANUALLY_BURIED,
QueueType.MANUALLY_BURIED,
cardAfterUpdate.queue,
)

Expand All @@ -1199,7 +1198,7 @@ class ContentProviderTest : InstrumentedTest() {
// verify that the card is not already suspended
assertNotEquals(
"Card is not suspended before test",
Consts.QUEUE_TYPE_SUSPENDED,
QueueType.SUSPENDED,
card!!.queue,
)

Expand All @@ -1225,7 +1224,7 @@ class ContentProviderTest : InstrumentedTest() {
// verify that it did get suspended
// --------------------------------
val cardAfterUpdate = col.getCard(cardId)
assertEquals("Card is suspended", Consts.QUEUE_TYPE_SUSPENDED, cardAfterUpdate.queue)
assertEquals("Card is suspended", QueueType.SUSPENDED, cardAfterUpdate.queue)

// cleanup, unsuspend card and reschedule
// --------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ import com.ichi2.anki.CollectionManager
import com.ichi2.anki.utils.EnsureAllFilesAccessRule
import com.ichi2.annotations.DuplicatedCode
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
import com.ichi2.libanki.QueueType
import com.ichi2.libanki.utils.TimeManager
import com.ichi2.testutils.common.IgnoreFlakyTestsInCIRule
import kotlinx.coroutines.runBlocking
Expand Down Expand Up @@ -149,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.queue = QueueType.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 @@ -276,7 +276,7 @@ open class AnkiDroidJsAPI(
"cardLeft" -> convertToByteArray(apiContract, currentCard.left)
"cardODid" -> convertToByteArray(apiContract, currentCard.oDid)
"cardODue" -> convertToByteArray(apiContract, currentCard.oDue)
"cardQueue" -> convertToByteArray(apiContract, currentCard.queue)
"cardQueue" -> convertToByteArray(apiContract, currentCard.queue.code)
"cardLapses" -> convertToByteArray(apiContract, currentCard.lapses)
"cardDue" -> convertToByteArray(apiContract, currentCard.due)
"deckName" -> convertToByteArray(apiContract, Decks.basename(activity.getColUnsafe.decks.name(currentCard.did)))
Expand Down
42 changes: 24 additions & 18 deletions AnkiDroid/src/main/java/com/ichi2/anki/CardBrowser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,10 @@ 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
import com.ichi2.libanki.DeckId
import com.ichi2.libanki.DeckNameId
import com.ichi2.libanki.NoteId
import com.ichi2.libanki.QueueType
import com.ichi2.libanki.SortOrder
import com.ichi2.libanki.Sound
import com.ichi2.libanki.TemplateManager
Expand Down Expand Up @@ -1382,7 +1382,7 @@ open class CardBrowser :
launchCatchingTask {
val selectedCardIds = viewModel.queryAllSelectedCardIds()
// Only new cards may be repositioned (If any non-new found show error dialog and return false)
if (selectedCardIds.any { getColUnsafe.getCard(it).queue != Consts.QUEUE_TYPE_NEW }) {
if (selectedCardIds.any { getColUnsafe.getCard(it).queue != QueueType.NEW }) {
showDialogFragment(
SimpleMessageDialog.newInstance(
title = getString(R.string.vague_error),
Expand Down Expand Up @@ -2271,7 +2271,7 @@ open class CardBrowser :
val colorAttr =
if (isMarked(col, card.note(col))) {
R.attr.markedColor
} else if (card.queue == Consts.QUEUE_TYPE_SUSPENDED) {
} else if (card.queue == QueueType.SUSPENDED) {
R.attr.suspendedColor
} else {
android.R.attr.colorBackground
Expand Down Expand Up @@ -2331,7 +2331,8 @@ open class CardBrowser :
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)
CardType.RELEARNING,
-> roundedTimeSpanUnformatted(AnkiDroidApp.instance, card.ivl * SECONDS_PER_DAY)
}

private fun queryAvgIntervalForNotes(): String {
Expand Down Expand Up @@ -2589,7 +2590,7 @@ open class CardBrowser :
card: Card,
): String {
var t = nextDue(col, card)
if (card.queue < 0) {
if (!card.queue.reviewable()) {
t = "($t)"
}
return t
Expand All @@ -2602,19 +2603,24 @@ 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 == 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 ""
}
date =
if (card.isInDynamicDeck) {
return AnkiDroidApp.appResources.getString(R.string.card_browser_due_filtered_card)
} else if (card.queue == QueueType.LRN) {
due.toLong()
} else if (card.queue == QueueType.NEW || card.type == CardType.NEW) {
return due.toString()
} else if (card.queue == QueueType.REV ||
card.queue == QueueType.DAY_LEARN_RELEARN ||
card.type == CardType.REV &&
!card.queue.reviewable()
) {
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
12 changes: 6 additions & 6 deletions AnkiDroid/src/main/java/com/ichi2/anki/Reviewer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ import com.ichi2.annotations.NeedsTest
import com.ichi2.libanki.Card
import com.ichi2.libanki.CardId
import com.ichi2.libanki.Collection
import com.ichi2.libanki.Consts
import com.ichi2.libanki.QueueType
import com.ichi2.libanki.sched.Counts
import com.ichi2.libanki.sched.CurrentQueueState
import com.ichi2.libanki.undoableOp
Expand Down Expand Up @@ -1089,10 +1089,10 @@ open class Reviewer :
if (ease == Ease.AGAIN && wasLeech) {
state.topCard.load(getColUnsafe)
val leechMessage: String =
if (state.topCard.queue < 0) {
resources.getString(R.string.leech_suspend_notification)
} else {
if (state.topCard.queue.reviewable()) {
resources.getString(R.string.leech_notification)
} else {
resources.getString(R.string.leech_suspend_notification)
}
showSnackbar(leechMessage, Snackbar.LENGTH_SHORT)
}
Expand Down Expand Up @@ -1617,7 +1617,7 @@ open class Reviewer :
false
} else {
getColUnsafe.db.queryScalar(
"select 1 from cards where nid = ? and id != ? and queue != " + Consts.QUEUE_TYPE_SUSPENDED + " limit 1",
"select 1 from cards where nid = ? and id != ? and queue != " + QueueType.SUSPENDED.code + " limit 1",
currentCard!!.nid,
currentCard!!.id,
) == 1
Expand All @@ -1631,7 +1631,7 @@ open class Reviewer :
false
} else {
getColUnsafe.db.queryScalar(
"select 1 from cards where nid = ? and id != ? and queue >= " + Consts.QUEUE_TYPE_NEW + " limit 1",
"select 1 from cards where nid = ? and id != ? and queue >= " + QueueType.NEW.code + " limit 1",
currentCard!!.nid,
currentCard!!.id,
) == 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ import com.ichi2.annotations.NeedsTest
import com.ichi2.libanki.Card
import com.ichi2.libanki.CardId
import com.ichi2.libanki.ChangeManager
import com.ichi2.libanki.Consts
import com.ichi2.libanki.Consts.QUEUE_TYPE_MANUALLY_BURIED
import com.ichi2.libanki.Consts.QUEUE_TYPE_SIBLING_BURIED
import com.ichi2.libanki.DeckId
import com.ichi2.libanki.NoteId
import com.ichi2.libanki.QueueType
import com.ichi2.libanki.QueueType.MANUALLY_BURIED
import com.ichi2.libanki.QueueType.SIBLING_BURIED
import com.ichi2.libanki.undoableOp
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.Job
Expand Down Expand Up @@ -539,7 +539,7 @@ class CardBrowserViewModel(
val cardIds = queryAllSelectedCardIds()

undoableOp<OpChanges> {
val wantUnsuspend = cardIds.all { getCard(it).queue == Consts.QUEUE_TYPE_SUSPENDED }
val wantUnsuspend = cardIds.all { getCard(it).queue == QueueType.SUSPENDED }
if (wantUnsuspend) {
sched.unsuspendCards(cardIds)
} else {
Expand All @@ -566,7 +566,7 @@ class CardBrowserViewModel(
}

// https://github.com/ankitects/anki/blob/074becc0cee1e9ae59be701ad6c26787f74b4594/qt/aqt/browser/browser.py#L896-L902
fun Card.isBuried(): Boolean = queue == QUEUE_TYPE_MANUALLY_BURIED || queue == QUEUE_TYPE_SIBLING_BURIED
fun Card.isBuried(): Boolean = queue == MANUALLY_BURIED || queue == SIBLING_BURIED

val cardIds = queryAllSelectedCardIds()

Expand Down
16 changes: 11 additions & 5 deletions AnkiDroid/src/main/java/com/ichi2/anki/servicelayer/NoteService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ 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
import com.ichi2.libanki.NoteTypeId
import com.ichi2.libanki.QueueType
import com.ichi2.libanki.exception.EmptyMediaException
import com.ichi2.libanki.undoableOp
import com.ichi2.utils.CollectionUtils.average
Expand Down Expand Up @@ -231,7 +231,10 @@ 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? {
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 @@ -251,7 +254,10 @@ 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? {
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 All @@ -275,7 +281,7 @@ fun Card.avgIntervalOfNote(col: Collection) = NoteService.avgInterval(col, note(
suspend fun isBuryNoteAvailable(card: Card): Boolean =
withCol {
db.queryScalar(
"select 1 from cards where nid = ? and id != ? and queue >= " + Consts.QUEUE_TYPE_NEW + " limit 1",
"select 1 from cards where nid = ? and id != ? and queue >= " + QueueType.NEW.code + " limit 1",
card.nid,
card.id,
) == 1
Expand All @@ -284,7 +290,7 @@ suspend fun isBuryNoteAvailable(card: Card): Boolean =
suspend fun isSuspendNoteAvailable(card: Card): Boolean =
withCol {
db.queryScalar(
"select 1 from cards where nid = ? and id != ? and queue != " + Consts.QUEUE_TYPE_SUSPENDED + " limit 1",
"select 1 from cards where nid = ? and id != ? and queue != " + QueueType.SUSPENDED.code + " limit 1",
card.nid,
card.id,
) == 1
Expand Down
9 changes: 3 additions & 6 deletions AnkiDroid/src/main/java/com/ichi2/libanki/Card.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import anki.decks.deckId
import anki.notes.noteId
import com.ichi2.anki.Flag
import com.ichi2.anki.utils.ext.ifZero
import com.ichi2.libanki.Consts.CardQueue
import com.ichi2.libanki.CardType
import com.ichi2.libanki.TemplateManager.TemplateRenderContext.TemplateRenderOutput
import com.ichi2.libanki.utils.LibAnkiAlias
Expand Down Expand Up @@ -78,9 +77,7 @@ open class Card : Cloneable {

var type: CardType = CardType.NEW

@get:CardQueue
@CardQueue
var queue = 0
var queue = QueueType.NEW
var due: Int = 0
var ivl = 0
var factor = 0
Expand Down Expand Up @@ -130,7 +127,7 @@ open class Card : Cloneable {
mod = card.mtimeSecs
usn = card.usn
type = CardType.fromCode(card.ctype)
queue = card.queue
queue = QueueType.fromCode(card.queue)
due = card.due
ivl = card.interval
factor = card.easeFactor
Expand All @@ -154,7 +151,7 @@ open class Card : Cloneable {
deckId = did
templateIdx = ord
ctype = type.code
queue = this@Card.queue
queue = this@Card.queue.code
due = this@Card.due
interval = ivl
easeFactor = factor
Expand Down
Loading

0 comments on commit 141681c

Please sign in to comment.