diff --git a/AnkiDroid/src/androidTest/java/com/ichi2/anki/tests/InstrumentedTest.kt b/AnkiDroid/src/androidTest/java/com/ichi2/anki/tests/InstrumentedTest.kt index e81961a78d10..9471cc71ffeb 100644 --- a/AnkiDroid/src/androidTest/java/com/ichi2/anki/tests/InstrumentedTest.kt +++ b/AnkiDroid/src/androidTest/java/com/ichi2/anki/tests/InstrumentedTest.kt @@ -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 @@ -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")!! diff --git a/AnkiDroid/src/androidTest/java/com/ichi2/anki/tests/libanki/MediaTest.kt b/AnkiDroid/src/androidTest/java/com/ichi2/anki/tests/libanki/MediaTest.kt index 7fffa4df4928..0633b3bd2b58 100644 --- a/AnkiDroid/src/androidTest/java/com/ichi2/anki/tests/libanki/MediaTest.kt +++ b/AnkiDroid/src/androidTest/java/com/ichi2/anki/tests/libanki/MediaTest.kt @@ -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, "") 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, "") testCol.addNote(f) diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/NoteEditorFragment.kt b/AnkiDroid/src/main/java/com/ichi2/anki/NoteEditorFragment.kt index a0da43e2faca..7f7554748369 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/NoteEditorFragment.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/NoteEditorFragment.kt @@ -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) } } @@ -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 @@ -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) { diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/preferences/DevOptionsFragment.kt b/AnkiDroid/src/main/java/com/ichi2/anki/preferences/DevOptionsFragment.kt index 12490fdb93d7..71fefbbab082 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/preferences/DevOptionsFragment.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/preferences/DevOptionsFragment.kt @@ -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 @@ -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, ) } diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/provider/CardContentProvider.kt b/AnkiDroid/src/main/java/com/ichi2/anki/provider/CardContentProvider.kt index d2284acdadb2..d9058bb184ab 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/provider/CardContentProvider.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/provider/CardContentProvider.kt @@ -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() diff --git a/AnkiDroid/src/test/java/com/ichi2/anki/ReviewerTest.kt b/AnkiDroid/src/test/java/com/ichi2/anki/ReviewerTest.kt index d1d44ae53a6d..d416f56beb91 100644 --- a/AnkiDroid/src/test/java/com/ichi2/anki/ReviewerTest.kt +++ b/AnkiDroid/src/test/java/com/ichi2/anki/ReviewerTest.kt @@ -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")!! diff --git a/AnkiDroid/src/test/java/com/ichi2/anki/libanki/NoteTypeTest.kt b/AnkiDroid/src/test/java/com/ichi2/anki/libanki/NoteTypeTest.kt index 8b0c96deb341..ab08da17dce3 100644 --- a/AnkiDroid/src/test/java/com/ichi2/anki/libanki/NoteTypeTest.kt +++ b/AnkiDroid/src/test/java/com/ichi2/anki/libanki/NoteTypeTest.kt @@ -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() @@ -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")) @@ -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) @@ -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}}")) @@ -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}}" @@ -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 = @@ -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) @@ -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 = @@ -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}}" @@ -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 diff --git a/libanki/src/main/java/com/ichi2/anki/libanki/Notetypes.kt b/libanki/src/main/java/com/ichi2/anki/libanki/Notetypes.kt index 91d458add6e2..27d8ad8821d1 100644 --- a/libanki/src/main/java/com/ichi2/anki/libanki/Notetypes.kt +++ b/libanki/src/main/java/com/ichi2/anki/libanki/Notetypes.kt @@ -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 @@ -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) { diff --git a/libanki/src/test/java/com/ichi2/anki/libanki/CardTest.kt b/libanki/src/test/java/com/ichi2/anki/libanki/CardTest.kt index 33a2695192c2..91de2f49c872 100644 --- a/libanki/src/test/java/com/ichi2/anki/libanki/CardTest.kt +++ b/libanki/src/test/java/com/ichi2/anki/libanki/CardTest.kt @@ -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) } @@ -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 { diff --git a/libanki/src/test/java/com/ichi2/anki/libanki/CollectionTest.kt b/libanki/src/test/java/com/ichi2/anki/libanki/CollectionTest.kt index bc34c5165d3a..773d490c5851 100644 --- a/libanki/src/test/java/com/ichi2/anki/libanki/CollectionTest.kt +++ b/libanki/src/test/java/com/ichi2/anki/libanki/CollectionTest.kt @@ -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}}" @@ -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) diff --git a/libanki/src/test/java/com/ichi2/anki/libanki/FinderTest.kt b/libanki/src/test/java/com/ichi2/anki/libanki/FinderTest.kt index d431fa6782b4..0e668f408961 100644 --- a/libanki/src/test/java/com/ichi2/anki/libanki/FinderTest.kt +++ b/libanki/src/test/java/com/ichi2/anki/libanki/FinderTest.kt @@ -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 { diff --git a/libanki/src/test/java/com/ichi2/anki/libanki/SchedulerTest.kt b/libanki/src/test/java/com/ichi2/anki/libanki/SchedulerTest.kt index 6c0b25c3da71..56b91d31c9b5 100644 --- a/libanki/src/test/java/com/ichi2/anki/libanki/SchedulerTest.kt +++ b/libanki/src/test/java/com/ichi2/anki/libanki/SchedulerTest.kt @@ -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}}" diff --git a/libanki/testutils/src/main/java/com/ichi2/anki/libanki/testutils/AnkiTest.kt b/libanki/testutils/src/main/java/com/ichi2/anki/libanki/testutils/AnkiTest.kt index a1f2e121123e..ce3fdb1ff079 100644 --- a/libanki/testutils/src/main/java/com/ichi2/anki/libanki/testutils/AnkiTest.kt +++ b/libanki/testutils/src/main/java/com/ichi2/anki/libanki/testutils/AnkiTest.kt @@ -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")!! diff --git a/libanki/testutils/src/main/java/com/ichi2/anki/libanki/testutils/ext/Collection.kt b/libanki/testutils/src/main/java/com/ichi2/anki/libanki/testutils/ext/Collection.kt index 981ebed115f1..6224b828dc50 100644 --- a/libanki/testutils/src/main/java/com/ichi2/anki/libanki/testutils/ext/Collection.kt +++ b/libanki/testutils/src/main/java/com/ichi2/anki/libanki/testutils/ext/Collection.kt @@ -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))