Fix deck data
This commit is contained in:
1 parent
9a7578727b
commit
08ab55f283
7 files changed
+145
-96
No files matched your search
+4
-4
@@ -11,7 +11,7 @@ use iced_core::window::Position;
|
|||||||
use iced_core::window::settings::PlatformSpecific;
|
use iced_core::window::settings::PlatformSpecific;
|
||||||
use iced_core::{Font, Size, Theme};
|
use iced_core::{Font, Size, Theme};
|
||||||
use log::info;
|
use log::info;
|
||||||
use parking_lot::RwLock;
|
use parking_lot::{Mutex, RwLock};
|
||||||
use rusqlite::Connection;
|
use rusqlite::Connection;
|
||||||
use crate::data::repositories::Repository;
|
use crate::data::repositories::Repository;
|
||||||
use crate::data_provider::card_states::CardStateRepository;
|
use crate::data_provider::card_states::CardStateRepository;
|
||||||
@@ -65,7 +65,7 @@ pub struct AppState {
|
|||||||
pub decks: DeckRepository,
|
pub decks: DeckRepository,
|
||||||
pub word_groups: WordGroupRepository,
|
pub word_groups: WordGroupRepository,
|
||||||
pub card_states: CardStateRepository,
|
pub card_states: CardStateRepository,
|
||||||
pub connection: Arc<RwLock<SqliteConnection>>,
|
pub connection: Arc<Mutex<SqliteConnection>>,
|
||||||
pub sync_data: Arc<RwLock<AppSettings>>,
|
pub sync_data: Arc<RwLock<AppSettings>>,
|
||||||
pub history: HistoryRepository,
|
pub history: HistoryRepository,
|
||||||
}
|
}
|
||||||
@@ -78,7 +78,7 @@ impl Default for AppState {
|
|||||||
|
|
||||||
impl AppState {
|
impl AppState {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
let connection = Arc::new(RwLock::new(SqliteConnection::default()));
|
let connection = Arc::new(Mutex::new(SqliteConnection::default()));
|
||||||
Self {
|
Self {
|
||||||
dictionary: WordRepository::new(connection.clone()),
|
dictionary: WordRepository::new(connection.clone()),
|
||||||
decks: DeckRepository::new(connection.clone()),
|
decks: DeckRepository::new(connection.clone()),
|
||||||
@@ -95,7 +95,7 @@ impl AppState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn fill_state(state: &mut AppState) {
|
pub fn fill_state(state: &mut AppState) {
|
||||||
let connection = state.connection.read();
|
let connection = state.connection.lock();
|
||||||
|
|
||||||
let setting = load_settings(&connection);
|
let setting = load_settings(&connection);
|
||||||
|
|
||||||
|
|||||||
+95
-22
@@ -1,3 +1,6 @@
|
|||||||
|
use std::sync::Arc;
|
||||||
|
use std::time::Instant;
|
||||||
|
use chrono::Utc;
|
||||||
use crate::app::AppState;
|
use crate::app::AppState;
|
||||||
use crate::core::srs::OrderModule::{Fsrs, RandomSRS, SemiRandomSRS, WorstWordsSRS};
|
use crate::core::srs::OrderModule::{Fsrs, RandomSRS, SemiRandomSRS, WorstWordsSRS};
|
||||||
use crate::core::srs::{
|
use crate::core::srs::{
|
||||||
@@ -5,14 +8,16 @@ use crate::core::srs::{
|
|||||||
WordOpenMode, WorstWordsSRSModule,
|
WordOpenMode, WorstWordsSRSModule,
|
||||||
};
|
};
|
||||||
use crate::data::entities::*;
|
use crate::data::entities::*;
|
||||||
use crate::data_provider::card_states::update_stat_score;
|
use crate::data_provider::card_states::{update_stat_score, CardStateRepository, CardStateRepositoryInner};
|
||||||
use crate::notification::{MaybeError, NotificationData};
|
use crate::notification::{MaybeError, NotificationData};
|
||||||
use chrono::Utc;
|
use log::info;
|
||||||
|
use rhai::{Engine, Scope};
|
||||||
|
use crate::data::repositories::Repository;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct DeckData {
|
pub struct DeckData {
|
||||||
pub(crate) words: Vec<WordData>,
|
pub(crate) words: Vec<EntityHandle<WordData>>,
|
||||||
pub(crate) set: Vec<CardState>,
|
pub(crate) set: Vec<EntityHandle<CardState>>,
|
||||||
current_word_index: Option<usize>,
|
current_word_index: Option<usize>,
|
||||||
state: AppState,
|
state: AppState,
|
||||||
order_module: OrderModule,
|
order_module: OrderModule,
|
||||||
@@ -51,57 +56,68 @@ impl DeckData {
|
|||||||
// })
|
// })
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn next(&mut self) -> (WordData, CardState) {
|
pub fn next(&mut self) -> (EntityHandle<WordData>, EntityHandle<CardState>) {
|
||||||
|
let repo = self.state.card_states.read();
|
||||||
|
let links = Self::set_links(self.set.as_slice(), &repo);
|
||||||
let index = match self.order_module {
|
let index = match self.order_module {
|
||||||
SemiRandomSRS(ref mut module) => {
|
SemiRandomSRS(ref mut module) => {
|
||||||
if !module.initialized {
|
if !module.initialized {
|
||||||
module.init(self.set.as_slice())
|
module.init(links.as_slice());
|
||||||
}
|
}
|
||||||
module.next(self.set.as_slice())
|
module.next(links.as_slice())
|
||||||
}
|
}
|
||||||
|
|
||||||
RandomSRS(ref mut module) => {
|
RandomSRS(ref mut module) => {
|
||||||
if !module.initialized {
|
if !module.initialized {
|
||||||
module.init(self.set.as_slice())
|
module.init(links.as_slice())
|
||||||
}
|
}
|
||||||
module.next(self.set.as_slice())
|
module.next(links.as_slice())
|
||||||
}
|
}
|
||||||
WorstWordsSRS(ref mut module) => {
|
WorstWordsSRS(ref mut module) => {
|
||||||
if !module.initialized {
|
if !module.initialized {
|
||||||
module.init(self.set.as_slice())
|
module.init(links.as_slice())
|
||||||
}
|
}
|
||||||
module.next(self.set.as_slice())
|
module.next(links.as_slice())
|
||||||
}
|
}
|
||||||
Fsrs(ref mut module) => {
|
Fsrs(ref mut module) => {
|
||||||
if !module.initialized {
|
if !module.initialized {
|
||||||
module.init(self.set.as_slice())
|
module.init(links.as_slice())
|
||||||
}
|
}
|
||||||
module.next(self.set.as_slice())
|
module.next(links.as_slice())
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
self.current_word_index = Some(index);
|
self.current_word_index = Some(index);
|
||||||
(self.words[index].clone(), self.set[index].clone())
|
(self.words[index].clone(), self.set[index].clone())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn set_links<'a>(set: &[EntityHandle<CardState>], repo: &'a CardStateRepositoryInner) -> Vec<&'a CardState> {
|
||||||
|
set.iter().map(|h| repo.get_ref_static(*h).unwrap()).collect::<Vec<_>>()
|
||||||
|
}
|
||||||
|
|
||||||
pub fn open(&mut self, status: WordOpenMode) -> MaybeError {
|
pub fn open(&mut self, status: WordOpenMode) -> MaybeError {
|
||||||
if let Some(index) = self.current_word_index {
|
if let Some(index) = self.current_word_index {
|
||||||
let word: &mut CardState = &mut self.set[index];
|
let mut repo = self.state.card_states.write();
|
||||||
let old_score = word.score;
|
let card_state_handle = &mut self.set[index];
|
||||||
word.update(status);
|
let card_state = repo.get_mut(card_state_handle).unwrap();
|
||||||
let new_score = word.score;
|
let old_score = card_state.score;
|
||||||
|
card_state.update(status);
|
||||||
|
let new_score = card_state.score;
|
||||||
|
|
||||||
|
let mut fsrs = false;
|
||||||
match self.order_module {
|
match self.order_module {
|
||||||
SemiRandomSRS(ref mut module) => {
|
SemiRandomSRS(ref mut module) => {
|
||||||
module.open(status, index, word);
|
module.open(status, index, card_state);
|
||||||
}
|
}
|
||||||
RandomSRS(ref mut module) => {
|
RandomSRS(ref mut module) => {
|
||||||
module.open(status, index, word);
|
module.open(status, index, card_state);
|
||||||
}
|
}
|
||||||
WorstWordsSRS(ref mut module) => {
|
WorstWordsSRS(ref mut module) => {
|
||||||
module.open(status, index, word);
|
module.open(status, index, card_state);
|
||||||
}
|
}
|
||||||
Fsrs(ref mut module) => {
|
Fsrs(ref mut module) => {
|
||||||
module.open(status, index, word);
|
module.open(status, index, card_state);
|
||||||
|
fsrs = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -114,7 +130,21 @@ impl DeckData {
|
|||||||
OrderMode::FullRandom => RandomSRS(RandomSRSModule::new()),
|
OrderMode::FullRandom => RandomSRS(RandomSRSModule::new()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
update_stat_score(word, &self.state.connection.read())?;
|
|
||||||
|
let word_id = card_state.word_id;
|
||||||
|
repo.save(card_state_handle)?;
|
||||||
|
|
||||||
|
let mut history = self.state.history.write();
|
||||||
|
history.create_now(HistoryNote {
|
||||||
|
id: Default::default(),
|
||||||
|
timestamp: Utc::now(),
|
||||||
|
set_id: self.settings.id,
|
||||||
|
word_id,
|
||||||
|
answer: status.clone(),
|
||||||
|
before: old_score,
|
||||||
|
after: new_score,
|
||||||
|
fsrs,
|
||||||
|
})?;
|
||||||
todo!("Implement history pushing")
|
todo!("Implement history pushing")
|
||||||
// let _ = push_note(
|
// let _ = push_note(
|
||||||
// self.settings.id,
|
// self.settings.id,
|
||||||
@@ -133,4 +163,47 @@ impl DeckData {
|
|||||||
pub fn len(&self) -> usize {
|
pub fn len(&self) -> usize {
|
||||||
self.set.len()
|
self.set.len()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn list(deck: &DeckSettings, state: &AppState) -> Result<Vec<EntityHandle<WordData>>, NotificationData> {
|
||||||
|
let time = Instant::now();
|
||||||
|
let mut list = vec![];
|
||||||
|
let engine = Engine::new();
|
||||||
|
|
||||||
|
let ast = engine.compile(&deck.filter);
|
||||||
|
if let Ok(ast) = ast {
|
||||||
|
let groups = state.word_groups.read().iter().cloned().collect::<Vec<_>>();
|
||||||
|
let dictionary = &state.dictionary.read();
|
||||||
|
let query = dictionary.query_par(Arc::new(move |word| {
|
||||||
|
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();
|
||||||
|
let group_name = groups[groups.binary_search_by_key(&word.group_id, |g| g.id).unwrap()].name.clone();
|
||||||
|
scope
|
||||||
|
.push_constant("id", word.id.0 as i64)
|
||||||
|
.push_constant("key", word.key.clone())
|
||||||
|
.push_constant("value", word.value.clone())
|
||||||
|
.push_constant("tags", word.tags.clone())
|
||||||
|
.push_constant("more", more)
|
||||||
|
.push_constant(
|
||||||
|
"group",
|
||||||
|
group_name,
|
||||||
|
);
|
||||||
|
|
||||||
|
let result = engine.eval_ast_with_scope::<bool>(&mut scope, &ast);
|
||||||
|
if let Ok(result) = result {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
false
|
||||||
|
}));
|
||||||
|
|
||||||
|
info!("Collecting available words is {:?}", time.elapsed());
|
||||||
|
let handles =query.handles().collect();
|
||||||
|
drop(query);
|
||||||
|
Ok(handles)
|
||||||
|
} else {
|
||||||
|
Ok(list)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
+12
-12
@@ -31,9 +31,9 @@ pub enum OrderModule {
|
|||||||
Fsrs(FsrsModule),
|
Fsrs(FsrsModule),
|
||||||
}
|
}
|
||||||
pub trait SRSModule {
|
pub trait SRSModule {
|
||||||
fn next(&mut self, set: &[CardState]) -> usize;
|
fn next(&mut self, set: &[&CardState]) -> usize;
|
||||||
fn open(&mut self, status: WordOpenMode, index: usize, updated_word: &mut CardState);
|
fn open(&mut self, status: WordOpenMode, index: usize, updated_word: &mut CardState);
|
||||||
fn init(&mut self, set: &[CardState]);
|
fn init(&mut self, set: &[&CardState]);
|
||||||
}
|
}
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct RandomSRSModule {
|
pub struct RandomSRSModule {
|
||||||
@@ -49,7 +49,7 @@ impl RandomSRSModule {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl SRSModule for RandomSRSModule {
|
impl SRSModule for RandomSRSModule {
|
||||||
fn next(&mut self, set: &[CardState]) -> usize {
|
fn next(&mut self, set: &[&CardState]) -> usize {
|
||||||
match self.basket.pop() {
|
match self.basket.pop() {
|
||||||
Some(index) => index,
|
Some(index) => index,
|
||||||
None => {
|
None => {
|
||||||
@@ -62,7 +62,7 @@ impl SRSModule for RandomSRSModule {
|
|||||||
|
|
||||||
fn open(&mut self, _: WordOpenMode, _: usize, _: &mut CardState) {}
|
fn open(&mut self, _: WordOpenMode, _: usize, _: &mut CardState) {}
|
||||||
|
|
||||||
fn init(&mut self, set: &[CardState]) {
|
fn init(&mut self, set: &[&CardState]) {
|
||||||
self.initialized = true;
|
self.initialized = true;
|
||||||
self.basket = (0..set.len()).collect::<Vec<usize>>();
|
self.basket = (0..set.len()).collect::<Vec<usize>>();
|
||||||
self.basket.shuffle(&mut rand::rng())
|
self.basket.shuffle(&mut rand::rng())
|
||||||
@@ -86,7 +86,7 @@ impl SemiRandomSRSModule {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl SRSModule for SemiRandomSRSModule {
|
impl SRSModule for SemiRandomSRSModule {
|
||||||
fn next(&mut self, set: &[CardState]) -> usize {
|
fn next(&mut self, set: &[&CardState]) -> usize {
|
||||||
let index = self.last_weights.sample(&mut self.generator);
|
let index = self.last_weights.sample(&mut self.generator);
|
||||||
|
|
||||||
if self.history.contains(&index) && self.history.len() != 1 {
|
if self.history.contains(&index) && self.history.len() != 1 {
|
||||||
@@ -106,7 +106,7 @@ impl SRSModule for SemiRandomSRSModule {
|
|||||||
.update_weights(&[(index, &new_weight)])
|
.update_weights(&[(index, &new_weight)])
|
||||||
.expect("update weights error");
|
.expect("update weights error");
|
||||||
}
|
}
|
||||||
fn init(&mut self, set: &[CardState]) {
|
fn init(&mut self, set: &[&CardState]) {
|
||||||
self.initialized = true;
|
self.initialized = true;
|
||||||
let weights = set
|
let weights = set
|
||||||
.iter()
|
.iter()
|
||||||
@@ -116,7 +116,7 @@ impl SRSModule for SemiRandomSRSModule {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl SemiRandomSRSModule {
|
impl SemiRandomSRSModule {
|
||||||
fn history_len(&self, set: &[CardState]) -> usize {
|
fn history_len(&self, set: &[&CardState]) -> usize {
|
||||||
((set.len() as f32 * MAX_HISTORY_LEN_PART) as usize).clamp(1, MAX_HISTORY_LEN)
|
((set.len() as f32 * MAX_HISTORY_LEN_PART) as usize).clamp(1, MAX_HISTORY_LEN)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -130,7 +130,7 @@ pub struct WorstWordsSRSModule {
|
|||||||
rounds_count: u8,
|
rounds_count: u8,
|
||||||
}
|
}
|
||||||
impl SRSModule for WorstWordsSRSModule {
|
impl SRSModule for WorstWordsSRSModule {
|
||||||
fn next(&mut self, set: &[CardState]) -> usize {
|
fn next(&mut self, set: &[&CardState]) -> usize {
|
||||||
if self.rounds_remaining == 0 {
|
if self.rounds_remaining == 0 {
|
||||||
self.fill_pool(set);
|
self.fill_pool(set);
|
||||||
self.rounds_remaining = self.rounds_count;
|
self.rounds_remaining = self.rounds_count;
|
||||||
@@ -148,7 +148,7 @@ impl SRSModule for WorstWordsSRSModule {
|
|||||||
|
|
||||||
fn open(&mut self, _: WordOpenMode, _: usize, _: &mut CardState) {}
|
fn open(&mut self, _: WordOpenMode, _: usize, _: &mut CardState) {}
|
||||||
|
|
||||||
fn init(&mut self, _: &[CardState]) {
|
fn init(&mut self, _: &[&CardState]) {
|
||||||
self.initialized = true;
|
self.initialized = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -163,7 +163,7 @@ impl WorstWordsSRSModule {
|
|||||||
queue: vec![],
|
queue: vec![],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn fill_pool(&mut self, set: &[CardState]) {
|
fn fill_pool(&mut self, set: &[&CardState]) {
|
||||||
let mut sorted = set.iter().enumerate().collect::<Vec<_>>();
|
let mut sorted = set.iter().enumerate().collect::<Vec<_>>();
|
||||||
sorted.sort_by_key(|c| c.1.score);
|
sorted.sort_by_key(|c| c.1.score);
|
||||||
let mut worst = sorted[0..self.pool_size]
|
let mut worst = sorted[0..self.pool_size]
|
||||||
@@ -187,7 +187,7 @@ pub struct FsrsModule {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl SRSModule for FsrsModule {
|
impl SRSModule for FsrsModule {
|
||||||
fn next(&mut self, _: &[CardState]) -> usize {
|
fn next(&mut self, _: &[&CardState]) -> usize {
|
||||||
if self.current_group.is_empty() {
|
if self.current_group.is_empty() {
|
||||||
self.fill_partial_pool();
|
self.fill_partial_pool();
|
||||||
}
|
}
|
||||||
@@ -233,7 +233,7 @@ impl SRSModule for FsrsModule {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn init(&mut self, set: &[CardState]) {
|
fn init(&mut self, set: &[&CardState]) {
|
||||||
self.rng = rng();
|
self.rng = rng();
|
||||||
let today = Local::now().date_naive();
|
let today = Local::now().date_naive();
|
||||||
self.today_set = set
|
self.today_set = set
|
||||||
|
|||||||
@@ -7,9 +7,10 @@ use fsrs::MemoryState;
|
|||||||
use log::info;
|
use log::info;
|
||||||
use rusqlite::{Connection, Error, Params, Row, params};
|
use rusqlite::{Connection, Error, Params, Row, params};
|
||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
use crate::data::repositories::DataRepository;
|
use crate::data::repositories::{DataRepository, DataRepositoryInner};
|
||||||
|
|
||||||
pub type CardStateRepository = DataRepository<CardState, SqliteDbLayer<CardState, CardStatesQuerySet>>;
|
pub type CardStateRepository = DataRepository<CardState, SqliteDbLayer<CardState, CardStatesQuerySet>>;
|
||||||
|
pub type CardStateRepositoryInner = DataRepositoryInner<CardState, SqliteDbLayer<CardState, CardStatesQuerySet>>;
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct CardStatesQuerySet;
|
pub struct CardStatesQuerySet;
|
||||||
impl TableAccessQuerySet<CardState> for CardStatesQuerySet {
|
impl TableAccessQuerySet<CardState> for CardStatesQuerySet {
|
||||||
|
|||||||
+2
-3
@@ -1,6 +1,5 @@
|
|||||||
use crate::app::{fill_state, AppState};
|
use crate::app::{fill_state, AppState};
|
||||||
use crate::data::Id;
|
use crate::data_provider::sqlite::{create_db};
|
||||||
use crate::data_provider::sqlite::{create_db, SqliteConnection};
|
|
||||||
use crate::data_provider::web_api::{
|
use crate::data_provider::web_api::{
|
||||||
get_local_version, get_web_version, load_data, set_local_version,
|
get_local_version, get_web_version, load_data, set_local_version,
|
||||||
};
|
};
|
||||||
@@ -149,7 +148,7 @@ impl ScreenState {
|
|||||||
info!("Loading data");
|
info!("Loading data");
|
||||||
let state = &mut self.app_state;
|
let state = &mut self.app_state;
|
||||||
if cfg!(windows) {
|
if cfg!(windows) {
|
||||||
state.connection.write().finish();
|
state.connection.lock().finish();
|
||||||
}
|
}
|
||||||
let db_file = db_path();
|
let db_file = db_path();
|
||||||
let temp_db_file = temp_db_path();
|
let temp_db_file = temp_db_path();
|
||||||
|
|||||||
@@ -162,29 +162,9 @@ impl RepetitionState {
|
|||||||
Err(err) => push_error!(err),
|
Err(err) => push_error!(err),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
self.increment_counter();
|
|
||||||
Task::none()
|
Task::none()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn increment_counter(&mut self) {
|
|
||||||
let mut state = self.state;
|
|
||||||
let id = self.settings.id;
|
|
||||||
let activity = state.activity.get_mut(&id);
|
|
||||||
|
|
||||||
let now = Local::now().date_naive();
|
|
||||||
|
|
||||||
if let Some(activity) = activity {
|
|
||||||
let (last_day, counter) = activity.last_mut().unwrap();
|
|
||||||
if *last_day == now {
|
|
||||||
*counter += 1;
|
|
||||||
} else {
|
|
||||||
activity.push((now, 1));
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
state.activity.insert(id, vec![(now, 1)]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn draw_forward(&self) -> Element<'_, RepetitionMessage> {
|
fn draw_forward(&self) -> Element<'_, RepetitionMessage> {
|
||||||
self.draw_card_view(self.settings.forward.as_str())
|
self.draw_card_view(self.settings.forward.as_str())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
use crate::app::AppState;
|
use crate::app::AppState;
|
||||||
use crate::data::entities::DeckSettings;
|
use crate::data::entities::{DeckSettings, EntityHandle};
|
||||||
use crate::data_provider::card_states::{delete_stats, load_stats_of_deck};
|
use crate::data_provider::card_states::{delete_stats, load_stats_of_deck};
|
||||||
use crate::navigation::Page::PreviousPage;
|
use crate::navigation::Page::PreviousPage;
|
||||||
use crate::navigation::{NavigatedPage, Page, RootMessage};
|
use crate::navigation::{NavigatedPage, Page, RootMessage};
|
||||||
@@ -16,31 +16,25 @@ use iced_core::{Padding, Theme, color};
|
|||||||
use parking_lot::Mutex;
|
use parking_lot::Mutex;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
use crate::core::deck::DeckData;
|
||||||
|
use crate::data::repositories::BatchedRepository;
|
||||||
|
|
||||||
pub struct RepetitionSettingsState {
|
pub struct RepetitionSettingsState {
|
||||||
set: DeckSettings,
|
settings: EntityHandle<DeckSettings>,
|
||||||
index: usize,
|
|
||||||
state: AppState,
|
state: AppState,
|
||||||
real_delete: bool,
|
real_delete: bool,
|
||||||
correct_filter: bool,
|
correct_filter: bool,
|
||||||
filter_changed: bool,
|
filter_changed: bool,
|
||||||
|
filtered_words: Option<u32>
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RepetitionSettingsState {
|
impl RepetitionSettingsState {
|
||||||
pub(crate) fn new(index: usize, state: AppState) -> RepetitionSettingsState {
|
pub(crate) fn new(settings: EntityHandle<DeckSettings>, state: AppState) -> RepetitionSettingsState {
|
||||||
let set;
|
|
||||||
{
|
|
||||||
let local_state = state.lock();
|
|
||||||
set = local_state.decks[index].clone();
|
|
||||||
}
|
|
||||||
let filter_state = set.check_filter();
|
|
||||||
|
|
||||||
RepetitionSettingsState {
|
RepetitionSettingsState {
|
||||||
index,
|
settings,
|
||||||
set,
|
|
||||||
state,
|
state,
|
||||||
real_delete: false,
|
real_delete: false,
|
||||||
correct_filter: filter_state,
|
correct_filter: false,
|
||||||
filter_changed: false,
|
filter_changed: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -61,34 +55,34 @@ impl NavigatedPage<RepetitionSettingsMessage> for RepetitionSettingsState {
|
|||||||
Back => {}
|
Back => {}
|
||||||
Save => {
|
Save => {
|
||||||
let mut state = self.state.lock();
|
let mut state = self.state.lock();
|
||||||
if let Err(e) = update_deck(&mut self.set, &state.connection) {
|
if let Err(e) = update_deck(&mut self.settings, &state.connection) {
|
||||||
return push_error_task!(e);
|
return push_error_task!(e);
|
||||||
}
|
}
|
||||||
if self.filter_changed
|
if self.filter_changed
|
||||||
&& let Err(e) = update_deck_stats(&self.set, &state)
|
&& let Err(e) = update_deck_stats(&self.settings, &state)
|
||||||
{
|
{
|
||||||
return push_error_task!(e);
|
return push_error_task!(e);
|
||||||
}
|
}
|
||||||
state.decks[self.index] = self.set.clone();
|
state.decks[self.index] = self.settings.clone();
|
||||||
}
|
}
|
||||||
SetName(new) => {
|
SetName(new) => {
|
||||||
self.set.name = new;
|
self.settings.name = new;
|
||||||
}
|
}
|
||||||
SetForward(new) => {
|
SetForward(new) => {
|
||||||
self.set.forward = new;
|
self.settings.forward = new;
|
||||||
}
|
}
|
||||||
SetBackward(backward) => {
|
SetBackward(backward) => {
|
||||||
self.set.backward = backward;
|
self.settings.backward = backward;
|
||||||
}
|
}
|
||||||
SetFilter(new) => {
|
SetFilter(new) => {
|
||||||
self.set.filter = new;
|
self.settings.filter = new;
|
||||||
self.correct_filter = self.set.check_filter();
|
self.correct_filter = self.settings.check_filter();
|
||||||
self.filter_changed = true;
|
self.filter_changed = true;
|
||||||
}
|
}
|
||||||
TryFilter => {
|
TryFilter => {
|
||||||
let state = self.state.lock();
|
let state = self.state.lock();
|
||||||
let count = self.set.get_word_list(&state).len();
|
let count = self.settings.get_word_list(&state).len();
|
||||||
self.set.count = Some(count);
|
self.settings.count = Some(count);
|
||||||
}
|
}
|
||||||
DeleteSet => {
|
DeleteSet => {
|
||||||
if !self.real_delete {
|
if !self.real_delete {
|
||||||
@@ -99,7 +93,7 @@ impl NavigatedPage<RepetitionSettingsMessage> for RepetitionSettingsState {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
let mut state = self.state.lock();
|
let mut state = self.state.lock();
|
||||||
if let Err(e) = delete_set(&self.set, &state.connection) {
|
if let Err(e) = delete_set(&self.settings, &state.connection) {
|
||||||
return push_error_task!(e);
|
return push_error_task!(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -118,22 +112,22 @@ impl NavigatedPage<RepetitionSettingsMessage> for RepetitionSettingsState {
|
|||||||
column![
|
column![
|
||||||
column![
|
column![
|
||||||
text!("Название набора"),
|
text!("Название набора"),
|
||||||
text_input("", &self.set.name).on_input(SetName),
|
text_input("", &self.settings.name).on_input(SetName),
|
||||||
]
|
]
|
||||||
.spacing(QUARTER_SPACING),
|
.spacing(QUARTER_SPACING),
|
||||||
column![
|
column![
|
||||||
text!("Передняя сторона"),
|
text!("Передняя сторона"),
|
||||||
text_input("", &self.set.forward).on_input(SetForward),
|
text_input("", &self.settings.forward).on_input(SetForward),
|
||||||
]
|
]
|
||||||
.spacing(QUARTER_SPACING),
|
.spacing(QUARTER_SPACING),
|
||||||
column![
|
column![
|
||||||
text!("Задняя сторона"),
|
text!("Задняя сторона"),
|
||||||
text_input("", &self.set.backward).on_input(SetBackward),
|
text_input("", &self.settings.backward).on_input(SetBackward),
|
||||||
]
|
]
|
||||||
.spacing(QUARTER_SPACING),
|
.spacing(QUARTER_SPACING),
|
||||||
column![
|
column![
|
||||||
text!("Фильтр"),
|
text!("Фильтр"),
|
||||||
text_input("", &self.set.filter)
|
text_input("", &self.settings.filter)
|
||||||
.style(|x, status| {
|
.style(|x, status| {
|
||||||
let mut primary = Theme::default()(x, status);
|
let mut primary = Theme::default()(x, status);
|
||||||
if !self.correct_filter {
|
if !self.correct_filter {
|
||||||
@@ -145,7 +139,7 @@ impl NavigatedPage<RepetitionSettingsMessage> for RepetitionSettingsState {
|
|||||||
button("Проверить фильтр")
|
button("Проверить фильтр")
|
||||||
.style(jl_button)
|
.style(jl_button)
|
||||||
.on_press(TryFilter),
|
.on_press(TryFilter),
|
||||||
self.count_view(&self.set),
|
self.count_view(&self.settings),
|
||||||
]
|
]
|
||||||
.spacing(QUARTER_SPACING),
|
.spacing(QUARTER_SPACING),
|
||||||
]
|
]
|
||||||
@@ -179,8 +173,8 @@ impl NavigatedPage<RepetitionSettingsMessage> for RepetitionSettingsState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn update_deck_stats(deck: &DeckSettings, state: &AppState) -> MaybeError {
|
fn update_deck_stats(deck: &DeckSettings, state: &AppState) -> MaybeError {
|
||||||
let mut db_stats = load_stats_of_deck(deck, &state.connection).expect("Wrong db operation");
|
let mut db_stats = state.card_states.read().query_par(Arc::new(|s| s.set_id == deck.id));
|
||||||
let filtered_words = deck.get_word_list(state);
|
let filtered_words = DeckData::list(deck, state)?;
|
||||||
filtered_words.iter().for_each(|word| {
|
filtered_words.iter().for_each(|word| {
|
||||||
let word_id = state.dictionary[*word].id;
|
let word_id = state.dictionary[*word].id;
|
||||||
if let Some(index) = db_stats.iter().position(|stat| stat.word_id == word_id) {
|
if let Some(index) = db_stats.iter().position(|stat| stat.word_id == word_id) {
|
||||||
@@ -188,13 +182,15 @@ fn update_deck_stats(deck: &DeckSettings, state: &AppState) -> MaybeError {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
delete_stats(db_stats.as_slice(), &state.connection)?;
|
let repo = state.card_states.write();
|
||||||
|
// repo.delete_many();
|
||||||
|
// delete_stats(db_stats.as_slice(), &state.connection)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RepetitionSettingsState {
|
impl RepetitionSettingsState {
|
||||||
fn count_view(&self, set: &DeckSettings) -> Element<'_, RepetitionSettingsMessage> {
|
fn count_view(&self, set: &DeckSettings) -> Element<'_, RepetitionSettingsMessage> {
|
||||||
if let Some(count) = set.count {
|
if let Some(count) = self.filtered_words {
|
||||||
return text!("Количество слов: {}", count).into();
|
return text!("Количество слов: {}", count).into();
|
||||||
}
|
}
|
||||||
space().into()
|
space().into()
|
||||||
|
|||||||
Reference in new issue
Block a user