Fixing partial adding bug

This commit is contained in:
2026-08-16 14:23:00 +03:00
parent 51c8d7be74
commit 11b38479c1
4 changed files with 41 additions and 63 deletions
+36 -36
View File
@@ -11,18 +11,17 @@ use crate::styling::*;
use crate::{AppState, RootMessage};
use chrono::{Days, Local, Utc};
use hashbrown::{HashMap, HashSet};
use iced::widget::button::{danger, Status};
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::{
Column, Row, button, column, container, lazy, radio, row, scrollable, space, svg, text,
text_input, tooltip,
button, column, container, lazy, radio, row, scrollable, space, svg, text, text_input, tooltip,
Column, Row,
};
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)]
@@ -30,6 +29,7 @@ pub struct RepetitionsState {
selected_set: Option<usize>,
correct_filters: Vec<bool>,
current_sets_cards_cache: HashMap<usize, (Vec<usize>, Vec<usize>)>,
word_id_index_map: HashMap<u32, usize>,
pub state: Arc<Mutex<AppState>>,
}
@@ -93,12 +93,16 @@ impl NavigatedPage<RepetitionsMessage> for RepetitionsState {
self.selected_set = Some(index);
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)
let added: Vec<_> = load_stats_of_set(&set, &state.connection)
.iter()
.map(|c| c.word_id as usize)
.map(|c| self.word_id_index_map[&c.word_id])
.collect();
println!("Already exists {:?}", added);
let total = set.get_word_list(&state);
println!("Available{:?}", total);
self.current_sets_cards_cache.insert(index, (added, total));
}
state.card_sets[index] = set;
}
@@ -159,12 +163,21 @@ impl NavigatedPage<RepetitionsMessage> for RepetitionsState {
.take(count)
.cloned()
.collect();
for x in &cache.0 {
println!("Already exists {:?}", state.dictionary.get(*x).unwrap());
}
for w in &adding {
println!("{:?}", state.dictionary.get(*w).unwrap());
}
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());
Self::append_words(&state, set, adding.into_iter());
}
}
Task::none()
@@ -196,11 +209,18 @@ impl NavigatedPage<RepetitionsMessage> for RepetitionsState {
impl RepetitionsState {
pub(crate) fn new(state: Arc<Mutex<AppState>>) -> RepetitionsState {
let count = state.lock().unwrap().card_sets.len();
let state_ = state.lock().unwrap();
let count = state_.card_sets.len();
let mut map = HashMap::with_capacity(state_.dictionary.len());
state_.dictionary.iter().enumerate().for_each(|(index, word)| {map.insert(word.id, index);});
drop(state_);
RepetitionsState {
selected_set: None,
correct_filters: vec![true; count],
current_sets_cards_cache: Default::default(),
word_id_index_map: map,
state,
}
}
@@ -217,18 +237,15 @@ impl RepetitionsState {
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());
.filter(|i| !created_set.contains(i)).cloned();
Self::append_words(&self.state.lock().unwrap(), set, required);
}
fn append_words(state: &AppState, set: &CardSetSettings, indices: &[usize]) {
fn append_words(state: &AppState, set: &CardSetSettings, indices: impl Iterator<Item = usize>) {
let words = &state.dictionary;
let stats = &mut indices
.iter()
.map(|i| {
let word = words.get(*i).unwrap();
let word = words.get(i).unwrap();
CardStatistics {
id: 0,
word_id: word.id,
@@ -350,10 +367,10 @@ impl RepetitionsState {
.spacing(DEFAULT_SPACING),
]
.spacing(DEFAULT_SPACING)
.width(Length::FillPortion(3))
.width(Length::FillPortion(2))
.into();
}
space().width(Length::FillPortion(3)).into()
space().width(Length::FillPortion(2)).into()
}
fn filled_set_data_view(&self, set: &CardSetSettings) -> Column<'_, RepetitionsMessage> {
@@ -440,15 +457,6 @@ impl RepetitionsState {
.into()
}
fn words_words_view(&self, set: &CardSetSettings) -> Element<'_, RepetitionsMessage> {
column![
text!("Худшие слова"),
container(scrollable(self.worst_words_list(set)).height(200)).style(bordered_box),
]
.spacing(DEFAULT_SPACING)
.into()
}
fn word_append_panel(&self, set: &CardSetSettings) -> Element<'_, RepetitionsMessage> {
column![
text!("Режим добавления карточек"),
@@ -467,7 +475,7 @@ impl RepetitionsState {
)
]
.spacing(HALF_SPACING),
self.adder_panel(&set),
self.adder_panel(set),
]
.spacing(HALF_SPACING)
.into()
@@ -486,14 +494,6 @@ impl RepetitionsState {
.into(),
}
}
fn worst_words_list(&self, set: &CardSetSettings) -> Element<'_, RepetitionsMessage> {
let mut column = Column::new();
for word in set.worst_words_list.clone().unwrap() {
column = column.push(text!("{} | {}", &word.key, &word.value));
}
column.into()
}
fn count_comparator_view(&self) -> Element<'_, RepetitionsMessage> {
let cache = &self.current_sets_cards_cache[&self.selected_set.unwrap()];