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 authored and lukstbit committed Jan 4, 2025
1 parent 9817aeb commit a01983b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 19 deletions.
30 changes: 22 additions & 8 deletions AnkiDroid/src/main/java/com/ichi2/libanki/Consts.kt
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,28 @@ sealed class QueueType(
}
}

// model types
sealed class NoteTypeKind(
val code: Int,
) {
object Std : NoteTypeKind(0)

object Cloze : NoteTypeKind(1)

class Unknown(
code: Int,
) : NoteTypeKind(code)

companion object {
fun fromCode(code: Int) =
when (code) {
0 -> Std
1 -> Cloze
else -> Unknown(code)
}
}
}

object Consts {
// dynamic deck order
const val DYN_OLDEST = 0
Expand All @@ -111,14 +133,6 @@ object Consts {
@IntDef(DYN_OLDEST, DYN_RANDOM, DYN_SMALLINT, DYN_BIGINT, DYN_LAPSES, DYN_ADDED, DYN_DUE, DYN_REVADDED, DYN_DUEPRIORITY)
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

const val STARTING_FACTOR = 2500

/** Only used by the dialog shown to user */
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 @@ -116,7 +115,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[index]
}
// may differ in cloze case
Expand Down
12 changes: 5 additions & 7 deletions AnkiDroid/src/main/java/com/ichi2/libanki/NotetypeJson.kt
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,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 @@ -129,12 +129,10 @@ class NotetypeJson : JSONObject {
put("sortf", value)
}

// 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)
}

/**
Expand Down
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 @@ -43,7 +43,6 @@ import anki.notetypes.restoreNotetypeToStockRequest
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.fromJsonBytes
Expand Down Expand Up @@ -600,7 +599,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

0 comments on commit a01983b

Please sign in to comment.