Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import com.ichi2.anki.libanki.Card
import com.ichi2.anki.libanki.CardType
import com.ichi2.anki.libanki.Collection
import com.ichi2.anki.libanki.Note
import com.ichi2.anki.libanki.NotetypeJson
import com.ichi2.anki.libanki.Notetypes
import com.ichi2.anki.libanki.QueueType
import com.ichi2.anki.testutil.addNote
Expand Down Expand Up @@ -216,6 +217,15 @@ abstract class InstrumentedTest {

val notetypes get() = col.notetypes

/**
* Returns the current default notetype for adding new cards.
*
* @see Collection.defaultNotetype
*/
@DuplicatedCode("From AnkiTest")
val Notetypes.current: NotetypeJson
get() = this.get(col.defaultsForAdding().notetypeId)!!

val Notetypes.basic
get() = byName("Basic")!!

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,12 @@ class MediaTest : InstrumentedTest() {
val file = createNonEmptyFile("fake.png")
testCol.media.addFile(file)
// add a note which references it
var f = testCol.newNote(testCol.notetypes.current())
var f = testCol.newNote(testCol.notetypes.current)
f.setField(0, "one")
f.setField(1, "<img src='fake.png'>")
testCol.addNote(f)
// and one which references a non-existent file
f = testCol.newNote(testCol.notetypes.current())
f = testCol.newNote(testCol.notetypes.current)
f.setField(0, "one")
f.setField(1, "<img src='fake2.png'>")
testCol.addNote(f)
Expand Down
11 changes: 5 additions & 6 deletions AnkiDroid/src/main/java/com/ichi2/anki/NoteEditorFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2364,9 +2364,8 @@ class NoteEditorFragment :
}

if (!getColUnsafe.config.getBool(ConfigKey.Bool.ADDING_DEFAULTS_TO_CURRENT_DECK)) {
return getColUnsafe.notetypes.current().let {
Timber.d("Adding to deck of note type, noteType: %s", it.name)
return@let it.did
return getColUnsafe.defaultsForAdding().deckId.also { deckId ->
Timber.d("Adding to configured default deck: %d", deckId)
}
}

Expand Down Expand Up @@ -2403,8 +2402,8 @@ class NoteEditorFragment :
editorNote =
if (note == null || addNote) {
getColUnsafe.run {
val notetype = notetypes.current()
Note.fromNotetypeId(this@run, notetype.id)
val notetypeId = defaultsForAdding().notetypeId
Note.fromNotetypeId(this@run, notetypeId)
}
} else {
note
Expand Down Expand Up @@ -2767,7 +2766,7 @@ class NoteEditorFragment :
}

private fun changeNoteType(newId: NoteTypeId) {
val oldNoteTypeId = getColUnsafe.notetypes.current().id
val oldNoteTypeId = getColUnsafe.defaultsForAdding().notetypeId
Timber.i("Changing note type to '%d", newId)

if (oldNoteTypeId == newId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import com.ichi2.anki.R
import com.ichi2.anki.analytics.UsageAnalytics
import com.ichi2.anki.dialogs.TtsVoicesDialogFragment
import com.ichi2.anki.launchCatchingTask
import com.ichi2.anki.libanki.Note
import com.ichi2.anki.settings.Prefs
import com.ichi2.anki.showThemedToast
import com.ichi2.anki.snackbar.showSnackbar
Expand Down Expand Up @@ -234,8 +235,9 @@ class DevOptionsFragment : SettingsFragment() {
}
withCol {
val deck = decks.addNormalDeckWithName(deckName(i))
val ntid = defaultsForAdding().notetypeId
addNote(
newNote(notetypes.current()).apply { setField(0, "$i") },
Note.fromNotetypeId(this, ntid).apply { setField(0, "$i") },
deck.id,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1312,7 +1312,7 @@ class CardContentProvider : ContentProvider() {
col: Collection,
): NoteTypeId =
if (uri.pathSegments[1] == FlashCardsContract.Model.CURRENT_MODEL_ID) {
col.notetypes.current().id
col.defaultsForAdding().notetypeId
} else {
try {
uri.pathSegments[1].toLong()
Expand Down
2 changes: 1 addition & 1 deletion AnkiDroid/src/test/java/com/ichi2/anki/ReviewerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ class ReviewerTest : RobolectricTest() {
@Throws(ConfirmModSchemaException::class)
@KotlinCleanup("use a assertNotNull which returns rather than !!")
private fun addNoteWithThreeCards() {
var notetype = col.notetypes.copy(col.notetypes.current())
var notetype = col.notetypes.copy(col.notetypes.current)
notetype.name = "Three"
col.notetypes.add(notetype)
notetype = col.notetypes.byName("Three")!!
Expand Down
22 changes: 11 additions & 11 deletions AnkiDroid/src/test/java/com/ichi2/anki/libanki/NoteTypeTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class NoteTypeTest : JvmTest() {
@Test
fun test_frontSide_field() {
// #8951 - Anki Special-cases {{FrontSide}} on the front to return empty string
val noteType = col.notetypes.current()
val noteType = col.notetypes.current
noteType.templates[0].qfmt = "{{Front}}{{FrontSide}}"
col.notetypes.save(noteType)
val note = col.newNote()
Expand All @@ -62,7 +62,7 @@ class NoteTypeTest : JvmTest() {
@Test
fun test_field_named_frontSide() {
// #8951 - A field named "FrontSide" is ignored - this matches Anki 2.1.34 (8af8f565)
val noteType = col.notetypes.current()
val noteType = col.notetypes.current

// Add a field called FrontSide and FrontSide2 (to ensure that fields are added correctly)
col.notetypes.addFieldModChanged(noteType, col.notetypes.newField("FrontSide"))
Expand Down Expand Up @@ -95,13 +95,13 @@ class NoteTypeTest : JvmTest() {
note.setItem("Back", "2")
col.addNote(note)
assertEquals(1, col.cardCount())
col.notetypes.remove(col.notetypes.current().id)
col.notetypes.remove(col.notetypes.current.id)
assertEquals(0, col.cardCount())
}

@Test
fun test_modelCopy() {
val noteType = col.notetypes.current()
val noteType = col.notetypes.current
val noteType2 = col.notetypes.copy(noteType)
assertEquals("Basic copy", noteType2.name)
assertNotEquals(noteType2.id, noteType.id)
Expand All @@ -120,7 +120,7 @@ class NoteTypeTest : JvmTest() {
note.setItem("Front", "1")
note.setItem("Back", "2")
col.addNote(note)
val noteType = col.notetypes.current()
val noteType = col.notetypes.current
// make sure renaming a field updates the templates
col.notetypes.renameFieldLegacy(noteType, noteType.fields[0], "NewFront")
assertThat(noteType.templates[0].qfmt, containsString("{{NewFront}}"))
Expand Down Expand Up @@ -228,7 +228,7 @@ class NoteTypeTest : JvmTest() {
@Test
@Throws(ConfirmModSchemaException::class)
fun test_templates() {
val noteType = col.notetypes.current()
val noteType = col.notetypes.current
var t =
Notetypes.newTemplate("Reverse").apply {
qfmt = "{{Back}}"
Expand Down Expand Up @@ -283,7 +283,7 @@ class NoteTypeTest : JvmTest() {
@Throws(ConfirmModSchemaException::class)
fun test_cloze_ordinals() {
col.notetypes.setCurrent(col.notetypes.byName("Cloze")!!)
val noteType = col.notetypes.current()
val noteType = col.notetypes.current

// We replace the default Cloze template
val t =
Expand Down Expand Up @@ -312,7 +312,7 @@ class NoteTypeTest : JvmTest() {
@Test
fun test_text() {
val noteType =
col.notetypes.current().apply {
col.notetypes.current.apply {
templates[0].qfmt = "{{text:Front}}"
}
col.notetypes.save(noteType)
Expand Down Expand Up @@ -393,7 +393,7 @@ class NoteTypeTest : JvmTest() {
@Suppress("SpellCheckingInspection") // chaine
fun test_chained_mods() {
col.notetypes.setCurrent(col.notetypes.byName("Cloze")!!)
val noteType = col.notetypes.current()
val noteType = col.notetypes.current

// We replace the default Cloze template
val t =
Expand Down Expand Up @@ -427,7 +427,7 @@ class NoteTypeTest : JvmTest() {
fun test_modelChange() {
val cloze = col.notetypes.byName("Cloze")
// enable second template and add a note
val basic = col.notetypes.current()
val basic = col.notetypes.current
val t =
Notetypes.newTemplate("Reverse").apply {
qfmt = "{{Back}}"
Expand Down Expand Up @@ -555,7 +555,7 @@ class NoteTypeTest : JvmTest() {

@Test
fun test_updateNotetype_clears_cache() {
val noteType = col.notetypes.current()
val noteType = col.notetypes.current
val originalName = noteType.name
val noteTypeId = noteType.id

Expand Down
4 changes: 3 additions & 1 deletion libanki/src/main/java/com/ichi2/anki/libanki/Notetypes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
package com.ichi2.anki.libanki

import androidx.annotation.CheckResult
import androidx.annotation.VisibleForTesting
import anki.collection.OpChanges
import anki.collection.OpChangesWithId
import anki.notetypes.ChangeNotetypeInfo
Expand Down Expand Up @@ -145,7 +146,8 @@ class Notetypes(
*/

/** Get current model.*/
@RustCleanup("Should use defaultsForAdding() instead")
@VisibleForTesting
@Deprecated("Use col.defaultsForAdding()")
fun current(forDeck: Boolean = true): NotetypeJson {
var noteType = get(col.decks.current().getLongOrNull("mid"))
if (!forDeck || noteType == null) {
Expand Down
4 changes: 2 additions & 2 deletions libanki/src/test/java/com/ichi2/anki/libanki/CardTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class CardTest : InMemoryAnkiTest() {
note.setItem("Back", "2")
col.addNote(note)
val c = note.cards()[0]
col.notetypes.current().id
col.notetypes.current.id
assertEquals(0, c.template().ord)
}

Expand All @@ -84,7 +84,7 @@ class CardTest : InMemoryAnkiTest() {
note.setItem("Back", "")
col.addNote(note)
assertEquals(1, note.numberOfCards())
val noteType = col.notetypes.current()
val noteType = col.notetypes.current
// adding a new template should automatically create cards
var t =
Notetypes.newTemplate("rev").apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class CollectionTest : InMemoryAnkiTest() {
var n = col.addNote(note)
assertEquals(1, n)
// test multiple cards - add another template
val noteType = col.notetypes.current()
val noteType = col.notetypes.current
val t = Notetypes.newTemplate("Reverse")
t.qfmt = "{{Back}}"
t.afmt = "{{Front}}"
Expand Down Expand Up @@ -189,7 +189,7 @@ class CollectionTest : InMemoryAnkiTest() {
@Test
@Ignore("Pending port of media search from Rust code")
fun test_furigana() {
val noteType = col.notetypes.current()
val noteType = col.notetypes.current
// filter should work
noteType.templates[0].qfmt = "{{kana:Front}}"
col.notetypes.save(noteType)
Expand Down
2 changes: 1 addition & 1 deletion libanki/src/test/java/com/ichi2/anki/libanki/FinderTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ class FinderTest : InMemoryAnkiTest() {
note.setItem("Back", "sheep")
col.addNote(note)
val catCard = note.cards()[0]
var noteType = col.notetypes.current()
var noteType = col.notetypes.current
noteType = col.notetypes.copy(noteType)
val t =
Notetypes.newTemplate("Reverse").apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -887,7 +887,7 @@ open class SchedulerTest : InMemoryAnkiTest() {
@Throws(Exception::class)
fun test_ordcycleV2() {
// add two more templates and set second active
val noteType = col.notetypes.current()
val noteType = col.notetypes.current
var t =
Notetypes.newTemplate("Reverse").apply {
qfmt = "{{Back}}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,14 @@ interface AnkiTest {
testBody()
}

/**
* Returns the current default notetype for adding new cards.
*
* @see Collection.defaultNotetype
*/
val Notetypes.current: NotetypeJson
get() = this.get(col.defaultsForAdding().notetypeId)!!

val Notetypes.basic
get() = byName("Basic")!!

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,5 @@ fun Collection.createBasicTypingNoteType(name: String): NotetypeJson {
* the configuration (curModel)
* @return The new note
*/
@Suppress("DEPRECATION")
fun Collection.newNote(forDeck: Boolean = true): Note = newNote(notetypes.current(forDeck))