From 5a067d4806aaf5202802d1632c203166e4877935 Mon Sep 17 00:00:00 2001 From: Arthur Milchior Date: Sun, 3 Nov 2024 23:24:54 +0100 Subject: [PATCH] NF: ModelType is an enum Improve typing --- .../main/java/com/ichi2/libanki/Collection.kt | 5 ++++- .../src/main/java/com/ichi2/libanki/Consts.kt | 16 +++++++--------- .../src/main/java/com/ichi2/libanki/Note.kt | 4 ++-- .../main/java/com/ichi2/libanki/NotetypeJson.kt | 13 +++++++------ .../src/main/java/com/ichi2/libanki/Notetypes.kt | 4 ++-- .../src/test/java/com/ichi2/libanki/ModelTest.kt | 5 +++-- 6 files changed, 25 insertions(+), 22 deletions(-) diff --git a/AnkiDroid/src/main/java/com/ichi2/libanki/Collection.kt b/AnkiDroid/src/main/java/com/ichi2/libanki/Collection.kt index 1bb81525ccc5..952ce7f74a2d 100644 --- a/AnkiDroid/src/main/java/com/ichi2/libanki/Collection.kt +++ b/AnkiDroid/src/main/java/com/ichi2/libanki/Collection.kt @@ -40,6 +40,9 @@ import anki.search.BrowserRow import anki.search.SearchNode import anki.sync.SyncAuth import anki.sync.SyncStatusResponse +import com.ichi2.anki.Flag +import com.ichi2.libanki.Consts.ModelType +import com.ichi2.libanki.Consts.ModelType.Companion.toModelType import com.ichi2.libanki.Utils.ids2str import com.ichi2.libanki.backend.model.toBackendNote import com.ichi2.libanki.backend.model.toProtoBuf @@ -611,7 +614,7 @@ class Collection( // invalid ords for (m in notetypes.all()) { // ignore clozes - if (m.getInt("type") != Consts.MODEL_STD) { + if (m.getInt("type").toModelType() == ModelType.CLOZE) { 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 be5516dfe37d..a3172f0fb387 100644 --- a/AnkiDroid/src/main/java/com/ichi2/libanki/Consts.kt +++ b/AnkiDroid/src/main/java/com/ichi2/libanki/Consts.kt @@ -15,9 +15,6 @@ ****************************************************************************************/ package com.ichi2.libanki -import androidx.annotation.IntDef -import kotlin.annotation.Retention - object Consts { // Queue types enum class QueueType { @@ -78,12 +75,13 @@ object Consts { } // 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 ModelType { + STD, + CLOZE; + companion object { + fun Int.toModelType() = ModelType.entries[this] + } + } 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 448dab754d63..398acfdda5d6 100644 --- a/AnkiDroid/src/main/java/com/ichi2/libanki/Note.kt +++ b/AnkiDroid/src/main/java/com/ichi2/libanki/Note.kt @@ -21,7 +21,7 @@ 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.Consts.ModelType import com.ichi2.libanki.backend.model.toBackendNote import com.ichi2.libanki.utils.NotInLibAnki import com.ichi2.libanki.utils.set @@ -117,7 +117,7 @@ class Note : Cloneable { val template = if (customTemplate != null) { customTemplate.deepClone() } else { - val index = if (model.type == MODEL_STD) ord else 0 + val index = if (model.type == ModelType.STD) 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 ad47812be4ed..4d1c8a146a37 100644 --- a/AnkiDroid/src/main/java/com/ichi2/libanki/NotetypeJson.kt +++ b/AnkiDroid/src/main/java/com/ichi2/libanki/NotetypeJson.kt @@ -17,6 +17,8 @@ package com.ichi2.libanki import androidx.annotation.CheckResult +import com.ichi2.libanki.Consts.ModelType +import com.ichi2.libanki.Consts.ModelType.Companion.toModelType import com.ichi2.utils.KotlinCleanup import com.ichi2.utils.deepClonedInto import com.ichi2.utils.toStringList @@ -74,9 +76,9 @@ class NotetypeJson : JSONObject { val templatesNames: List get() = getJSONArray("tmpls").toStringList("name") val isStd: Boolean - get() = getInt("type") == Consts.MODEL_STD + get() = getInt("type").toModelType() == ModelType.STD val isCloze: Boolean - get() = getInt("type") == Consts.MODEL_CLOZE + get() = getInt("type").toModelType() == ModelType.CLOZE /** * @param sfld Fields of a note of this note type @@ -131,10 +133,9 @@ class NotetypeJson : JSONObject { } // TODO: Not constrained - @Consts.ModelType - var type: Int - get() = getInt("type") + var type: ModelType + get() = getInt("type").toModelType() set(value) { - put("type", value) + put("type", value.ordinal) } } diff --git a/AnkiDroid/src/main/java/com/ichi2/libanki/Notetypes.kt b/AnkiDroid/src/main/java/com/ichi2/libanki/Notetypes.kt index f3f16fa1f155..ce153f003d23 100644 --- a/AnkiDroid/src/main/java/com/ichi2/libanki/Notetypes.kt +++ b/AnkiDroid/src/main/java/com/ichi2/libanki/Notetypes.kt @@ -40,7 +40,7 @@ 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.Consts.ModelType.CLOZE import com.ichi2.libanki.Utils.checksum import com.ichi2.libanki.backend.BackendUtils import com.ichi2.libanki.backend.BackendUtils.toJsonBytes @@ -544,7 +544,7 @@ class Notetypes(val col: Collection) { ): 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.type == CLOZE || newModel.type == CLOZE) { 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 4a1045d34c83..dd8eb67468df 100644 --- a/AnkiDroid/src/test/java/com/ichi2/libanki/ModelTest.kt +++ b/AnkiDroid/src/test/java/com/ichi2/libanki/ModelTest.kt @@ -16,7 +16,8 @@ package com.ichi2.libanki import androidx.test.ext.junit.runners.AndroidJUnit4 -import com.ichi2.libanki.Consts.MODEL_CLOZE +import com.ichi2.libanki.Consts.ModelType.CLOZE +import com.ichi2.libanki.Consts.ModelType.Companion.toModelType import com.ichi2.libanki.Utils.stripHTML import com.ichi2.libanki.exception.ConfirmModSchemaException import com.ichi2.testutils.JvmTest @@ -506,7 +507,7 @@ class NotetypeTest : JvmTest() { } private fun reqSize(notetype: NotetypeJson?) { - if (notetype!!.getInt("type") == MODEL_CLOZE) { + if (notetype!!.getInt("type").toModelType() == CLOZE) { return } assertEquals(