code cleanup
This commit is contained in:
+71
-36
@@ -10,7 +10,9 @@ use crate::{AppState, RootMessage};
|
||||
use iced::widget::button::{danger, Status};
|
||||
pub use iced::widget::button::{Catalog, Style};
|
||||
use iced::widget::container::bordered_box;
|
||||
use iced::widget::{button, column, container, radio, row, scrollable, space, text, text_input, Column};
|
||||
use iced::widget::{
|
||||
button, column, container, radio, row, scrollable, space, text, text_input, Column,
|
||||
};
|
||||
use iced::{Background, Border, Center, Color, Element, Fill, Length, Shadow, Task, Theme};
|
||||
use rhai::{Engine, Scope};
|
||||
use std::sync::{Arc, Mutex};
|
||||
@@ -19,7 +21,7 @@ use std::sync::{Arc, Mutex};
|
||||
pub struct RepetitionsState {
|
||||
selected_set: Option<usize>,
|
||||
correct_filters: Vec<bool>,
|
||||
|
||||
|
||||
pub state: Arc<Mutex<AppState>>,
|
||||
}
|
||||
|
||||
@@ -111,36 +113,48 @@ impl RepetitionsState {
|
||||
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);
|
||||
},
|
||||
}
|
||||
RepetitionsMessage::SetOpenMode(mode) => {
|
||||
state.card_sets.get_mut(self.selected_set.unwrap()).unwrap().open_mode = mode;
|
||||
state
|
||||
.card_sets
|
||||
.get_mut(self.selected_set.unwrap())
|
||||
.unwrap()
|
||||
.open_mode = mode;
|
||||
}
|
||||
}
|
||||
Task::none()
|
||||
}
|
||||
|
||||
pub fn view(&self) -> Element<'_, RepetitionsMessage> {
|
||||
back_overlay(row![
|
||||
column![
|
||||
scrollable(self.sets_list()).height(Fill),
|
||||
button("Добавить").style(jl_button)
|
||||
.width(Fill)
|
||||
.on_press(RepetitionsMessage::CreateSet),
|
||||
]
|
||||
.spacing(DEFAULT_SPACING)
|
||||
.width(Length::FillPortion(1)),
|
||||
self.selected_set_view(),
|
||||
self.launch_button()
|
||||
back_overlay(
|
||||
row![
|
||||
column![
|
||||
scrollable(self.sets_list()).height(Fill),
|
||||
button("Добавить")
|
||||
.style(jl_button)
|
||||
.width(Fill)
|
||||
.on_press(RepetitionsMessage::CreateSet),
|
||||
]
|
||||
.spacing(DEFAULT_SPACING)
|
||||
.width(Length::FillPortion(1)),
|
||||
self.selected_set_view(),
|
||||
self.launch_button()
|
||||
]
|
||||
.align_y(Center)
|
||||
.spacing(DEFAULT_SPACING)
|
||||
.width(Fill)
|
||||
.height(Fill).into(), RepetitionsMessage::Back)
|
||||
.height(Fill)
|
||||
.into(),
|
||||
RepetitionsMessage::Back,
|
||||
)
|
||||
}
|
||||
|
||||
fn launch_button(&self) -> Element<'_, RepetitionsMessage> {
|
||||
if let Some(set) = self.selected_set && self.state.lock().unwrap().card_sets[set].id != 0 {
|
||||
return button(text!("▷").height(Fill).center()).style(jl_button)
|
||||
if let Some(set) = self.selected_set
|
||||
&& self.state.lock().unwrap().card_sets[set].id != 0
|
||||
{
|
||||
return button(text!("▷").height(Fill).center())
|
||||
.style(jl_button)
|
||||
.height(200)
|
||||
.on_press(RepetitionsMessage::GoToRepetition)
|
||||
.into();
|
||||
@@ -165,17 +179,28 @@ impl RepetitionsState {
|
||||
.on_input(RepetitionsMessage::SetBackward),
|
||||
text!("Фильтр"),
|
||||
text_input("", &set.filter).on_input(RepetitionsMessage::SetFilter),
|
||||
button("Проверить фильтр").style(jl_button).on_press(RepetitionsMessage::TryFilter),
|
||||
button("Проверить фильтр")
|
||||
.style(jl_button)
|
||||
.on_press(RepetitionsMessage::TryFilter),
|
||||
self.count_view(&set),
|
||||
radio("Обычный режим", SetOrderMode::Default, Some(set.open_mode), RepetitionsMessage::SetOpenMode),
|
||||
radio(
|
||||
"Обычный режим",
|
||||
SetOrderMode::Default,
|
||||
Some(set.open_mode),
|
||||
RepetitionsMessage::SetOpenMode
|
||||
),
|
||||
self.words_words_view(&set),
|
||||
button("История").style(jl_button).on_press(RepetitionsMessage::GoToHistory),
|
||||
button("История")
|
||||
.style(jl_button)
|
||||
.on_press(RepetitionsMessage::GoToHistory),
|
||||
]
|
||||
.spacing(DEFAULT_SPACING)
|
||||
)
|
||||
.height(Fill),
|
||||
row![
|
||||
button("Сохранить").style(jl_button).on_press(RepetitionsMessage::Save),
|
||||
button("Сохранить")
|
||||
.style(jl_button)
|
||||
.on_press(RepetitionsMessage::Save),
|
||||
button("Удалить")
|
||||
.style(danger)
|
||||
.on_press(RepetitionsMessage::DeleteSet)
|
||||
@@ -193,16 +218,24 @@ impl RepetitionsState {
|
||||
column![
|
||||
text!("Худшие слова"),
|
||||
container(scrollable(self.worst_words_list(&set)).height(200)).style(bordered_box),
|
||||
radio("Начать с плохих слов", SetOrderMode::TrainWorstFirst, Some(set.open_mode), RepetitionsMessage::SetOpenMode),
|
||||
radio("Полностью случайно", SetOrderMode::FullRandom, Some(set.open_mode), RepetitionsMessage::SetOpenMode)
|
||||
radio(
|
||||
"Начать с плохих слов",
|
||||
SetOrderMode::TrainWorstFirst,
|
||||
Some(set.open_mode),
|
||||
RepetitionsMessage::SetOpenMode
|
||||
),
|
||||
radio(
|
||||
"Полностью случайно",
|
||||
SetOrderMode::FullRandom,
|
||||
Some(set.open_mode),
|
||||
RepetitionsMessage::SetOpenMode
|
||||
)
|
||||
]
|
||||
.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() {
|
||||
@@ -226,14 +259,18 @@ impl RepetitionsState {
|
||||
column = column.push(
|
||||
button(text!("{}", set.name.clone()))
|
||||
.on_press_with(move || RepetitionsMessage::SelectSet(i.clone()))
|
||||
.style(move |_x: &Theme, status : Status| Style {
|
||||
background: if status == Status::Hovered {Some(Background::Color(Color::WHITE.scale_alpha(0.1)))} else { None },
|
||||
.style(move |_x: &Theme, status: Status| Style {
|
||||
background: if status == Status::Hovered {
|
||||
Some(Background::Color(Color::WHITE.scale_alpha(0.1)))
|
||||
} else {
|
||||
None
|
||||
},
|
||||
text_color: if self.correct_filters[i.clone()] {
|
||||
_x.palette().primary
|
||||
} else {
|
||||
_x.palette().warning
|
||||
},
|
||||
border: Border{
|
||||
border: Border {
|
||||
color: Default::default(),
|
||||
width: 0.0,
|
||||
radius: 8.0.into(),
|
||||
@@ -247,7 +284,6 @@ impl RepetitionsState {
|
||||
|
||||
column
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
@@ -277,7 +313,7 @@ pub struct CardSetSettings {
|
||||
pub filter: String,
|
||||
pub count: Option<usize>,
|
||||
pub worst_words_list: Option<Vec<WordData>>,
|
||||
pub open_mode: SetOrderMode
|
||||
pub open_mode: SetOrderMode,
|
||||
}
|
||||
|
||||
impl CardSetSettings {
|
||||
@@ -346,7 +382,7 @@ impl CardSetSettings {
|
||||
self.forward == "speech" || self.backward == "speech"
|
||||
}
|
||||
|
||||
fn update_worst_words(&mut self, state: &AppState){
|
||||
fn update_worst_words(&mut self, state: &AppState) {
|
||||
if let Some(_) = self.worst_words_list {
|
||||
return;
|
||||
}
|
||||
@@ -360,15 +396,14 @@ impl CardSetSettings {
|
||||
.iter()
|
||||
.take_while(|word| word.calculated_score() < avg)
|
||||
.map(|stat| {
|
||||
state.dictionary[ state
|
||||
state.dictionary[state
|
||||
.dictionary
|
||||
.binary_search_by_key(&stat.word_id, |x| x.id)
|
||||
.unwrap()].clone()
|
||||
.unwrap()]
|
||||
.clone()
|
||||
})
|
||||
.collect();
|
||||
|
||||
self.worst_words_list = Some(bad.clone());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user