Move tag collapse method to the backend

This commit is contained in:
abdo 2021-01-09 16:55:08 +03:00
parent 1be789f25f
commit f7f509c70d
5 changed files with 45 additions and 36 deletions

View file

@ -80,10 +80,9 @@ class TagManager:
res = self.col.db.list(query)
return list(set(self.split(" ".join(res))))
def toggle_browser_collapse(self, name: str):
tag = self.col.backend.get_tag(name)
tag.config.browser_collapsed = not tag.config.browser_collapsed
self.col.backend.update_tag(tag)
def set_collapsed(self, tag: str, collapsed: bool):
"Set browser collapse state for tag, registering the tag if missing."
self.col.backend.set_tag_collapsed(name=tag, collapsed=collapsed)
# Bulk addition/removal from notes
#############################################################

View file

@ -21,13 +21,7 @@ from anki.consts import *
from anki.lang import without_unicode_isolation
from anki.models import NoteType
from anki.notes import Note
from anki.rsbackend import (
ConcatSeparator,
DeckTreeNode,
InvalidInput,
NotFoundError,
TagTreeNode,
)
from anki.rsbackend import ConcatSeparator, DeckTreeNode, InvalidInput, TagTreeNode
from anki.stats import CardStats
from anki.utils import htmlToTextLine, ids2str, isMac, isWin
from aqt import AnkiQt, gui_hooks
@ -1152,18 +1146,10 @@ QTableView {{ gridline-color: {grid} }}
return lambda: self.setFilter("tag", full_name)
def toggle_expand():
full_name = head + node.name # pylint: disable=cell-var-from-loop
def toggle(_):
try:
self.mw.col.tags.toggle_browser_collapse(full_name)
except NotFoundError:
# tag is missing, register it first
self.mw.col.tags.register([full_name])
self.mw.col.tags.toggle_browser_collapse(full_name)
return toggle
return lambda _: self.mw.col.tags.set_collapsed(
full_name, not node.collapsed
)
item = SidebarItem(
node.name,

View file

@ -214,6 +214,7 @@ service BackendService {
rpc AllTags (Empty) returns (AllTagsOut);
rpc GetTag (String) returns (Tag);
rpc UpdateTag (Tag) returns (Bool);
rpc SetTagCollapsed (SetTagCollapsedIn) returns (Bool);
rpc ClearTag (String) returns (Bool);
rpc TagTree (Empty) returns (TagTreeNode);
@ -796,6 +797,11 @@ message AllTagsOut {
repeated Tag tags = 1;
}
message SetTagCollapsedIn {
string name = 1;
bool collapsed = 2;
}
message TagConfig {
bool browser_collapsed = 1;
}

View file

@ -44,6 +44,7 @@ use crate::{
get_remote_sync_meta, sync_abort, sync_login, FullSyncProgress, NormalSyncProgress,
SyncActionRequired, SyncAuth, SyncMeta, SyncOutput, SyncStage,
},
tags::Tag,
template::RenderedNode,
text::{extract_av_tags, strip_av_tags, AVTag},
timestamp::TimestampSecs,
@ -1316,6 +1317,24 @@ impl BackendService for Backend {
})
}
fn set_tag_collapsed(&self, input: pb::SetTagCollapsedIn) -> BackendResult<pb::Bool> {
self.with_col(|col| {
let name = &input.name;
let tag: Result<Tag> = if let Some(tag) = col.storage.get_tag(name)? {
Ok(tag)
} else {
// tag is missing, register it
let t = Tag {
name: name.to_owned(),
..Default::default()
};
Ok(col.register_tag(t)?.0)
};
tag?.config.browser_collapsed = input.collapsed;
Ok(pb::Bool { val: true })
})
}
fn register_tags(&self, input: pb::RegisterTagsIn) -> BackendResult<pb::Bool> {
self.with_col(|col| {
col.transact(None, |col| {

View file

@ -232,11 +232,11 @@ impl Collection {
usn,
..Default::default()
})?;
if t.0.is_empty() {
if t.0.name.is_empty() {
continue;
}
added |= t.1;
seen.insert(UniCase::new(t.0));
seen.insert(UniCase::new(t.0.name));
}
// exit early if no non-empty tags
@ -247,28 +247,27 @@ impl Collection {
// return the sorted, canonified tags
let mut tags = seen.into_iter().collect::<Vec<_>>();
tags.sort_unstable();
let tags: Vec<_> = tags
.into_iter()
.map(|s| s.into_inner().to_string())
.collect();
let tags: Vec<_> = tags.into_iter().map(|s| s.into_inner()).collect();
Ok((tags, added))
}
pub(crate) fn register_tag<'a>(&self, tag: Tag) -> Result<(Cow<'a, str>, bool)> {
pub(crate) fn register_tag(&self, tag: Tag) -> Result<(Tag, bool)> {
let native_name = human_tag_name_to_native(&tag.name);
let mut t = Tag {
name: native_name.clone(),
..tag
};
if native_name.is_empty() {
return Ok(("".into(), false));
return Ok((t, false));
}
if let Some(preferred) = self.storage.preferred_tag_case(&native_name)? {
Ok((native_tag_name_to_human(&preferred).into(), false))
t.name = native_tag_name_to_human(&preferred);
Ok((t, false))
} else {
let mut t = Tag {
name: native_name.clone(),
..tag
};
self.storage.register_tag(&mut t)?;
Ok((native_tag_name_to_human(&native_name).into(), true))
t.name = native_tag_name_to_human(&t.name);
Ok((t, true))
}
}