mirror of
https://github.com/ankitects/anki.git
synced 2025-09-23 08:22:24 -04:00
Show progress of note imports
Note import is the slowest part, so showing progress here makes the UI feel more responsive.
This commit is contained in:
parent
d645824098
commit
23eda19677
5 changed files with 24 additions and 7 deletions
|
@ -79,3 +79,8 @@ importing-processed-media-file =
|
||||||
}
|
}
|
||||||
importing-importing-collection = Importing collection...
|
importing-importing-collection = Importing collection...
|
||||||
importing-failed-to-import-media-file = Failed to import media file: { $debugInfo }
|
importing-failed-to-import-media-file = Failed to import media file: { $debugInfo }
|
||||||
|
importing-processed-notes =
|
||||||
|
{ $count ->
|
||||||
|
[one] Processed { $count } note...
|
||||||
|
*[other] Processed { $count } notes...
|
||||||
|
}
|
||||||
|
|
|
@ -89,7 +89,10 @@ impl Backend {
|
||||||
fn import_progress_fn(&self) -> impl FnMut(ImportProgress) -> Result<()> {
|
fn import_progress_fn(&self) -> impl FnMut(ImportProgress) -> Result<()> {
|
||||||
let mut handler = self.new_progress_handler();
|
let mut handler = self.new_progress_handler();
|
||||||
move |progress| {
|
move |progress| {
|
||||||
let throttle = matches!(progress, ImportProgress::Media(_));
|
let throttle = matches!(
|
||||||
|
progress,
|
||||||
|
ImportProgress::Media(_) | ImportProgress::Notes(_)
|
||||||
|
);
|
||||||
if handler.update(Progress::Import(progress), throttle) {
|
if handler.update(Progress::Import(progress), throttle) {
|
||||||
Ok(())
|
Ok(())
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -111,6 +111,7 @@ pub(super) fn progress_to_proto(progress: Option<Progress>, tr: &I18n) -> pb::Pr
|
||||||
ImportProgress::Collection => tr.importing_importing_collection(),
|
ImportProgress::Collection => tr.importing_importing_collection(),
|
||||||
ImportProgress::Media(n) => tr.importing_processed_media_file(n),
|
ImportProgress::Media(n) => tr.importing_processed_media_file(n),
|
||||||
ImportProgress::MediaCheck(n) => tr.media_check_checked(n),
|
ImportProgress::MediaCheck(n) => tr.media_check_checked(n),
|
||||||
|
ImportProgress::Notes(n) => tr.importing_processed_notes(n),
|
||||||
}
|
}
|
||||||
.into(),
|
.into(),
|
||||||
),
|
),
|
||||||
|
|
|
@ -10,4 +10,5 @@ pub enum ImportProgress {
|
||||||
Collection,
|
Collection,
|
||||||
Media(usize),
|
Media(usize),
|
||||||
MediaCheck(usize),
|
MediaCheck(usize),
|
||||||
|
Notes(usize),
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,9 +10,12 @@ use std::{
|
||||||
|
|
||||||
use sha1::Sha1;
|
use sha1::Sha1;
|
||||||
|
|
||||||
use super::{media::MediaUseMap, Context};
|
use super::{media::MediaUseMap, Context, ProgressFn};
|
||||||
use crate::{
|
use crate::{
|
||||||
import_export::package::{media::safe_normalized_file_name, LogNote, NoteLog},
|
import_export::{
|
||||||
|
package::{media::safe_normalized_file_name, LogNote, NoteLog},
|
||||||
|
ImportProgress,
|
||||||
|
},
|
||||||
prelude::*,
|
prelude::*,
|
||||||
text::{replace_media_refs, strip_html_preserving_media_filenames, CowMapping},
|
text::{replace_media_refs, strip_html_preserving_media_filenames, CowMapping},
|
||||||
};
|
};
|
||||||
|
@ -100,7 +103,7 @@ impl Context<'_> {
|
||||||
) -> Result<NoteImports> {
|
) -> Result<NoteImports> {
|
||||||
let mut ctx = NoteContext::new(self.usn, self.target_col, media_map)?;
|
let mut ctx = NoteContext::new(self.usn, self.target_col, media_map)?;
|
||||||
ctx.import_notetypes(mem::take(&mut self.data.notetypes))?;
|
ctx.import_notetypes(mem::take(&mut self.data.notetypes))?;
|
||||||
ctx.import_notes(mem::take(&mut self.data.notes))?;
|
ctx.import_notes(mem::take(&mut self.data.notes), self.progress_fn)?;
|
||||||
Ok(ctx.imports)
|
Ok(ctx.imports)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -176,8 +179,12 @@ impl<'n> NoteContext<'n> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn import_notes(&mut self, notes: Vec<Note>) -> Result<()> {
|
fn import_notes(&mut self, notes: Vec<Note>, progress_fn: &mut ProgressFn) -> Result<()> {
|
||||||
for mut note in notes {
|
for (idx, mut note) in notes.into_iter().enumerate() {
|
||||||
|
// FIXME: use ProgressHandler.increment()?
|
||||||
|
if idx % 17 == 0 {
|
||||||
|
progress_fn(ImportProgress::Notes(idx))?;
|
||||||
|
}
|
||||||
if let Some(notetype_id) = self.remapped_notetypes.get(¬e.notetype_id) {
|
if let Some(notetype_id) = self.remapped_notetypes.get(¬e.notetype_id) {
|
||||||
if self.target_guids.contains_key(¬e.guid) {
|
if self.target_guids.contains_key(¬e.guid) {
|
||||||
// TODO: Log ignore
|
// TODO: Log ignore
|
||||||
|
@ -356,7 +363,7 @@ mod test {
|
||||||
];
|
];
|
||||||
let mut ctx = NoteContext::new(Usn(1), &mut col, &mut media_map).unwrap();
|
let mut ctx = NoteContext::new(Usn(1), &mut col, &mut media_map).unwrap();
|
||||||
ctx.remapped_notetypes.insert(NotetypeId(123), basic_ntid);
|
ctx.remapped_notetypes.insert(NotetypeId(123), basic_ntid);
|
||||||
ctx.import_notes(notes).unwrap();
|
ctx.import_notes(notes, &mut |_progress| Ok(())).unwrap();
|
||||||
|
|
||||||
assert_log(
|
assert_log(
|
||||||
&ctx.imports.log.new,
|
&ctx.imports.log.new,
|
||||||
|
|
Loading…
Reference in a new issue