Skip to content

Commit

Permalink
refactor(libanki): rename legacy methods & fix camelCase
Browse files Browse the repository at this point in the history
It was agreed we use camelCase consistently and
 that snake_case was to be applied by `@LibAnkiAlias`

A few legacy methods would have conflicted

rename:
* addField -> addFieldLegacy
* renameField -> renameFieldLegacy
* remField -> remFieldLegacy
* moveField -> moveFieldLegacy

implementation:
search for `fun.*_` inside `libAnki`

Fixes 11582
  • Loading branch information
david-allison authored and BrayanDSO committed Aug 4, 2024
1 parent d1c63b1 commit 54cd95b
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 36 deletions.
6 changes: 3 additions & 3 deletions AnkiDroid/src/main/java/com/ichi2/anki/ModelFieldEditor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ class ModelFieldEditor : AnkiActivity(), LocaleSelectionDialogHandler {
withProgress(message = getString(R.string.model_field_editor_changing)) {
val result = withCol {
try {
notetypes.remField(notetype, noteFields.getJSONObject(currentPos))
notetypes.remFieldLegacy(notetype, noteFields.getJSONObject(currentPos))
true
} catch (e: ConfirmModSchemaException) {
// Should never be reached
Expand Down Expand Up @@ -380,7 +380,7 @@ class ModelFieldEditor : AnkiActivity(), LocaleSelectionDialogHandler {
val result = withCol {
Timber.d("doInBackgroundRepositionField")
try {
notetypes.moveField(notetype, noteFields.getJSONObject(currentPos), index)
notetypes.moveFieldLegacy(notetype, noteFields.getJSONObject(currentPos), index)
true
} catch (e: ConfirmModSchemaException) {
e.log()
Expand All @@ -404,7 +404,7 @@ class ModelFieldEditor : AnkiActivity(), LocaleSelectionDialogHandler {
val fieldLabel = fieldNameInput!!.text.toString()
.replace("[\\n\\r]".toRegex(), "")
val field = noteFields.getJSONObject(currentPos)
getColUnsafe.notetypes.renameField(notetype, field, fieldLabel)
getColUnsafe.notetypes.renameFieldLegacy(notetype, field, fieldLabel)
initialize()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -901,7 +901,7 @@ class CardContentProvider : ContentProvider() {
?: throw IllegalArgumentException("field name missing for model: $mid")
val field: JSONObject = notetypes.newField(name)
try {
notetypes.addField(existingModel, field)
notetypes.addFieldLegacy(existingModel, field)

val flds: JSONArray = existingModel.getJSONArray("flds")
return ContentUris.withAppendedId(uri, (flds.length() - 1).toLong())
Expand Down
33 changes: 18 additions & 15 deletions AnkiDroid/src/main/java/com/ichi2/libanki/Notetypes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -340,19 +340,19 @@ class Notetypes(val col: Collection) {

/** Modifies schema */
@LibAnkiAlias("add_field")
fun add_field(notetype: NotetypeJson, field: Field) {
fun addField(notetype: NotetypeJson, field: Field) {
notetype.flds.append(field)
}

/** Modifies schema. */
@LibAnkiAlias("remove_field")
fun remove_field(notetype: NotetypeJson, field: Field) {
fun removeField(notetype: NotetypeJson, field: Field) {
notetype.flds.remove(field)
}

/** Modifies schema. */
@LibAnkiAlias("reposition_field")
fun reposition_field(notetype: NotetypeJson, field: Field, idx: Int) {
fun repositionField(notetype: NotetypeJson, field: Field, idx: Int) {
val oldidx = notetype.flds.index(field).get()
if (oldidx == idx) {
return
Expand All @@ -363,7 +363,7 @@ class Notetypes(val col: Collection) {
}

@LibAnkiAlias("rename_field")
fun rename_field(notetype: NotetypeJson, field: Field, new_name: String) {
fun renameField(notetype: NotetypeJson, field: Field, new_name: String) {
assert(notetype.flds.jsonObjectIterable().contains(field))
field["name"] = new_name
}
Expand All @@ -378,26 +378,29 @@ class Notetypes(val col: Collection) {
/*
legacy
*/

fun addField(notetype: NotetypeJson, field: Field) {
add_field(notetype, field)
@RustCleanup("legacy")
fun addFieldLegacy(notetype: NotetypeJson, field: Field) {
addField(notetype, field)
if (notetype.id != 0L) {
save(notetype)
}
}

fun remField(notetype: NotetypeJson, field: Field) {
remove_field(notetype, field)
@RustCleanup("legacy")
fun remFieldLegacy(notetype: NotetypeJson, field: Field) {
removeField(notetype, field)
save(notetype)
}

fun moveField(notetype: NotetypeJson, field: Field, idx: Int) {
reposition_field(notetype, field, idx)
@RustCleanup("legacy")
fun moveFieldLegacy(notetype: NotetypeJson, field: Field, idx: Int) {
repositionField(notetype, field, idx)
save(notetype)
}

fun renameField(notetype: NotetypeJson, field: Field, newName: String) {
rename_field(notetype, field, newName)
@RustCleanup("legacy")
fun renameFieldLegacy(notetype: NotetypeJson, field: Field, newName: String) {
renameField(notetype, field, newName)
save(notetype)
}

Expand All @@ -410,7 +413,7 @@ class Notetypes(val col: Collection) {
fun addFieldInNewModel(notetype: NotetypeJson, field: JSONObject) {
Assert.that(isModelNew(notetype), "Model was assumed to be new, but is not")
try {
addField(notetype, field)
addFieldLegacy(notetype, field)
} catch (e: ConfirmModSchemaException) {
Timber.w(e, "Unexpected mod schema")
CrashReportService.sendExceptionReport(e, "addFieldInNewModel: Unexpected mod schema")
Expand All @@ -437,7 +440,7 @@ class Notetypes(val col: Collection) {
// mod is already changed, it never has to throw
// ConfirmModSchemaException.
Assert.that(col.schemaChanged(), "Mod was assumed to be already changed, but is not")
addField(notetype, field)
addFieldLegacy(notetype, field)
}

fun addTemplateModChanged(notetype: NotetypeJson, template: JSONObject) {
Expand Down
12 changes: 6 additions & 6 deletions AnkiDroid/src/test/java/com/ichi2/libanki/CardTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,11 @@ class CardTest : JvmTest() {
val models = col.notetypes
val model = models.byName("Basic")
assertNotNull(model)
models.renameField(model, model.getJSONArray("flds").getJSONObject(0), "A")
models.renameField(model, model.getJSONArray("flds").getJSONObject(1), "B")
models.renameFieldLegacy(model, model.getJSONArray("flds").getJSONObject(0), "A")
models.renameFieldLegacy(model, model.getJSONArray("flds").getJSONObject(1), "B")
val fld2 = models.newField("C")
fld2.put("ord", JSONObject.NULL)
models.addField(model, fld2)
models.addFieldLegacy(model, fld2)
val tmpls = model.getJSONArray("tmpls")
tmpls.getJSONObject(0).put("qfmt", "{{A}}{{B}}{{C}}")
// ensure first card is always generated,
Expand Down Expand Up @@ -169,11 +169,11 @@ class CardTest : JvmTest() {
val model = models.byName("Basic")
assertNotNull(model)
val tmpls = model.getJSONArray("tmpls")
models.renameField(model, model.getJSONArray("flds").getJSONObject(0), "First")
models.renameField(model, model.getJSONArray("flds").getJSONObject(1), "Front")
models.renameFieldLegacy(model, model.getJSONArray("flds").getJSONObject(0), "First")
models.renameFieldLegacy(model, model.getJSONArray("flds").getJSONObject(1), "Front")
val fld2 = models.newField("AddIfEmpty")
fld2.put("name", "AddIfEmpty")
models.addField(model, fld2)
models.addFieldLegacy(model, fld2)

// ensure first card is always generated,
// because at last one card is generated
Expand Down
20 changes: 10 additions & 10 deletions AnkiDroid/src/test/java/com/ichi2/libanki/ModelTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,15 @@ class NotetypeTest : JvmTest() {
col.addNote(note)
val m = col.notetypes.current()
// make sure renaming a field updates the templates
col.notetypes.renameField(m, m.getJSONArray("flds").getJSONObject(0), "NewFront")
col.notetypes.renameFieldLegacy(m, m.getJSONArray("flds").getJSONObject(0), "NewFront")
assertThat(
m.getJSONArray("tmpls").getJSONObject(0).getString("qfmt"),
containsString("{{NewFront}}")
)
val h = col.notetypes.scmhash(m)
// add a field
var field: JSONObject? = col.notetypes.newField("foo")
col.notetypes.addField(m, field!!)
col.notetypes.addFieldLegacy(m, field!!)
assertEquals(
listOf("1", "2", ""),
col.getNote(
Expand All @@ -151,10 +151,10 @@ class NotetypeTest : JvmTest() {
assertNotEquals(h, col.notetypes.scmhash(m))
// rename it
field = m.getJSONArray("flds").getJSONObject(2)
col.notetypes.renameField(m, field, "bar")
col.notetypes.renameFieldLegacy(m, field, "bar")
assertEquals("", col.getNote(col.notetypes.nids(m)[0]).getItem("bar"))
// delete back
col.notetypes.remField(m, m.getJSONArray("flds").getJSONObject(1))
col.notetypes.remFieldLegacy(m, m.getJSONArray("flds").getJSONObject(1))
assertEquals(
listOf("1", ""),
col.getNote(
Expand All @@ -164,7 +164,7 @@ class NotetypeTest : JvmTest() {
).fields
)
// move 0 -> 1
col.notetypes.moveField(m, m.getJSONArray("flds").getJSONObject(0), 1)
col.notetypes.moveFieldLegacy(m, m.getJSONArray("flds").getJSONObject(0), 1)
assertEquals(
listOf("", "1"),
col.getNote(
Expand All @@ -174,7 +174,7 @@ class NotetypeTest : JvmTest() {
).fields
)
// move 1 -> 0
col.notetypes.moveField(m, m.getJSONArray("flds").getJSONObject(1), 0)
col.notetypes.moveFieldLegacy(m, m.getJSONArray("flds").getJSONObject(1), 0)
assertEquals(
listOf("1", ""),
col.getNote(
Expand All @@ -185,7 +185,7 @@ class NotetypeTest : JvmTest() {
)
// add another and put in middle
field = col.notetypes.newField("baz")
col.notetypes.addField(m, field)
col.notetypes.addFieldLegacy(m, field)
note = col.getNote(col.notetypes.nids(m)[0])
note.setItem("baz", "2")
note.flush()
Expand All @@ -198,7 +198,7 @@ class NotetypeTest : JvmTest() {
).fields
)
// move 2 -> 1
col.notetypes.moveField(m, m.getJSONArray("flds").getJSONObject(2), 1)
col.notetypes.moveFieldLegacy(m, m.getJSONArray("flds").getJSONObject(2), 1)
assertEquals(
listOf("1", "2", ""),
col.getNote(
Expand All @@ -208,7 +208,7 @@ class NotetypeTest : JvmTest() {
).fields
)
// move 0 -> 2
col.notetypes.moveField(m, m.getJSONArray("flds").getJSONObject(0), 2)
col.notetypes.moveFieldLegacy(m, m.getJSONArray("flds").getJSONObject(0), 2)
assertEquals(
listOf("2", "", "1"),
col.getNote(
Expand All @@ -218,7 +218,7 @@ class NotetypeTest : JvmTest() {
).fields
)
// move 0 -> 1
col.notetypes.moveField(m, m.getJSONArray("flds").getJSONObject(0), 1)
col.notetypes.moveFieldLegacy(m, m.getJSONArray("flds").getJSONObject(0), 1)
assertEquals(
listOf("", "2", "1"),
col.getNote(
Expand Down
2 changes: 1 addition & 1 deletion AnkiDroid/src/test/java/com/ichi2/testutils/TestClass.kt
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ interface TestClass {
fun addField(notetype: NotetypeJson, name: String) {
val models = col.notetypes
try {
models.addField(notetype, models.newField(name))
models.addFieldLegacy(notetype, models.newField(name))
} catch (e: ConfirmModSchemaException) {
throw RuntimeException(e)
}
Expand Down

0 comments on commit 54cd95b

Please sign in to comment.