Skip to content

Commit c04dede

Browse files
committed
Move CollectionOperations.saveNoteType() to CardTemplateNoteType
It was used only there, allows removal of the now empty async package.
1 parent 5062a02 commit c04dede

File tree

2 files changed

+35
-59
lines changed

2 files changed

+35
-59
lines changed

AnkiDroid/src/main/java/com/ichi2/anki/CardTemplateNotetype.kt

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import com.ichi2.anki.libanki.CardTemplate
2727
import com.ichi2.anki.libanki.Collection
2828
import com.ichi2.anki.libanki.NoteTypeId
2929
import com.ichi2.anki.libanki.NotetypeJson
30-
import com.ichi2.async.saveNoteType
3130
import com.ichi2.compat.CompatHelper.Companion.compat
3231
import com.ichi2.compat.CompatHelper.Companion.getSerializableCompat
3332
import timber.log.Timber
@@ -103,6 +102,41 @@ class CardTemplateNotetype(
103102
return saveNoteType(col, notetype, adjustedTemplateChanges)
104103
}
105104

105+
/**
106+
* Handles everything for a note type change at once - template add / deletes as well as content updates
107+
*/
108+
@KotlinCleanup("strongly type templateChanges")
109+
fun saveNoteType(
110+
col: Collection,
111+
notetype: NotetypeJson,
112+
templateChanges: ArrayList<Array<Any>>,
113+
) {
114+
Timber.d("saveNoteType")
115+
val oldNoteType = col.notetypes.get(notetype.id)
116+
117+
// TODO: make undoable
118+
val newTemplates = notetype.templates
119+
for (change in templateChanges) {
120+
val oldTemplates = oldNoteType!!.templates
121+
when (change[1] as ChangeType) {
122+
ChangeType.ADD -> {
123+
Timber.d("saveNoteType() adding template %s", change[0])
124+
col.notetypes.addTemplate(oldNoteType, newTemplates[change[0] as Int])
125+
}
126+
ChangeType.DELETE -> {
127+
Timber.d("saveNoteType() deleting template currently at ordinal %s", change[0])
128+
col.notetypes.remTemplate(oldNoteType, oldTemplates[change[0] as Int])
129+
}
130+
}
131+
}
132+
133+
// required for Rust: the modified time can't go backwards, and we updated the note type by adding fields
134+
// This could be done better
135+
notetype.mod = oldNoteType!!.mod
136+
col.notetypes.save(notetype)
137+
col.notetypes.update(notetype)
138+
}
139+
106140
/**
107141
* Template deletes shift card ordinals in the database. To operate without saving, we must keep track to apply in order.
108142
* In addition, we don't want to persist a template add just to delete it later, so we combine those if they happen

AnkiDroid/src/main/java/com/ichi2/async/CollectionOperations.kt

Lines changed: 0 additions & 58 deletions
This file was deleted.

0 commit comments

Comments
 (0)