Adding words by parts
This commit is contained in:
+171
-46
@@ -1,6 +1,7 @@
|
||||
use crate::data_provider::card_sets::{delete_set, update_card_set};
|
||||
use crate::data_provider::card_stats::{add_stat_list, load_stats_of_set};
|
||||
use crate::history::HistoryState;
|
||||
use crate::lang::{AppendMode, CardSetSettings, OrderMode};
|
||||
use crate::lang::{AppendMode, CardSetSettings, CardStatistics, OrderMode};
|
||||
use crate::navigation::Page::{History, PreviousPage, Repetition, RepetitionSettings};
|
||||
use crate::navigation::{NavigatedPage, Page};
|
||||
use crate::repetition::RepetitionState;
|
||||
@@ -8,22 +9,27 @@ use crate::repetition_settings::RepetitionSettingsState;
|
||||
use crate::repetitions::RepetitionsMessage::*;
|
||||
use crate::styling::*;
|
||||
use crate::{AppState, RootMessage};
|
||||
use chrono::{Days, Local};
|
||||
use iced::widget::button::{danger, Status};
|
||||
use chrono::{Days, Local, Utc};
|
||||
use hashbrown::{HashMap, HashSet};
|
||||
pub use iced::widget::button::{Catalog, Style};
|
||||
use iced::widget::button::{Status, danger};
|
||||
use iced::widget::container::bordered_box;
|
||||
use iced::widget::tooltip::Position::Top;
|
||||
use iced::widget::{button, column, container, lazy, radio, row, scrollable, space, svg, text, text_input, tooltip, Column, Row};
|
||||
use iced::widget::{
|
||||
Column, Row, button, column, container, lazy, radio, row, scrollable, space, svg, text,
|
||||
text_input, tooltip,
|
||||
};
|
||||
use iced::{Background, Border, Center, Color, Element, Fill, Length, Shadow, Task, Theme};
|
||||
use iced_core::Padding;
|
||||
use iced_core::border::Radius;
|
||||
use iced_core::svg::Handle;
|
||||
use iced_core::Padding;
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct RepetitionsState {
|
||||
selected_set: Option<usize>,
|
||||
correct_filters: Vec<bool>,
|
||||
current_sets_cards_cache: HashMap<usize, (Vec<usize>, Vec<usize>)>,
|
||||
pub state: Arc<Mutex<AppState>>,
|
||||
}
|
||||
|
||||
@@ -43,6 +49,11 @@ impl NavigatedPage<RepetitionsMessage> for RepetitionsState {
|
||||
{
|
||||
card_set = self.state.lock().unwrap().card_sets[self.selected_set.unwrap()].clone();
|
||||
}
|
||||
|
||||
if card_set.append_mode == AppendMode::Full {
|
||||
self.append_all_words(&card_set);
|
||||
}
|
||||
|
||||
Some(Repetition(RepetitionState::new(card_set, clone)))
|
||||
} else if let GoToSettings = message {
|
||||
Some(RepetitionSettings(RepetitionSettingsState::new(
|
||||
@@ -53,9 +64,9 @@ impl NavigatedPage<RepetitionsMessage> for RepetitionsState {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn navigated(&mut self) {
|
||||
self.selected_set = None;
|
||||
self.current_sets_cards_cache.clear();
|
||||
}
|
||||
fn update(&mut self, message: RepetitionsMessage) -> Task<RootMessage> {
|
||||
let mut state = self.state.lock().unwrap();
|
||||
@@ -80,8 +91,15 @@ impl NavigatedPage<RepetitionsMessage> for RepetitionsState {
|
||||
}
|
||||
SelectSet(index) => {
|
||||
self.selected_set = Some(index);
|
||||
let mut set = state.card_sets.get(index).unwrap().clone();
|
||||
set.update_worst_words(&state);
|
||||
let set = state.card_sets.get(index).unwrap().clone();
|
||||
if !self.current_sets_cards_cache.contains_key(&index) {
|
||||
let added = load_stats_of_set(&set, &state.connection)
|
||||
.iter()
|
||||
.map(|c| c.word_id as usize)
|
||||
.collect();
|
||||
let total = set.get_word_list(&state);
|
||||
self.current_sets_cards_cache.insert(index, (added, total));
|
||||
}
|
||||
state.card_sets[index] = set;
|
||||
}
|
||||
SetName(new) => {
|
||||
@@ -123,6 +141,31 @@ impl NavigatedPage<RepetitionsMessage> for RepetitionsState {
|
||||
let set = state.card_sets.get_mut(self.selected_set.unwrap()).unwrap();
|
||||
set.append_mode = mode;
|
||||
}
|
||||
AppendWords(count) => {
|
||||
let adding: Vec<usize>;
|
||||
let set = state.card_sets.get(self.selected_set.unwrap()).unwrap();
|
||||
let index = self.selected_set.unwrap();
|
||||
{
|
||||
let total_cache = &mut self.current_sets_cards_cache;
|
||||
let cache = total_cache.get_mut(&index).unwrap().clone();
|
||||
let mut created_set = HashSet::with_capacity(cache.0.len());
|
||||
cache.0.iter().for_each(|c| {
|
||||
created_set.insert(c);
|
||||
});
|
||||
adding = cache
|
||||
.1
|
||||
.iter()
|
||||
.filter(|c| !created_set.contains(c))
|
||||
.take(count)
|
||||
.cloned()
|
||||
.collect();
|
||||
let mut new_current = cache.0.clone();
|
||||
new_current.append(&mut adding.clone());
|
||||
total_cache.insert(index, (new_current, cache.1.clone()));
|
||||
}
|
||||
|
||||
Self::append_words(&state, set, adding.as_slice());
|
||||
}
|
||||
}
|
||||
Task::none()
|
||||
}
|
||||
@@ -141,15 +184,14 @@ impl NavigatedPage<RepetitionsMessage> for RepetitionsState {
|
||||
.width(Length::FillPortion(1)),
|
||||
self.selected_set_view(),
|
||||
]
|
||||
.align_y(Center)
|
||||
.spacing(DEFAULT_SPACING)
|
||||
.width(Fill)
|
||||
.height(Fill)
|
||||
.into(),
|
||||
.align_y(Center)
|
||||
.spacing(DEFAULT_SPACING)
|
||||
.width(Fill)
|
||||
.height(Fill)
|
||||
.into(),
|
||||
Back,
|
||||
)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
impl RepetitionsState {
|
||||
@@ -158,12 +200,49 @@ impl RepetitionsState {
|
||||
RepetitionsState {
|
||||
selected_set: None,
|
||||
correct_filters: vec![true; count],
|
||||
current_sets_cards_cache: Default::default(),
|
||||
state,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl RepetitionsState {
|
||||
fn append_all_words(&self, set: &CardSetSettings) {
|
||||
let index = self.selected_set.unwrap();
|
||||
let cache = &self.current_sets_cards_cache[&index];
|
||||
let mut created_set = HashSet::with_capacity(cache.0.len());
|
||||
cache.0.iter().for_each(|c| {
|
||||
created_set.insert(c);
|
||||
});
|
||||
let required = cache
|
||||
.1
|
||||
.iter()
|
||||
.filter(|i| !created_set.contains(i))
|
||||
.cloned()
|
||||
.collect::<Vec<_>>();
|
||||
Self::append_words(&self.state.lock().unwrap(), set, required.as_slice());
|
||||
}
|
||||
|
||||
fn append_words(state: &AppState, set: &CardSetSettings, indices: &[usize]) {
|
||||
let words = &state.dictionary;
|
||||
let stats = &mut indices
|
||||
.iter()
|
||||
.map(|i| {
|
||||
let word = words.get(*i).unwrap();
|
||||
CardStatistics {
|
||||
id: 0,
|
||||
word_id: word.id,
|
||||
last_open: Utc::now(),
|
||||
score: 1,
|
||||
set_id: set.id,
|
||||
}
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
if !stats.is_empty() {
|
||||
add_stat_list(stats, &state.connection);
|
||||
}
|
||||
}
|
||||
|
||||
fn launch_delete_button(&self, set: &CardSetSettings) -> Element<'_, RepetitionsMessage> {
|
||||
if set.id != 0 {
|
||||
@@ -227,18 +306,33 @@ impl RepetitionsState {
|
||||
]
|
||||
.spacing(DEFAULT_SPACING)
|
||||
} else {
|
||||
self.filled_set_data_view(&set)
|
||||
column![
|
||||
self.filled_set_data_view(&set),
|
||||
self.word_append_panel(&set),
|
||||
radio(
|
||||
"Обычный режим",
|
||||
OrderMode::Default,
|
||||
Some(set.open_mode),
|
||||
SetOpenMode
|
||||
),
|
||||
radio(
|
||||
"Начать с плохих слов",
|
||||
OrderMode::TrainWorstFirst,
|
||||
Some(set.open_mode),
|
||||
SetOpenMode
|
||||
),
|
||||
radio(
|
||||
"Полностью случайно",
|
||||
OrderMode::FullRandom,
|
||||
Some(set.open_mode),
|
||||
SetOpenMode
|
||||
),
|
||||
button("История").style(jl_button).on_press(GoToHistory),
|
||||
]
|
||||
.spacing(HALF_SPACING)
|
||||
}
|
||||
},
|
||||
radio(
|
||||
"Обычный режим",
|
||||
OrderMode::Default,
|
||||
Some(set.open_mode),
|
||||
SetOpenMode
|
||||
),
|
||||
// self.words_words_view(&set),
|
||||
self.word_append_panel(&set),
|
||||
button("История").style(jl_button).on_press(GoToHistory),
|
||||
]
|
||||
.spacing(DEFAULT_SPACING)
|
||||
.padding(Padding {
|
||||
@@ -263,7 +357,8 @@ impl RepetitionsState {
|
||||
}
|
||||
|
||||
fn filled_set_data_view(&self, set: &CardSetSettings) -> Column<'_, RepetitionsMessage> {
|
||||
column![self.activity_bar(set)].align_x(Center)
|
||||
column![self.activity_bar(set), self.count_comparator_view()]
|
||||
.align_x(Center)
|
||||
.width(Fill)
|
||||
.spacing(DEFAULT_SPACING)
|
||||
}
|
||||
@@ -275,7 +370,6 @@ impl RepetitionsState {
|
||||
let mut counts: Vec<u32> = vec![0; 30 * 7];
|
||||
let now = Local::now().date_naive();
|
||||
|
||||
|
||||
if let Some(history) = history {
|
||||
for (date, activity) in history {
|
||||
let distance = now - *date;
|
||||
@@ -302,7 +396,6 @@ impl RepetitionsState {
|
||||
})
|
||||
.spacing(QUARTER_SPACING);
|
||||
|
||||
|
||||
let mut iter = counts.iter();
|
||||
for i in 0..30 {
|
||||
let mut column = Column::new().spacing(QUARTER_SPACING);
|
||||
@@ -311,13 +404,17 @@ impl RepetitionsState {
|
||||
let value = *iter.next().unwrap() as f32;
|
||||
let k = (value / MAX_DAY_COUNT).min(1.0) * 0.9 + 0.1;
|
||||
|
||||
let date = (*now).checked_sub_days(Days::new(30 * 7 - i * 7 - j - 1)).unwrap();
|
||||
let date = (*now)
|
||||
.checked_sub_days(Days::new(30 * 7 - i * 7 - j - 1))
|
||||
.unwrap();
|
||||
|
||||
column = column.push(tooltip(
|
||||
iced::widget::container(space().height(15).width(15)).style(
|
||||
move |x: &Theme| container::Style {
|
||||
text_color: None,
|
||||
background: Some(Background::Color(x.palette().primary.scale_alpha(k))),
|
||||
background: Some(Background::Color(
|
||||
x.palette().primary.scale_alpha(k),
|
||||
)),
|
||||
border: Border {
|
||||
color: Default::default(),
|
||||
width: 0.0,
|
||||
@@ -334,27 +431,19 @@ impl RepetitionsState {
|
||||
|
||||
row = row.push(column);
|
||||
}
|
||||
scrollable(column!["Карта активности", row].spacing(QUARTER_SPACING).align_x(Center))
|
||||
}).into()
|
||||
|
||||
scrollable(
|
||||
column!["Карта активности", row]
|
||||
.spacing(QUARTER_SPACING)
|
||||
.align_x(Center),
|
||||
)
|
||||
})
|
||||
.into()
|
||||
}
|
||||
|
||||
fn words_words_view(&self, set: &CardSetSettings) -> Element<'_, RepetitionsMessage> {
|
||||
column![
|
||||
text!("Худшие слова"),
|
||||
container(scrollable(self.worst_words_list(set)).height(200)).style(bordered_box),
|
||||
radio(
|
||||
"Начать с плохих слов",
|
||||
OrderMode::TrainWorstFirst,
|
||||
Some(set.open_mode),
|
||||
SetOpenMode
|
||||
),
|
||||
radio(
|
||||
"Полностью случайно",
|
||||
OrderMode::FullRandom,
|
||||
Some(set.open_mode),
|
||||
SetOpenMode
|
||||
)
|
||||
]
|
||||
.spacing(DEFAULT_SPACING)
|
||||
.into()
|
||||
@@ -363,10 +452,40 @@ impl RepetitionsState {
|
||||
fn word_append_panel(&self, set: &CardSetSettings) -> Element<'_, RepetitionsMessage> {
|
||||
column![
|
||||
text!("Режим добавления карточек"),
|
||||
row![radio("Добавлять все доступные", AppendMode::Full, Some(set.append_mode), SetAppendMode), radio("Добавлять вручную", AppendMode::Manual, Some(set.append_mode), SetAppendMode)].spacing(HALF_SPACING),
|
||||
].spacing(HALF_SPACING).into()
|
||||
row![
|
||||
radio(
|
||||
"Добавлять все доступные",
|
||||
AppendMode::Full,
|
||||
Some(set.append_mode),
|
||||
SetAppendMode
|
||||
),
|
||||
radio(
|
||||
"Добавлять вручную",
|
||||
AppendMode::Manual,
|
||||
Some(set.append_mode),
|
||||
SetAppendMode
|
||||
)
|
||||
]
|
||||
.spacing(HALF_SPACING),
|
||||
self.adder_panel(&set),
|
||||
]
|
||||
.spacing(HALF_SPACING)
|
||||
.into()
|
||||
}
|
||||
|
||||
fn adder_panel(&self, set: &CardSetSettings) -> Element<'_, RepetitionsMessage> {
|
||||
match set.append_mode {
|
||||
AppendMode::Full => space().into(),
|
||||
AppendMode::Manual => row![
|
||||
button("+5").on_press(AppendWords(5)),
|
||||
button("+10").on_press(AppendWords(10)),
|
||||
button("+15").on_press(AppendWords(15)),
|
||||
button("+20").on_press(AppendWords(20))
|
||||
]
|
||||
.spacing(HALF_SPACING)
|
||||
.into(),
|
||||
}
|
||||
}
|
||||
fn worst_words_list(&self, set: &CardSetSettings) -> Element<'_, RepetitionsMessage> {
|
||||
let mut column = Column::new();
|
||||
|
||||
@@ -376,6 +495,12 @@ impl RepetitionsState {
|
||||
column.into()
|
||||
}
|
||||
|
||||
fn count_comparator_view(&self) -> Element<'_, RepetitionsMessage> {
|
||||
let cache = &self.current_sets_cards_cache[&self.selected_set.unwrap()];
|
||||
let now = cache.0.len();
|
||||
let available = cache.1.len();
|
||||
text!("{} слова добавлено из {}", now, available).into()
|
||||
}
|
||||
fn count_view(&self, set: &CardSetSettings) -> Element<'_, RepetitionsMessage> {
|
||||
if let Some(count) = set.count {
|
||||
return text!("Количество слов: {}", count).into();
|
||||
@@ -435,5 +560,5 @@ pub enum RepetitionsMessage {
|
||||
SetAppendMode(AppendMode),
|
||||
GoToHistory,
|
||||
GoToSettings,
|
||||
AppendWords(usize),
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user