Compressing word fields
This commit is contained in:
1 parent
e5622c70de
commit
0de809fcce
4 files changed
+121
-81
No files matched your search
+42
-4
@@ -1,16 +1,17 @@
|
||||
use crate::app::AppState;
|
||||
use crate::data::entities::WordData;
|
||||
use crate::data::Id;
|
||||
use crate::data::entities::WordGroup;
|
||||
use crate::data::entities::{CustomField, WordData};
|
||||
use crate::data::repositories::{BatchedRepository, Repository};
|
||||
use crate::data_provider::import::{ImportData, ImportGroup, get_words_of_group, load_groups};
|
||||
use crate::fs::*;
|
||||
use crate::jl_button;
|
||||
use crate::navigation::{NavigatedPage, Page, RootMessage};
|
||||
use crate::notification::NotificationData::OpenFileError;
|
||||
use crate::notification::{MaybeError, NotificationData};
|
||||
use crate::pages::import::ImportMessage::*;
|
||||
use crate::push_error;
|
||||
use crate::styling::*;
|
||||
use crate::{jl_button, push_error_task};
|
||||
use iced::widget::button::danger;
|
||||
use iced::widget::container::success;
|
||||
use iced::widget::{
|
||||
@@ -25,6 +26,7 @@ use log::info;
|
||||
use parking_lot::Mutex;
|
||||
use rfd::AsyncFileDialog;
|
||||
use rusqlite::Connection;
|
||||
use std::collections::HashMap;
|
||||
use std::fs::{File, OpenOptions};
|
||||
use std::io::BufReader;
|
||||
use std::path::PathBuf;
|
||||
@@ -428,7 +430,19 @@ impl ImportState {
|
||||
let separator = self.separator.clone();
|
||||
let skip_empty = self.skip_empty;
|
||||
let group = self.import_data.as_ref().unwrap().0[self.selected_index].clone();
|
||||
let map_indices = Self::get_mapping_indices(&group);
|
||||
let progress = self.progress.clone().unwrap();
|
||||
if let Err(err) = Self::ensure_fields(&state, &map_indices) {
|
||||
return push_error_task!(err);
|
||||
}
|
||||
let fields: HashMap<String, Id> = HashMap::from_iter(
|
||||
state
|
||||
.custom_fields
|
||||
.read()
|
||||
.iter()
|
||||
.map(|f| (f.identifier.clone(), f.id)),
|
||||
);
|
||||
|
||||
info!("{}", group.name);
|
||||
Task::perform(
|
||||
async move {
|
||||
@@ -439,7 +453,6 @@ impl ImportState {
|
||||
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 group_handle;
|
||||
{
|
||||
let group_entity = WordGroup {
|
||||
@@ -474,7 +487,7 @@ impl ImportState {
|
||||
word.tags = collected_string;
|
||||
}
|
||||
&_ => {
|
||||
word.additional.insert((*dest).clone(), collected_string);
|
||||
word.additional.insert(fields[dest], collected_string);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -554,4 +567,29 @@ impl ImportState {
|
||||
|_| RootMessage::Import(NextProgress),
|
||||
)
|
||||
}
|
||||
|
||||
fn ensure_fields(
|
||||
state: &AppState,
|
||||
import_group: &Vec<(String, Vec<usize>)>,
|
||||
) -> Result<(), NotificationData> {
|
||||
let mut fields_repo = state.custom_fields.write();
|
||||
let mut current_fields: Vec<String> = fields_repo
|
||||
.iter()
|
||||
.map(|field| field.identifier.clone())
|
||||
.collect();
|
||||
current_fields.append(&mut vec!["key".to_string(), "value".to_string(), "tags".to_string()]);
|
||||
for (field, _) in import_group {
|
||||
if current_fields.contains(field) {
|
||||
continue;
|
||||
}
|
||||
|
||||
fields_repo.create_now(CustomField {
|
||||
id: Default::default(),
|
||||
name: field.clone(),
|
||||
identifier: field.clone(),
|
||||
})?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user