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 Nov 12, 2024
1 parent 0b2b4b3 commit 5a067d4
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 22 deletions.
5 changes: 4 additions & 1 deletion AnkiDroid/src/main/java/com/ichi2/libanki/Collection.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
16 changes: 7 additions & 9 deletions AnkiDroid/src/main/java/com/ichi2/libanki/Consts.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
****************************************************************************************/
package com.ichi2.libanki

import androidx.annotation.IntDef
import kotlin.annotation.Retention

object Consts {
// Queue types
enum class QueueType {
Expand Down Expand Up @@ -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

Expand Down
4 changes: 2 additions & 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,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
Expand Down Expand Up @@ -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
Expand Down
13 changes: 7 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,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
Expand Down Expand Up @@ -74,9 +76,9 @@ class NotetypeJson : JSONObject {
val templatesNames: List<String>
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
Expand Down Expand Up @@ -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)
}
}
4 changes: 2 additions & 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,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
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 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,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
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 5a067d4

Please sign in to comment.