From e73359510d15c5c92bc9eb806c02f87d5e0667b9 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Thu, 1 Apr 2021 23:50:16 +1000 Subject: [PATCH] move filtered deck labels to backend - use strum to generate an iterator for the protobuf enum so we don't forget to add new labels if extending in the future - no add-ons appear to be using dynOrderLabels(), so it has been removed @RumovZ perhaps a similar approach might work for listing the available browser columns as well? --- pylib/anki/consts.py | 15 --------------- pylib/anki/decks.py | 4 ++-- pylib/anki/scheduler/base.py | 3 +++ qt/aqt/filtered_deck.py | 8 ++++---- rslib/backend.proto | 1 + rslib/build/protobuf.rs | 4 ++++ rslib/src/backend/decks.rs | 6 +++++- rslib/src/decks/filtered.rs | 23 +++++++++++++++++++++++ 8 files changed, 42 insertions(+), 22 deletions(-) diff --git a/pylib/anki/consts.py b/pylib/anki/consts.py index 4f9a765eb..065b9e61e 100644 --- a/pylib/anki/consts.py +++ b/pylib/anki/consts.py @@ -124,18 +124,3 @@ def newCardSchedulingLabels( 1: tr.scheduling_show_new_cards_after_reviews(), 2: tr.scheduling_show_new_cards_before_reviews(), } - - -def dynOrderLabels(col: Optional[anki.collection.Collection]) -> Dict[int, Any]: - tr = _tr(col) - return { - 0: tr.decks_oldest_seen_first(), - 1: tr.decks_random(), - 2: tr.decks_increasing_intervals(), - 3: tr.decks_decreasing_intervals(), - 4: tr.decks_most_lapses(), - 5: tr.decks_order_added(), - 6: tr.decks_order_due(), - 7: tr.decks_latest_added_first(), - 8: tr.decks_relative_overdueness(), - } diff --git a/pylib/anki/decks.py b/pylib/anki/decks.py index 18b84e5fa..0b593d68e 100644 --- a/pylib/anki/decks.py +++ b/pylib/anki/decks.py @@ -581,11 +581,11 @@ class DeckManager: def nameMap(self) -> Dict[str, DeckDict]: return {d["name"]: d for d in self.all()} - # Dynamic decks + # Filtered decks ########################################################################## def new_filtered(self, name: str) -> DeckId: - "Return a new dynamic deck and set it as the current deck." + "For new code, prefer col.sched.get_or_create_filtered_deck()." did = self.id(name, type=DEFAULT_DECK_CONF_ID) self.select(did) return did diff --git a/pylib/anki/scheduler/base.py b/pylib/anki/scheduler/base.py index 8f9eed5e4..280d9a7ec 100644 --- a/pylib/anki/scheduler/base.py +++ b/pylib/anki/scheduler/base.py @@ -105,6 +105,9 @@ select id from cards where did in %s and queue = {QUEUE_TYPE_REV} and due <= ? l ) -> OpChangesWithId: return self.col._backend.add_or_update_filtered_deck(deck) + def filtered_deck_order_labels(self) -> Sequence[str]: + return self.col._backend.filtered_deck_order_labels() + # Suspending & burying ########################################################################## diff --git a/qt/aqt/filtered_deck.py b/qt/aqt/filtered_deck.py index fded6c7ef..d449ef907 100644 --- a/qt/aqt/filtered_deck.py +++ b/qt/aqt/filtered_deck.py @@ -67,13 +67,13 @@ class FilteredDeckConfigDialog(QDialog): self.close() def _initial_dialog_setup(self) -> None: - import anki.consts as cs - self.form = aqt.forms.filtered_deck.Ui_Dialog() self.form.setupUi(self) - self.form.order.addItems(list(cs.dynOrderLabels(self.mw.col).values())) - self.form.order_2.addItems(list(cs.dynOrderLabels(self.mw.col).values())) + order_labels = self.col.sched.filtered_deck_order_labels() + + self.form.order.addItems(order_labels) + self.form.order_2.addItems(order_labels) qconnect(self.form.resched.stateChanged, self._onReschedToggled) diff --git a/rslib/backend.proto b/rslib/backend.proto index 88993b155..e88c7f3e8 100644 --- a/rslib/backend.proto +++ b/rslib/backend.proto @@ -150,6 +150,7 @@ service DecksService { rpc RenameDeck(RenameDeckIn) returns (OpChanges); rpc GetOrCreateFilteredDeck(DeckId) returns (FilteredDeckForUpdate); rpc AddOrUpdateFilteredDeck(FilteredDeckForUpdate) returns (OpChangesWithId); + rpc FilteredDeckOrderLabels(Empty) returns (StringList); } service NotesService { diff --git a/rslib/build/protobuf.rs b/rslib/build/protobuf.rs index 4d7b95718..090433735 100644 --- a/rslib/build/protobuf.rs +++ b/rslib/build/protobuf.rs @@ -89,6 +89,10 @@ pub fn write_backend_proto_rs() { config .out_dir(&out_dir) .service_generator(service_generator()) + .type_attribute( + "FilteredDeck.SearchTerm.Order", + "#[derive(strum::EnumIter)]", + ) .compile_protos(&[&backend_proto], &[&proto_dir, &out_dir]) .unwrap(); } diff --git a/rslib/src/backend/decks.rs b/rslib/src/backend/decks.rs index 80de68d26..1a4b34fbd 100644 --- a/rslib/src/backend/decks.rs +++ b/rslib/src/backend/decks.rs @@ -4,7 +4,7 @@ use super::Backend; use crate::{ backend_proto::{self as pb}, - decks::{Deck, DeckId, DeckSchema11}, + decks::{Deck, DeckId, DeckSchema11, FilteredSearchOrder}, prelude::*, scheduler::filtered::FilteredDeckForUpdate, }; @@ -153,6 +153,10 @@ impl DecksService for Backend { .map(|out| out.map(i64::from)) .map(Into::into) } + + fn filtered_deck_order_labels(&self, _input: pb::Empty) -> Result { + Ok(FilteredSearchOrder::labels(&self.tr).into()) + } } impl From for DeckId { diff --git a/rslib/src/decks/filtered.rs b/rslib/src/decks/filtered.rs index c4dc68c28..e5c3d0e04 100644 --- a/rslib/src/decks/filtered.rs +++ b/rslib/src/decks/filtered.rs @@ -1,6 +1,8 @@ // Copyright: Ankitects Pty Ltd and contributors // License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html +use strum::IntoEnumIterator; + pub use crate::backend_proto::{ deck_kind::Kind as DeckKind, Deck as DeckProto, DeckCommon, DeckKind as DeckKindProto, FilteredDeck, NormalDeck, @@ -41,3 +43,24 @@ impl Deck { matches!(self.kind, DeckKind::Filtered(_)) } } + +impl FilteredSearchOrder { + pub fn labels(tr: &I18n) -> Vec { + FilteredSearchOrder::iter().map(|v| v.label(tr)).collect() + } + + fn label(self, tr: &I18n) -> String { + match self { + FilteredSearchOrder::OldestReviewedFirst => tr.decks_oldest_seen_first(), + FilteredSearchOrder::Random => tr.decks_random(), + FilteredSearchOrder::IntervalsAscending => tr.decks_increasing_intervals(), + FilteredSearchOrder::IntervalsDescending => tr.decks_decreasing_intervals(), + FilteredSearchOrder::Lapses => tr.decks_most_lapses(), + FilteredSearchOrder::Added => tr.decks_order_added(), + FilteredSearchOrder::Due => tr.decks_order_due(), + FilteredSearchOrder::ReverseAdded => tr.decks_latest_added_first(), + FilteredSearchOrder::DuePriority => tr.decks_relative_overdueness(), + } + .into() + } +}