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?
This commit is contained in:
Damien Elmes 2021-04-01 23:50:16 +10:00
parent f55fe6e518
commit e73359510d
8 changed files with 42 additions and 22 deletions

View File

@ -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(),
}

View File

@ -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

View File

@ -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
##########################################################################

View File

@ -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)

View File

@ -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 {

View File

@ -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();
}

View File

@ -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<pb::StringList> {
Ok(FilteredSearchOrder::labels(&self.tr).into())
}
}
impl From<pb::DeckId> for DeckId {

View File

@ -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<String> {
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()
}
}