diff --git a/AnkiDroid/src/main/java/com/ichi2/libanki/Collection.kt b/AnkiDroid/src/main/java/com/ichi2/libanki/Collection.kt index 1bf5fcebfb15..5d322a95a70c 100644 --- a/AnkiDroid/src/main/java/com/ichi2/libanki/Collection.kt +++ b/AnkiDroid/src/main/java/com/ichi2/libanki/Collection.kt @@ -623,7 +623,7 @@ class Collection( // invalid ords for (m in notetypes.all()) { // ignore clozes - if (m.getInt("type") != Consts.MODEL_STD) { + if (m.isCloze) { continue } // Make a list of valid ords for this model diff --git a/AnkiDroid/src/main/java/com/ichi2/libanki/Consts.kt b/AnkiDroid/src/main/java/com/ichi2/libanki/Consts.kt index 14c0717ef204..026bccd7924f 100644 --- a/AnkiDroid/src/main/java/com/ichi2/libanki/Consts.kt +++ b/AnkiDroid/src/main/java/com/ichi2/libanki/Consts.kt @@ -77,12 +77,17 @@ object Consts { annotation class DynPriority // model types - const val MODEL_STD = 0 - const val MODEL_CLOZE = 1 - - @Retention(AnnotationRetention.SOURCE) - @IntDef(MODEL_STD, MODEL_CLOZE) - annotation class ModelType + enum class NoteTypeKind( + val code: Int, + ) { + STD(0), + CLOZE(1), + ; + + companion object { + fun fromCode(code: Int) = NoteTypeKind.entries.first { it.code == code } + } + } const val STARTING_FACTOR = 2500 diff --git a/AnkiDroid/src/main/java/com/ichi2/libanki/Note.kt b/AnkiDroid/src/main/java/com/ichi2/libanki/Note.kt index 4247388ef2da..51782c2396a5 100644 --- a/AnkiDroid/src/main/java/com/ichi2/libanki/Note.kt +++ b/AnkiDroid/src/main/java/com/ichi2/libanki/Note.kt @@ -21,7 +21,6 @@ import androidx.annotation.CheckResult import androidx.annotation.VisibleForTesting import anki.notes.NoteFieldsCheckResponse import com.ichi2.libanki.Consts.DEFAULT_DECK_ID -import com.ichi2.libanki.Consts.MODEL_STD import com.ichi2.libanki.backend.model.toBackendNote import com.ichi2.libanki.utils.NotInLibAnki import com.ichi2.libanki.utils.set @@ -117,7 +116,7 @@ class Note : Cloneable { if (customTemplate != null) { customTemplate.deepClone() } else { - val index = if (model.type == MODEL_STD) ord else 0 + val index = if (model.isStd) ord else 0 model.tmpls.getJSONObject(index) } // may differ in cloze case diff --git a/AnkiDroid/src/main/java/com/ichi2/libanki/NotetypeJson.kt b/AnkiDroid/src/main/java/com/ichi2/libanki/NotetypeJson.kt index 1d2e31337b90..861a653600f7 100644 --- a/AnkiDroid/src/main/java/com/ichi2/libanki/NotetypeJson.kt +++ b/AnkiDroid/src/main/java/com/ichi2/libanki/NotetypeJson.kt @@ -17,6 +17,7 @@ package com.ichi2.libanki import androidx.annotation.CheckResult +import com.ichi2.libanki.Consts.NoteTypeKind import com.ichi2.utils.KotlinCleanup import com.ichi2.utils.deepClonedInto import com.ichi2.utils.toStringList @@ -74,9 +75,9 @@ class NotetypeJson : JSONObject { val templatesNames: List get() = getJSONArray("tmpls").toStringList("name") val isStd: Boolean - get() = getInt("type") == Consts.MODEL_STD + get() = type == NoteTypeKind.STD val isCloze: Boolean - get() = getInt("type") == Consts.MODEL_CLOZE + get() = type == NoteTypeKind.CLOZE /** * @param sfld Fields of a note of this note type @@ -132,10 +133,9 @@ class NotetypeJson : JSONObject { } // TODO: Not constrained - @Consts.ModelType - var type: Int - get() = getInt("type") + var type: NoteTypeKind + get() = NoteTypeKind.fromCode(getInt("type")) set(value) { - put("type", value) + put("type", value.code) } } diff --git a/AnkiDroid/src/main/java/com/ichi2/libanki/Notetypes.kt b/AnkiDroid/src/main/java/com/ichi2/libanki/Notetypes.kt index cf7ce87d5dcc..029a415dc972 100644 --- a/AnkiDroid/src/main/java/com/ichi2/libanki/Notetypes.kt +++ b/AnkiDroid/src/main/java/com/ichi2/libanki/Notetypes.kt @@ -40,7 +40,6 @@ import anki.notetypes.StockNotetype import com.google.protobuf.ByteString import com.ichi2.anki.CrashReportService import com.ichi2.annotations.NeedsTest -import com.ichi2.libanki.Consts.MODEL_CLOZE import com.ichi2.libanki.Utils.checksum import com.ichi2.libanki.backend.BackendUtils import com.ichi2.libanki.backend.BackendUtils.toJsonBytes @@ -606,7 +605,7 @@ class Notetypes( ): OpChanges { val fieldMap = convertLegacyMap(fmap, newModel.fieldsNames.size) val templateMap = - if (cmap.isEmpty() || noteType.type == MODEL_CLOZE || newModel.type == MODEL_CLOZE) { + if (cmap.isEmpty() || noteType.isCloze || newModel.isCloze) { listOf() } else { convertLegacyMap(cmap, newModel.templatesNames.size) diff --git a/AnkiDroid/src/test/java/com/ichi2/libanki/ModelTest.kt b/AnkiDroid/src/test/java/com/ichi2/libanki/ModelTest.kt index a59662e7b934..787d7b943cd6 100644 --- a/AnkiDroid/src/test/java/com/ichi2/libanki/ModelTest.kt +++ b/AnkiDroid/src/test/java/com/ichi2/libanki/ModelTest.kt @@ -16,7 +16,6 @@ package com.ichi2.libanki import androidx.test.ext.junit.runners.AndroidJUnit4 -import com.ichi2.libanki.Consts.MODEL_CLOZE import com.ichi2.libanki.Utils.stripHTML import com.ichi2.libanki.exception.ConfirmModSchemaException import com.ichi2.testutils.JvmTest @@ -510,7 +509,7 @@ class NotetypeTest : JvmTest() { } private fun reqSize(notetype: NotetypeJson?) { - if (notetype!!.getInt("type") == MODEL_CLOZE) { + if (notetype!!.isCloze) { return } assertEquals(