Optimized again dictionary page and working import

This commit is contained in:
2026-08-14 23:03:15 +03:00
parent 17e06256c1
commit eb6c9c5084
9 changed files with 605 additions and 95 deletions
+68 -41
View File
@@ -1,6 +1,7 @@
use crate::data_provider::words::{delete_group, delete_word, update_group, update_word};
use crate::dictionary::DictionaryMessage::*;
use crate::dictionary_test::DictionaryQuizState;
use crate::import::ImportState;
use crate::lang::{WordData, WordGroup};
use crate::navigation::Page::{Import, Word};
use crate::navigation::{NavigatedPage, Page};
@@ -14,7 +15,8 @@ use iced::widget::button::{danger, text};
use iced::widget::space::horizontal;
use iced::widget::text_input::default;
use iced::widget::*;
use iced::{Border, Color, Length, Shadow, Task};
use iced::{Border, Color, Shadow, Task};
use iced_core::Length::Fill;
use rand::random_range;
use std::collections::{HashMap, HashSet};
use std::fs;
@@ -22,8 +24,6 @@ use std::ops::Add;
use std::path::PathBuf;
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
use iced_core::Length::Fill;
use crate::import::ImportState;
#[derive(Clone)]
pub struct DictionaryState {
@@ -61,7 +61,7 @@ pub enum DictionaryMessage {
DeleteGroup,
ChangeDirection,
TrySave(usize),
ToImport
ToImport,
}
impl NavigatedPage<DictionaryMessage> for DictionaryState {
@@ -101,12 +101,18 @@ impl NavigatedPage<DictionaryMessage> for DictionaryState {
}
}
if let ToImport = message {
return Some(Import(ImportState::new(self.state.clone())))
return Some(Import(ImportState::new(self.state.clone())));
}
None
}
fn navigated(&mut self) {}
fn navigated(&mut self) {
let len = self.state.lock().unwrap().dictionary.len();
self.include_map = vec![false; len];
self.tag_map = Default::default();
self.update_tags();
}
fn update(&mut self, message: DictionaryMessage) -> Task<RootMessage> {
match message {
@@ -227,15 +233,23 @@ impl NavigatedPage<DictionaryMessage> for DictionaryState {
if self.selected_group_index == 0 {
return Task::none();
}
let state = &mut self.state.lock().unwrap();
{
let state = &mut self.state.lock().unwrap();
if let Some(group) = state.word_groups.get(self.selected_group_index) {
let connection = &state.connection;
if let Some(group) = state.word_groups.get(self.selected_group_index) {
let remove_group_id = group.id;
let connection = &state.connection;
delete_group(group, connection);
state.word_groups.remove(self.selected_group_index);
self.selected_group_index = 0;
delete_group(group, connection);
state.word_groups.remove(self.selected_group_index);
state
.dictionary
.retain(|word| word.group_id != remove_group_id);
self.selected_group_index = 0;
}
}
self.update_tags()
}
ChangeDirection => {
self.reverse_list = !self.reverse_list;
@@ -262,8 +276,9 @@ impl NavigatedPage<DictionaryMessage> for DictionaryState {
iced::widget::column![
self.groups_panel(),
row![horizontal().width(8), self.words_list(),],
row![button("Добавить слово").style(jl_button).on_press(NewWord),
horizontal().width(Fill),
row![
button("Добавить слово").style(jl_button).on_press(NewWord),
horizontal().width(Fill),
button("Импорт").style(text).on_press(ToImport),
]
]
@@ -319,21 +334,25 @@ impl DictionaryState {
fn words_list(&self) -> iced::Element<'_, DictionaryMessage> {
let time = Instant::now();
let mut col = Column::new().width(Length::Fill);
let mut col = Column::new().width(Fill);
let mut range = (0..self.include_map.len()).collect::<Vec<_>>();
let state = self.state.lock().unwrap();
let group_id = state.word_groups[self.selected_group_index].id;
let dict = &mut state.dictionary.clone();
if self.reverse_list {
dict.reverse()
} else {
range = range.iter().rev().map(|x| *x).collect::<Vec<usize>>();
}
let dict = &state.dictionary;
let mut index = 0;
for access_index in 0..dict.len() {
let mut i = access_index;
if self.reverse_list {
i = dict.len() - access_index - 1;
}
let word = &dict[i];
if word.group_id != group_id {
continue;
}
for word in dict {
let i = range.pop().unwrap();
if !self.search.is_empty() {
if word.key.contains(&self.search) == false
&& word.value.contains(&self.search) == false
@@ -343,10 +362,6 @@ impl DictionaryState {
}
}
if word.group_id != group_id {
continue;
}
let word_line_data = WordLineState {
is_included: self.include_map[i],
key: word.key.clone(),
@@ -356,9 +371,11 @@ impl DictionaryState {
index: i,
};
index += 1;
let lazy_line = lazy(word_line_data, move |data| {
let index = data.index;
let mut line = Row::new().width(Length::Fill).align_y(Center);
let mut line = Row::new().width(Fill).align_y(Center);
line = line.push(
checkbox(data.is_included)
.label("")
@@ -369,7 +386,7 @@ impl DictionaryState {
line = line.push(
text_input("Слово", &data.key)
.size(ACCENT_FONT_SIZE)
.width(Length::Fill)
.width(Fill)
.on_input(move |string| SetKey(index, string))
.on_submit(SubmitWord(index))
.style(|x, status| {
@@ -381,7 +398,7 @@ impl DictionaryState {
line = line.push(
text_input("Перевод", &data.value)
.size(ACCENT_FONT_SIZE)
.width(Length::Fill)
.width(Fill)
.on_input(move |string| SetValue(index, string))
.on_submit(SubmitWord(index))
.style(|x, status| {
@@ -394,7 +411,7 @@ impl DictionaryState {
line = line.push(
text_input("Теги", &data.tags)
.size(ACCENT_FONT_SIZE)
.width(Length::Fill)
.width(Fill)
.on_input(move |string| SetTags(index, string))
.on_submit(SubmitWord(index))
.style(|x, status| {
@@ -420,14 +437,16 @@ impl DictionaryState {
button("").on_press(WordAction(index)).width(15)
};
line = line.push(line_button()).push(space().width(10));
println!("Updating word {}", &data.key);
line
});
col = col.push(lazy_line);
}
println!("Drawing {index} lines");
println!("Words rendering time: {:?}", time.elapsed());
scrollable(col).height(Length::Fill).into()
scrollable(col).height(Fill).into()
}
fn filters(&self) -> iced::Element<'_, DictionaryMessage> {
@@ -436,7 +455,7 @@ impl DictionaryState {
iced::widget::column![
text_input("Поиск", &self.search)
.on_input(Search)
.width(Length::Fill),
.width(Fill),
text!("Всего слов: {}", dict.len()),
text!(
"Выбрано слов: {}",
@@ -449,10 +468,10 @@ impl DictionaryState {
toggler(self.reverse)
.label("Обратный тест")
.on_toggle(SetReverse),
button(text!("Тест").center().width(Length::Fill))
button(text!("Тест").center().width(Fill))
.style(cta_button)
.on_press(Test)
.width(Length::Fill),
.width(Fill),
]
.width(250)
.spacing(DEFAULT_SPACING)
@@ -460,7 +479,7 @@ impl DictionaryState {
}
fn tags_selector(&self) -> iced::Element<'_, DictionaryMessage> {
let mut col = Column::new().width(Length::Fill);
let mut col = Column::new().width(Fill);
col = col.push(
button("Сбросить")
.on_press(ResetTags)
@@ -482,7 +501,7 @@ impl DictionaryState {
)
}
container(scrollable(col)).height(Length::Fill).into()
container(scrollable(col)).height(Fill).into()
}
fn update_tags(&mut self) {
@@ -535,7 +554,7 @@ impl DictionaryState {
.iter()
.map(|word| (split_with_coma(word.tags.as_str()), word.group_id))
.map(|(tags, word_group_id)| {
tags.iter().all(|t| include_tags.contains(t)) && word_group_id == group_id
tags.iter().all(|t| include_tags.contains(t)) && tags.len() != 0 && word_group_id == group_id
})
.collect();
@@ -562,7 +581,7 @@ impl DictionaryState {
let group = state.word_groups[self.selected_group_index].clone();
iced::widget::column![
scrollable(row).width(Length::Fill).horizontal(),
scrollable(row).width(Fill).horizontal(),
row![
button("").on_press(ChangeDirection).style(jl_button),
text_input("Название группы слов", &group.name)
@@ -570,12 +589,20 @@ impl DictionaryState {
.width(250)
.on_submit(SaveGroup),
horizontal(),
button("Удалить").style(danger).on_press(DeleteGroup),
self.group_delete_button(),
]
.spacing(DEFAULT_SPACING / 2.0)
]
.into()
}
fn group_delete_button(&self) -> iced::Element<'_, DictionaryMessage> {
if self.selected_group_index != 0 {
button("Удалить").style(danger).on_press(DeleteGroup).into()
} else {
space().into()
}
}
}
pub fn split_with_coma(ts: &str) -> Vec<String> {