Files
jap_learn/src/core/deck.rs
T

135 lines
4.4 KiB
Rust
Raw Normal View History

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 01:37:00 +03:00
#[derive(Clone)]
pub struct DeckData {
pub(crate) words: Vec<WordData>,
2026-08-30 20:43:31 +03:00
pub(crate) set: Vec<CardState>,
2026-08-24 01:37:00 +03:00
current_word_index: Option<usize>,
2026-08-31 11:46:35 +03:00
state: AppState,
2026-08-24 01:37:00 +03:00
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,
2026-08-31 11:46:35 +03:00
state: AppState,
2026-08-24 23:27:24 +03:00
) -> Result<Self, NotificationData> {
2026-08-24 01:37:00 +03:00
let state_for = state.clone();
2026-08-31 11:46:35 +03:00
let stats = load_stats_of_deck(settings, &state.connection.read())?;
2026-08-24 01:37:00 +03:00
let words = stats
.iter()
.map(|stat| {
2026-08-31 11:46:35 +03:00
state
2026-08-24 01:37:00 +03:00
.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
})
2026-08-31 11:46:35 +03:00
.map(|index| &state.dictionary[index])
2026-08-24 01:37:00 +03:00
.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
}
2026-08-30 20:43:31 +03:00
pub fn next(&mut self) -> (WordData, CardState) {
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 {
2026-08-30 20:43:31 +03:00
let word: &mut CardState = &mut self.set[index];
2026-08-24 23:27:24 +03:00
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-31 11:46:35 +03:00
update_stat_score(word, &self.state.connection.read())?;
2026-08-24 23:27:24 +03:00
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()
}
}