From 126a92c239b135f39961bbed5f4516a760f5cae0 Mon Sep 17 00:00:00 2001 From: RumovZ Date: Sat, 9 Apr 2022 23:42:05 +0200 Subject: [PATCH] Handle card due dates and original due/did --- rslib/src/import_export/gather.rs | 2 + .../src/import_export/package/apkg/import.rs | 57 +++++++++++++++---- 2 files changed, 49 insertions(+), 10 deletions(-) diff --git a/rslib/src/import_export/gather.rs b/rslib/src/import_export/gather.rs index e96ba9a1c..a7ee4e2d5 100644 --- a/rslib/src/import_export/gather.rs +++ b/rslib/src/import_export/gather.rs @@ -26,6 +26,7 @@ pub(super) struct ExchangeData { pub(super) revlog: Vec, pub(super) deck_configs: Vec, pub(super) media_paths: HashSet, + 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()?; diff --git a/rslib/src/import_export/package/apkg/import.rs b/rslib/src/import_export/package/apkg/import.rs index 843c4fd83..7bf339ad8 100644 --- a/rslib/src/import_export/package/apkg/import.rs +++ b/rslib/src/import_export/package/apkg/import.rs @@ -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 { + 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); + } + } +}