Partial dictionary rewrite
This commit is contained in:
1 parent
1509947cb0
commit
f2035ca749
12 files changed
+304
-363
No files matched your search
+163
-200
@@ -1,7 +1,8 @@
|
||||
use crate::app::AppState;
|
||||
use crate::data::Id;
|
||||
use crate::data::entities::{WordData, WordGroup};
|
||||
use crate::data_provider::words::{delete_group, delete_word, update_group, update_word};
|
||||
use crate::data::entities::{EntityHandle, WordData, WordGroup};
|
||||
use crate::data::repositories::{Query, Repository};
|
||||
use crate::data_provider::words::{WordRepository, WordDataLayer, WordQuerySet, WordRepositoryInner};
|
||||
use crate::navigation::Page::{Import, Word};
|
||||
use crate::navigation::{NavigatedPage, Page, RootMessage};
|
||||
use crate::pages::dictionary::DictionaryMessage::*;
|
||||
@@ -22,37 +23,38 @@ use iced::widget::*;
|
||||
use iced::{Border, Color, Shadow, Task};
|
||||
use iced_core::Length::Fill;
|
||||
use log::info;
|
||||
use parking_lot::Mutex;
|
||||
use rand::random_range;
|
||||
use std::ops::Add;
|
||||
use std::sync::Arc;
|
||||
use std::time::{Duration, Instant};
|
||||
use crate::data_provider::word_groups::{WordGroupDataLayer, WordGroupRepositoryInner};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct DictionaryState {
|
||||
state: AppState,
|
||||
include_map: Vec<bool>,
|
||||
included: HashSet<EntityHandle<WordData>>,
|
||||
tag_map: HashMap<String, bool>,
|
||||
words_tags: Vec<(EntityHandle<WordData>, Vec<String>)>,
|
||||
reverse: bool,
|
||||
search: String,
|
||||
no_typing: bool,
|
||||
selected_group_index: usize,
|
||||
selected_group: EntityHandle<WordGroup>,
|
||||
reverse_list: bool,
|
||||
auto_save_queue: HashMap<usize, DateTime<Utc>>,
|
||||
auto_save_queue: HashMap<EntityHandle<WordData>, DateTime<Utc>>,
|
||||
displayed_words_query: Query<WordData, WordRepositoryInner, WordDataLayer>,
|
||||
groups_query: Query<WordGroup, WordGroupRepositoryInner, WordGroupDataLayer>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
#[derive(Clone)]
|
||||
pub enum DictionaryMessage {
|
||||
Back,
|
||||
SetTags(usize, String),
|
||||
SetKey(usize, String),
|
||||
SetValue(usize, String),
|
||||
SubmitWord(usize),
|
||||
WordAction(usize),
|
||||
SetTags(EntityHandle<WordData>, String),
|
||||
SetKey(EntityHandle<WordData>, String),
|
||||
SetValue(EntityHandle<WordData>, String),
|
||||
WordAction(EntityHandle<WordData>),
|
||||
NewWord,
|
||||
Include(usize, bool),
|
||||
Include(EntityHandle<WordData>, bool),
|
||||
IncludeTag(String, bool),
|
||||
Test,
|
||||
GotoTest,
|
||||
ResetTags,
|
||||
SetReverse(bool),
|
||||
Search(String),
|
||||
@@ -60,11 +62,11 @@ pub enum DictionaryMessage {
|
||||
CreateGroup,
|
||||
EditGroup(String),
|
||||
SaveGroup,
|
||||
SelectGroup(usize),
|
||||
SelectGroup(EntityHandle<WordGroup>),
|
||||
DeleteGroup,
|
||||
ChangeDirection,
|
||||
TrySave(usize),
|
||||
ToImport,
|
||||
TrySave(EntityHandle<WordData>),
|
||||
GotoImport,
|
||||
}
|
||||
|
||||
impl NavigatedPage<DictionaryMessage> for DictionaryState {
|
||||
@@ -72,19 +74,15 @@ impl NavigatedPage<DictionaryMessage> for DictionaryState {
|
||||
if let Back = message {
|
||||
return Some(Page::PreviousPage);
|
||||
}
|
||||
if let Test = message
|
||||
&& self.include_map.iter().any(|x| *x)
|
||||
if let GotoTest = message
|
||||
&& !self.included.is_empty()
|
||||
{
|
||||
let mut words = vec![];
|
||||
let dict = &self.state.lock().dictionary;
|
||||
let dict = &self.state.dictionary.read();
|
||||
|
||||
words = self
|
||||
.include_map
|
||||
.iter()
|
||||
.zip(0..self.include_map.len())
|
||||
.filter(|(flag, _)| **flag)
|
||||
.map(|(_, index)| dict[index].clone())
|
||||
.collect();
|
||||
let mut query = dict.query(Arc::new(|_| true));
|
||||
let handles = query.handles(dict);
|
||||
words = handles.filter(|h| self.included.contains(h)).collect();
|
||||
|
||||
return Some(Page::DictionaryQuiz(DictionaryQuizState::new(
|
||||
words,
|
||||
@@ -92,27 +90,19 @@ impl NavigatedPage<DictionaryMessage> for DictionaryState {
|
||||
self.no_typing,
|
||||
)));
|
||||
}
|
||||
if let WordAction(index) = message {
|
||||
let word: WordData;
|
||||
{
|
||||
let state = self.state.lock();
|
||||
let dict = &state.dictionary;
|
||||
word = dict[*index].clone();
|
||||
}
|
||||
if word.id.is_valid() {
|
||||
return Some(Word(WordState::new(word, *index, self.state.clone())));
|
||||
}
|
||||
if let WordAction(handle) = message
|
||||
&& handle.real()
|
||||
{
|
||||
return Some(Word(WordState::new(*handle, self.state.clone())));
|
||||
}
|
||||
if let ToImport = message {
|
||||
if let GotoImport = message {
|
||||
return Some(Import(ImportState::new(self.state.clone())));
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
fn navigated(&mut self) {
|
||||
let len = self.state.lock().dictionary.len();
|
||||
|
||||
self.include_map = vec![false; len];
|
||||
self.included = Default::default();
|
||||
self.tag_map = Default::default();
|
||||
self.update_tags();
|
||||
}
|
||||
@@ -120,34 +110,30 @@ impl NavigatedPage<DictionaryMessage> for DictionaryState {
|
||||
fn update(&mut self, message: DictionaryMessage) -> Task<RootMessage> {
|
||||
match message {
|
||||
NewWord => {
|
||||
let mut state = self.state.lock();
|
||||
let mut dictionary = self.state.dictionary.write();
|
||||
let mut word = WordData::new();
|
||||
word.group_id = state.word_groups[self.selected_group_index].id;
|
||||
|
||||
let dict = &mut state.dictionary;
|
||||
dict.push(word);
|
||||
self.include_map.push(false);
|
||||
word.group_id = self.selected_group.id;
|
||||
dictionary.create(word);
|
||||
}
|
||||
|
||||
SetKey(i, v) => {
|
||||
{
|
||||
let dict = &mut self.state.lock().dictionary;
|
||||
dict[i].key = v;
|
||||
}
|
||||
return self.launch_auto_save_offset(i);
|
||||
SetKey(handle, v) => {
|
||||
let mut dictionary = self.state.dictionary.write();
|
||||
dictionary.get_mut_static(handle).unwrap().key = v;
|
||||
drop(dictionary);
|
||||
return self.launch_auto_save_offset(handle);
|
||||
}
|
||||
SetValue(i, v) => {
|
||||
{
|
||||
let dict = &mut self.state.lock().dictionary;
|
||||
dict[i].value = v
|
||||
}
|
||||
return self.launch_auto_save_offset(i);
|
||||
SetValue(handle, v) => {
|
||||
let mut dictionary = self.state.dictionary.write();
|
||||
dictionary.get_mut_static(handle).unwrap().value = v;
|
||||
drop(dictionary);
|
||||
return self.launch_auto_save_offset(handle);
|
||||
}
|
||||
SetTags(i, mut v) => {
|
||||
{
|
||||
let dict = &mut self.state.lock().dictionary;
|
||||
let mut dictionary = self.state.dictionary.write();
|
||||
|
||||
let current_tags_value = dict[i].tags.clone();
|
||||
let word = dictionary.get_mut_static(i).unwrap();
|
||||
let current_tags_value = word.tags.clone();
|
||||
|
||||
if v.ends_with(", ") && v.len() < current_tags_value.len() {
|
||||
v = v[..v.len() - 2].to_string()
|
||||
@@ -157,7 +143,7 @@ impl NavigatedPage<DictionaryMessage> for DictionaryState {
|
||||
v.remove(index);
|
||||
}
|
||||
|
||||
dict[i].tags = v;
|
||||
word.tags = v;
|
||||
}
|
||||
|
||||
self.update_tags();
|
||||
@@ -165,93 +151,76 @@ impl NavigatedPage<DictionaryMessage> for DictionaryState {
|
||||
}
|
||||
|
||||
WordAction(i) => {
|
||||
let state = &mut self.state.lock();
|
||||
let dict = &mut state.dictionary;
|
||||
let word = dict.remove(i);
|
||||
self.include_map.remove(i);
|
||||
self.auto_save_queue.remove(&i);
|
||||
if let Err(err) = delete_word(&word, &state.connection) {
|
||||
let mut dictionary = self.state.dictionary.write();
|
||||
|
||||
if let Err(err) = dictionary.delete(i) {
|
||||
return push_error_task!(err);
|
||||
}
|
||||
}
|
||||
Include(i, b) => self.include_map[i] = b,
|
||||
IncludeTag(t, v) => {
|
||||
let index;
|
||||
{
|
||||
let state = self.state.lock();
|
||||
index = state.word_groups[self.selected_group_index].id;
|
||||
Include(i, b) => {
|
||||
if b {
|
||||
self.included.insert(i);
|
||||
} else {
|
||||
self.included.remove(&i);
|
||||
}
|
||||
}
|
||||
IncludeTag(t, v) => {
|
||||
self.tag_map.insert(t, v);
|
||||
self.update_words_include(index)
|
||||
self.update_words_include(self.selected_group.id)
|
||||
}
|
||||
|
||||
ResetTags => {
|
||||
self.tag_map.iter_mut().for_each(|(_, v)| *v = false);
|
||||
self.include_map.iter_mut().for_each(|x| *x = false)
|
||||
self.included.clear();
|
||||
}
|
||||
SetReverse(v) => self.reverse = v,
|
||||
Search(s) => {
|
||||
self.search = s;
|
||||
}
|
||||
SetTyping(b) => self.no_typing = b,
|
||||
SubmitWord(i) => return self.save_word(i),
|
||||
Back => {}
|
||||
Test => {}
|
||||
GotoTest => {}
|
||||
CreateGroup => {
|
||||
let state = &mut self.state.lock();
|
||||
|
||||
state.word_groups.push(WordGroup {
|
||||
let mut groups = self.state.word_groups.write();
|
||||
groups.create(WordGroup {
|
||||
id: 0.into(),
|
||||
name: format!("Группа слов {}", random_range(100..1000)),
|
||||
});
|
||||
}
|
||||
EditGroup(new) => {
|
||||
let state = &mut self.state.lock();
|
||||
let group = state.word_groups.get_mut(self.selected_group_index);
|
||||
let mut groups = self.state.word_groups.write();
|
||||
|
||||
let group = groups.get_mut(&mut self.selected_group);
|
||||
if let Some(group) = group {
|
||||
group.name = new.clone();
|
||||
group.name = new;
|
||||
}
|
||||
}
|
||||
SaveGroup => {
|
||||
let state = &mut self.state.lock();
|
||||
let connection = &state.connection;
|
||||
let mut group = state.word_groups[self.selected_group_index].clone();
|
||||
let mut groups = self.state.word_groups.write();
|
||||
|
||||
if let Err(err) = update_group(&mut group, connection) {
|
||||
if let Err(err) = groups.save(&mut self.selected_group) {
|
||||
return push_error_task!(err);
|
||||
}
|
||||
|
||||
state.word_groups[self.selected_group_index] = group;
|
||||
}
|
||||
SelectGroup(i) => {
|
||||
self.selected_group_index = i;
|
||||
let index;
|
||||
{
|
||||
let state = self.state.lock();
|
||||
index = state.word_groups[i].id;
|
||||
}
|
||||
self.update_words_include(index)
|
||||
self.selected_group = i;
|
||||
self.update_words_include(i.id)
|
||||
}
|
||||
DeleteGroup => {
|
||||
if self.selected_group_index == 0 {
|
||||
if self.selected_group.id.0 == 1 {
|
||||
return Task::none();
|
||||
}
|
||||
{
|
||||
let state = &mut self.state.lock();
|
||||
let mut groups = self.state.word_groups.write();
|
||||
let delete_group = self.selected_group;
|
||||
self.selected_group = groups.handle_by_id(Id(1));
|
||||
|
||||
if let Some(group) = state.word_groups.get(self.selected_group_index) {
|
||||
let remove_group_id = group.id;
|
||||
let connection = &state.connection;
|
||||
if let Err(err) = groups.delete(delete_group) {
|
||||
return push_error_task!(err);
|
||||
}
|
||||
|
||||
if let Err(err) = delete_group(group, connection) {
|
||||
return push_error_task!(err);
|
||||
}
|
||||
|
||||
state.word_groups.remove(self.selected_group_index);
|
||||
state
|
||||
.dictionary
|
||||
.retain(|word| word.group_id != remove_group_id);
|
||||
self.selected_group_index = 0;
|
||||
if let Err(err) = self.state.dictionary.write().reload() {
|
||||
return push_error_task!(err);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -260,18 +229,21 @@ impl NavigatedPage<DictionaryMessage> for DictionaryState {
|
||||
ChangeDirection => {
|
||||
self.reverse_list = !self.reverse_list;
|
||||
}
|
||||
TrySave(word_index) => {
|
||||
TrySave(mut handle) => {
|
||||
let now = Utc::now();
|
||||
if !self.auto_save_queue.contains_key(&word_index) {
|
||||
if !self.auto_save_queue.contains_key(&handle) {
|
||||
return Task::none();
|
||||
}
|
||||
if self.auto_save_queue[&word_index] <= now {
|
||||
self.auto_save_queue.remove(&word_index);
|
||||
if self.auto_save_queue[&handle] <= now {
|
||||
self.auto_save_queue.remove(&handle);
|
||||
|
||||
return self.save_word(word_index);
|
||||
let mut dictionary = self.state.dictionary.write();
|
||||
|
||||
let _ = dictionary.save(&mut handle);
|
||||
return Task::none();
|
||||
}
|
||||
}
|
||||
ToImport => {}
|
||||
GotoImport => {}
|
||||
}
|
||||
|
||||
Task::none()
|
||||
@@ -286,7 +258,7 @@ impl NavigatedPage<DictionaryMessage> for DictionaryState {
|
||||
row![
|
||||
self.add_word_button(),
|
||||
horizontal().width(Fill),
|
||||
button("Импорт").style(text).on_press(ToImport),
|
||||
button("Импорт").style(text).on_press(GotoImport),
|
||||
]
|
||||
]
|
||||
.spacing(5),
|
||||
@@ -301,42 +273,41 @@ impl NavigatedPage<DictionaryMessage> for DictionaryState {
|
||||
|
||||
impl DictionaryState {
|
||||
pub fn new(state: AppState) -> Self {
|
||||
let len = state.lock().dictionary.len();
|
||||
|
||||
let words_query = state.dictionary.read().empty_query_par();
|
||||
let groups_query = state.word_groups.read().query(Arc::new(|_| true));
|
||||
let mut result = DictionaryState {
|
||||
include_map: vec![false; len],
|
||||
selected_group_index: 0,
|
||||
included: Default::default(),
|
||||
selected_group: EntityHandle::new(Id(1), 0, true),
|
||||
state,
|
||||
tag_map: HashMap::new(),
|
||||
words_tags: vec![],
|
||||
reverse: false,
|
||||
search: "".to_string(),
|
||||
no_typing: true,
|
||||
reverse_list: true,
|
||||
auto_save_queue: HashMap::new(),
|
||||
displayed_words_query: words_query,
|
||||
groups_query,
|
||||
};
|
||||
|
||||
result.init();
|
||||
result.update_tags();
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
fn save_word(&mut self, i: usize) -> Task<RootMessage> {
|
||||
let state = &mut self.state.lock();
|
||||
let connection = &state.connection;
|
||||
let word = &mut state.dictionary[i].clone();
|
||||
|
||||
if let Err(err) = update_word(word, connection) {
|
||||
return push_error_task!(err);
|
||||
}
|
||||
|
||||
state.dictionary[i] = word.clone();
|
||||
Task::none()
|
||||
fn init(&mut self) {
|
||||
self.displayed_words_query = self.state.dictionary.read().query_par(Arc::new(|word| word.group_id == Id(1)));
|
||||
}
|
||||
|
||||
fn launch_auto_save_offset(&mut self, index: usize) -> Task<RootMessage> {
|
||||
fn setup_words_query(&mut self, id: Id) {
|
||||
// self.displayed_words_query = self.state.dictionary.read().query_par(Arc::new(|word| word.group_id == Self::get_id(self)));
|
||||
}
|
||||
|
||||
fn launch_auto_save_offset(&mut self, handle: EntityHandle<WordData>) -> Task<RootMessage> {
|
||||
let save_time = Utc::now().add(TimeDelta::milliseconds(900));
|
||||
self.auto_save_queue.insert(index, save_time);
|
||||
let message = RootMessage::Dictionary(TrySave(index));
|
||||
self.auto_save_queue.insert(handle, save_time);
|
||||
let message = RootMessage::Dictionary(TrySave(handle));
|
||||
Task::perform(
|
||||
async { tokio::time::sleep(Duration::from_secs(1)).await },
|
||||
|_| message,
|
||||
@@ -347,22 +318,17 @@ impl DictionaryState {
|
||||
let time = Instant::now();
|
||||
let mut col = Column::new().width(Fill);
|
||||
|
||||
let state = self.state.lock();
|
||||
let group_id = state.word_groups[self.selected_group_index].id;
|
||||
let query = &self.displayed_words_query;
|
||||
let dictionary = self.state.dictionary.read();
|
||||
|
||||
let dict = &state.dictionary;
|
||||
|
||||
let mut index = 0;
|
||||
|
||||
for access_index in 0..dict.len() {
|
||||
for access_index in 0..query.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;
|
||||
i = query.len() - access_index - 1;
|
||||
}
|
||||
let word_handle = &query[i];
|
||||
let word = dictionary.get_ref_static(*word_handle).unwrap();
|
||||
|
||||
if !self.search.is_empty()
|
||||
&& !word.key.contains(&self.search)
|
||||
@@ -373,32 +339,28 @@ impl DictionaryState {
|
||||
}
|
||||
|
||||
let word_line_data = WordLineState {
|
||||
is_included: self.include_map[i],
|
||||
key: word.key.clone(),
|
||||
value: word.value.clone(),
|
||||
tags: word.tags.clone(),
|
||||
id: word.id,
|
||||
index: i,
|
||||
is_included: self.included.contains(word_handle),
|
||||
key: word.key.clone().into(),
|
||||
value: word.value.clone().into(),
|
||||
tags: word.tags.clone().into(),
|
||||
handle: word_handle.clone(),
|
||||
};
|
||||
|
||||
index += 1;
|
||||
|
||||
let lazy_line = lazy(word_line_data, move |data| {
|
||||
let index = data.index;
|
||||
let mut line = Row::new().width(Fill).align_y(Center);
|
||||
line = line.push(
|
||||
checkbox(data.is_included)
|
||||
.label("")
|
||||
.spacing(15)
|
||||
.on_toggle(move |b| Include(index, b)),
|
||||
.on_toggle(move |b| Include(data.handle, b)),
|
||||
);
|
||||
|
||||
line = line.push(
|
||||
text_input("Слово", &data.key)
|
||||
.size(ACCENT_FONT_SIZE)
|
||||
.width(Fill)
|
||||
.on_input(move |string| SetKey(index, string))
|
||||
.on_submit(SubmitWord(index))
|
||||
.on_input(move |string| SetKey(data.handle, string))
|
||||
.style(|x, status| {
|
||||
let mut default_style = default(x, status);
|
||||
default_style.border.radius = 0.0.into();
|
||||
@@ -409,8 +371,7 @@ impl DictionaryState {
|
||||
text_input("Перевод", &data.value)
|
||||
.size(ACCENT_FONT_SIZE)
|
||||
.width(Fill)
|
||||
.on_input(move |string| SetValue(index, string))
|
||||
.on_submit(SubmitWord(index))
|
||||
.on_input(move |string| SetValue(data.handle, string))
|
||||
.style(|x, status| {
|
||||
let mut default_style = default(x, status);
|
||||
default_style.border.radius = 0.0.into();
|
||||
@@ -419,11 +380,10 @@ impl DictionaryState {
|
||||
);
|
||||
|
||||
line = line.push(
|
||||
text_input("Теги", &data.tags)
|
||||
text_input("Теги", data.tags.as_ref())
|
||||
.size(ACCENT_FONT_SIZE)
|
||||
.width(Fill)
|
||||
.on_input(move |string| SetTags(index, string))
|
||||
.on_submit(SubmitWord(index))
|
||||
.on_input(move |string| SetTags(data.handle, string))
|
||||
.style(|x, status| {
|
||||
let mut default_style = default(x, status);
|
||||
default_style.border.radius = 0.0.into();
|
||||
@@ -432,9 +392,9 @@ impl DictionaryState {
|
||||
);
|
||||
|
||||
let line_button = || {
|
||||
let action = WordAction(index);
|
||||
let action = WordAction(data.handle);
|
||||
|
||||
if !data.id.is_valid() {
|
||||
if !data.handle.real() {
|
||||
return button("-").on_press(action).style(|_x, _status| Style {
|
||||
background: None,
|
||||
text_color: Color::BLACK,
|
||||
@@ -444,22 +404,20 @@ impl DictionaryState {
|
||||
});
|
||||
}
|
||||
|
||||
button("").on_press(WordAction(index)).width(15)
|
||||
button("").on_press(WordAction(data.handle)).width(15)
|
||||
};
|
||||
line = line.push(line_button()).push(space().width(10));
|
||||
line
|
||||
line.push(line_button()).push(space().width(10))
|
||||
});
|
||||
|
||||
col = col.push(lazy_line);
|
||||
}
|
||||
|
||||
info!("Drawing {index} lines");
|
||||
info!("Words rendering time: {:?}", time.elapsed());
|
||||
scrollable(col).height(Fill).into()
|
||||
}
|
||||
|
||||
fn filters(&self) -> iced::Element<'_, DictionaryMessage> {
|
||||
let dict = &self.state.lock().dictionary;
|
||||
let dict = &self.state.dictionary.read();
|
||||
|
||||
iced::widget::column![
|
||||
text_input("Поиск", &self.search)
|
||||
@@ -468,7 +426,7 @@ impl DictionaryState {
|
||||
text!("Всего слов: {}", dict.len()),
|
||||
text!(
|
||||
"Выбрано слов: {}",
|
||||
self.include_map.iter().filter(|i| **i).count()
|
||||
self.included.iter().count()
|
||||
),
|
||||
self.tags_selector(),
|
||||
toggler(self.no_typing)
|
||||
@@ -479,7 +437,7 @@ impl DictionaryState {
|
||||
.on_toggle(SetReverse),
|
||||
button(text!("Тест").center().width(Fill))
|
||||
.style(cta_button)
|
||||
.on_press(Test)
|
||||
.on_press(GotoTest)
|
||||
.width(Fill),
|
||||
]
|
||||
.width(250)
|
||||
@@ -515,7 +473,7 @@ impl DictionaryState {
|
||||
|
||||
fn update_tags(&mut self) {
|
||||
let mut tags_list: HashSet<String> = HashSet::new();
|
||||
let dict = &self.state.lock().dictionary;
|
||||
let dict = &self.state.dictionary.write();
|
||||
|
||||
dict.iter().for_each(|element| {
|
||||
split_with_coma(element.tags.as_str())
|
||||
@@ -552,22 +510,29 @@ impl DictionaryState {
|
||||
.collect::<Vec<String>>();
|
||||
|
||||
if include_tags.is_empty() {
|
||||
self.include_map.iter_mut().for_each(|x| *x = false);
|
||||
self.included.clear();
|
||||
return;
|
||||
}
|
||||
|
||||
let dict = &self.state.lock().dictionary;
|
||||
let dict = &self.state.dictionary.read();
|
||||
let time = Instant::now();
|
||||
|
||||
self.include_map = dict
|
||||
.iter()
|
||||
.map(|word| (split_with_coma(word.tags.as_str()), word.group_id))
|
||||
.map(|(tags, word_group_id)| {
|
||||
!tags.is_empty()
|
||||
&& tags.iter().all(|t| include_tags.contains(t))
|
||||
&& word_group_id == group_id
|
||||
})
|
||||
.collect();
|
||||
self.included.clear();
|
||||
for (handle, tags) in &mut self.words_tags {
|
||||
if let Some(word) = dict.get_ref(handle) {
|
||||
if word.group_id != group_id {
|
||||
continue;
|
||||
}
|
||||
|
||||
if !tags.is_empty()
|
||||
&& tags.iter().all(|t| include_tags.contains(t)) {
|
||||
self.included.insert(*handle);
|
||||
}
|
||||
}
|
||||
else {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
info!("Time {}", time.elapsed().as_micros());
|
||||
}
|
||||
@@ -576,18 +541,19 @@ impl DictionaryState {
|
||||
let mut row = Row::new();
|
||||
row = row.push(button("+").style(text).on_press(CreateGroup));
|
||||
|
||||
let state = &self.state.lock();
|
||||
let groups = &state.word_groups;
|
||||
let groups = &self.state.word_groups.read();
|
||||
|
||||
for (index, group) in groups.iter().enumerate() {
|
||||
for mut group in self.groups_query.handles(groups) {
|
||||
let group_name = groups.get(&mut group).unwrap().name.clone();
|
||||
row = row.push(
|
||||
button(text!("{}", group.name.clone()))
|
||||
button(text!("{}", group_name))
|
||||
.style(text)
|
||||
.on_press(SelectGroup(index)),
|
||||
.on_press(SelectGroup(group)),
|
||||
);
|
||||
}
|
||||
|
||||
let group = state.word_groups[self.selected_group_index].clone();
|
||||
let groups = self.state.word_groups.read();
|
||||
let group = groups.get_static(self.selected_group.clone()).unwrap();
|
||||
|
||||
iced::widget::column![
|
||||
scrollable(row).width(Fill).horizontal(),
|
||||
@@ -606,7 +572,7 @@ impl DictionaryState {
|
||||
}
|
||||
|
||||
fn group_delete_button(&self) -> iced::Element<'_, DictionaryMessage> {
|
||||
if self.selected_group_index != 0 {
|
||||
if self.selected_group.real() && self.selected_group.id != 1.into() {
|
||||
button("Удалить").style(danger).on_press(DeleteGroup).into()
|
||||
} else {
|
||||
space().into()
|
||||
@@ -614,11 +580,9 @@ impl DictionaryState {
|
||||
}
|
||||
|
||||
fn add_word_button(&self) -> iced::Element<'_, DictionaryMessage> {
|
||||
let state = self.state.lock();
|
||||
let group_id = state.word_groups[self.selected_group_index].id;
|
||||
let button = button("Добавить слово").style(jl_button);
|
||||
|
||||
if group_id.is_valid() {
|
||||
if self.selected_group.real() {
|
||||
return button.on_press(NewWord).into();
|
||||
}
|
||||
|
||||
@@ -636,9 +600,8 @@ pub fn split_with_coma(ts: &str) -> Vec<String> {
|
||||
#[derive(Hash, PartialEq, Eq)]
|
||||
struct WordLineState {
|
||||
is_included: bool,
|
||||
key: String,
|
||||
value: String,
|
||||
tags: String,
|
||||
id: Id,
|
||||
index: usize,
|
||||
key: Box<str>,
|
||||
value: Box<str>,
|
||||
tags: Box<str>,
|
||||
handle: EntityHandle<WordData>,
|
||||
}
|
||||
Reference in new issue
Block a user