Skip to content

Commit

Permalink
NF: replace mid with noteTypeId
Browse files Browse the repository at this point in the history
Co-authored-by: David Allison <[email protected]>
  • Loading branch information
2 people authored and BrayanDSO committed Jan 16, 2025
1 parent 50ba4e0 commit 290b43b
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -570,10 +570,10 @@ class ContentProviderTest : InstrumentedTest() {
val noteTypeUri = cr.insert(FlashCardsContract.Model.CONTENT_URI, cv)
assertNotNull("Check inserted note type isn't null", noteTypeUri)
assertNotNull("Check last path segment exists", noteTypeUri!!.lastPathSegment)
val mid = noteTypeUri.lastPathSegment!!.toLong()
val noteTypeId = noteTypeUri.lastPathSegment!!.toLong()
var col = reopenCol()
try {
var noteType = col.notetypes.get(mid)
var noteType = col.notetypes.get(noteTypeId)
assertNotNull("Check note type", noteType)
assertEquals("Check note type name", TEST_NOTE_TYPE_NAME, noteType!!.getString("name"))
assertEquals(
Expand Down Expand Up @@ -602,7 +602,7 @@ class ContentProviderTest : InstrumentedTest() {
greaterThan(0),
)
col = reopenCol()
noteType = col.notetypes.get(mid)
noteType = col.notetypes.get(noteTypeId)
assertNotNull("Check note type", noteType)
assertEquals("Check css", TEST_NOTE_TYPE_CSS, noteType!!.getString("css"))
// Update each of the templates in note type (to test updating NOTE_TYPES_ID_TEMPLATES_ID Uri)
Expand All @@ -626,7 +626,7 @@ class ContentProviderTest : InstrumentedTest() {
greaterThan(0),
)
col = reopenCol()
noteType = col.notetypes.get(mid)
noteType = col.notetypes.get(noteTypeId)
assertNotNull("Check note type", noteType)
val template = noteType!!.tmpls[i]
assertEquals(
Expand All @@ -643,7 +643,7 @@ class ContentProviderTest : InstrumentedTest() {
// Delete the note type (this will force a full-sync)
col.modSchemaNoCheck()
try {
val noteType = col.notetypes.get(mid)
val noteType = col.notetypes.get(noteTypeId)
assertNotNull("Check note type", noteType)
col.notetypes.rem(noteType!!)
} catch (e: ConfirmModSchemaException) {
Expand Down
10 changes: 5 additions & 5 deletions AnkiDroid/src/main/java/com/ichi2/anki/NoteEditor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ class NoteEditor :
NoteEditorActivityResultCallback {
// Model can change regardless of exit type - update ourselves and CardBrowser
reloadRequired = true
editorNote!!.notetype = getColUnsafe.notetypes.get(editorNote!!.mid)!!
editorNote!!.notetype = getColUnsafe.notetypes.get(editorNote!!.noteTypeId)!!
if (currentEditedCard == null ||
!editorNote!!
.cardIds(getColUnsafe)
Expand Down Expand Up @@ -1680,7 +1680,7 @@ class NoteEditor :
private suspend fun getCurrentMultimediaEditableNote(): MultimediaEditableNote {
val note = NoteService.createEmptyNote(editorNote!!.notetype)
val fields = currentFieldStrings.requireNoNulls()
withCol { NoteService.updateMultimediaNoteFromFields(this@withCol, fields, editorNote!!.mid, note) }
withCol { NoteService.updateMultimediaNoteFromFields(this@withCol, fields, editorNote!!.noteTypeId, note) }

return note
}
Expand Down Expand Up @@ -2797,9 +2797,9 @@ class NoteEditor :
fun getFieldForTest(index: Int): FieldEditText = editFields!![index]

@VisibleForTesting(otherwise = VisibleForTesting.NONE)
fun setCurrentlySelectedModel(mid: NoteTypeId) {
val position = allModelIds!!.indexOf(mid)
check(position != -1) { "$mid not found" }
fun setCurrentlySelectedModel(noteTypeId: NoteTypeId) {
val position = allModelIds!!.indexOf(noteTypeId)
check(position != -1) { "$noteTypeId not found" }
noteTypeSpinner!!.setSelection(position)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -558,15 +558,15 @@ class CardContentProvider : ContentProvider() {
}
NOTE_TYPES_ID_TEMPLATES -> throw IllegalArgumentException("Cannot update templates in bulk")
NOTE_TYPES_ID_TEMPLATES_ID -> {
val mid = values!!.getAsLong(FlashCardsContract.CardTemplate.MODEL_ID)
val noteTypeId = values!!.getAsLong(FlashCardsContract.CardTemplate.MODEL_ID)
val ord = values.getAsInteger(FlashCardsContract.CardTemplate.ORD)
val name = values.getAsString(FlashCardsContract.CardTemplate.NAME)
val qfmt = values.getAsString(FlashCardsContract.CardTemplate.QUESTION_FORMAT)
val afmt = values.getAsString(FlashCardsContract.CardTemplate.ANSWER_FORMAT)
val bqfmt = values.getAsString(FlashCardsContract.CardTemplate.BROWSER_QUESTION_FORMAT)
val bafmt = values.getAsString(FlashCardsContract.CardTemplate.BROWSER_ANSWER_FORMAT)
// Throw exception if read-only fields are included
if (mid != null || ord != null) {
if (noteTypeId != null || ord != null) {
throw IllegalArgumentException("Updates to mid or ord are not allowed")
}
// Update the noteType
Expand Down Expand Up @@ -893,8 +893,8 @@ class CardContentProvider : ContentProvider() {
noteTypes.add(newNoteType)

// Get the mid and return a URI
val mid = newNoteType.getLong("id").toString()
Uri.withAppendedPath(FlashCardsContract.Model.CONTENT_URI, mid)
val noteTypeId = newNoteType.getLong("id").toString()
Uri.withAppendedPath(FlashCardsContract.Model.CONTENT_URI, noteTypeId)
} catch (e: JSONException) {
Timber.e(e, "Could not set a field of new note type %s", noteTypeName)
null
Expand All @@ -904,10 +904,10 @@ class CardContentProvider : ContentProvider() {
NOTE_TYPES_ID_TEMPLATES -> {
run {
val notetypes: Notetypes = col.notetypes
val mid: NoteTypeId = getNoteTypeIdFromUri(uri, col)
val noteTypeId: NoteTypeId = getNoteTypeIdFromUri(uri, col)
val existingNoteType: NotetypeJson =
notetypes.get(mid)
?: throw IllegalArgumentException("note type missing: $mid")
notetypes.get(noteTypeId)
?: throw IllegalArgumentException("note type missing: $noteTypeId")
val name: String = values!!.getAsString(FlashCardsContract.CardTemplate.NAME)
val qfmt: String = values.getAsString(FlashCardsContract.CardTemplate.QUESTION_FORMAT)
val afmt: String = values.getAsString(FlashCardsContract.CardTemplate.ANSWER_FORMAT)
Expand Down Expand Up @@ -936,13 +936,13 @@ class CardContentProvider : ContentProvider() {
NOTE_TYPES_ID_FIELDS -> {
run {
val notetypes: Notetypes = col.notetypes
val mid: NoteTypeId = getNoteTypeIdFromUri(uri, col)
val noteTypeId: NoteTypeId = getNoteTypeIdFromUri(uri, col)
val existingNoteType: NotetypeJson =
notetypes.get(mid)
?: throw IllegalArgumentException("note type missing: $mid")
notetypes.get(noteTypeId)
?: throw IllegalArgumentException("note type missing: $noteTypeId")
val name: String =
values!!.getAsString(FlashCardsContract.Model.FIELD_NAME)
?: throw IllegalArgumentException("field name missing for note type: $mid")
?: throw IllegalArgumentException("field name missing for note type: $noteTypeId")
val field = notetypes.newField(name)
try {
notetypes.addFieldLegacy(existingNoteType, field)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ object NoteService {
editorNoteDst: Note,
) {
if (noteSrc is MultimediaEditableNote) {
if (noteSrc.modelId != editorNoteDst.mid) {
if (noteSrc.modelId != editorNoteDst.noteTypeId) {
throw RuntimeException("Source and Destination Note ID do not match.")
}
val totalFields: Int = noteSrc.numberOfFields
Expand Down
11 changes: 6 additions & 5 deletions AnkiDroid/src/main/java/com/ichi2/libanki/Note.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ import anki.notes.NoteFieldsCheckResponse
import com.ichi2.libanki.Consts.DEFAULT_DECK_ID
import com.ichi2.libanki.backend.model.toBackendNote
import com.ichi2.libanki.utils.NotInLibAnki
import com.ichi2.libanki.utils.set
import com.ichi2.utils.KotlinCleanup
import com.ichi2.utils.deepClone
import com.ichi2.utils.emptyStringArray
import java.util.AbstractSet
import java.util.regex.Pattern
Expand All @@ -42,8 +40,11 @@ class Note : Cloneable {
private set
lateinit var notetype: NotetypeJson

var mid: NoteTypeId = 0L
private set
val noteTypeId: NoteTypeId
get() = mid

/** for upstream compatibility, use [noteTypeId] outside libAnki */
private var mid: NoteTypeId = 0L
lateinit var tags: MutableList<String>
lateinit var fields: MutableList<String>
private var fMap: Map<String, Pair<Int, Field>>? = null
Expand Down Expand Up @@ -83,7 +84,7 @@ class Note : Cloneable {
id = note.id
guId = note.guid
mid = note.notetypeId
notetype = col.notetypes.get(mid)!! // not in libAnki
notetype = col.notetypes.get(noteTypeId)!! // not in libAnki
mod = note.mtimeSecs
usn = note.usn
// the lists in the protobuf are NOT mutable, even though they cast to MutableList
Expand Down
10 changes: 5 additions & 5 deletions AnkiDroid/src/main/java/com/ichi2/libanki/Notetypes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,12 @@ class Notetypes(
/** Get current model.*/
@RustCleanup("Should use defaultsForAdding() instead")
fun current(forDeck: Boolean = true): NotetypeJson {
var m = get(col.decks.current().getLongOrNull("mid"))
if (!forDeck || m == null) {
m = get(col.config.get("curModel") ?: 1L)
var noteType = get(col.decks.current().getLongOrNull("mid"))
if (!forDeck || noteType == null) {
noteType = get(col.config.get("curModel") ?: 1L)
}
if (m != null) {
return m
if (noteType != null) {
return noteType
}
return get(allNamesAndIds().first().id)!!
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fun Note.toBackendNote(): anki.notes.Note {
return note {
id = note.id
guid = note.guId!!
notetypeId = note.mid
notetypeId = note.noteTypeId
mtimeSecs = note.mod
usn = note.usn
tags.addAll(note.tags.asIterable())
Expand Down

0 comments on commit 290b43b

Please sign in to comment.