Skip to content

Commit df5f1bf

Browse files
Arthur-Milchiordavid-allison
authored andcommitted
NF: remove variables which are simply col.notetypes
David noted that `val noteTypes = col.notetypes` is useless in #18113. I find it simpler to just go over all of them and replace them instead of having to do it everywhere. This is low priority, don't review if you don't have time for it
1 parent 96c6abb commit df5f1bf

File tree

8 files changed

+63
-89
lines changed

8 files changed

+63
-89
lines changed

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

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -261,11 +261,10 @@ class CardContentProvider : ContentProvider() {
261261
rv
262262
}
263263
NOTE_TYPES -> {
264-
val noteTypes = col.notetypes
265264
val columns = projection ?: FlashCardsContract.Model.DEFAULT_PROJECTION
266265
val rv = MatrixCursor(columns, 1)
267-
for (noteTypeId: NoteTypeId in noteTypes.ids()) {
268-
addNoteTypeToCursor(noteTypeId, noteTypes, rv, columns)
266+
for (noteTypeId: NoteTypeId in col.notetypes.ids()) {
267+
addNoteTypeToCursor(noteTypeId, col.notetypes, rv, columns)
269268
}
270269
rv
271270
}
@@ -278,13 +277,12 @@ class CardContentProvider : ContentProvider() {
278277
}
279278
NOTE_TYPES_ID_TEMPLATES -> {
280279
// Direct access note type templates
281-
val noteTypes = col.notetypes
282-
val currentNoteType = noteTypes.get(getNoteTypeIdFromUri(uri, col))
280+
val currentNoteType = col.notetypes.get(getNoteTypeIdFromUri(uri, col))
283281
val columns = projection ?: FlashCardsContract.CardTemplate.DEFAULT_PROJECTION
284282
val rv = MatrixCursor(columns, 1)
285283
try {
286284
for ((idx, template) in currentNoteType!!.tmpls.withIndex()) {
287-
addTemplateToCursor(template, currentNoteType, idx + 1, noteTypes, rv, columns)
285+
addTemplateToCursor(template, currentNoteType, idx + 1, col.notetypes, rv, columns)
288286
}
289287
} catch (e: JSONException) {
290288
throw IllegalArgumentException("Note type is malformed", e)
@@ -293,14 +291,13 @@ class CardContentProvider : ContentProvider() {
293291
}
294292
NOTE_TYPES_ID_TEMPLATES_ID -> {
295293
// Direct access note type template with specific ID
296-
val noteTypes = col.notetypes
297294
val ord = uri.lastPathSegment!!.toInt()
298-
val currentNoteType = noteTypes.get(getNoteTypeIdFromUri(uri, col))
295+
val currentNoteType = col.notetypes.get(getNoteTypeIdFromUri(uri, col))
299296
val columns = projection ?: FlashCardsContract.CardTemplate.DEFAULT_PROJECTION
300297
val rv = MatrixCursor(columns, 1)
301298
try {
302299
val template = getTemplateFromUri(uri, col)
303-
addTemplateToCursor(template, currentNoteType, ord + 1, noteTypes, rv, columns)
300+
addTemplateToCursor(template, currentNoteType, ord + 1, col.notetypes, rv, columns)
304301
} catch (e: JSONException) {
305302
throw IllegalArgumentException("Note type is malformed", e)
306303
}
@@ -848,13 +845,12 @@ class CardContentProvider : ContentProvider() {
848845
throw IllegalArgumentException("Cannot set a filtered deck as default deck for a note type")
849846
}
850847
// Create a new note type
851-
val noteTypes = col.notetypes
852-
val newNoteType = noteTypes.new(noteTypeName)
848+
val newNoteType = col.notetypes.new(noteTypeName)
853849
return try {
854850
// Add the fields
855851
val allFields = Utils.splitFields(fieldNames)
856852
for (f: String? in allFields) {
857-
noteTypes.addFieldInNewNoteType(newNoteType, noteTypes.newField(f!!))
853+
col.notetypes.addFieldInNewNoteType(newNoteType, col.notetypes.newField(f!!))
858854
}
859855
// Add some empty card templates
860856
var idx = 0
@@ -867,7 +863,7 @@ class CardContentProvider : ContentProvider() {
867863
answerField = allFields[1]
868864
}
869865
t.afmt = "{{FrontSide}}\\n\\n<hr id=answer>\\n\\n{{$answerField}}"
870-
noteTypes.addTemplateInNewNoteType(newNoteType, t)
866+
col.notetypes.addTemplateInNewNoteType(newNoteType, t)
871867
idx++
872868
}
873869
// Add the CSS if specified
@@ -891,7 +887,7 @@ class CardContentProvider : ContentProvider() {
891887
newNoteType.put("latexPre", latexPre)
892888
}
893889
// Add the note type to collection (from this point on edits will require a full-sync)
894-
noteTypes.add(newNoteType)
890+
col.notetypes.add(newNoteType)
895891

896892
// Get the mid and return a URI
897893
val noteTypeId = newNoteType.getLong("id").toString()
@@ -904,10 +900,9 @@ class CardContentProvider : ContentProvider() {
904900
NOTE_TYPES_ID -> throw IllegalArgumentException("Not possible to insert note type with specific ID")
905901
NOTE_TYPES_ID_TEMPLATES -> {
906902
run {
907-
val notetypes: Notetypes = col.notetypes
908903
val noteTypeId: NoteTypeId = getNoteTypeIdFromUri(uri, col)
909904
val existingNoteType: NotetypeJson =
910-
notetypes.get(noteTypeId)
905+
col.notetypes.get(noteTypeId)
911906
?: throw IllegalArgumentException("note type missing: $noteTypeId")
912907
val name: String = values!!.getAsString(FlashCardsContract.CardTemplate.NAME)
913908
val qfmt: String = values.getAsString(FlashCardsContract.CardTemplate.QUESTION_FORMAT)
@@ -922,8 +917,8 @@ class CardContentProvider : ContentProvider() {
922917
tmpl.bqfmt = bqfmt
923918
tmpl.bafmt = bafmt
924919
}
925-
notetypes.addTemplate(existingNoteType, t)
926-
notetypes.update(existingNoteType)
920+
col.notetypes.addTemplate(existingNoteType, t)
921+
col.notetypes.update(existingNoteType)
927922
t = existingNoteType.tmpls.last()
928923
return ContentUris.withAppendedId(uri, t.ord.toLong())
929924
} catch (e: ConfirmModSchemaException) {
@@ -936,17 +931,16 @@ class CardContentProvider : ContentProvider() {
936931
NOTE_TYPES_ID_TEMPLATES_ID -> throw IllegalArgumentException("Not possible to insert template with specific ORD")
937932
NOTE_TYPES_ID_FIELDS -> {
938933
run {
939-
val notetypes: Notetypes = col.notetypes
940934
val noteTypeId: NoteTypeId = getNoteTypeIdFromUri(uri, col)
941935
val existingNoteType: NotetypeJson =
942-
notetypes.get(noteTypeId)
936+
col.notetypes.get(noteTypeId)
943937
?: throw IllegalArgumentException("note type missing: $noteTypeId")
944938
val name: String =
945939
values!!.getAsString(FlashCardsContract.Model.FIELD_NAME)
946940
?: throw IllegalArgumentException("field name missing for note type: $noteTypeId")
947-
val field = notetypes.newField(name)
941+
val field = col.notetypes.newField(name)
948942
try {
949-
notetypes.addFieldLegacy(existingNoteType, field)
943+
col.notetypes.addFieldLegacy(existingNoteType, field)
950944

951945
val flds: JSONArray = existingNoteType.getJSONArray("flds")
952946
return ContentUris.withAppendedId(uri, (flds.length() - 1).toLong())

AnkiDroid/src/test/java/com/ichi2/anki/AnkiDroidJsAPITest.kt

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,8 @@ class AnkiDroidJsAPITest : RobolectricTest() {
4040
@Test
4141
fun ankiGetNextTimeTest() =
4242
runTest {
43-
val models = col.notetypes
4443
val didA = addDeck("Test", setAsSelected = true)
45-
val basic = models.byName(BASIC_MODEL_NAME)
44+
val basic = col.notetypes.byName(BASIC_MODEL_NAME)
4645
basic!!.put("did", didA)
4746
addBasicNote("foo", "bar")
4847

@@ -74,9 +73,8 @@ class AnkiDroidJsAPITest : RobolectricTest() {
7473
@Test
7574
fun ankiTestCurrentCard() =
7675
runTest {
77-
val models = col.notetypes
7876
val didA = addDeck("Test", setAsSelected = true)
79-
val basic = models.byName(BASIC_MODEL_NAME)
77+
val basic = col.notetypes.byName(BASIC_MODEL_NAME)
8078
basic!!.put("did", didA)
8179
addBasicNote("foo", "bar")
8280

@@ -185,9 +183,8 @@ class AnkiDroidJsAPITest : RobolectricTest() {
185183
@Test
186184
fun ankiJsUiTest() =
187185
runTest {
188-
val models = col.notetypes
189186
val didA = addDeck("Test", setAsSelected = true)
190-
val basic = models.byName(BASIC_MODEL_NAME)
187+
val basic = col.notetypes.byName(BASIC_MODEL_NAME)
191188
basic!!.put("did", didA)
192189
addBasicNote("foo", "bar")
193190

@@ -228,9 +225,8 @@ class AnkiDroidJsAPITest : RobolectricTest() {
228225
fun ankiMarkAndFlagCardTest() =
229226
runTest {
230227
// js api test for marking and flagging card
231-
val models = col.notetypes
232228
val didA = addDeck("Test", setAsSelected = true)
233-
val basic = models.byName(BASIC_MODEL_NAME)
229+
val basic = col.notetypes.byName(BASIC_MODEL_NAME)
234230
basic!!.put("did", didA)
235231
addBasicNote("foo", "bar")
236232

@@ -289,9 +285,8 @@ class AnkiDroidJsAPITest : RobolectricTest() {
289285
// add five notes, four will be buried and suspended
290286
// count number of notes, if buried or suspended then
291287
// in scheduling the count will be less than previous scheduling
292-
val models = col.notetypes
293288
val didA = addDeck("Test", setAsSelected = true)
294-
val basic = models.byName(BASIC_MODEL_NAME)
289+
val basic = col.notetypes.byName(BASIC_MODEL_NAME)
295290
basic!!.put("did", didA)
296291
addBasicNote("foo", "bar")
297292
addBasicNote("baz", "bak")
@@ -358,9 +353,8 @@ class AnkiDroidJsAPITest : RobolectricTest() {
358353
fun ankiSetCardDueTest() =
359354
runTest {
360355
TimeManager.reset()
361-
val models = col.notetypes
362356
val didA = addDeck("Test", setAsSelected = true)
363-
val basic = models.byName(BASIC_MODEL_NAME)
357+
val basic = col.notetypes.byName(BASIC_MODEL_NAME)
364358
basic!!.put("did", didA)
365359
addBasicNote("foo", "bar")
366360
addBasicNote("baz", "bak")

AnkiDroid/src/test/java/com/ichi2/libanki/CardTest.kt

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -69,22 +69,21 @@ class CardTest : JvmTest() {
6969
col.addNote(note)
7070
assertEquals(1, note.numberOfCards())
7171
val noteType = col.notetypes.current()
72-
val noteTypes = col.notetypes
7372
// adding a new template should automatically create cards
7473
var t =
7574
Notetypes.newTemplate("rev").apply {
7675
qfmt = "{{Front}}1"
7776
afmt = ""
7877
}
79-
noteTypes.addTemplateModChanged(noteType, t)
80-
noteTypes.save(noteType)
78+
col.notetypes.addTemplateModChanged(noteType, t)
79+
col.notetypes.save(noteType)
8180
assertEquals(2, note.numberOfCards())
8281
// if the template is changed to remove cards, they'll be removed
8382
t =
8483
noteType.tmpls[1].apply {
8584
qfmt = "{{Back}}"
8685
}
87-
noteTypes.save(noteType)
86+
col.notetypes.save(noteType)
8887
val rep = col.emptyCids()
8988
col.removeCardsAndOrphanedNotes(rep)
9089
assertEquals(1, note.numberOfCards())
@@ -121,22 +120,21 @@ class CardTest : JvmTest() {
121120
@Test
122121
@Throws(ConfirmModSchemaException::class)
123122
fun test_gen_or() {
124-
val models = col.notetypes
125-
val model = models.byName("Basic")
123+
val model = col.notetypes.byName("Basic")
126124
assertNotNull(model)
127-
models.renameFieldLegacy(model, model.flds[0], "A")
128-
models.renameFieldLegacy(model, model.flds[1], "B")
129-
val fld2 = models.newField("C")
125+
col.notetypes.renameFieldLegacy(model, model.flds[0], "A")
126+
col.notetypes.renameFieldLegacy(model, model.flds[1], "B")
127+
val fld2 = col.notetypes.newField("C")
130128
fld2.setOrd(null)
131-
models.addFieldLegacy(model, fld2)
129+
col.notetypes.addFieldLegacy(model, fld2)
132130
model.tmpls[0].qfmt = "{{A}}{{B}}{{C}}"
133131
// ensure first card is always generated,
134132
// because at last one card is generated
135133
val tmpl = Notetypes.newTemplate("AND_OR")
136134
tmpl.qfmt = " {{A}} {{#B}} {{#C}} {{B}} {{/C}} {{/B}}"
137-
models.addTemplate(model, tmpl)
138-
models.save(model)
139-
models.setCurrent(model)
135+
col.notetypes.addTemplate(model, tmpl)
136+
col.notetypes.save(model)
137+
col.notetypes.setCurrent(model)
140138
var note = col.newNote()
141139
note.setItem("A", "foo")
142140
col.addNote(note)
@@ -169,24 +167,23 @@ class CardTest : JvmTest() {
169167
@Test
170168
@Throws(ConfirmModSchemaException::class)
171169
fun test_gen_not() {
172-
val models = col.notetypes
173-
val model = models.byName("Basic")
170+
val model = col.notetypes.byName("Basic")
174171
assertNotNull(model)
175172
val tmpls = model.tmpls
176-
models.renameFieldLegacy(model, model.flds[0], "First")
177-
models.renameFieldLegacy(model, model.flds[1], "Front")
178-
val fld2 = models.newField("AddIfEmpty")
173+
col.notetypes.renameFieldLegacy(model, model.flds[0], "First")
174+
col.notetypes.renameFieldLegacy(model, model.flds[1], "Front")
175+
val fld2 = col.notetypes.newField("AddIfEmpty")
179176
fld2.name = "AddIfEmpty"
180-
models.addFieldLegacy(model, fld2)
177+
col.notetypes.addFieldLegacy(model, fld2)
181178

182179
// ensure first card is always generated,
183180
// because at last one card is generated
184181
tmpls[0].qfmt = "{{AddIfEmpty}}{{Front}}{{First}}"
185182
val tmpl = Notetypes.newTemplate("NOT")
186183
tmpl.qfmt = " {{^AddIfEmpty}} {{Front}} {{/AddIfEmpty}} "
187-
models.addTemplate(model, tmpl)
188-
models.save(model)
189-
models.setCurrent(model)
184+
col.notetypes.addTemplate(model, tmpl)
185+
col.notetypes.save(model)
186+
col.notetypes.setCurrent(model)
190187
var note = col.newNote()
191188
note.setItem("First", "foo")
192189
note.setItem("AddIfEmpty", "foo")

AnkiDroid/src/test/java/com/ichi2/libanki/CollectionTest.kt

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,11 @@ class CollectionTest : JvmTest() {
112112
assertEquals(1, n)
113113
// test multiple cards - add another template
114114
val noteType = col.notetypes.current()
115-
val noteTypes = col.notetypes
116115
val t = Notetypes.newTemplate("Reverse")
117116
t.qfmt = "{{Back}}"
118117
t.afmt = "{{Front}}"
119-
noteTypes.addTemplateModChanged(noteType, t)
120-
noteTypes.save(noteType)
118+
col.notetypes.addTemplateModChanged(noteType, t)
119+
col.notetypes.save(noteType)
121120
assertEquals(2, col.cardCount())
122121
// creating new notes should use both cards
123122
note = col.newNote()
@@ -192,11 +191,10 @@ class CollectionTest : JvmTest() {
192191
@Test
193192
@Ignore("Pending port of media search from Rust code")
194193
fun test_furigana() {
195-
val noteTypes = col.notetypes
196-
val noteType = noteTypes.current()
194+
val noteType = col.notetypes.current()
197195
// filter should work
198196
noteType.tmpls[0].qfmt = "{{kana:Front}}"
199-
noteTypes.save(noteType)
197+
col.notetypes.save(noteType)
200198
val n = col.newNote()
201199
n.setItem("Front", "foo[abc]")
202200
col.addNote(n)
@@ -209,7 +207,7 @@ class CollectionTest : JvmTest() {
209207
assertThat("Question «$question» does not contains «anki:play».", question, Matchers.containsString("anki:play"))
210208
// it shouldn't throw an error while people are editing
211209
noteType.tmpls[0].qfmt = "{{kana:}}"
212-
noteTypes.save(noteType)
210+
col.notetypes.save(noteType)
213211
c.question(true)
214212
}
215213

AnkiDroid/src/test/java/com/ichi2/libanki/FinderTest.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,14 +136,13 @@ class FinderTest : JvmTest() {
136136
val catCard = note.cards()[0]
137137
var noteType = col.notetypes.current()
138138
noteType = col.notetypes.copy(noteType)
139-
val noteTypes = col.notetypes
140139
val t =
141140
Notetypes.newTemplate("Reverse").apply {
142141
qfmt = "{{Back}}"
143142
afmt = "{{Front}}"
144143
}
145-
noteTypes.addTemplateModChanged(noteType, t)
146-
noteTypes.save(noteType)
144+
col.notetypes.addTemplateModChanged(noteType, t)
145+
col.notetypes.save(noteType)
147146
note = col.newNote()
148147
note.setItem("Front", "test")
149148
note.setItem("Back", "foo bar")

0 commit comments

Comments
 (0)