remove some duplicate code & add deck.or() helper

This commit is contained in:
Damien Elmes 2021-05-14 22:35:52 +10:00
parent 390a8421aa
commit 35063316d3
4 changed files with 26 additions and 38 deletions

View File

@ -36,6 +36,16 @@ use crate::{
define_newtype!(DeckId, i64);
impl DeckId {
pub(crate) fn or(self, other: DeckId) -> Self {
if self.0 == 0 {
other
} else {
self
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Deck {
pub id: DeckId,

View File

@ -135,11 +135,7 @@ impl Collection {
map.entry("Tags").or_insert_with(|| tags.into());
map.entry("Type").or_insert_with(|| nt.name.clone().into());
let deck_name: Cow<str> = self
.get_deck(if card.original_deck_id.0 > 0 {
card.original_deck_id
} else {
card.deck_id
})?
.get_deck(card.original_deck_id.or(card.deck_id))?
.map(|d| d.human_name().into())
.unwrap_or_else(|| "(Deck)".into());
let subdeck_name = deck_name.rsplit("::").next().unwrap();

View File

@ -72,11 +72,7 @@ impl Card {
}
pub(crate) fn original_or_current_deck_id(&self) -> DeckId {
if self.original_deck_id.0 > 0 {
self.original_deck_id
} else {
self.deck_id
}
self.original_deck_id.or(self.deck_id)
}
pub(crate) fn remove_from_filtered_deck_restoring_queue(&mut self, sched: SchedulerVersion) {

View File

@ -217,6 +217,18 @@ impl Collection {
let selected_deck_limits = limits[parent_count];
let mut queues = QueueBuilder::new(sort_options);
let get_bury_mode = |home_deck: DeckId| {
deck_map
.get(&home_deck)
.and_then(|deck| deck.config_id())
.and_then(|config_id| config.get(&config_id))
.map(|config| BuryMode {
bury_new: config.inner.bury_new,
bury_reviews: config.inner.bury_reviews,
})
.unwrap_or_default()
};
for (deck, mut limit) in decks.iter().zip(limits).skip(parent_count) {
if limit.review > 0 {
self.storage.for_each_due_card_in_deck(
@ -224,40 +236,14 @@ impl Collection {
timing.next_day_at,
deck.id,
|queue, card| {
let home_deck = if card.original_deck_id.0 == 0 {
deck.id
} else {
card.original_deck_id
};
let bury = deck_map
.get(&home_deck)
.and_then(|deck| deck.config_id())
.and_then(|config_id| config.get(&config_id))
.map(|config| BuryMode {
bury_new: config.inner.bury_new,
bury_reviews: config.inner.bury_reviews,
})
.unwrap_or_default();
let bury = get_bury_mode(card.original_deck_id.or(deck.id));
queues.add_due_card(&mut limit, queue, card, bury)
},
)?;
}
if limit.new > 0 {
self.storage.for_each_new_card_in_deck(deck.id, |card| {
let home_deck = if card.original_deck_id.0 == 0 {
deck.id
} else {
card.original_deck_id
};
let bury = deck_map
.get(&home_deck)
.and_then(|deck| deck.config_id())
.and_then(|config_id| config.get(&config_id))
.map(|config| BuryMode {
bury_new: config.inner.bury_new,
bury_reviews: config.inner.bury_reviews,
})
.unwrap_or_default();
let bury = get_bury_mode(card.original_deck_id.or(deck.id));
queues.add_new_card(&mut limit, card, bury)
})?;
}