Anki/rslib/src/decks/current.rs
Damien Elmes 6e954e82a5 current deck change is now undoable
- make sure we set flag in changes when config var changed
- move current deck get/set into backend
- set_config() now returns a bool indicating whether a change was
made, so other operations can be gated off it
- active decks generation is deferred until sched.reset()
2021-04-06 21:52:06 +10:00

41 lines
1.3 KiB
Rust

// Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
use std::sync::Arc;
use crate::{config::ConfigKey, prelude::*};
impl Collection {
pub fn set_current_deck(&mut self, deck: DeckId) -> Result<OpOutput<()>> {
self.transact(Op::SetCurrentDeck, |col| col.set_current_deck_inner(deck))
}
/// Fetch the current deck, falling back to the default if the previously
/// selected deck is invalid.
pub fn get_current_deck(&mut self) -> Result<Arc<Deck>> {
if let Some(deck) = self.get_deck(self.get_current_deck_id())? {
return Ok(deck);
}
self.get_deck(DeckId(1))?.ok_or(AnkiError::NotFound)
}
}
impl Collection {
/// The returned id may reference a deck that does not exist;
/// prefer using get_current_deck() instead.
pub(crate) fn get_current_deck_id(&self) -> DeckId {
self.get_config_optional(ConfigKey::CurrentDeckId)
.unwrap_or(DeckId(1))
}
fn set_current_deck_inner(&mut self, deck: DeckId) -> Result<()> {
if self.set_current_deck_id(deck)? {
self.state.card_queues = None;
}
Ok(())
}
fn set_current_deck_id(&mut self, did: DeckId) -> Result<bool> {
self.set_config(ConfigKey::CurrentDeckId, &did)
}
}