Files
jap_learn/src/core/deck.rs
T

127 lines
4.3 KiB
Rust
Raw Normal View History

2026-08-24 01:37:00 +03:00
use crate::app::AppState;
2026-08-24 23:27:24 +03:00
use crate::core::srs::{
OrderMode, OrderModule, RandomSRSModule, SRSModule, SemiRandomSRSModule, WordOpenMode,
WorstWordsSRSModule,
};
2026-08-24 11:35:37 +03:00
use crate::data::entities::*;
2026-08-24 11:41:20 +03:00
use crate::data_provider::card_stats::{load_stats_of_deck, update_stat_score};
use crate::data_provider::history::push_note;
2026-08-24 23:42:58 +03:00
use crate::notification::{MaybeError, NotificationData};
2026-08-24 11:41:20 +03:00
use chrono::Utc;
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,
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-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,
order_module: match settings.open_mode {
OrderMode::Default => OrderModule::SemiRandomSRS(SemiRandomSRSModule::new()),
OrderMode::TrainWorstFirst => {
OrderModule::WorstWordsSRS(WorstWordsSRSModule::new())
}
OrderMode::FullRandom => OrderModule::RandomSRS(RandomSRSModule::new()),
},
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) {
let index = match self.order_module.clone() {
OrderModule::SemiRandomSRS(mut module) => {
if !module.initialized {
module.init(self)
}
let index = module.next(self);
self.order_module = OrderModule::SemiRandomSRS(module);
index
}
OrderModule::RandomSRS(mut module) => {
if !module.initialized {
module.init(self)
}
let index = module.next(self);
self.order_module = OrderModule::RandomSRS(module);
index
}
OrderModule::WorstWordsSRS(mut module) => {
if !module.initialized {
module.init(self)
}
let index = module.next(self);
self.order_module = OrderModule::WorstWordsSRS(module);
index
}
};
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 {
OrderModule::SemiRandomSRS(ref mut module) => {
module.open(status, index, word);
}
OrderModule::RandomSRS(ref mut module) => {
module.open(status, index, word);
}
OrderModule::WorstWordsSRS(ref mut module) => {
module.open(status, index, word);
}
2026-08-24 01:37:00 +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()
}
}