2026-05-05 14:26:34 +03:00
|
|
|
use crate::data_provider::words::{delete_group, delete_word, update_group, update_word};
|
|
|
|
|
use crate::dictionary::DictionaryMessage::{
|
|
|
|
|
ChangeDirection, DeleteGroup, EditGroup, SaveGroup, Test,
|
|
|
|
|
};
|
2026-02-22 11:07:57 +03:00
|
|
|
use crate::dictionary_test::DictionaryQuizState;
|
2026-05-05 14:26:34 +03:00
|
|
|
use crate::lang::{WordData, WordGroup};
|
2026-04-17 12:38:05 +03:00
|
|
|
use crate::word::WordState;
|
|
|
|
|
use crate::Page::Word;
|
2026-08-01 00:15:28 +03:00
|
|
|
use crate::{jl_text_input, AppState, NavigatedPage, Page, RootMessage, DEFAULT_SPACING};
|
2026-05-10 22:15:03 +03:00
|
|
|
use chrono::{DateTime, TimeDelta, Utc};
|
2026-02-22 11:07:57 +03:00
|
|
|
use iced::alignment::Vertical::Center;
|
|
|
|
|
use iced::widget::button::Style;
|
2026-05-05 14:26:34 +03:00
|
|
|
use iced::widget::button::{danger, text};
|
|
|
|
|
use iced::widget::space::horizontal;
|
2026-02-22 11:07:57 +03:00
|
|
|
use iced::widget::*;
|
|
|
|
|
use iced::{Border, Color, Length, Shadow, Task};
|
2026-05-05 14:26:34 +03:00
|
|
|
use rand::random_range;
|
2026-05-12 16:57:05 +03:00
|
|
|
use std::collections::{HashMap, HashSet};
|
2026-02-22 11:07:57 +03:00
|
|
|
use std::fs;
|
2026-05-10 22:15:03 +03:00
|
|
|
use std::ops::Add;
|
2026-02-22 11:07:57 +03:00
|
|
|
use std::path::PathBuf;
|
2026-04-09 22:45:31 +03:00
|
|
|
use std::sync::{Arc, Mutex};
|
2026-05-12 17:12:03 +03:00
|
|
|
use std::time::{Duration, Instant};
|
2026-02-22 11:07:57 +03:00
|
|
|
use DictionaryMessage::Back;
|
|
|
|
|
|
2026-04-09 22:45:31 +03:00
|
|
|
#[derive(Clone)]
|
2026-02-22 11:07:57 +03:00
|
|
|
pub struct DictionaryState {
|
2026-04-09 22:45:31 +03:00
|
|
|
state: Arc<Mutex<AppState>>,
|
2026-02-22 11:07:57 +03:00
|
|
|
include_map: Vec<bool>,
|
|
|
|
|
tag_map: HashMap<String, bool>,
|
2026-03-17 12:16:35 +03:00
|
|
|
reverse: bool,
|
|
|
|
|
search: String,
|
|
|
|
|
no_typing: bool,
|
2026-05-05 14:26:34 +03:00
|
|
|
selected_group_index: usize,
|
|
|
|
|
reverse_list: bool,
|
2026-05-10 22:15:03 +03:00
|
|
|
auto_save_queue: HashMap<usize, DateTime<Utc>>,
|
2026-02-22 11:07:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
|
pub enum DictionaryMessage {
|
|
|
|
|
Back,
|
|
|
|
|
SetTags(usize, String),
|
|
|
|
|
SetKey(usize, String),
|
|
|
|
|
SetValue(usize, String),
|
2026-04-13 22:25:58 +03:00
|
|
|
SubmitWord(usize),
|
2026-04-17 12:38:05 +03:00
|
|
|
WordAction(usize),
|
2026-02-22 11:07:57 +03:00
|
|
|
NewWord,
|
|
|
|
|
Include(usize, bool),
|
|
|
|
|
IncludeTag(String, bool),
|
|
|
|
|
Test,
|
|
|
|
|
ResetTags,
|
|
|
|
|
SetReverse(bool),
|
2026-03-17 12:16:35 +03:00
|
|
|
Search(String),
|
|
|
|
|
SetTyping(bool),
|
2026-05-05 14:26:34 +03:00
|
|
|
CreateGroup,
|
|
|
|
|
EditGroup(String),
|
|
|
|
|
SaveGroup,
|
|
|
|
|
SelectGroup(usize),
|
|
|
|
|
DeleteGroup,
|
|
|
|
|
ChangeDirection,
|
2026-05-10 22:15:03 +03:00
|
|
|
TrySave(usize),
|
2026-02-22 11:07:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl NavigatedPage<DictionaryMessage> for DictionaryState {
|
|
|
|
|
fn navigate(&self, message: &DictionaryMessage) -> Option<Page> {
|
|
|
|
|
if let Back = message {
|
|
|
|
|
return Some(Page::PreviousPage);
|
|
|
|
|
}
|
|
|
|
|
if let Test = message {
|
|
|
|
|
if self.include_map.iter().any(|x| *x) {
|
|
|
|
|
let mut words = vec![];
|
2026-04-09 22:45:31 +03:00
|
|
|
let dict = &self.state.lock().unwrap().dictionary;
|
|
|
|
|
|
2026-05-12 16:57:05 +03:00
|
|
|
words = self
|
|
|
|
|
.include_map
|
2026-05-12 17:12:03 +03:00
|
|
|
.iter()
|
2026-05-12 16:57:05 +03:00
|
|
|
.zip(0..self.include_map.len())
|
|
|
|
|
.filter(|(flag, _)| **flag)
|
|
|
|
|
.map(|(_, index)| dict[index].clone())
|
|
|
|
|
.collect();
|
|
|
|
|
|
2026-03-17 12:16:35 +03:00
|
|
|
return Some(Page::DictionaryQuiz(DictionaryQuizState::new(
|
|
|
|
|
words,
|
|
|
|
|
self.reverse,
|
|
|
|
|
self.no_typing,
|
|
|
|
|
)));
|
2026-02-22 11:07:57 +03:00
|
|
|
}
|
|
|
|
|
}
|
2026-04-17 12:38:05 +03:00
|
|
|
if let DictionaryMessage::WordAction(index) = message {
|
2026-05-05 14:26:34 +03:00
|
|
|
let word: WordData;
|
2026-04-17 12:38:05 +03:00
|
|
|
{
|
|
|
|
|
let state = self.state.lock().unwrap();
|
|
|
|
|
let dict = &state.dictionary;
|
|
|
|
|
word = dict[*index].clone();
|
|
|
|
|
}
|
|
|
|
|
if word.id != 0 {
|
|
|
|
|
return Some(Word(WordState::new(word, *index, self.state.clone())));
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-02-22 11:07:57 +03:00
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl DictionaryState {
|
2026-04-09 22:45:31 +03:00
|
|
|
pub fn new(state: Arc<Mutex<AppState>>) -> Self {
|
|
|
|
|
let len = state.lock().unwrap().dictionary.len();
|
2026-05-05 14:26:34 +03:00
|
|
|
|
2026-02-22 11:07:57 +03:00
|
|
|
let mut result = DictionaryState {
|
2026-04-09 22:45:31 +03:00
|
|
|
include_map: vec![false; len],
|
2026-05-05 14:26:34 +03:00
|
|
|
selected_group_index: 0,
|
2026-04-09 22:45:31 +03:00
|
|
|
state,
|
2026-02-22 11:07:57 +03:00
|
|
|
tag_map: HashMap::new(),
|
|
|
|
|
reverse: false,
|
2026-03-17 12:16:35 +03:00
|
|
|
search: "".to_string(),
|
2026-05-10 22:15:03 +03:00
|
|
|
no_typing: true,
|
2026-05-05 14:26:34 +03:00
|
|
|
reverse_list: true,
|
2026-05-10 22:15:03 +03:00
|
|
|
auto_save_queue: HashMap::new(),
|
2026-02-22 11:07:57 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
result.update_tags();
|
2026-03-17 12:16:35 +03:00
|
|
|
|
2026-02-22 11:07:57 +03:00
|
|
|
result
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn update(&mut self, message: DictionaryMessage) -> Task<RootMessage> {
|
|
|
|
|
match message {
|
|
|
|
|
DictionaryMessage::NewWord => {
|
2026-05-05 14:26:34 +03:00
|
|
|
let mut state = self.state.lock().unwrap();
|
|
|
|
|
let mut word = WordData::new();
|
|
|
|
|
word.group_id = state.word_groups[self.selected_group_index].id.clone();
|
|
|
|
|
|
|
|
|
|
let dict = &mut state.dictionary;
|
|
|
|
|
dict.push(word);
|
2026-02-22 11:07:57 +03:00
|
|
|
self.include_map.push(false);
|
|
|
|
|
}
|
2026-04-13 22:25:58 +03:00
|
|
|
|
2026-04-09 22:45:31 +03:00
|
|
|
DictionaryMessage::SetKey(i, v) => {
|
2026-05-10 22:15:03 +03:00
|
|
|
{
|
|
|
|
|
let dict = &mut self.state.lock().unwrap().dictionary;
|
|
|
|
|
dict[i].key = v;
|
|
|
|
|
}
|
2026-05-12 16:57:05 +03:00
|
|
|
return self.launch_auto_save_offset(i);
|
2026-04-09 22:45:31 +03:00
|
|
|
}
|
|
|
|
|
DictionaryMessage::SetValue(i, v) => {
|
2026-05-10 22:15:03 +03:00
|
|
|
{
|
|
|
|
|
let dict = &mut self.state.lock().unwrap().dictionary;
|
|
|
|
|
dict[i].value = v
|
|
|
|
|
}
|
2026-05-12 16:57:05 +03:00
|
|
|
return self.launch_auto_save_offset(i);
|
2026-04-13 22:25:58 +03:00
|
|
|
}
|
2026-05-25 12:23:32 +03:00
|
|
|
DictionaryMessage::SetTags(i, mut v) => {
|
2026-04-09 22:45:31 +03:00
|
|
|
{
|
|
|
|
|
let dict = &mut self.state.lock().unwrap().dictionary;
|
2026-05-25 12:23:32 +03:00
|
|
|
|
|
|
|
|
let current_tags_value = dict[i].tags.clone();
|
|
|
|
|
|
|
|
|
|
if v.ends_with(", ") && v.len() < current_tags_value.len() {
|
|
|
|
|
v = v[..v.len() - 2].to_string()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
while v.contains(",,") {
|
|
|
|
|
let index = v.find(",,").unwrap();
|
|
|
|
|
v.remove(index);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-09 22:45:31 +03:00
|
|
|
dict.get_mut(i).unwrap().tags = v;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-22 11:07:57 +03:00
|
|
|
self.update_tags();
|
2026-05-12 16:57:05 +03:00
|
|
|
return self.launch_auto_save_offset(i);
|
2026-02-22 11:07:57 +03:00
|
|
|
}
|
2026-04-13 22:25:58 +03:00
|
|
|
|
2026-04-17 12:38:05 +03:00
|
|
|
DictionaryMessage::WordAction(i) => {
|
2026-04-13 22:25:58 +03:00
|
|
|
let state = &mut self.state.lock().unwrap();
|
|
|
|
|
let dict = &mut state.dictionary;
|
|
|
|
|
let word = dict.remove(i);
|
2026-05-08 14:29:43 +03:00
|
|
|
self.include_map.remove(i);
|
2026-05-10 22:15:03 +03:00
|
|
|
self.auto_save_queue.remove(&i);
|
2026-04-13 22:25:58 +03:00
|
|
|
delete_word(&word, &state.connection)
|
2026-02-22 11:07:57 +03:00
|
|
|
}
|
|
|
|
|
DictionaryMessage::Include(i, b) => self.include_map[i] = b,
|
|
|
|
|
DictionaryMessage::IncludeTag(t, v) => {
|
2026-05-12 16:57:05 +03:00
|
|
|
let index: u32;
|
2026-05-10 22:15:03 +03:00
|
|
|
{
|
2026-05-12 16:57:05 +03:00
|
|
|
let state = self.state.lock().unwrap();
|
2026-05-10 22:15:03 +03:00
|
|
|
index = state.word_groups[self.selected_group_index].id;
|
|
|
|
|
}
|
2026-02-22 11:07:57 +03:00
|
|
|
self.tag_map.insert(t, v);
|
2026-05-10 22:15:03 +03:00
|
|
|
self.update_words_include(index)
|
2026-02-22 11:07:57 +03:00
|
|
|
}
|
2026-04-13 22:25:58 +03:00
|
|
|
|
2026-02-22 11:07:57 +03:00
|
|
|
DictionaryMessage::ResetTags => {
|
|
|
|
|
self.tag_map.iter_mut().for_each(|(_, v)| *v = false);
|
|
|
|
|
self.include_map.iter_mut().for_each(|x| *x = false)
|
|
|
|
|
}
|
|
|
|
|
DictionaryMessage::SetReverse(v) => self.reverse = v,
|
2026-03-17 12:16:35 +03:00
|
|
|
DictionaryMessage::Search(s) => {
|
|
|
|
|
self.search = s;
|
|
|
|
|
}
|
|
|
|
|
DictionaryMessage::SetTyping(b) => self.no_typing = b,
|
2026-05-12 16:57:05 +03:00
|
|
|
DictionaryMessage::SubmitWord(i) => self.save_word(i),
|
2026-05-05 14:26:34 +03:00
|
|
|
Back => {}
|
|
|
|
|
Test => {}
|
|
|
|
|
DictionaryMessage::CreateGroup => {
|
|
|
|
|
let state = &mut self.state.lock().unwrap();
|
|
|
|
|
|
|
|
|
|
state.word_groups.push(WordGroup {
|
|
|
|
|
id: 0,
|
|
|
|
|
name: format!("Группа слов {}", random_range(100..1000)),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
EditGroup(new) => {
|
|
|
|
|
let state = &mut self.state.lock().unwrap();
|
|
|
|
|
let group = state.word_groups.get_mut(self.selected_group_index);
|
|
|
|
|
if let Some(group) = group {
|
|
|
|
|
group.name = new.clone();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
SaveGroup => {
|
|
|
|
|
let state = &mut self.state.lock().unwrap();
|
|
|
|
|
let connection = &state.connection;
|
|
|
|
|
let group = &mut state
|
|
|
|
|
.word_groups
|
|
|
|
|
.get(self.selected_group_index)
|
|
|
|
|
.unwrap()
|
|
|
|
|
.clone();
|
|
|
|
|
|
|
|
|
|
update_group(group, connection);
|
|
|
|
|
state.word_groups[self.selected_group_index] = group.clone();
|
|
|
|
|
}
|
|
|
|
|
DictionaryMessage::SelectGroup(i) => {
|
|
|
|
|
self.selected_group_index = i;
|
2026-05-12 16:57:05 +03:00
|
|
|
let index: u32;
|
2026-05-10 22:15:03 +03:00
|
|
|
{
|
|
|
|
|
let state = self.state.lock().unwrap();
|
|
|
|
|
index = state.word_groups[i].id;
|
|
|
|
|
}
|
|
|
|
|
self.update_words_include(index)
|
2026-05-05 14:26:34 +03:00
|
|
|
}
|
|
|
|
|
DeleteGroup => {
|
|
|
|
|
if self.selected_group_index == 0 {
|
|
|
|
|
return Task::none();
|
|
|
|
|
}
|
|
|
|
|
let state = &mut self.state.lock().unwrap();
|
|
|
|
|
|
|
|
|
|
if let Some(group) = state.word_groups.get(self.selected_group_index) {
|
|
|
|
|
let connection = &state.connection;
|
|
|
|
|
|
|
|
|
|
delete_group(group, connection);
|
|
|
|
|
state.word_groups.remove(self.selected_group_index);
|
|
|
|
|
self.selected_group_index = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
ChangeDirection => {
|
|
|
|
|
self.reverse_list = !self.reverse_list;
|
2026-05-12 16:57:05 +03:00
|
|
|
}
|
2026-05-10 22:15:03 +03:00
|
|
|
DictionaryMessage::TrySave(word_index) => {
|
|
|
|
|
let now = Utc::now();
|
2026-05-12 16:57:05 +03:00
|
|
|
if !self.auto_save_queue.contains_key(&word_index) {
|
|
|
|
|
return Task::none();
|
|
|
|
|
}
|
2026-05-10 22:15:03 +03:00
|
|
|
if self.auto_save_queue[&word_index] <= now {
|
|
|
|
|
self.auto_save_queue.remove(&word_index);
|
|
|
|
|
self.save_word(word_index);
|
|
|
|
|
}
|
2026-05-05 14:26:34 +03:00
|
|
|
}
|
2026-02-22 11:07:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Task::none()
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-10 22:15:03 +03:00
|
|
|
fn save_word(&mut self, i: usize) {
|
|
|
|
|
let state = &mut self.state.lock().unwrap();
|
|
|
|
|
let connection = &state.connection;
|
|
|
|
|
let word = &mut state.dictionary.get(i).unwrap().clone();
|
|
|
|
|
|
|
|
|
|
update_word(word, &connection);
|
|
|
|
|
state.dictionary[i] = word.clone();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn launch_auto_save_offset(&mut self, index: usize) -> Task<RootMessage> {
|
2026-05-12 16:57:05 +03:00
|
|
|
let save_time = Utc::now().add(TimeDelta::milliseconds(900));
|
2026-05-10 22:15:03 +03:00
|
|
|
self.auto_save_queue.insert(index, save_time);
|
|
|
|
|
let message = RootMessage::Dictionary(DictionaryMessage::TrySave(index));
|
2026-05-12 16:57:05 +03:00
|
|
|
Task::perform(
|
|
|
|
|
async { tokio::time::sleep(Duration::from_secs(1)).await },
|
|
|
|
|
|_| message,
|
|
|
|
|
)
|
2026-05-10 22:15:03 +03:00
|
|
|
}
|
|
|
|
|
|
2026-02-22 11:07:57 +03:00
|
|
|
pub fn view(&self) -> iced::Element<'_, DictionaryMessage> {
|
2026-03-17 12:16:35 +03:00
|
|
|
container(
|
|
|
|
|
row![
|
|
|
|
|
iced::widget::column![
|
|
|
|
|
button("Назад").on_press(Back),
|
2026-05-05 14:26:34 +03:00
|
|
|
self.groups_panel(),
|
2026-03-17 12:16:35 +03:00
|
|
|
self.words_list(),
|
2026-04-16 09:56:57 +03:00
|
|
|
button("Добавить слово").on_press(DictionaryMessage::NewWord),
|
2026-02-22 11:07:57 +03:00
|
|
|
]
|
2026-03-17 12:16:35 +03:00
|
|
|
.spacing(5),
|
|
|
|
|
self.filters()
|
2026-02-22 11:07:57 +03:00
|
|
|
]
|
2026-05-25 12:23:32 +03:00
|
|
|
.spacing(DEFAULT_SPACING),
|
2026-03-17 12:16:35 +03:00
|
|
|
)
|
2026-05-25 12:23:32 +03:00
|
|
|
.padding(10)
|
|
|
|
|
.into()
|
2026-02-22 11:07:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn words_list(&self) -> iced::Element<'_, DictionaryMessage> {
|
|
|
|
|
let mut col = Column::new().width(Length::Fill);
|
|
|
|
|
|
2026-05-05 14:26:34 +03:00
|
|
|
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>>();
|
|
|
|
|
}
|
2026-04-09 22:45:31 +03:00
|
|
|
|
|
|
|
|
for word in dict {
|
2026-05-05 14:26:34 +03:00
|
|
|
let i = range.pop().unwrap();
|
2026-03-17 12:16:35 +03:00
|
|
|
if !self.search.is_empty() {
|
|
|
|
|
if word.key.contains(&self.search) == false
|
|
|
|
|
&& word.value.contains(&self.search) == false
|
2026-07-29 13:30:49 +03:00
|
|
|
&& word.tags.contains(&self.search) == false
|
2026-03-17 12:16:35 +03:00
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-05 14:26:34 +03:00
|
|
|
if word.group_id != group_id {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-22 11:07:57 +03:00
|
|
|
let mut line = Row::new().width(Length::Fill).align_y(Center);
|
|
|
|
|
line = line
|
|
|
|
|
.push(
|
|
|
|
|
checkbox(self.include_map[i])
|
|
|
|
|
.on_toggle(move |b| DictionaryMessage::Include(i, b)),
|
|
|
|
|
)
|
|
|
|
|
.push(space().width(10));
|
|
|
|
|
|
|
|
|
|
line = line.push(
|
2026-08-01 00:15:28 +03:00
|
|
|
jl_text_input("Слово", &word.key)
|
2026-03-17 12:16:35 +03:00
|
|
|
.size(20)
|
2026-02-22 11:07:57 +03:00
|
|
|
.width(Length::Fill)
|
2026-04-13 22:25:58 +03:00
|
|
|
.on_input(move |string| DictionaryMessage::SetKey(i, string))
|
|
|
|
|
.on_submit(DictionaryMessage::SubmitWord(i)),
|
2026-02-22 11:07:57 +03:00
|
|
|
);
|
|
|
|
|
line = line.push(
|
2026-08-01 00:15:28 +03:00
|
|
|
jl_text_input("Перевод", &word.value)
|
2026-03-17 12:16:35 +03:00
|
|
|
.size(20)
|
2026-02-22 11:07:57 +03:00
|
|
|
.width(Length::Fill)
|
2026-04-13 22:25:58 +03:00
|
|
|
.on_input(move |string| DictionaryMessage::SetValue(i, string))
|
|
|
|
|
.on_submit(DictionaryMessage::SubmitWord(i)),
|
2026-02-22 11:07:57 +03:00
|
|
|
);
|
|
|
|
|
line = line.push(
|
2026-08-01 00:15:28 +03:00
|
|
|
jl_text_input("Тэги", &word.tags)
|
2026-03-17 12:16:35 +03:00
|
|
|
.size(20)
|
2026-02-22 11:07:57 +03:00
|
|
|
.width(Length::Fill)
|
2026-04-13 22:25:58 +03:00
|
|
|
.on_input(move |string| DictionaryMessage::SetTags(i, string))
|
|
|
|
|
.on_submit(DictionaryMessage::SubmitWord(i)),
|
2026-02-22 11:07:57 +03:00
|
|
|
);
|
|
|
|
|
|
2026-04-17 12:38:05 +03:00
|
|
|
let line_button = || {
|
2026-05-05 14:26:34 +03:00
|
|
|
let action = DictionaryMessage::WordAction(i);
|
|
|
|
|
|
2026-04-17 12:38:05 +03:00
|
|
|
if word.id == 0 {
|
2026-05-05 14:26:34 +03:00
|
|
|
return button("-").on_press(action).style(|_x, _status| Style {
|
|
|
|
|
background: None,
|
|
|
|
|
text_color: Color::BLACK,
|
|
|
|
|
border: Border::default(),
|
|
|
|
|
shadow: Shadow::default(),
|
|
|
|
|
snap: false,
|
|
|
|
|
});
|
2026-04-17 12:38:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
button("")
|
2026-05-05 14:26:34 +03:00
|
|
|
.on_press(DictionaryMessage::WordAction(i))
|
2026-04-17 12:38:05 +03:00
|
|
|
.width(15)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
line = line.push(line_button()).push(space().width(10));
|
2026-02-22 11:07:57 +03:00
|
|
|
|
|
|
|
|
col = col.push(line);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
scrollable(col).height(Length::Fill).into()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn filters(&self) -> iced::Element<'_, DictionaryMessage> {
|
2026-04-09 22:45:31 +03:00
|
|
|
let dict = &self.state.lock().unwrap().dictionary;
|
|
|
|
|
|
2026-02-22 11:07:57 +03:00
|
|
|
iced::widget::column![
|
2026-08-01 00:15:28 +03:00
|
|
|
jl_text_input("Поиск", &self.search)
|
2026-03-17 12:16:35 +03:00
|
|
|
.on_input(DictionaryMessage::Search)
|
|
|
|
|
.width(Length::Fill),
|
2026-04-09 22:45:31 +03:00
|
|
|
text!("Всего слов: {}", dict.len()),
|
2026-02-22 11:07:57 +03:00
|
|
|
text!(
|
|
|
|
|
"Выбрано слов: {}",
|
|
|
|
|
self.include_map.iter().filter(|i| **i).count()
|
|
|
|
|
),
|
|
|
|
|
self.tags_selector(),
|
2026-03-17 12:16:35 +03:00
|
|
|
toggler(self.no_typing)
|
|
|
|
|
.label("Без набора")
|
|
|
|
|
.on_toggle(DictionaryMessage::SetTyping),
|
|
|
|
|
toggler(self.reverse)
|
|
|
|
|
.label("Обратный тест")
|
|
|
|
|
.on_toggle(DictionaryMessage::SetReverse),
|
2026-02-22 11:07:57 +03:00
|
|
|
button(text!("Тест").center().width(Length::Fill))
|
|
|
|
|
.on_press(Test)
|
|
|
|
|
.width(Length::Fill),
|
|
|
|
|
]
|
2026-05-25 12:23:32 +03:00
|
|
|
.width(250)
|
|
|
|
|
.spacing(DEFAULT_SPACING)
|
|
|
|
|
.into()
|
2026-02-22 11:07:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn tags_selector(&self) -> iced::Element<'_, DictionaryMessage> {
|
|
|
|
|
let mut col = Column::new().width(Length::Fill);
|
|
|
|
|
col = col.push(
|
|
|
|
|
button("Сбросить")
|
|
|
|
|
.on_press(DictionaryMessage::ResetTags)
|
|
|
|
|
.style(|x: &Theme, _status| Style {
|
|
|
|
|
background: None,
|
|
|
|
|
text_color: x.palette().primary,
|
|
|
|
|
border: Border::default(),
|
|
|
|
|
shadow: Shadow::default(),
|
|
|
|
|
snap: false,
|
|
|
|
|
}),
|
|
|
|
|
);
|
2026-03-17 12:16:35 +03:00
|
|
|
let mut sorted_tags = self.tag_map.iter().collect::<Vec<_>>();
|
|
|
|
|
sorted_tags.sort();
|
|
|
|
|
for tag in sorted_tags {
|
2026-02-22 11:07:57 +03:00
|
|
|
col = col.push(
|
|
|
|
|
checkbox(*tag.1)
|
|
|
|
|
.label(tag.0)
|
|
|
|
|
.on_toggle(|x1| DictionaryMessage::IncludeTag(tag.0.clone(), x1)),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
container(scrollable(col)).height(Length::Fill).into()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn update_tags(&mut self) {
|
2026-05-12 16:57:05 +03:00
|
|
|
let mut tags_list: HashSet<String> = HashSet::new();
|
2026-04-09 22:45:31 +03:00
|
|
|
let dict = &self.state.lock().unwrap().dictionary;
|
|
|
|
|
|
2026-05-12 16:57:05 +03:00
|
|
|
dict.iter().for_each(|element| {
|
|
|
|
|
split_with_coma(element.tags.as_str())
|
|
|
|
|
.iter()
|
|
|
|
|
.for_each(|tag| {
|
|
|
|
|
tags_list.insert(tag.clone());
|
|
|
|
|
});
|
|
|
|
|
});
|
2026-02-22 11:07:57 +03:00
|
|
|
|
2026-05-25 12:23:32 +03:00
|
|
|
|
2026-02-22 11:07:57 +03:00
|
|
|
let current_tags = self
|
|
|
|
|
.tag_map
|
|
|
|
|
.keys()
|
|
|
|
|
.map(|k| k.clone().to_string())
|
|
|
|
|
.collect::<Vec<String>>();
|
|
|
|
|
|
2026-05-12 16:57:05 +03:00
|
|
|
current_tags
|
|
|
|
|
.iter()
|
|
|
|
|
.filter(|tag| !tags_list.contains(*tag))
|
|
|
|
|
.for_each(|tag| {
|
|
|
|
|
self.tag_map.remove(tag);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
tags_list.iter().for_each(|tag| {
|
|
|
|
|
self.tag_map.insert(tag.clone(), false);
|
|
|
|
|
});
|
2026-02-22 11:07:57 +03:00
|
|
|
}
|
|
|
|
|
|
2026-05-10 22:15:03 +03:00
|
|
|
fn update_words_include(&mut self, group_id: u32) {
|
2026-02-22 11:07:57 +03:00
|
|
|
let include_tags = self
|
|
|
|
|
.tag_map
|
|
|
|
|
.iter()
|
2026-05-12 16:57:05 +03:00
|
|
|
.filter(|(_, value)| **value)
|
2026-02-22 11:07:57 +03:00
|
|
|
.map(|(t, _)| t.clone())
|
|
|
|
|
.collect::<Vec<String>>();
|
|
|
|
|
|
|
|
|
|
if include_tags.is_empty() {
|
|
|
|
|
self.include_map.iter_mut().for_each(|x| *x = false);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-09 22:45:31 +03:00
|
|
|
let dict = &self.state.lock().unwrap().dictionary;
|
2026-05-12 17:12:03 +03:00
|
|
|
let time = Instant::now();
|
2026-04-09 22:45:31 +03:00
|
|
|
|
2026-05-25 12:23:32 +03:00
|
|
|
self.include_map = dict
|
|
|
|
|
.iter()
|
2026-05-12 16:57:05 +03:00
|
|
|
.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
|
2026-05-25 12:23:32 +03:00
|
|
|
})
|
|
|
|
|
.collect();
|
2026-05-12 17:12:03 +03:00
|
|
|
|
|
|
|
|
println!("Time {}", time.elapsed().as_micros());
|
2026-02-22 11:07:57 +03:00
|
|
|
}
|
2026-05-05 14:26:34 +03:00
|
|
|
|
|
|
|
|
fn groups_panel(&self) -> iced::Element<'_, DictionaryMessage> {
|
|
|
|
|
let mut row = Row::new();
|
|
|
|
|
row = row.push(
|
|
|
|
|
button("+")
|
|
|
|
|
.style(text)
|
|
|
|
|
.on_press(DictionaryMessage::CreateGroup),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let state = &self.state.lock().unwrap();
|
|
|
|
|
let groups = &state.word_groups;
|
|
|
|
|
|
|
|
|
|
let mut index = 0;
|
|
|
|
|
for group in groups {
|
|
|
|
|
row = row.push(
|
|
|
|
|
button(text!("{}", group.name.clone()))
|
|
|
|
|
.style(text)
|
|
|
|
|
.on_press(DictionaryMessage::SelectGroup(index)),
|
|
|
|
|
);
|
|
|
|
|
index = index + 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let group = state.word_groups[self.selected_group_index].clone();
|
|
|
|
|
|
|
|
|
|
iced::widget::column![
|
|
|
|
|
scrollable(row).width(Length::Fill).horizontal(),
|
|
|
|
|
row![
|
|
|
|
|
button("⇳").on_press(ChangeDirection),
|
2026-08-01 00:15:28 +03:00
|
|
|
jl_text_input("Название группы слов", &group.name)
|
2026-05-05 14:26:34 +03:00
|
|
|
.on_input(EditGroup)
|
|
|
|
|
.width(250)
|
|
|
|
|
.on_submit(SaveGroup),
|
|
|
|
|
horizontal(),
|
|
|
|
|
button("Удалить").style(danger).on_press(DeleteGroup),
|
|
|
|
|
]
|
|
|
|
|
.spacing(DEFAULT_SPACING)
|
|
|
|
|
]
|
2026-05-25 12:23:32 +03:00
|
|
|
.into()
|
2026-05-05 14:26:34 +03:00
|
|
|
}
|
2026-02-22 11:07:57 +03:00
|
|
|
}
|
|
|
|
|
|
2026-05-12 16:57:05 +03:00
|
|
|
pub fn split_with_coma(ts: &str) -> Vec<String> {
|
2026-02-22 11:07:57 +03:00
|
|
|
ts.split(',')
|
|
|
|
|
.map(|ts| ts.to_lowercase().trim().to_string())
|
|
|
|
|
.filter(|t| !t.is_empty())
|
|
|
|
|
.collect::<Vec<String>>()
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-29 12:57:46 +03:00
|
|
|
|
2026-04-13 22:25:58 +03:00
|
|
|
pub fn app_data_dir() -> PathBuf {
|
|
|
|
|
let mut dir = dirs::data_dir().unwrap();
|
|
|
|
|
dir.push("jap_learn");
|
|
|
|
|
if !dir.exists() {
|
|
|
|
|
fs::create_dir(dir.clone()).unwrap();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dir
|
|
|
|
|
}
|
2026-07-29 12:57:46 +03:00
|
|
|
|
|
|
|
|
pub fn app_cache_dir() -> PathBuf {
|
|
|
|
|
let mut dir = dirs::cache_dir().unwrap();
|
|
|
|
|
dir.push("jap_learn");
|
|
|
|
|
if !dir.exists() {
|
|
|
|
|
fs::create_dir(dir.clone()).unwrap();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dir
|
|
|
|
|
}
|