Handle card due dates and original due/did

This commit is contained in:
RumovZ 2022-04-09 23:42:05 +02:00
parent a528106af0
commit 126a92c239
2 changed files with 49 additions and 10 deletions

View file

@ -26,6 +26,7 @@ pub(super) struct ExchangeData {
pub(super) revlog: Vec<RevlogEntry>,
pub(super) deck_configs: Vec<DeckConfig>,
pub(super) media_paths: HashSet<PathBuf>,
pub(super) days_elapsed: u32,
}
impl ExchangeData {
@ -35,6 +36,7 @@ impl ExchangeData {
search: impl TryIntoSearch,
with_scheduling: bool,
) -> Result<()> {
self.days_elapsed = col.timing_today()?.days_elapsed;
self.notes = col.gather_notes(search)?;
self.cards = col.gather_cards()?;
self.decks = col.gather_decks()?;

View file

@ -15,7 +15,9 @@ use tempfile::NamedTempFile;
use zip::ZipArchive;
use crate::{
card::{CardQueue, CardType},
collection::CollectionBuilder,
config::SchedulerVersion,
decks::NormalDeck,
import_export::{
gather::ExchangeData,
@ -396,23 +398,28 @@ impl<'a> Context<'a> {
fn import_cards(&mut self) -> Result<()> {
let existing = self.target_col.storage.all_cards_as_nid_and_ord()?;
let collection_delta = self.collection_delta()?;
let version = self.target_col.scheduler_info()?.version;
for mut card in mem::take(&mut self.data.cards) {
if self.conflicting_notes.contains(&card.note_id) {
continue;
}
self.remap_note_id(&mut card);
if existing.contains(&(card.note_id, card.template_idx)) {
if !self.conflicting_notes.contains(&card.note_id) {
self.remap_note_id(&mut card);
if !existing.contains(&(card.note_id, card.template_idx)) {
self.remap_deck_id(&mut card);
card.shift_collection_relative_dates(collection_delta);
card.maybe_remove_from_filtered_deck(version);
self.add_card(&mut card)?;
}
// TODO: maybe update
continue;
}
self.remap_deck_id(&mut card);
// TODO: adjust collection-relative due times
// TODO: remove cards from filtered decks
self.add_card(&mut card)?;
}
Ok(())
}
/// The number of days the source collection is ahead of the target collection.
fn collection_delta(&mut self) -> Result<i32> {
Ok(self.data.days_elapsed as i32 - self.target_col.timing_today()?.days_elapsed as i32)
}
fn add_card(&mut self, card: &mut Card) -> Result<()> {
card.usn = self.usn;
if self.target_col.add_card_if_unique_undoable(card)? {
@ -497,3 +504,33 @@ impl Notetype {
hasher.digest().bytes()
}
}
impl Card {
/// `delta` is the number days the card's source collection is ahead of the
/// target collection.
fn shift_collection_relative_dates(&mut self, delta: i32) {
if self.due_in_days_since_collection_creation() {
self.due -= delta;
}
if self.original_due_in_days_since_collection_creation() && self.original_due != 0 {
self.original_due -= delta;
}
}
fn due_in_days_since_collection_creation(&self) -> bool {
matches!(self.queue, CardQueue::Review | CardQueue::DayLearn)
|| self.ctype == CardType::Review
}
fn original_due_in_days_since_collection_creation(&self) -> bool {
self.ctype == CardType::Review
}
fn maybe_remove_from_filtered_deck(&mut self, version: SchedulerVersion) {
if self.is_filtered() {
// instead of moving between decks, the deck is converted to a regular one
self.original_deck_id = self.deck_id;
self.remove_from_filtered_deck_restoring_queue(version);
}
}
}