Show the number of cards added when adding (#4310)

* modify `generate_cards_for_note` to return count

* modify `add_note` to return count

* show the number of cards added when adding
This commit is contained in:
llama 2025-09-02 16:06:49 +08:00 committed by GitHub
parent 4ac80061ca
commit 153b972dfd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 23 additions and 15 deletions

View file

@ -1,5 +1,10 @@
adding-add-shortcut-ctrlandenter = Add (shortcut: ctrl+enter)
adding-added = Added
adding-added-cards =
Added { $count ->
[one] { $count } card
*[other] { $count } cards
}
adding-discard-current-input = Discard current input?
adding-keep-editing = Keep Editing
adding-edit = Edit "{ $val }"

View file

@ -59,7 +59,7 @@ message AddNoteRequest {
}
message AddNoteResponse {
collection.OpChanges changes = 1;
collection.OpChangesWithCount changes = 1;
int64 note_id = 2;
}

View file

@ -528,7 +528,7 @@ class Collection(DeprecatedNamesMixin):
def new_note(self, notetype: NotetypeDict) -> Note:
return Note(self, notetype)
def add_note(self, note: Note, deck_id: DeckId) -> OpChanges:
def add_note(self, note: Note, deck_id: DeckId) -> OpChangesWithCount:
hooks.note_will_be_added(self, note, deck_id)
out = self._backend.add_note(note=note._to_backend_note(), deck_id=deck_id)
note.id = NoteId(out.note_id)

View file

@ -8,7 +8,7 @@ from collections.abc import Callable
import aqt.editor
import aqt.forms
from anki._legacy import deprecated
from anki.collection import OpChanges, SearchNode
from anki.collection import OpChanges, OpChangesWithCount, SearchNode
from anki.decks import DeckId
from anki.models import NotetypeId
from anki.notes import Note, NoteFieldsCheckResult, NoteId
@ -294,13 +294,13 @@ class AddCards(QMainWindow):
target_deck_id = self.deck_chooser.selected_deck_id
def on_success(changes: OpChanges) -> None:
def on_success(changes: OpChangesWithCount) -> None:
# only used for detecting changed sticky fields on close
self._last_added_note = note
self.addHistory(note)
tooltip(tr.adding_added(), period=500)
tooltip(tr.adding_added_cards(count=changes.count), period=500)
av_player.stop_and_clear_queue()
self._load_new_note(sticky_fields_from=note)
gui_hooks.add_cards_did_add_note(note)

View file

@ -18,7 +18,7 @@ def add_note(
parent: QWidget,
note: Note,
target_deck_id: DeckId,
) -> CollectionOp[OpChanges]:
) -> CollectionOp[OpChangesWithCount]:
return CollectionOp(parent, lambda col: col.add_note(note, target_deck_id))

View file

@ -87,7 +87,7 @@ impl TryFrom<anki_proto::notes::AddNoteRequest> for AddNoteRequest {
}
impl Collection {
pub fn add_note(&mut self, note: &mut Note, did: DeckId) -> Result<OpOutput<()>> {
pub fn add_note(&mut self, note: &mut Note, did: DeckId) -> Result<OpOutput<usize>> {
self.transact(Op::AddNote, |col| col.add_note_inner(note, did))
}
@ -372,7 +372,7 @@ impl Collection {
Ok(())
}
pub(crate) fn add_note_inner(&mut self, note: &mut Note, did: DeckId) -> Result<()> {
pub(crate) fn add_note_inner(&mut self, note: &mut Note, did: DeckId) -> Result<usize> {
let nt = self
.get_notetype(note.notetype_id)?
.or_invalid("missing note type")?;
@ -383,10 +383,11 @@ impl Collection {
note.prepare_for_update(ctx.notetype, normalize_text)?;
note.set_modified(ctx.usn);
self.add_note_only_undoable(note)?;
self.generate_cards_for_new_note(&ctx, note, did)?;
let count = self.generate_cards_for_new_note(&ctx, note, did)?;
self.set_last_deck_for_notetype(note.notetype_id, did)?;
self.set_last_notetype_for_deck(did, note.notetype_id)?;
self.set_current_notetype_id(note.notetype_id)
self.set_current_notetype_id(note.notetype_id)?;
Ok(count)
}
pub fn update_note(&mut self, note: &mut Note) -> Result<OpOutput<()>> {

View file

@ -215,7 +215,7 @@ impl Collection {
ctx: &CardGenContext<impl Deref<Target = Notetype>>,
note: &Note,
target_deck_id: DeckId,
) -> Result<()> {
) -> Result<usize> {
self.generate_cards_for_note(
ctx,
note,
@ -231,7 +231,8 @@ impl Collection {
note: &Note,
) -> Result<()> {
let existing = self.storage.existing_cards_for_note(note.id)?;
self.generate_cards_for_note(ctx, note, &existing, ctx.last_deck, &mut Default::default())
self.generate_cards_for_note(ctx, note, &existing, ctx.last_deck, &mut Default::default())?;
Ok(())
}
fn generate_cards_for_note(
@ -241,12 +242,13 @@ impl Collection {
existing: &[AlreadyGeneratedCardInfo],
target_deck_id: Option<DeckId>,
cache: &mut CardGenCache,
) -> Result<()> {
) -> Result<usize> {
let cards = ctx.new_cards_required(note, existing, true);
if cards.is_empty() {
return Ok(());
return Ok(0);
}
self.add_generated_cards(note.id, &cards, target_deck_id, cache)
self.add_generated_cards(note.id, &cards, target_deck_id, cache)?;
Ok(cards.len())
}
pub(crate) fn generate_cards_for_notetype(