2026-08-24 01:37:00 +03:00
|
|
|
use crate::app::AppState;
|
2026-08-29 22:02:01 +03:00
|
|
|
use crate::core::srs::OrderModule::{Fsrs, RandomSRS, SemiRandomSRS, WorstWordsSRS};
|
2026-08-24 23:27:24 +03:00
|
|
|
use crate::core::srs::{
|
2026-08-29 21:31:54 +03:00
|
|
|
FsrsModule, OrderMode, OrderModule, RandomSRSModule, SRSModule, SemiRandomSRSModule,
|
|
|
|
|
WordOpenMode, WorstWordsSRSModule,
|
2026-08-24 23:27:24 +03:00
|
|
|
};
|
2026-08-24 11:35:37 +03:00
|
|
|
use crate::data::entities::*;
|
2026-08-29 21:32:54 +03:00
|
|
|
use crate::data_provider::card_states::{load_stats_of_deck, update_stat_score};
|
2026-08-24 11:41:20 +03:00
|
|
|
use crate::data_provider::history::push_note;
|
2026-08-24 23:42:58 +03:00
|
|
|
use crate::notification::{MaybeError, NotificationData};
|
2026-08-29 21:31:54 +03:00
|
|
|
use chrono::Utc;
|
2026-08-24 11:41:20 +03:00
|
|
|
use parking_lot::Mutex;
|
|
|
|
|
use std::sync::Arc;
|
2026-08-24 01:37:00 +03:00
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
|
pub struct DeckData {
|
|
|
|
|
pub(crate) words: Vec<WordData>,
|
|
|
|
|
pub(crate) set: Vec<CardStatistics>,
|
|
|
|
|
current_word_index: Option<usize>,
|
|
|
|
|
state: Arc<Mutex<AppState>>,
|
|
|
|
|
order_module: OrderModule,
|
2026-08-29 21:31:54 +03:00
|
|
|
open_mode: OrderMode,
|
2026-08-24 01:37:00 +03:00
|
|
|
settings: DeckSettings,
|
|
|
|
|
}
|
|
|
|
|
impl DeckData {
|
2026-08-24 23:27:24 +03:00
|
|
|
pub fn new(
|
|
|
|
|
settings: &DeckSettings,
|
|
|
|
|
state: Arc<Mutex<AppState>>,
|
|
|
|
|
) -> Result<Self, NotificationData> {
|
2026-08-24 01:37:00 +03:00
|
|
|
let state_for = state.clone();
|
|
|
|
|
let state_locked = state.lock();
|
|
|
|
|
|
2026-08-24 18:16:41 +03:00
|
|
|
let stats = load_stats_of_deck(settings, &state_locked.connection)?;
|
2026-08-24 01:37:00 +03:00
|
|
|
let words = stats
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|stat| {
|
|
|
|
|
state_locked
|
|
|
|
|
.dictionary
|
|
|
|
|
.binary_search_by_key(&stat.word_id, |w| w.id)
|
2026-08-24 18:16:41 +03:00
|
|
|
.expect("No word found in dictionary")
|
2026-08-24 01:37:00 +03:00
|
|
|
})
|
|
|
|
|
.map(|index| &state_locked.dictionary[index])
|
|
|
|
|
.cloned()
|
|
|
|
|
.collect();
|
2026-08-29 21:32:54 +03:00
|
|
|
|
2026-08-24 18:16:41 +03:00
|
|
|
Ok(Self {
|
2026-08-24 01:37:00 +03:00
|
|
|
set: stats,
|
|
|
|
|
words,
|
|
|
|
|
current_word_index: None,
|
|
|
|
|
state: state_for,
|
2026-08-29 22:02:01 +03:00
|
|
|
order_module: Fsrs(FsrsModule::new()),
|
2026-08-29 21:31:54 +03:00
|
|
|
open_mode: settings.open_mode,
|
2026-08-24 01:37:00 +03:00
|
|
|
settings: settings.clone(),
|
2026-08-24 18:16:41 +03:00
|
|
|
})
|
2026-08-24 01:37:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn next(&mut self) -> (WordData, CardStatistics) {
|
2026-08-25 01:05:15 +03:00
|
|
|
let index = match self.order_module {
|
2026-08-29 21:31:54 +03:00
|
|
|
SemiRandomSRS(ref mut module) => {
|
|
|
|
|
if !module.initialized {
|
|
|
|
|
module.init(self.set.as_slice())
|
|
|
|
|
}
|
|
|
|
|
module.next(self.set.as_slice())
|
|
|
|
|
}
|
2026-08-29 21:32:54 +03:00
|
|
|
|
2026-08-29 21:31:54 +03:00
|
|
|
RandomSRS(ref mut module) => {
|
2026-08-24 01:37:00 +03:00
|
|
|
if !module.initialized {
|
2026-08-25 01:05:15 +03:00
|
|
|
module.init(self.set.as_slice())
|
2026-08-24 01:37:00 +03:00
|
|
|
}
|
2026-08-25 01:05:15 +03:00
|
|
|
module.next(self.set.as_slice())
|
2026-08-24 01:37:00 +03:00
|
|
|
}
|
2026-08-29 21:31:54 +03:00
|
|
|
WorstWordsSRS(ref mut module) => {
|
2026-08-24 01:37:00 +03:00
|
|
|
if !module.initialized {
|
2026-08-25 01:05:15 +03:00
|
|
|
module.init(self.set.as_slice())
|
2026-08-24 01:37:00 +03:00
|
|
|
}
|
2026-08-25 01:05:15 +03:00
|
|
|
module.next(self.set.as_slice())
|
2026-08-24 01:37:00 +03:00
|
|
|
}
|
2026-08-29 22:02:01 +03:00
|
|
|
Fsrs(ref mut module) => {
|
2026-08-24 01:37:00 +03:00
|
|
|
if !module.initialized {
|
2026-08-25 01:05:15 +03:00
|
|
|
module.init(self.set.as_slice())
|
2026-08-24 01:37:00 +03:00
|
|
|
}
|
2026-08-25 01:05:15 +03:00
|
|
|
module.next(self.set.as_slice())
|
2026-08-24 01:37:00 +03:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
self.current_word_index = Some(index);
|
|
|
|
|
(self.words[index].clone(), self.set[index].clone())
|
|
|
|
|
}
|
2026-08-24 23:42:58 +03:00
|
|
|
pub fn open(&mut self, status: WordOpenMode) -> MaybeError {
|
2026-08-24 23:27:24 +03:00
|
|
|
if let Some(index) = self.current_word_index {
|
|
|
|
|
let word: &mut CardStatistics = &mut self.set[index];
|
|
|
|
|
let old_score = word.score;
|
|
|
|
|
word.update(status);
|
|
|
|
|
let new_score = word.score;
|
2026-08-24 01:37:00 +03:00
|
|
|
|
2026-08-24 23:27:24 +03:00
|
|
|
match self.order_module {
|
2026-08-29 21:31:54 +03:00
|
|
|
SemiRandomSRS(ref mut module) => {
|
|
|
|
|
module.open(status, index, word);
|
|
|
|
|
}
|
|
|
|
|
RandomSRS(ref mut module) => {
|
2026-08-24 23:27:24 +03:00
|
|
|
module.open(status, index, word);
|
|
|
|
|
}
|
2026-08-29 21:31:54 +03:00
|
|
|
WorstWordsSRS(ref mut module) => {
|
2026-08-24 23:27:24 +03:00
|
|
|
module.open(status, index, word);
|
|
|
|
|
}
|
2026-08-29 22:02:01 +03:00
|
|
|
Fsrs(ref mut module) => {
|
2026-08-24 23:27:24 +03:00
|
|
|
module.open(status, index, word);
|
|
|
|
|
}
|
2026-08-24 01:37:00 +03:00
|
|
|
}
|
2026-08-29 21:31:54 +03:00
|
|
|
|
2026-08-29 22:02:01 +03:00
|
|
|
if let Fsrs(ref fsrs) = self.order_module
|
|
|
|
|
&& fsrs.finished()
|
|
|
|
|
{
|
|
|
|
|
self.order_module = match self.open_mode {
|
|
|
|
|
OrderMode::Default => SemiRandomSRS(SemiRandomSRSModule::new()),
|
|
|
|
|
OrderMode::TrainWorstFirst => WorstWordsSRS(WorstWordsSRSModule::new()),
|
|
|
|
|
OrderMode::FullRandom => RandomSRS(RandomSRSModule::new()),
|
2026-08-29 21:31:54 +03:00
|
|
|
}
|
|
|
|
|
}
|
2026-08-24 23:27:24 +03:00
|
|
|
update_stat_score(word, &self.state.lock().connection)?;
|
|
|
|
|
let _ = push_note(
|
|
|
|
|
self.settings.id,
|
|
|
|
|
HistoryItem {
|
|
|
|
|
timestamp: Utc::now(),
|
|
|
|
|
word_id: word.word_id,
|
|
|
|
|
mode: status,
|
|
|
|
|
before: old_score,
|
|
|
|
|
after: new_score,
|
|
|
|
|
},
|
|
|
|
|
);
|
2026-08-24 01:37:00 +03:00
|
|
|
}
|
2026-08-24 23:27:24 +03:00
|
|
|
|
2026-08-24 18:16:41 +03:00
|
|
|
Ok(())
|
2026-08-24 01:37:00 +03:00
|
|
|
}
|
|
|
|
|
pub fn len(&self) -> usize {
|
|
|
|
|
self.set.len()
|
|
|
|
|
}
|
|
|
|
|
}
|