@@ -27,7 +27,6 @@ import com.ichi2.anki.libanki.CardTemplate
2727import com.ichi2.anki.libanki.Collection
2828import com.ichi2.anki.libanki.NoteTypeId
2929import com.ichi2.anki.libanki.NotetypeJson
30- import com.ichi2.async.saveNoteType
3130import com.ichi2.compat.CompatHelper.Companion.compat
3231import com.ichi2.compat.CompatHelper.Companion.getSerializableCompat
3332import 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
0 commit comments