handle changed sort field index

This commit is contained in:
Damien Elmes 2020-04-24 15:31:32 +10:00
parent ea8e0ef6a2
commit d6706e1f0e
4 changed files with 28 additions and 10 deletions

View File

@ -404,7 +404,7 @@ where
&self.mgr.media_folder,
)? {
// note was modified, needs saving
note.prepare_for_update(nt, usn)?;
note.prepare_for_update(nt, Some(usn))?;
self.ctx.storage.update_note(&note)?;
collection_modified = true;
}

View File

@ -63,7 +63,8 @@ impl Note {
Ok(())
}
pub fn prepare_for_update(&mut self, nt: &NoteType, usn: Usn) -> Result<()> {
/// Prepare note for saving to the database. If usn is provided, mtime will be bumped.
pub fn prepare_for_update(&mut self, nt: &NoteType, usn: Option<Usn>) -> Result<()> {
assert!(nt.id == self.ntid);
if nt.fields.len() != self.fields.len() {
return Err(AnkiError::invalid_input(format!(
@ -87,8 +88,10 @@ impl Note {
};
self.sort_field = Some(sort_field.into());
self.checksum = Some(checksum);
self.mtime = TimestampSecs::now();
self.usn = usn;
if let Some(usn) = usn {
self.mtime = TimestampSecs::now();
self.usn = usn;
}
Ok(())
}
@ -190,7 +193,7 @@ impl Collection {
did: DeckID,
) -> Result<()> {
self.canonify_note_tags(note, ctx.usn)?;
note.prepare_for_update(&ctx.notetype, ctx.usn)?;
note.prepare_for_update(&ctx.notetype, Some(ctx.usn))?;
self.storage.add_note(note)?;
self.generate_cards_for_new_note(ctx, note, did)
}
@ -211,7 +214,7 @@ impl Collection {
note: &mut Note,
) -> Result<()> {
self.canonify_note_tags(note, ctx.usn)?;
note.prepare_for_update(ctx.notetype, ctx.usn)?;
note.prepare_for_update(ctx.notetype, Some(ctx.usn))?;
self.generate_cards_for_existing_note(ctx, note)?;
self.storage.update_note(note)?;

View File

@ -328,7 +328,11 @@ impl Collection {
}
self.transact(None, |col| {
if let Some(existing_notetype) = existing {
col.update_notes_for_changed_fields(nt, existing_notetype.fields.len())?;
col.update_notes_for_changed_fields(
nt,
existing_notetype.fields.len(),
existing_notetype.config.sort_field_idx,
)?;
col.update_cards_for_changed_templates(nt, existing_notetype.templates.len())?;
}

View File

@ -54,11 +54,22 @@ impl Collection {
&mut self,
nt: &NoteType,
previous_field_count: usize,
previous_sort_idx: u32,
) -> Result<()> {
let ords: Vec<_> = nt.fields.iter().map(|f| f.ord).collect();
if !ords_changed(&ords, previous_field_count) {
// nothing to do
return Ok(());
if nt.config.sort_field_idx == previous_sort_idx {
// only need to update sort field
let nids = self.search_notes_only(&format!("mid:{}", nt.id))?;
for nid in nids {
let mut note = self.storage.get_note(nid)?.unwrap();
note.prepare_for_update(nt, None)?;
self.storage.update_note(&note)?;
}
} else {
// nothing to do
return Ok(());
}
}
self.storage.set_schema_modified()?;
@ -81,7 +92,7 @@ impl Collection {
})
.map(Into::into)
.collect();
note.prepare_for_update(nt, usn)?;
note.prepare_for_update(nt, Some(usn))?;
self.storage.update_note(&note)?;
}
Ok(())