Skip to content

Commit

Permalink
NF: ModelType is an enum
Browse files Browse the repository at this point in the history
Improve typing
  • Loading branch information
Arthur-Milchior committed Dec 12, 2024
1 parent c6f1b00 commit 74c7155
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 19 deletions.
2 changes: 1 addition & 1 deletion AnkiDroid/src/main/java/com/ichi2/libanki/Collection.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 11 additions & 6 deletions AnkiDroid/src/main/java/com/ichi2/libanki/Consts.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 1 addition & 2 deletions AnkiDroid/src/main/java/com/ichi2/libanki/Note.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions AnkiDroid/src/main/java/com/ichi2/libanki/NotetypeJson.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -74,9 +75,9 @@ class NotetypeJson : JSONObject {
val templatesNames: List<String>
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
Expand Down Expand Up @@ -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)
}
}
3 changes: 1 addition & 2 deletions AnkiDroid/src/main/java/com/ichi2/libanki/Notetypes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions AnkiDroid/src/test/java/com/ichi2/libanki/ModelTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -510,7 +509,7 @@ class NotetypeTest : JvmTest() {
}

private fun reqSize(notetype: NotetypeJson?) {
if (notetype!!.getInt("type") == MODEL_CLOZE) {
if (notetype!!.isCloze) {
return
}
assertEquals(
Expand Down

0 comments on commit 74c7155

Please sign in to comment.