Rewrite history page
This commit is contained in:
1 parent
f19a6f64c3
commit
532640058e
11 files changed
+201
-230
No files matched your search
+33
-30
@@ -1,5 +1,5 @@
|
||||
use crate::app::AppState;
|
||||
use crate::data::entities::{WordData, WordGroup};
|
||||
use crate::data::entities::{EntityData, WordData, WordGroup};
|
||||
use crate::data_provider::import::{ImportData, ImportGroup, get_words_of_group, load_groups};
|
||||
use crate::data_provider::words::{add_group, add_words};
|
||||
use crate::fs::*;
|
||||
@@ -29,6 +29,7 @@ use std::path::PathBuf;
|
||||
use std::sync::Arc;
|
||||
use tokio::task::spawn_blocking;
|
||||
use zip::ZipArchive;
|
||||
use crate::data::repositories::{BatchedRepository, Repository};
|
||||
|
||||
const DEFAULT_FIELDS: [&str; 6] = ["key", "value", "tags", "reading", "context", "description"];
|
||||
|
||||
@@ -431,29 +432,18 @@ impl ImportState {
|
||||
info!("{}", group.name);
|
||||
Task::perform(
|
||||
async move {
|
||||
spawn_blocking(move || {
|
||||
let words_list = spawn_blocking(move || {
|
||||
let temp_file_path = temp_import_path();
|
||||
let connection =
|
||||
Connection::open(&temp_file_path).map_err(|_| OpenFileError)?;
|
||||
let connection = Connection::open(&temp_file_path).map_err(|_| OpenFileError)?;
|
||||
let import_list = get_words_of_group(&connection, group.id)?;
|
||||
let mut words_list = Vec::with_capacity(import_list.len());
|
||||
|
||||
let map_indices = Self::get_mapping_indices(&group);
|
||||
let mut group_entity = WordGroup {
|
||||
id: 0.into(),
|
||||
name: group.name.clone(),
|
||||
};
|
||||
{
|
||||
let state = state.lock();
|
||||
add_group(&mut group_entity, &state.connection)?;
|
||||
}
|
||||
|
||||
let group_id = group_entity.id;
|
||||
for import in import_list {
|
||||
let mut word = WordData::new();
|
||||
|
||||
word.tags = import.tags.trim().replace(" ", ", ");
|
||||
word.group_id = group_id;
|
||||
for (dest, indices) in &map_indices {
|
||||
let collected_string = Self::collect_strings(
|
||||
&import.fields,
|
||||
@@ -480,24 +470,37 @@ impl ImportState {
|
||||
words_list.push(word);
|
||||
}
|
||||
|
||||
let mut state = state.lock();
|
||||
let connection = &mut state.connection;
|
||||
let mut index = 0;
|
||||
let total_len = words_list.len() as f32 / 1024.0;
|
||||
for word in &mut words_list.chunks_mut(1024) {
|
||||
add_words(word, connection)?;
|
||||
index += 1;
|
||||
let mut progress = progress.lock();
|
||||
*progress = (index as f32 / total_len).min(1.0);
|
||||
}
|
||||
|
||||
state.dictionary.append(&mut words_list);
|
||||
state.word_groups.push(group_entity);
|
||||
|
||||
Ok(())
|
||||
Ok::<_, NotificationData>(words_list)
|
||||
})
|
||||
.await
|
||||
.unwrap()
|
||||
.unwrap()?;
|
||||
|
||||
let group_handle = {
|
||||
let group_entity = WordGroup {
|
||||
id: 0.into(),
|
||||
name: group.name.clone(),
|
||||
};
|
||||
let mut groups_repo = state.word_groups.write();
|
||||
groups_repo.create_now(group_entity)?
|
||||
};
|
||||
|
||||
let group_id = group_handle.id;
|
||||
let mut words_list = words_list;
|
||||
for word in &mut words_list {
|
||||
word.group_id = group_id;
|
||||
}
|
||||
|
||||
let mut words_repo = state.dictionary.write();
|
||||
let mut index = 0;
|
||||
let total_len = words_list.len() as f32 / 1024.0;
|
||||
for words in &mut words_list.chunks_mut(1024) {
|
||||
words_repo.create_now_many(words)?;
|
||||
index += 1;
|
||||
let mut progress = progress.lock();
|
||||
*progress = (index as f32 / total_len).min(1.0);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
},
|
||||
|r: Result<(), NotificationData>| match r {
|
||||
Ok(_) => RootMessage::Import(ImportFinished),
|
||||
|
||||
Reference in new issue
Block a user