Skip to content

Commit

Permalink
refactor(libanki): extract & document 'CardTemplate[s]'
Browse files Browse the repository at this point in the history
similar to 095a492

A couple of 'optStr' calls remain using the underlying object
did is not handled

Co-authored-by: Arthur Milchior <[email protected]>
  • Loading branch information
david-allison and Arthur-Milchior committed Dec 30, 2024
1 parent bf52e33 commit a42ffcc
Show file tree
Hide file tree
Showing 29 changed files with 549 additions and 330 deletions.
2 changes: 2 additions & 0 deletions .idea/dictionaries/anki.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
<w>apkg</w>
<w>apkgaa</w>
<w>bafmt</w>
<w>bfont</w>
<w>bqfmt</w>
<w>bsize</w>
<w>cid</w>
<w>cids</w>
<w>cloze</w>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,9 @@ class ContentProviderTest : InstrumentedTest() {
TEST_NOTE_FIELDS.toMutableList(),
)
assertEquals("Check that tag was set correctly", TEST_TAG, addedNote.tags[0])
val noteType: JSONObject? = col.notetypes.get(noteTypeId)
val noteType: NotetypeJson? = col.notetypes.get(noteTypeId)
assertNotNull("Check note type", noteType)
val expectedNumCards = noteType!!.getJSONArray("tmpls").length()
val expectedNumCards = noteType!!.tmpls.length()
assertEquals("Check that correct number of cards generated", expectedNumCards, addedNote.numberOfCards(col))
// Now delete the note
cr.delete(newNoteUri, null, null)
Expand Down Expand Up @@ -286,7 +286,7 @@ class ContentProviderTest : InstrumentedTest() {
val noteTypeUri = ContentUris.withAppendedId(FlashCardsContract.Model.CONTENT_URI, noteTypeId)
val testIndex =
TEST_NOTE_TYPE_CARDS.size - 1 // choose the last one because not the same as the basic note type template
val expectedOrd = noteType.getJSONArray("tmpls").length()
val expectedOrd = noteType.tmpls.length()
val cv =
ContentValues().apply {
put(FlashCardsContract.CardTemplate.NAME, TEST_NOTE_TYPE_CARDS[testIndex])
Expand All @@ -308,21 +308,21 @@ class ContentProviderTest : InstrumentedTest() {
)
noteType = col.notetypes.get(noteTypeId)
assertNotNull("Check note type", noteType)
val template = noteType!!.getJSONArray("tmpls").getJSONObject(expectedOrd)
val template = noteType!!.tmpls[expectedOrd]
assertEquals(
"Check template JSONObject ord",
expectedOrd,
template.getInt("ord"),
template.ord,
)
assertEquals(
"Check template name",
TEST_NOTE_TYPE_CARDS[testIndex],
template.getString("name"),
template.name,
)
assertEquals("Check qfmt", TEST_NOTE_TYPE_QFMT[testIndex], template.getString("qfmt"))
assertEquals("Check afmt", TEST_NOTE_TYPE_AFMT[testIndex], template.getString("afmt"))
assertEquals("Check bqfmt", TEST_NOTE_TYPE_QFMT[testIndex], template.getString("bqfmt"))
assertEquals("Check bafmt", TEST_NOTE_TYPE_AFMT[testIndex], template.getString("bafmt"))
assertEquals("Check qfmt", TEST_NOTE_TYPE_QFMT[testIndex], template.qfmt)
assertEquals("Check afmt", TEST_NOTE_TYPE_AFMT[testIndex], template.afmt)
assertEquals("Check bqfmt", TEST_NOTE_TYPE_QFMT[testIndex], template.bqfmt)
assertEquals("Check bafmt", TEST_NOTE_TYPE_AFMT[testIndex], template.bafmt)
col.notetypes.rem(noteType)
}

Expand Down Expand Up @@ -576,7 +576,7 @@ class ContentProviderTest : InstrumentedTest() {
assertEquals(
"Check templates length",
TEST_NOTE_TYPE_CARDS.size,
noteType.getJSONArray("tmpls").length(),
noteType.tmpls.length(),
)
assertEquals(
"Check field length",
Expand Down Expand Up @@ -625,16 +625,16 @@ class ContentProviderTest : InstrumentedTest() {
col = reopenCol()
noteType = col.notetypes.get(mid)
assertNotNull("Check note type", noteType)
val template = noteType!!.getJSONArray("tmpls").getJSONObject(i)
val template = noteType!!.tmpls[i]
assertEquals(
"Check template name",
TEST_NOTE_TYPE_CARDS[i],
template.getString("name"),
template.name,
)
assertEquals("Check qfmt", TEST_NOTE_TYPE_QFMT[i], template.getString("qfmt"))
assertEquals("Check afmt", TEST_NOTE_TYPE_AFMT[i], template.getString("afmt"))
assertEquals("Check bqfmt", TEST_NOTE_TYPE_QFMT[i], template.getString("bqfmt"))
assertEquals("Check bafmt", TEST_NOTE_TYPE_AFMT[i], template.getString("bafmt"))
assertEquals("Check qfmt", TEST_NOTE_TYPE_QFMT[i], template.qfmt)
assertEquals("Check afmt", TEST_NOTE_TYPE_AFMT[i], template.afmt)
assertEquals("Check bqfmt", TEST_NOTE_TYPE_QFMT[i], template.bqfmt)
assertEquals("Check bafmt", TEST_NOTE_TYPE_AFMT[i], template.bafmt)
}
} finally {
// Delete the note type (this will force a full-sync)
Expand Down Expand Up @@ -1407,19 +1407,22 @@ class ContentProviderTest : InstrumentedTest() {
}
}

@KotlinCleanup("duplicate of TestClass method")
fun addNonClozeNoteType(
name: String,
fields: Array<String>,
qfmt: String?,
afmt: String?,
qfmt: String,
afmt: String,
): String {
val noteType = col.notetypes.new(name)
for (field in fields) {
col.notetypes.addFieldInNewModel(noteType, col.notetypes.newField(field))
}
val t = Notetypes.newTemplate("Card 1")
t.put("qfmt", qfmt)
t.put("afmt", afmt)
val t =
Notetypes.newTemplate("Card 1").also { t ->
t.qfmt = qfmt
t.afmt = afmt
}
col.notetypes.addTemplateInNewModel(noteType, t)
col.notetypes.add(noteType)
return name
Expand Down
11 changes: 10 additions & 1 deletion AnkiDroid/src/main/java/com/ichi2/anki/CardBrowser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2283,7 +2283,16 @@ open class CardBrowser :
CardBrowserColumn.SFLD -> card.note(col).sFld(col)
CardBrowserColumn.DECK -> col.decks.name(card.did)
CardBrowserColumn.TAGS -> card.note(col).stringTags(col)
CardBrowserColumn.CARD -> if (inCardMode) card.template(col).optString("name") else "${card.note(col).numberOfCards(col)}"
CardBrowserColumn.CARD ->
if (inCardMode) {
card
.template(
col,
).jsonObject
.optString("name")
} else {
"${card.note(col).numberOfCards(col)}"
}
CardBrowserColumn.DUE -> dueString(col, card)
CardBrowserColumn.EASE -> if (inCardMode) getEaseForCards() else getAvgEaseForNotes()
CardBrowserColumn.CHANGED -> LanguageUtil.getShortDateFormatFromS(if (inCardMode) card.mod else card.note(col).mod.toLong())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ import androidx.annotation.CheckResult
import androidx.appcompat.app.AlertDialog
import androidx.core.widget.doAfterTextChanged
import com.ichi2.anki.dialogs.DiscardChangesDialog
import com.ichi2.libanki.CardTemplate
import com.ichi2.utils.message
import com.ichi2.utils.negativeButton
import com.ichi2.utils.positiveButton
import com.ichi2.utils.show
import org.jetbrains.annotations.Contract
import org.json.JSONObject
import timber.log.Timber

/** Allows specification of the Question and Answer format of a card template in the Card Browser
Expand Down Expand Up @@ -187,9 +187,9 @@ class CardTemplateBrowserAppearanceEditor : AnkiActivity() {
val question: String = question ?: VALUE_USE_DEFAULT
val answer: String = answer ?: VALUE_USE_DEFAULT

fun applyTo(template: JSONObject) {
template.put("bqfmt", question)
template.put("bafmt", answer)
fun applyTo(template: CardTemplate) {
template.bqfmt = question
template.bafmt = answer
}

companion object {
Expand Down Expand Up @@ -220,12 +220,8 @@ class CardTemplateBrowserAppearanceEditor : AnkiActivity() {
@CheckResult
fun getIntentFromTemplate(
context: Context,
template: JSONObject,
): Intent {
val browserQuestionTemplate = template.getString("bqfmt")
val browserAnswerTemplate = template.getString("bafmt")
return getIntent(context, browserQuestionTemplate, browserAnswerTemplate)
}
template: CardTemplate,
): Intent = getIntent(context, template.bqfmt, template.bafmt)

@CheckResult
fun getIntent(
Expand Down
Loading

0 comments on commit a42ffcc

Please sign in to comment.