AppFlowy/frontend/rust-lib/flowy-database2/src/services/group/entities.rs
Richard Shiue a63a7ea611
feat: hidden kanban groups (#3907)
* feat: hide/unhide ui

* chore: implement collapsible side bar and adjust group header (#2)

* refactor: hidden columns into own file

* chore: adjust new group button position

* fix: flowy icon buton secondary color bleed

* chore: some UI adjustments

* fix: some regressions

* chore: proper group is_visible fetching

* chore: use a bloc to manage hidden groups

* fix: hiding groups not working

* chore: implement hidden group popups

* chore: proper ungrouped item column management

* chore: remove ungrouped items button

* chore: flowy hover build

* fix: clean up code

* test: integration tests

* fix: not null promise on null value

* fix: hide and unhide multiple groups

* chore: i18n and code review

* chore: missed review

* fix: rust-lib-test

* fix: dont completely remove flowyiconhovercolor

* chore: apply suggest

* fix: number of rows inside hidden groups not updating properly

* fix: hidden groups disappearing after collapse

* fix: hidden group title alignment

* fix: insert newly unhidden groups into the correct position

* chore: adjust padding all around

* feat: reorder hidden groups

* chore: adjust padding

* chore: collapse hidden groups section persist

* chore: no status group at beginning

* fix: hiding groups when grouping with other types

* chore: disable rename groups that arent supported

* chore: update appflowy board ref

* chore: better naming

* test: fix tests

---------

Co-authored-by: Mathias Mogensen <mathias@appflowy.io>
2023-11-13 16:14:31 +08:00

225 lines
5 KiB
Rust

use anyhow::bail;
use collab::core::any_map::AnyMapExtension;
use collab_database::database::gen_database_group_id;
use collab_database::rows::{RowDetail, RowId};
use collab_database::views::{GroupMap, GroupMapBuilder, GroupSettingBuilder, GroupSettingMap};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Default)]
pub struct GroupSetting {
pub id: String,
pub field_id: String,
pub field_type: i64,
pub groups: Vec<Group>,
pub content: String,
}
pub struct GroupChangesets {
pub changesets: Vec<GroupChangeset>,
}
impl From<Vec<GroupChangeset>> for GroupChangesets {
fn from(changesets: Vec<GroupChangeset>) -> Self {
Self { changesets }
}
}
#[derive(Clone, Default, Debug)]
pub struct GroupChangeset {
pub group_id: String,
pub field_id: String,
pub name: Option<String>,
pub visible: Option<bool>,
}
impl GroupSetting {
pub fn new(field_id: String, field_type: i64, content: String) -> Self {
Self {
id: gen_database_group_id(),
field_id,
field_type,
groups: vec![],
content,
}
}
}
const GROUP_ID: &str = "id";
const FIELD_ID: &str = "field_id";
const FIELD_TYPE: &str = "ty";
const GROUPS: &str = "groups";
const CONTENT: &str = "content";
impl TryFrom<GroupSettingMap> for GroupSetting {
type Error = anyhow::Error;
fn try_from(value: GroupSettingMap) -> Result<Self, Self::Error> {
match (
value.get_str_value(GROUP_ID),
value.get_str_value(FIELD_ID),
value.get_i64_value(FIELD_TYPE),
) {
(Some(id), Some(field_id), Some(field_type)) => {
let content = value.get_str_value(CONTENT).unwrap_or_default();
let groups = value.try_get_array(GROUPS);
Ok(Self {
id,
field_id,
field_type,
groups,
content,
})
},
_ => {
bail!("Invalid group setting data")
},
}
}
}
impl From<GroupSetting> for GroupSettingMap {
fn from(setting: GroupSetting) -> Self {
GroupSettingBuilder::new()
.insert_str_value(GROUP_ID, setting.id)
.insert_str_value(FIELD_ID, setting.field_id)
.insert_i64_value(FIELD_TYPE, setting.field_type)
.insert_maps(GROUPS, setting.groups)
.insert_str_value(CONTENT, setting.content)
.build()
}
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct Group {
pub id: String,
pub name: String,
#[serde(default = "GROUP_VISIBILITY")]
pub visible: bool,
}
impl TryFrom<GroupMap> for Group {
type Error = anyhow::Error;
fn try_from(value: GroupMap) -> Result<Self, Self::Error> {
match value.get_str_value("id") {
None => bail!("Invalid group data"),
Some(id) => {
let name = value.get_str_value("name").unwrap_or_default();
let visible = value.get_bool_value("visible").unwrap_or_default();
Ok(Self { id, name, visible })
},
}
}
}
impl From<Group> for GroupMap {
fn from(group: Group) -> Self {
GroupMapBuilder::new()
.insert_str_value("id", group.id)
.insert_str_value("name", group.name)
.insert_bool_value("visible", group.visible)
.build()
}
}
const GROUP_VISIBILITY: fn() -> bool = || true;
impl Group {
pub fn new(id: String, name: String) -> Self {
Self {
id,
name,
visible: true,
}
}
}
#[derive(Clone, Debug)]
pub struct GroupData {
pub id: String,
pub field_id: String,
pub name: String,
pub is_default: bool,
pub is_visible: bool,
pub(crate) rows: Vec<RowDetail>,
/// [filter_content] is used to determine which group the cell belongs to.
pub filter_content: String,
}
impl GroupData {
pub fn new(
id: String,
field_id: String,
name: String,
filter_content: String,
is_visible: bool,
) -> Self {
let is_default = id == field_id;
Self {
id,
field_id,
is_default,
is_visible,
name,
rows: vec![],
filter_content,
}
}
pub fn contains_row(&self, row_id: &RowId) -> bool {
self
.rows
.iter()
.any(|row_detail| &row_detail.row.id == row_id)
}
pub fn remove_row(&mut self, row_id: &RowId) {
match self
.rows
.iter()
.position(|row_detail| &row_detail.row.id == row_id)
{
None => {},
Some(pos) => {
self.rows.remove(pos);
},
}
}
pub fn add_row(&mut self, row_detail: RowDetail) {
match self.rows.iter().find(|r| r.row.id == row_detail.row.id) {
None => {
self.rows.push(row_detail);
},
Some(_) => {},
}
}
pub fn insert_row(&mut self, index: usize, row_detail: RowDetail) {
if index < self.rows.len() {
self.rows.insert(index, row_detail);
} else {
tracing::error!(
"Insert row index:{} beyond the bounds:{},",
index,
self.rows.len()
);
}
}
pub fn index_of_row(&self, row_id: &RowId) -> Option<usize> {
self
.rows
.iter()
.position(|row_detail| &row_detail.row.id == row_id)
}
pub fn number_of_row(&self) -> usize {
self.rows.len()
}
pub fn is_empty(&self) -> bool {
self.rows.is_empty()
}
}