Optimized again dictionary page and working import
This commit is contained in:
1 parent
17e06256c1
commit
eb6c9c5084
9 files changed
+605
-95
No files matched your search
+413
-23
@@ -1,13 +1,21 @@
|
||||
use crate::data_provider::import::{load_groups, ImportData};
|
||||
use crate::AppState;
|
||||
use crate::data_provider::import::{ImportData, ImportGroup, get_words_of_group, load_groups};
|
||||
use crate::data_provider::words::{add_group, add_words};
|
||||
use crate::dictionary::app_data_dir;
|
||||
use crate::import::ImportMessage::*;
|
||||
use crate::lang::{WordData, WordGroup};
|
||||
use crate::navigation::{NavigatedPage, Page, RootMessage};
|
||||
use crate::styling::*;
|
||||
use crate::AppState;
|
||||
use iced::widget::scrollable;
|
||||
use iced::widget::{button, column, row, text};
|
||||
use iced::widget::button::danger;
|
||||
use iced::widget::container::success;
|
||||
use iced::widget::{
|
||||
Row, button, checkbox, column, container, progress_bar, row, rule, text, text_input,
|
||||
};
|
||||
use iced::widget::{scrollable, space};
|
||||
use iced::{Element, Task};
|
||||
use iced_core::Alignment::Center;
|
||||
use iced_core::Length::Fill;
|
||||
use iced_core::Padding;
|
||||
use rfd::AsyncFileDialog;
|
||||
use rusqlite::Connection;
|
||||
use std::fs::{File, OpenOptions};
|
||||
@@ -17,12 +25,19 @@ use std::sync::{Arc, Mutex};
|
||||
use tokio::task::spawn_blocking;
|
||||
use zip::ZipArchive;
|
||||
|
||||
const DEFAULT_FIELDS: [&str; 6] = ["key", "value", "tags", "reading", "context", "description"];
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct ImportState {
|
||||
state: Arc<Mutex<AppState>>,
|
||||
path: Option<PathBuf>,
|
||||
import_data: Option<ImportData>,
|
||||
selected_index: usize,
|
||||
selected_property: Option<String>,
|
||||
custom_property_name: String,
|
||||
skip_empty: bool,
|
||||
separator: String,
|
||||
progress: Option<Arc<Mutex<f32>>>,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
@@ -31,10 +46,27 @@ pub enum ImportMessage {
|
||||
SelectFile,
|
||||
UpdateFile(PathBuf),
|
||||
UpdateImport(ImportData),
|
||||
NextGroup,
|
||||
PreviousGroup,
|
||||
OpenPropertyMapper(String),
|
||||
EditCustomProperty(String),
|
||||
SetupCustomProperty,
|
||||
SetupDefaultProperty(String),
|
||||
SetupDirect,
|
||||
RemoveMapping(String),
|
||||
SwitchSkipEmpty(bool),
|
||||
EditSeparator(String),
|
||||
StartImport,
|
||||
NextProgress,
|
||||
ImportFinished,
|
||||
}
|
||||
|
||||
impl NavigatedPage<ImportMessage> for ImportState {
|
||||
fn navigate(&self, message: &ImportMessage) -> Option<Page> {
|
||||
if let Some(_) = self.progress {
|
||||
return None;
|
||||
}
|
||||
|
||||
if let Back = message {
|
||||
return Some(Page::PreviousPage);
|
||||
}
|
||||
@@ -54,41 +86,248 @@ impl NavigatedPage<ImportMessage> for ImportState {
|
||||
UpdateImport(import) => {
|
||||
self.import_data = Some(import);
|
||||
}
|
||||
NextGroup => {
|
||||
self.selected_index += 1;
|
||||
self.selected_property = None;
|
||||
self.custom_property_name.clear();
|
||||
}
|
||||
PreviousGroup => {
|
||||
self.selected_index -= 1;
|
||||
self.selected_property = None;
|
||||
self.custom_property_name.clear();
|
||||
}
|
||||
OpenPropertyMapper(prop) => self.selected_property = Some(prop),
|
||||
EditCustomProperty(prop_name) => self.custom_property_name = prop_name,
|
||||
SetupCustomProperty => {
|
||||
self.setup_mapping(self.custom_property_name.clone());
|
||||
}
|
||||
SetupDefaultProperty(prop_name) => {
|
||||
self.setup_mapping(prop_name);
|
||||
}
|
||||
SetupDirect => {
|
||||
self.setup_mapping(self.selected_property.clone().unwrap());
|
||||
}
|
||||
RemoveMapping(pro_name) => {
|
||||
self.remove_mapping(pro_name);
|
||||
}
|
||||
SwitchSkipEmpty(skip) => {
|
||||
self.skip_empty = skip;
|
||||
}
|
||||
EditSeparator(separator) => {
|
||||
self.separator = separator.clone();
|
||||
}
|
||||
StartImport => {
|
||||
self.progress = Some(Arc::new(Mutex::new(0.0)));
|
||||
return Task::batch([self.start_import(), self.next_progress()]);
|
||||
}
|
||||
NextProgress => {
|
||||
if self.progress.is_none() {
|
||||
return Task::none();
|
||||
}
|
||||
return self.next_progress();
|
||||
}
|
||||
ImportFinished => {
|
||||
self.progress = None;
|
||||
let group = self
|
||||
.import_data.as_mut()
|
||||
.unwrap()
|
||||
.0
|
||||
.get_mut(self.selected_index)
|
||||
.unwrap();
|
||||
group.imported = true;
|
||||
}
|
||||
}
|
||||
Task::none()
|
||||
}
|
||||
|
||||
fn view(&self) -> Element<'_, ImportMessage> {
|
||||
back_overlay(
|
||||
scrollable(
|
||||
column![
|
||||
row![
|
||||
button("Выбрать файл").style(jl_button).on_press(SelectFile),
|
||||
text!("{}", {
|
||||
if let Some(path) = &self.path {
|
||||
path.to_string_lossy().to_string()
|
||||
} else {
|
||||
"Файл не выбран".to_string()
|
||||
}
|
||||
})
|
||||
]
|
||||
.align_y(Center)
|
||||
.spacing(DEFAULT_SPACING)
|
||||
]
|
||||
.spacing(DEFAULT_SPACING),
|
||||
)
|
||||
.into(),
|
||||
{
|
||||
if let Some(value) = &self.progress {
|
||||
column![progress_bar(0f32..=1f32, *value.lock().unwrap()),]
|
||||
.spacing(QUARTER_SPACING)
|
||||
.into()
|
||||
} else {
|
||||
scrollable(
|
||||
column![
|
||||
row![
|
||||
button("Выбрать файл").style(jl_button).on_press(SelectFile),
|
||||
text!("{}", {
|
||||
if let Some(path) = &self.path {
|
||||
path.to_string_lossy().to_string()
|
||||
} else {
|
||||
"Файл не выбран".to_string()
|
||||
}
|
||||
})
|
||||
]
|
||||
.align_y(Center)
|
||||
.spacing(DEFAULT_SPACING),
|
||||
self.selected_group()
|
||||
]
|
||||
.spacing(DEFAULT_SPACING),
|
||||
)
|
||||
.into()
|
||||
}
|
||||
},
|
||||
Back,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl ImportState {
|
||||
fn selected_group(&self) -> Element<'_, ImportMessage> {
|
||||
if let Some(data) = &self.import_data {
|
||||
let group = &data.0[self.selected_index];
|
||||
return column![
|
||||
row![
|
||||
self.back_button(),
|
||||
space().width(Fill),
|
||||
text!("{}", group.name.clone()),
|
||||
space().width(Fill),
|
||||
self.next_button()
|
||||
]
|
||||
.width(Fill),
|
||||
text!("Количество карточек: {}", group.length),
|
||||
self.property_mapper(group),
|
||||
self.import_settings(),
|
||||
self.import_button(group),
|
||||
]
|
||||
.spacing(DEFAULT_SPACING)
|
||||
.into();
|
||||
}
|
||||
space().into()
|
||||
}
|
||||
|
||||
fn import_button(&self, group: &ImportGroup) -> Element<'_, ImportMessage> {
|
||||
if group.imported {
|
||||
return container(text!("Группа слов успешно импортирована")).padding(HALF_SPACING).align_x(Center)
|
||||
.style(success)
|
||||
.width(Fill)
|
||||
.into();
|
||||
}
|
||||
button("Начать импорт")
|
||||
.style(cta_button)
|
||||
.on_press(StartImport)
|
||||
.into()
|
||||
}
|
||||
|
||||
fn property_mapper(&self, group: &ImportGroup) -> Element<'_, ImportMessage> {
|
||||
column![
|
||||
self.property_list(group),
|
||||
rule::horizontal(2),
|
||||
self.property_selector()
|
||||
]
|
||||
.spacing(HALF_SPACING)
|
||||
.into()
|
||||
}
|
||||
|
||||
fn property_list(&self, group: &ImportGroup) -> Element<'_, ImportMessage> {
|
||||
let mut row = Row::new();
|
||||
for prop in &group.fields {
|
||||
let primary_button_name = (*prop).clone();
|
||||
if let Some(mapped) = group.mapping.get(prop.clone().as_str()) {
|
||||
row = row.push(
|
||||
column![
|
||||
button(text!("{}", primary_button_name)),
|
||||
"↕",
|
||||
button(text!("{}", mapped))
|
||||
.style(danger)
|
||||
.on_press(RemoveMapping(mapped.as_str().to_string())),
|
||||
]
|
||||
.align_x(Center),
|
||||
);
|
||||
} else {
|
||||
row = row.push(
|
||||
button(text!("{}", primary_button_name))
|
||||
.on_press(OpenPropertyMapper(prop.clone())),
|
||||
);
|
||||
}
|
||||
}
|
||||
scrollable(row.spacing(HALF_SPACING).padding(Padding {
|
||||
top: 0.0,
|
||||
right: 0.0,
|
||||
bottom: DEFAULT_SPACING,
|
||||
left: 0.0,
|
||||
}))
|
||||
.horizontal()
|
||||
.into()
|
||||
}
|
||||
|
||||
fn property_selector(&self) -> Element<'_, ImportMessage> {
|
||||
if self.selected_property == None {
|
||||
return space().into();
|
||||
}
|
||||
column![
|
||||
self.available_properties(),
|
||||
button("Добавить напрямую").on_press(SetupDirect),
|
||||
row![
|
||||
text_input("Пользовательское поле", &self.custom_property_name)
|
||||
.on_input(EditCustomProperty),
|
||||
button("Установить привязку").on_press(SetupCustomProperty)
|
||||
]
|
||||
.spacing(QUARTER_SPACING)
|
||||
]
|
||||
.spacing(HALF_SPACING)
|
||||
.into()
|
||||
}
|
||||
|
||||
fn available_properties(&self) -> Element<'_, ImportMessage> {
|
||||
let mut row = Row::new();
|
||||
|
||||
for default in DEFAULT_FIELDS {
|
||||
// if group.mapping.values().any(|name| name == default) {
|
||||
// continue;
|
||||
// }
|
||||
row = row.push(
|
||||
button(text!("{default}")).on_press(SetupDefaultProperty(default.to_string())),
|
||||
);
|
||||
}
|
||||
row.spacing(HALF_SPACING).into()
|
||||
}
|
||||
|
||||
fn import_settings(&self) -> Element<'_, ImportMessage> {
|
||||
column![
|
||||
checkbox(self.skip_empty)
|
||||
.label("Пропускать пустые строки")
|
||||
.on_toggle(SwitchSkipEmpty),
|
||||
column![
|
||||
text!("Разделитель при множественном объединении"),
|
||||
text_input("", &self.separator).on_input(EditSeparator)
|
||||
]
|
||||
]
|
||||
.spacing(DEFAULT_SPACING)
|
||||
.into()
|
||||
}
|
||||
|
||||
fn back_button(&self) -> Element<'_, ImportMessage> {
|
||||
let mut button = button("←").style(button::text);
|
||||
if self.selected_index > 0 {
|
||||
button = button.on_press(PreviousGroup);
|
||||
}
|
||||
button.into()
|
||||
}
|
||||
|
||||
fn next_button(&self) -> Element<'_, ImportMessage> {
|
||||
let mut button = button("→").style(button::text);
|
||||
if self.selected_index < self.import_data.as_ref().unwrap().0.len() - 1 {
|
||||
button = button.on_press(NextGroup);
|
||||
}
|
||||
button.into()
|
||||
}
|
||||
}
|
||||
|
||||
impl ImportState {
|
||||
pub fn new(state: Arc<Mutex<AppState>>) -> ImportState {
|
||||
ImportState {
|
||||
state,
|
||||
path: None,
|
||||
import_data: None,
|
||||
selected_index: 0
|
||||
selected_index: 0,
|
||||
selected_property: None,
|
||||
custom_property_name: "".to_string(),
|
||||
skip_empty: true,
|
||||
separator: ", ".to_string(),
|
||||
progress: None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -161,4 +400,155 @@ impl ImportState {
|
||||
let data = load_groups(&connection);
|
||||
Ok(data)
|
||||
}
|
||||
fn setup_mapping(&mut self, property_name: String) {
|
||||
let group = self.import_data.as_mut().unwrap();
|
||||
let group = group.0.get_mut(self.selected_index).unwrap();
|
||||
group
|
||||
.mapping
|
||||
.insert(self.selected_property.clone().unwrap(), property_name);
|
||||
self.selected_property = None;
|
||||
self.custom_property_name.clear();
|
||||
}
|
||||
fn remove_mapping(&mut self, property_name: String) {
|
||||
let group = self.import_data.as_mut().unwrap();
|
||||
let group = group.0.get_mut(self.selected_index).unwrap();
|
||||
let remove_key = group
|
||||
.mapping
|
||||
.keys()
|
||||
.find(|key| group.mapping[*key] == property_name)
|
||||
.unwrap();
|
||||
group.mapping.remove(remove_key.clone().as_str());
|
||||
self.selected_property = None;
|
||||
self.custom_property_name.clear();
|
||||
}
|
||||
|
||||
fn start_import(&self) -> Task<RootMessage> {
|
||||
let state = self.state.clone();
|
||||
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 progress = self.progress.clone().unwrap();
|
||||
println!("{}", group.name);
|
||||
Task::perform(
|
||||
async move {
|
||||
spawn_blocking(move || {
|
||||
let temp_file_path = app_data_dir().join("import");
|
||||
let connection = Connection::open(&temp_file_path).map_err(|_| ())?;
|
||||
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,
|
||||
name: group.name.clone(),
|
||||
};
|
||||
{
|
||||
let state = state.lock().unwrap();
|
||||
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.clone();
|
||||
for (dest, indices) in &map_indices {
|
||||
let collected_string = Self::collect_strings(
|
||||
&import.fields,
|
||||
&indices,
|
||||
&separator,
|
||||
skip_empty,
|
||||
);
|
||||
match dest.as_str() {
|
||||
"key" => {
|
||||
word.key = collected_string;
|
||||
}
|
||||
"value" => {
|
||||
word.value = collected_string;
|
||||
}
|
||||
"tags" => {
|
||||
word.tags = collected_string;
|
||||
}
|
||||
&_ => {
|
||||
word.additional.insert((*dest).clone(), collected_string);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
println!("{word:?}");
|
||||
words_list.push(word);
|
||||
}
|
||||
|
||||
let mut state = state.lock().unwrap();
|
||||
let connection = &mut state.connection;
|
||||
let mut index = 0;
|
||||
let total_len = words_list.len() as f32 / 256.0;
|
||||
for word in &mut words_list.chunks_mut(256) {
|
||||
add_words(word, connection);
|
||||
index += 1;
|
||||
let mut progress = progress.lock().unwrap();
|
||||
*progress = index as f32 / total_len;
|
||||
}
|
||||
|
||||
state.dictionary.append(&mut words_list);
|
||||
state.word_groups.push(group_entity);
|
||||
|
||||
Ok(())
|
||||
})
|
||||
.await
|
||||
.unwrap()
|
||||
},
|
||||
|_: Result<(), ()>| RootMessage::Import(ImportFinished),
|
||||
)
|
||||
}
|
||||
|
||||
fn get_mapping_indices(group: &ImportGroup) -> Vec<(String, Vec<usize>)> {
|
||||
let mut final_props = group.mapping.values().cloned().collect::<Vec<_>>();
|
||||
final_props.sort();
|
||||
final_props.dedup();
|
||||
let mut result = final_props
|
||||
.iter()
|
||||
.map(|name| (name.clone(), Vec::<usize>::with_capacity(1)))
|
||||
.collect::<Vec<_>>();
|
||||
for key in group.mapping.keys() {
|
||||
let endpoint = group.mapping.get(key).unwrap();
|
||||
let property_index = group.fields.iter().position(|f| f == key).unwrap();
|
||||
let group_index = result
|
||||
.iter()
|
||||
.position(|(name, _)| name == endpoint)
|
||||
.unwrap();
|
||||
result.get_mut(group_index).unwrap().1.push(property_index);
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
fn collect_strings(
|
||||
properties: &Vec<String>,
|
||||
indices: &Vec<usize>,
|
||||
separator: &str,
|
||||
skip_empty: bool,
|
||||
) -> String {
|
||||
let mut working_words = Vec::with_capacity(indices.len());
|
||||
for i in 0..indices.len() {
|
||||
let index = indices[i];
|
||||
let str = properties.get(index).unwrap();
|
||||
if skip_empty && str.is_empty() {
|
||||
continue;
|
||||
}
|
||||
working_words.push(str.as_str());
|
||||
}
|
||||
|
||||
working_words.join(separator)
|
||||
}
|
||||
|
||||
fn next_progress(&self) -> Task<RootMessage> {
|
||||
Task::perform(
|
||||
async {
|
||||
tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
|
||||
},
|
||||
|_| RootMessage::Import(NextProgress),
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user