2026-04-13 22:25:58 +03:00
|
|
|
use crate::data_provider::card_sets::{delete_set, update_card_set};
|
2026-05-25 12:23:32 +03:00
|
|
|
use crate::data_provider::card_stats::load_stats_of_set;
|
2026-08-01 17:16:58 +03:00
|
|
|
use crate::history::HistoryState;
|
2026-05-25 12:23:32 +03:00
|
|
|
use crate::lang::{SetOrderMode, WordData};
|
2026-08-04 16:53:09 +03:00
|
|
|
use crate::navigation::Page::{History, PreviousPage, Repetition, RepetitionSettings};
|
2026-08-01 17:16:58 +03:00
|
|
|
use crate::navigation::{NavigatedPage, Page};
|
2026-04-13 22:25:58 +03:00
|
|
|
use crate::repetition::RepetitionState;
|
2026-08-01 17:16:58 +03:00
|
|
|
use crate::styling::*;
|
2026-08-01 13:04:18 +03:00
|
|
|
use crate::{AppState, RootMessage};
|
2026-04-13 22:25:58 +03:00
|
|
|
pub use iced::widget::button::{Catalog, Style};
|
2026-08-04 16:53:09 +03:00
|
|
|
use iced::widget::button::{Status, danger};
|
2026-05-25 12:23:32 +03:00
|
|
|
use iced::widget::container::bordered_box;
|
2026-08-01 19:43:07 +03:00
|
|
|
use iced::widget::{
|
2026-08-04 16:53:09 +03:00
|
|
|
Column, button, column, container, radio, row, scrollable, space, svg, text, text_input,
|
2026-08-01 19:43:07 +03:00
|
|
|
};
|
2026-08-01 19:36:43 +03:00
|
|
|
use iced::{Background, Border, Center, Color, Element, Fill, Length, Shadow, Task, Theme};
|
2026-08-04 14:09:40 +03:00
|
|
|
use iced_core::Padding;
|
2026-08-04 16:53:09 +03:00
|
|
|
use iced_core::svg::Handle;
|
2026-04-09 22:45:31 +03:00
|
|
|
use rhai::{Engine, Scope};
|
|
|
|
|
use std::sync::{Arc, Mutex};
|
2026-08-04 16:53:09 +03:00
|
|
|
use crate::repetition_settings::RepetitionSettingsState;
|
|
|
|
|
use crate::repetitions::RepetitionsMessage::*;
|
2026-04-09 22:45:31 +03:00
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
|
pub struct RepetitionsState {
|
|
|
|
|
selected_set: Option<usize>,
|
|
|
|
|
correct_filters: Vec<bool>,
|
2026-08-01 19:43:07 +03:00
|
|
|
|
2026-04-09 22:45:31 +03:00
|
|
|
pub state: Arc<Mutex<AppState>>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl NavigatedPage<RepetitionsMessage> for RepetitionsState {
|
|
|
|
|
fn navigate(&self, message: &RepetitionsMessage) -> Option<Page> {
|
2026-08-04 16:53:09 +03:00
|
|
|
if let Back = message {
|
2026-04-09 22:45:31 +03:00
|
|
|
Some(PreviousPage)
|
2026-08-04 16:53:09 +03:00
|
|
|
} else if let GoToHistory = message {
|
2026-07-29 12:57:46 +03:00
|
|
|
let card_set;
|
|
|
|
|
{
|
|
|
|
|
card_set = self.state.lock().unwrap().card_sets[self.selected_set.unwrap()].id;
|
|
|
|
|
}
|
|
|
|
|
Some(History(HistoryState::new(card_set, self.state.clone())))
|
2026-08-04 16:53:09 +03:00
|
|
|
} else if let GoToRepetition = message {
|
2026-04-13 22:25:58 +03:00
|
|
|
let clone = self.state.clone();
|
2026-04-14 15:19:46 +03:00
|
|
|
let card_set;
|
|
|
|
|
{
|
|
|
|
|
card_set = self.state.lock().unwrap().card_sets[self.selected_set.unwrap()].clone();
|
|
|
|
|
}
|
2026-05-25 12:23:32 +03:00
|
|
|
Some(Repetition(RepetitionState::new(card_set, clone)))
|
2026-08-04 16:53:09 +03:00
|
|
|
}
|
|
|
|
|
else if let GoToSettings = message {
|
|
|
|
|
Some(RepetitionSettings(RepetitionSettingsState {}))
|
|
|
|
|
}
|
|
|
|
|
else {
|
2026-04-09 22:45:31 +03:00
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl RepetitionsState {
|
|
|
|
|
pub(crate) fn new(state: Arc<Mutex<AppState>>) -> RepetitionsState {
|
2026-04-13 22:25:58 +03:00
|
|
|
let count = state.lock().unwrap().card_sets.len();
|
2026-04-09 22:45:31 +03:00
|
|
|
RepetitionsState {
|
|
|
|
|
selected_set: None,
|
2026-04-13 22:25:58 +03:00
|
|
|
correct_filters: vec![true; count],
|
2026-04-09 22:45:31 +03:00
|
|
|
state,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl RepetitionsState {
|
|
|
|
|
pub fn update(&mut self, message: RepetitionsMessage) -> Task<RootMessage> {
|
2026-04-13 22:25:58 +03:00
|
|
|
let mut state = self.state.lock().unwrap();
|
2026-04-09 22:45:31 +03:00
|
|
|
match message {
|
2026-08-04 16:53:09 +03:00
|
|
|
Next => {}
|
|
|
|
|
Back => {}
|
|
|
|
|
GoToRepetition => {}
|
|
|
|
|
GoToHistory => {}
|
|
|
|
|
GoToSettings => {}
|
|
|
|
|
CreateSet => {
|
2026-04-13 22:25:58 +03:00
|
|
|
let index = state.card_sets.len() + 1;
|
|
|
|
|
state
|
|
|
|
|
.card_sets
|
|
|
|
|
.push(CardSetSettings::with_name(format!("Card set #{}", index)));
|
2026-04-09 22:45:31 +03:00
|
|
|
self.correct_filters.push(true);
|
|
|
|
|
}
|
2026-08-04 16:53:09 +03:00
|
|
|
DeleteSet => {
|
2026-04-13 22:25:58 +03:00
|
|
|
let set = state.card_sets.remove(self.selected_set.unwrap());
|
2026-04-09 22:45:31 +03:00
|
|
|
self.correct_filters.remove(self.selected_set.unwrap());
|
|
|
|
|
self.selected_set = None;
|
2026-04-13 22:25:58 +03:00
|
|
|
delete_set(&set, &state.connection);
|
2026-04-09 22:45:31 +03:00
|
|
|
}
|
2026-08-04 16:53:09 +03:00
|
|
|
SelectSet(index) => {
|
2026-04-09 22:45:31 +03:00
|
|
|
self.selected_set = Some(index);
|
2026-05-25 12:23:32 +03:00
|
|
|
let mut set = state.card_sets.get(index).unwrap().clone();
|
|
|
|
|
set.update_worst_words(&state);
|
|
|
|
|
state.card_sets[index] = set;
|
2026-04-09 22:45:31 +03:00
|
|
|
}
|
2026-08-04 16:53:09 +03:00
|
|
|
SetName(new) => {
|
2026-04-13 22:25:58 +03:00
|
|
|
state.card_sets[self.selected_set.unwrap()].name = new;
|
2026-04-09 22:45:31 +03:00
|
|
|
}
|
2026-08-04 16:53:09 +03:00
|
|
|
Save => {
|
2026-04-13 22:25:58 +03:00
|
|
|
for i in 0..state.card_sets.len() {
|
|
|
|
|
self.correct_filters[i] = state.card_sets[i].check_filter();
|
2026-04-09 22:45:31 +03:00
|
|
|
}
|
|
|
|
|
|
2026-04-13 22:25:58 +03:00
|
|
|
let word = &mut state.card_sets[self.selected_set.unwrap()].clone();
|
|
|
|
|
|
|
|
|
|
update_card_set(word, &state.connection);
|
|
|
|
|
|
|
|
|
|
state.card_sets[self.selected_set.unwrap()] = word.clone();
|
2026-04-09 22:45:31 +03:00
|
|
|
}
|
2026-08-04 16:53:09 +03:00
|
|
|
SetForward(new) => {
|
2026-04-13 22:25:58 +03:00
|
|
|
state.card_sets[self.selected_set.unwrap()].forward = new;
|
2026-04-09 22:45:31 +03:00
|
|
|
}
|
2026-08-04 16:53:09 +03:00
|
|
|
SetBackward(new) => {
|
2026-04-13 22:25:58 +03:00
|
|
|
state.card_sets[self.selected_set.unwrap()].backward = new;
|
2026-04-09 22:45:31 +03:00
|
|
|
}
|
2026-08-04 16:53:09 +03:00
|
|
|
SetFilter(new) => {
|
2026-04-13 22:25:58 +03:00
|
|
|
state.card_sets[self.selected_set.unwrap()].filter = new;
|
2026-04-09 22:45:31 +03:00
|
|
|
}
|
2026-08-04 16:53:09 +03:00
|
|
|
TryFilter => {
|
2026-04-13 22:25:58 +03:00
|
|
|
let set = state.card_sets.get(self.selected_set.unwrap()).unwrap();
|
|
|
|
|
let count = set.get_word_list(&state).len();
|
|
|
|
|
state.card_sets[self.selected_set.unwrap()].count = Some(count);
|
2026-08-01 19:43:07 +03:00
|
|
|
}
|
2026-08-04 16:53:09 +03:00
|
|
|
SetOpenMode(mode) => {
|
2026-08-01 19:43:07 +03:00
|
|
|
state
|
|
|
|
|
.card_sets
|
|
|
|
|
.get_mut(self.selected_set.unwrap())
|
|
|
|
|
.unwrap()
|
|
|
|
|
.open_mode = mode;
|
2026-08-04 16:53:09 +03:00
|
|
|
},
|
2026-04-09 22:45:31 +03:00
|
|
|
}
|
|
|
|
|
Task::none()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn view(&self) -> Element<'_, RepetitionsMessage> {
|
2026-08-01 19:43:07 +03:00
|
|
|
back_overlay(
|
|
|
|
|
row![
|
|
|
|
|
column![
|
|
|
|
|
scrollable(self.sets_list()).height(Fill),
|
|
|
|
|
button("Добавить")
|
|
|
|
|
.style(jl_button)
|
|
|
|
|
.width(Fill)
|
2026-08-04 16:53:09 +03:00
|
|
|
.on_press(CreateSet),
|
2026-04-09 22:45:31 +03:00
|
|
|
]
|
2026-08-01 19:43:07 +03:00
|
|
|
.spacing(DEFAULT_SPACING)
|
|
|
|
|
.width(Length::FillPortion(1)),
|
|
|
|
|
self.selected_set_view(),
|
|
|
|
|
]
|
2026-08-01 19:36:43 +03:00
|
|
|
.align_y(Center)
|
|
|
|
|
.spacing(DEFAULT_SPACING)
|
|
|
|
|
.width(Fill)
|
2026-08-01 19:43:07 +03:00
|
|
|
.height(Fill)
|
|
|
|
|
.into(),
|
2026-08-04 16:53:09 +03:00
|
|
|
Back,
|
2026-08-01 19:43:07 +03:00
|
|
|
)
|
2026-04-09 22:45:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn launch_button(&self) -> Element<'_, RepetitionsMessage> {
|
2026-08-01 19:43:07 +03:00
|
|
|
if let Some(set) = self.selected_set
|
|
|
|
|
&& self.state.lock().unwrap().card_sets[set].id != 0
|
|
|
|
|
{
|
2026-08-04 14:09:40 +03:00
|
|
|
return button(text!("Начать повторение").width(200).center())
|
|
|
|
|
.style(cta_button)
|
2026-08-04 16:53:09 +03:00
|
|
|
.on_press(GoToRepetition)
|
2026-04-09 22:45:31 +03:00
|
|
|
.into();
|
|
|
|
|
}
|
|
|
|
|
space().into()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn selected_set_view(&self) -> Element<'_, RepetitionsMessage> {
|
|
|
|
|
if let Some(index) = self.selected_set {
|
2026-05-25 12:23:32 +03:00
|
|
|
let set;
|
|
|
|
|
{
|
|
|
|
|
set = self.state.lock().unwrap().card_sets[index].clone();
|
|
|
|
|
}
|
2026-04-09 22:45:31 +03:00
|
|
|
return column![
|
|
|
|
|
scrollable(
|
|
|
|
|
column![
|
2026-08-04 14:09:40 +03:00
|
|
|
column![
|
|
|
|
|
text!("Название набора"),
|
2026-08-04 16:53:09 +03:00
|
|
|
row![
|
|
|
|
|
text_input("", &set.name).on_input(SetName),
|
|
|
|
|
button(svg(Handle::from_memory(SETTINGS_ICON))).width(43).on_press(GoToSettings),
|
|
|
|
|
]
|
|
|
|
|
.spacing(HALF_SPACING)
|
2026-08-04 14:09:40 +03:00
|
|
|
]
|
|
|
|
|
.spacing(QUARTER_SPACING),
|
|
|
|
|
column![
|
|
|
|
|
text!("Передняя сторона"),
|
2026-08-04 16:53:09 +03:00
|
|
|
text_input("", &set.forward).on_input(SetForward),
|
2026-08-04 14:09:40 +03:00
|
|
|
]
|
|
|
|
|
.spacing(QUARTER_SPACING),
|
|
|
|
|
column![
|
|
|
|
|
text!("Задняя сторона"),
|
2026-08-04 16:53:09 +03:00
|
|
|
text_input("", &set.backward).on_input(SetBackward),
|
2026-08-04 14:09:40 +03:00
|
|
|
]
|
|
|
|
|
.spacing(QUARTER_SPACING),
|
|
|
|
|
column![
|
|
|
|
|
text!("Фильтр"),
|
2026-08-04 16:53:09 +03:00
|
|
|
text_input("", &set.filter).on_input(SetFilter),
|
2026-08-04 14:09:40 +03:00
|
|
|
button("Проверить фильтр")
|
|
|
|
|
.style(jl_button)
|
2026-08-04 16:53:09 +03:00
|
|
|
.on_press(TryFilter),
|
2026-08-04 14:09:40 +03:00
|
|
|
self.count_view(&set),
|
|
|
|
|
]
|
|
|
|
|
.spacing(QUARTER_SPACING),
|
2026-08-01 19:43:07 +03:00
|
|
|
radio(
|
|
|
|
|
"Обычный режим",
|
|
|
|
|
SetOrderMode::Default,
|
|
|
|
|
Some(set.open_mode),
|
2026-08-04 16:53:09 +03:00
|
|
|
SetOpenMode
|
2026-08-01 19:43:07 +03:00
|
|
|
),
|
2026-07-05 01:58:32 +03:00
|
|
|
self.words_words_view(&set),
|
2026-08-01 19:43:07 +03:00
|
|
|
button("История")
|
|
|
|
|
.style(jl_button)
|
2026-08-04 16:53:09 +03:00
|
|
|
.on_press(GoToHistory),
|
2026-04-09 22:45:31 +03:00
|
|
|
]
|
2026-04-16 09:56:57 +03:00
|
|
|
.spacing(DEFAULT_SPACING)
|
2026-08-04 14:09:40 +03:00
|
|
|
.padding(Padding {
|
|
|
|
|
top: 0.0,
|
|
|
|
|
right: DEFAULT_SPACING,
|
|
|
|
|
bottom: 0.0,
|
|
|
|
|
left: 0.0,
|
|
|
|
|
})
|
2026-04-09 22:45:31 +03:00
|
|
|
)
|
|
|
|
|
.height(Fill),
|
2026-04-13 22:25:58 +03:00
|
|
|
row![
|
2026-08-01 19:43:07 +03:00
|
|
|
button("Сохранить")
|
|
|
|
|
.style(jl_button)
|
2026-08-04 16:53:09 +03:00
|
|
|
.on_press(Save),
|
2026-04-13 22:25:58 +03:00
|
|
|
button("Удалить")
|
2026-04-17 12:38:05 +03:00
|
|
|
.style(danger)
|
2026-08-04 16:53:09 +03:00
|
|
|
.on_press(DeleteSet),
|
2026-08-04 14:09:40 +03:00
|
|
|
space().width(Fill),
|
|
|
|
|
self.launch_button()
|
2026-04-13 22:25:58 +03:00
|
|
|
]
|
2026-04-16 09:56:57 +03:00
|
|
|
.spacing(DEFAULT_SPACING),
|
2026-04-09 22:45:31 +03:00
|
|
|
]
|
2026-04-16 09:56:57 +03:00
|
|
|
.spacing(DEFAULT_SPACING)
|
2026-08-04 14:09:40 +03:00
|
|
|
.width(Length::FillPortion(3))
|
2026-04-13 22:25:58 +03:00
|
|
|
.into();
|
2026-04-09 22:45:31 +03:00
|
|
|
}
|
2026-08-04 14:09:40 +03:00
|
|
|
space().width(Length::FillPortion(3)).into()
|
2026-04-09 22:45:31 +03:00
|
|
|
}
|
|
|
|
|
|
2026-05-25 12:23:32 +03:00
|
|
|
fn words_words_view(&self, set: &CardSetSettings) -> Element<'_, RepetitionsMessage> {
|
|
|
|
|
column![
|
|
|
|
|
text!("Худшие слова"),
|
|
|
|
|
container(scrollable(self.worst_words_list(&set)).height(200)).style(bordered_box),
|
2026-08-01 19:43:07 +03:00
|
|
|
radio(
|
|
|
|
|
"Начать с плохих слов",
|
|
|
|
|
SetOrderMode::TrainWorstFirst,
|
|
|
|
|
Some(set.open_mode),
|
2026-08-04 16:53:09 +03:00
|
|
|
SetOpenMode
|
2026-08-01 19:43:07 +03:00
|
|
|
),
|
|
|
|
|
radio(
|
|
|
|
|
"Полностью случайно",
|
|
|
|
|
SetOrderMode::FullRandom,
|
|
|
|
|
Some(set.open_mode),
|
2026-08-04 16:53:09 +03:00
|
|
|
SetOpenMode
|
2026-08-01 19:43:07 +03:00
|
|
|
)
|
2026-05-25 12:23:32 +03:00
|
|
|
]
|
|
|
|
|
.spacing(DEFAULT_SPACING)
|
|
|
|
|
.into()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn worst_words_list(&self, set: &CardSetSettings) -> Element<'_, RepetitionsMessage> {
|
|
|
|
|
let mut column = Column::new();
|
|
|
|
|
|
|
|
|
|
for word in set.worst_words_list.clone().unwrap() {
|
2026-08-01 01:22:26 +03:00
|
|
|
column = column.push(text!("{} | {}", &word.key, &word.value));
|
2026-05-25 12:23:32 +03:00
|
|
|
}
|
|
|
|
|
column.into()
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-13 22:25:58 +03:00
|
|
|
fn count_view(&self, set: &CardSetSettings) -> Element<'_, RepetitionsMessage> {
|
|
|
|
|
if let Some(count) = set.count {
|
2026-08-01 19:36:43 +03:00
|
|
|
return text!("Количество слов: {}", count).into();
|
2026-04-09 22:45:31 +03:00
|
|
|
}
|
|
|
|
|
space().into()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn sets_list(&self) -> Column<'_, RepetitionsMessage> {
|
|
|
|
|
let mut column = Column::new();
|
|
|
|
|
let mut i = 0;
|
2026-04-13 22:25:58 +03:00
|
|
|
let sets = &self.state.lock().unwrap().card_sets;
|
|
|
|
|
for set in sets {
|
2026-04-09 22:45:31 +03:00
|
|
|
column = column.push(
|
|
|
|
|
button(text!("{}", set.name.clone()))
|
2026-08-04 16:53:09 +03:00
|
|
|
.on_press_with(move || SelectSet(i.clone()))
|
2026-08-01 19:43:07 +03:00
|
|
|
.style(move |_x: &Theme, status: Status| Style {
|
|
|
|
|
background: if status == Status::Hovered {
|
2026-08-04 14:09:40 +03:00
|
|
|
Some(Background::Color(Color::WHITE.scale_alpha(0.2)))
|
2026-08-01 19:43:07 +03:00
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
},
|
2026-04-09 22:45:31 +03:00
|
|
|
text_color: if self.correct_filters[i.clone()] {
|
2026-05-12 16:57:05 +03:00
|
|
|
_x.palette().primary
|
2026-04-09 22:45:31 +03:00
|
|
|
} else {
|
|
|
|
|
_x.palette().warning
|
|
|
|
|
},
|
2026-08-01 19:43:07 +03:00
|
|
|
border: Border {
|
2026-08-01 13:04:18 +03:00
|
|
|
color: Default::default(),
|
|
|
|
|
width: 0.0,
|
2026-08-04 14:09:40 +03:00
|
|
|
radius: BUTTON_RADIUS.into(),
|
2026-08-01 13:04:18 +03:00
|
|
|
},
|
2026-04-09 22:45:31 +03:00
|
|
|
shadow: Shadow::default(),
|
|
|
|
|
snap: false,
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
i += 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
column
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-04-14 15:19:46 +03:00
|
|
|
|
2026-05-25 12:23:32 +03:00
|
|
|
#[derive(Clone)]
|
2026-04-09 22:45:31 +03:00
|
|
|
pub enum RepetitionsMessage {
|
|
|
|
|
Next,
|
|
|
|
|
Back,
|
|
|
|
|
GoToRepetition,
|
|
|
|
|
CreateSet,
|
|
|
|
|
DeleteSet,
|
|
|
|
|
SetName(String),
|
|
|
|
|
SelectSet(usize),
|
|
|
|
|
Save,
|
|
|
|
|
SetForward(String),
|
|
|
|
|
SetBackward(String),
|
|
|
|
|
SetFilter(String),
|
|
|
|
|
TryFilter,
|
2026-07-05 01:58:32 +03:00
|
|
|
SetOpenMode(SetOrderMode),
|
|
|
|
|
GoToHistory,
|
2026-08-04 16:53:09 +03:00
|
|
|
GoToSettings,
|
2026-04-09 22:45:31 +03:00
|
|
|
}
|
|
|
|
|
|
2026-05-25 12:23:32 +03:00
|
|
|
#[derive(Clone)]
|
2026-04-13 22:25:58 +03:00
|
|
|
pub struct CardSetSettings {
|
|
|
|
|
pub id: u32,
|
|
|
|
|
pub name: String,
|
|
|
|
|
pub forward: String,
|
|
|
|
|
pub backward: String,
|
|
|
|
|
pub filter: String,
|
|
|
|
|
pub count: Option<usize>,
|
2026-05-25 12:23:32 +03:00
|
|
|
pub worst_words_list: Option<Vec<WordData>>,
|
2026-08-01 19:43:07 +03:00
|
|
|
pub open_mode: SetOrderMode,
|
2026-04-09 22:45:31 +03:00
|
|
|
}
|
|
|
|
|
|
2026-04-13 22:25:58 +03:00
|
|
|
impl CardSetSettings {
|
|
|
|
|
fn with_name(name: String) -> CardSetSettings {
|
|
|
|
|
CardSetSettings {
|
|
|
|
|
id: 0,
|
2026-04-09 22:45:31 +03:00
|
|
|
name,
|
|
|
|
|
forward: "".to_string(),
|
|
|
|
|
backward: "".to_string(),
|
|
|
|
|
filter: "true".to_string(),
|
|
|
|
|
count: None,
|
2026-05-25 12:23:32 +03:00
|
|
|
worst_words_list: None,
|
|
|
|
|
open_mode: SetOrderMode::Default,
|
2026-04-09 22:45:31 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn check_filter(&self) -> bool {
|
|
|
|
|
let engine = Engine::new();
|
|
|
|
|
let ast = engine.compile(&self.filter);
|
|
|
|
|
ast.is_ok()
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-05 12:02:59 +03:00
|
|
|
pub fn get_word_list(&self, state: &AppState) -> Vec<WordData> {
|
2026-04-09 22:45:31 +03:00
|
|
|
let mut list = vec![];
|
|
|
|
|
let engine = Engine::new();
|
|
|
|
|
let ast = engine.compile(&self.filter);
|
|
|
|
|
if ast.is_err() {
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let ast = ast.unwrap();
|
|
|
|
|
|
2026-05-10 22:15:03 +03:00
|
|
|
let groups = &state.word_groups;
|
|
|
|
|
|
2026-04-09 22:45:31 +03:00
|
|
|
for word in &state.dictionary {
|
|
|
|
|
let mut more = rhai::Map::new();
|
|
|
|
|
for iced in &word.additional {
|
|
|
|
|
more.insert(iced.0.clone().into(), iced.1.clone().into());
|
|
|
|
|
}
|
|
|
|
|
let mut scope = Scope::new();
|
2026-04-13 22:25:58 +03:00
|
|
|
scope
|
|
|
|
|
.push_constant("key", word.key.clone())
|
2026-04-09 22:45:31 +03:00
|
|
|
.push_constant("value", word.value.clone())
|
|
|
|
|
.push_constant("tags", word.tags.clone())
|
2026-05-10 22:15:03 +03:00
|
|
|
.push_constant("more", more)
|
2026-05-25 12:23:32 +03:00
|
|
|
.push_constant(
|
|
|
|
|
"group",
|
|
|
|
|
groups
|
|
|
|
|
.iter()
|
|
|
|
|
.find(|g| g.id == word.group_id)
|
|
|
|
|
.cloned()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.name,
|
|
|
|
|
);
|
2026-04-09 22:45:31 +03:00
|
|
|
|
|
|
|
|
let result = engine.eval_ast_with_scope::<bool>(&mut scope, &ast);
|
|
|
|
|
if result.is_ok() && result.unwrap() {
|
|
|
|
|
list.push(word.clone());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
list
|
|
|
|
|
}
|
2026-04-20 15:21:31 +03:00
|
|
|
|
|
|
|
|
pub fn require_speech(&self) -> bool {
|
2026-05-25 12:23:32 +03:00
|
|
|
self.forward == "speech" || self.backward == "speech"
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-01 19:43:07 +03:00
|
|
|
fn update_worst_words(&mut self, state: &AppState) {
|
2026-05-25 12:23:32 +03:00
|
|
|
if let Some(_) = self.worst_words_list {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let connection = &state.connection;
|
|
|
|
|
let mut stats = load_stats_of_set(self, connection);
|
|
|
|
|
stats.sort_by_key(|s| s.calculated_score() as i32);
|
|
|
|
|
let avg = stats.iter().map(|s| s.calculated_score()).sum::<f32>() / stats.len() as f32;
|
|
|
|
|
let avg = avg * 0.7;
|
|
|
|
|
let bad: Vec<WordData> = stats
|
|
|
|
|
.iter()
|
|
|
|
|
.take_while(|word| word.calculated_score() < avg)
|
|
|
|
|
.map(|stat| {
|
2026-08-01 19:43:07 +03:00
|
|
|
state.dictionary[state
|
2026-05-25 12:23:32 +03:00
|
|
|
.dictionary
|
|
|
|
|
.binary_search_by_key(&stat.word_id, |x| x.id)
|
2026-08-01 19:43:07 +03:00
|
|
|
.unwrap()]
|
|
|
|
|
.clone()
|
2026-05-25 12:23:32 +03:00
|
|
|
})
|
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
|
|
self.worst_words_list = Some(bad.clone());
|
2026-04-20 15:21:31 +03:00
|
|
|
}
|
2026-04-09 22:45:31 +03:00
|
|
|
}
|