Skip to content

Commit 83ce5b3

Browse files
david-allisonmikehardy
authored andcommitted
refactor(libanki): inline unnecessary methods
Since we moved to Kotlin, we no longer need to worry about `@throws`, therefore can inline: * addTemplateInNewNoteType -> addTemplate * addFieldInNewNoteType -> addFieldLegacy Removes a cyclic dependency: libAnki <-> AnkiDroid Issue 18015
1 parent 4d68684 commit 83ce5b3

File tree

4 files changed

+6
-46
lines changed

4 files changed

+6
-46
lines changed

AnkiDroid/src/androidTest/java/com/ichi2/anki/tests/ContentProviderTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1554,14 +1554,14 @@ class ContentProviderTest : InstrumentedTest() {
15541554
): String {
15551555
val noteType = col.notetypes.new(name)
15561556
for (field in fields) {
1557-
col.notetypes.addFieldInNewNoteType(noteType, col.notetypes.newField(field))
1557+
col.notetypes.addFieldLegacy(noteType, col.notetypes.newField(field))
15581558
}
15591559
val t =
15601560
Notetypes.newTemplate("Card 1").also { t ->
15611561
t.qfmt = qfmt
15621562
t.afmt = afmt
15631563
}
1564-
col.notetypes.addTemplateInNewNoteType(noteType, t)
1564+
col.notetypes.addTemplate(noteType, t)
15651565
col.notetypes.add(noteType)
15661566
return name
15671567
}

AnkiDroid/src/main/java/com/ichi2/anki/provider/CardContentProvider.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -852,7 +852,7 @@ class CardContentProvider : ContentProvider() {
852852
// Add the fields
853853
val allFields = Utils.splitFields(fieldNames)
854854
for (f: String? in allFields) {
855-
col.notetypes.addFieldInNewNoteType(newNoteType, col.notetypes.newField(f!!))
855+
col.notetypes.addFieldLegacy(newNoteType, col.notetypes.newField(f!!))
856856
}
857857
// Add some empty card templates
858858
var idx = 0
@@ -865,7 +865,7 @@ class CardContentProvider : ContentProvider() {
865865
answerField = allFields[1]
866866
}
867867
t.afmt = "{{FrontSide}}\\n\\n<hr id=answer>\\n\\n{{$answerField}}"
868-
col.notetypes.addTemplateInNewNoteType(newNoteType, t)
868+
col.notetypes.addTemplate(newNoteType, t)
869869
idx++
870870
}
871871
// Add the CSS if specified

AnkiDroid/src/main/java/com/ichi2/libanki/Notetypes.kt

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,12 @@ import anki.notetypes.NotetypeNameIdUseCount
4141
import anki.notetypes.StockNotetype
4242
import anki.notetypes.restoreNotetypeToStockRequest
4343
import com.google.protobuf.ByteString
44-
import com.ichi2.anki.CrashReportService
4544
import com.ichi2.anki.common.annotations.NeedsTest
4645
import com.ichi2.anki.common.time.TimeManager
4746
import com.ichi2.libanki.Utils.checksum
4847
import com.ichi2.libanki.backend.BackendUtils
4948
import com.ichi2.libanki.backend.BackendUtils.fromJsonBytes
5049
import com.ichi2.libanki.backend.BackendUtils.toJsonBytes
51-
import com.ichi2.libanki.exception.ConfirmModSchemaException
5250
import com.ichi2.libanki.utils.LibAnkiAlias
5351
import com.ichi2.libanki.utils.NotInLibAnki
5452
import com.ichi2.libanki.utils.append
@@ -424,43 +422,6 @@ class Notetypes(
424422
save(notetype)
425423
}
426424

427-
/**
428-
* similar to Anki's addField; but thanks to assumption that
429-
* model is new, it never has to throw
430-
* [ConfirmModSchemaException]
431-
*/
432-
@RustCleanup("Since Kotlin doesn't have throws, this may not be needed")
433-
fun addFieldInNewNoteType(
434-
notetype: NotetypeJson,
435-
field: Field,
436-
) {
437-
check(isModelNew(notetype)) { "Model was assumed to be new, but is not" }
438-
try {
439-
addFieldLegacy(notetype, field)
440-
} catch (e: ConfirmModSchemaException) {
441-
Timber.w(e, "Unexpected mod schema")
442-
CrashReportService.sendExceptionReport(e, "addFieldInNewModel: Unexpected mod schema")
443-
throw IllegalStateException("ConfirmModSchemaException should not be thrown", e)
444-
}
445-
}
446-
447-
fun addTemplateInNewNoteType(
448-
notetype: NotetypeJson,
449-
template: CardTemplate,
450-
) {
451-
// similar to addTemplate, but doesn't throw exception;
452-
// asserting the model is new.
453-
check(isModelNew(notetype)) { "Model was assumed to be new, but is not" }
454-
455-
try {
456-
addTemplate(notetype, template)
457-
} catch (e: ConfirmModSchemaException) {
458-
Timber.w(e, "Unexpected mod schema")
459-
CrashReportService.sendExceptionReport(e, "addTemplateInNewModel: Unexpected mod schema")
460-
throw IllegalStateException("ConfirmModSchemaException should not be thrown", e)
461-
}
462-
}
463-
464425
fun addFieldModChanged(
465426
notetype: NotetypeJson,
466427
field: Field,

AnkiDroid/src/test/java/com/ichi2/testutils/TestClass.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import com.ichi2.libanki.NotetypeJson
3232
import com.ichi2.libanki.Notetypes
3333
import com.ichi2.libanki.QueueType
3434
import com.ichi2.libanki.exception.ConfirmModSchemaException
35-
import com.ichi2.libanki.utils.set
3635
import com.ichi2.testutils.ext.addNote
3736
import com.ichi2.utils.LanguageUtil
3837
import kotlinx.coroutines.Dispatchers
@@ -123,14 +122,14 @@ interface TestClass {
123122
): String {
124123
val noteType = col.notetypes.new(name)
125124
for (field in fields) {
126-
col.notetypes.addFieldInNewNoteType(noteType, col.notetypes.newField(field))
125+
col.notetypes.addFieldLegacy(noteType, col.notetypes.newField(field))
127126
}
128127
val t =
129128
Notetypes.newTemplate("Card 1").also { tmpl ->
130129
tmpl.qfmt = qfmt
131130
tmpl.afmt = afmt
132131
}
133-
col.notetypes.addTemplateInNewNoteType(noteType, t)
132+
col.notetypes.addTemplate(noteType, t)
134133
col.notetypes.add(noteType)
135134
return name
136135
}

0 commit comments

Comments
 (0)