2022-08-17 14:33:45 +08:00
|
|
|
use crate::entities::{GroupPB, GroupRowsChangesetPB, RowPB};
|
2022-08-12 16:05:56 +08:00
|
|
|
use crate::services::cell::{decode_any_cell_data, CellBytesParser};
|
2022-08-12 09:49:07 +08:00
|
|
|
use bytes::Bytes;
|
2022-08-14 15:15:56 +08:00
|
|
|
use flowy_error::FlowyResult;
|
2022-08-12 09:49:07 +08:00
|
|
|
use flowy_grid_data_model::revision::{
|
2022-08-18 15:02:44 +08:00
|
|
|
FieldRevision, GroupConfigurationRevision, RowChangeset, RowRevision, TypeOptionDataDeserializer,
|
2022-08-12 09:49:07 +08:00
|
|
|
};
|
2022-08-12 16:05:56 +08:00
|
|
|
use indexmap::IndexMap;
|
2022-08-12 09:49:07 +08:00
|
|
|
use std::marker::PhantomData;
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
2022-08-12 16:05:56 +08:00
|
|
|
pub trait GroupGenerator {
|
|
|
|
type ConfigurationType;
|
|
|
|
type TypeOptionType;
|
|
|
|
|
2022-08-13 23:26:42 +08:00
|
|
|
fn generate_groups(
|
2022-08-18 15:02:44 +08:00
|
|
|
field_id: &str,
|
2022-08-12 16:05:56 +08:00
|
|
|
configuration: &Option<Self::ConfigurationType>,
|
|
|
|
type_option: &Option<Self::TypeOptionType>,
|
|
|
|
) -> Vec<Group>;
|
2022-08-12 09:49:07 +08:00
|
|
|
}
|
|
|
|
|
2022-08-17 14:33:45 +08:00
|
|
|
pub trait Groupable: Send + Sync {
|
2022-08-13 23:26:42 +08:00
|
|
|
type CellDataType;
|
|
|
|
fn can_group(&self, content: &str, cell_data: &Self::CellDataType) -> bool;
|
2022-08-17 16:48:16 +08:00
|
|
|
fn add_row_if_match(&mut self, row_rev: &RowRevision, cell_data: &Self::CellDataType) -> Vec<GroupRowsChangesetPB>;
|
|
|
|
fn remove_row_if_match(
|
|
|
|
&mut self,
|
|
|
|
row_rev: &RowRevision,
|
|
|
|
cell_data: &Self::CellDataType,
|
|
|
|
) -> Vec<GroupRowsChangesetPB>;
|
|
|
|
|
2022-08-17 19:29:14 +08:00
|
|
|
fn move_row_if_match(
|
|
|
|
&mut self,
|
2022-08-18 15:02:44 +08:00
|
|
|
field_rev: &FieldRevision,
|
2022-08-17 19:29:14 +08:00
|
|
|
row_rev: &RowRevision,
|
2022-08-18 15:02:44 +08:00
|
|
|
row_changeset: &mut RowChangeset,
|
2022-08-17 19:29:14 +08:00
|
|
|
cell_data: &Self::CellDataType,
|
|
|
|
to_row_id: &str,
|
|
|
|
) -> Vec<GroupRowsChangesetPB>;
|
2022-08-17 14:33:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub trait GroupController: GroupControllerSharedAction + Send + Sync {
|
2022-08-17 16:48:16 +08:00
|
|
|
fn will_create_row(&mut self, row_rev: &mut RowRevision, field_rev: &FieldRevision, group_id: &str);
|
2022-08-13 23:26:42 +08:00
|
|
|
}
|
|
|
|
|
2022-08-17 14:33:45 +08:00
|
|
|
pub trait GroupControllerSharedAction: Send + Sync {
|
2022-08-17 08:14:25 +08:00
|
|
|
// The field that is used for grouping the rows
|
2022-08-14 11:05:55 +08:00
|
|
|
fn field_id(&self) -> &str;
|
2022-08-18 21:43:05 +08:00
|
|
|
fn groups(&self) -> Vec<Group>;
|
2022-08-14 11:05:55 +08:00
|
|
|
fn group_rows(&mut self, row_revs: &[Arc<RowRevision>], field_rev: &FieldRevision) -> FlowyResult<()>;
|
2022-08-17 14:33:45 +08:00
|
|
|
fn did_update_row(
|
|
|
|
&mut self,
|
|
|
|
row_rev: &RowRevision,
|
|
|
|
field_rev: &FieldRevision,
|
|
|
|
) -> FlowyResult<Vec<GroupRowsChangesetPB>>;
|
2022-08-17 16:48:16 +08:00
|
|
|
|
|
|
|
fn did_delete_row(
|
|
|
|
&mut self,
|
|
|
|
row_rev: &RowRevision,
|
|
|
|
field_rev: &FieldRevision,
|
|
|
|
) -> FlowyResult<Vec<GroupRowsChangesetPB>>;
|
2022-08-17 19:29:14 +08:00
|
|
|
|
|
|
|
fn did_move_row(
|
|
|
|
&mut self,
|
|
|
|
row_rev: &RowRevision,
|
2022-08-18 15:02:44 +08:00
|
|
|
row_changeset: &mut RowChangeset,
|
2022-08-17 19:29:14 +08:00
|
|
|
field_rev: &FieldRevision,
|
|
|
|
to_row_id: &str,
|
|
|
|
) -> FlowyResult<Vec<GroupRowsChangesetPB>>;
|
2022-08-13 23:26:42 +08:00
|
|
|
}
|
|
|
|
|
2022-08-13 16:23:44 +08:00
|
|
|
const DEFAULT_GROUP_ID: &str = "default_group";
|
|
|
|
|
2022-08-13 23:26:42 +08:00
|
|
|
/// C: represents the group configuration structure
|
|
|
|
/// T: the type option data deserializer that impl [TypeOptionDataDeserializer]
|
2022-08-17 14:33:45 +08:00
|
|
|
/// G: the group generator, [GroupGenerator]
|
2022-08-13 23:26:42 +08:00
|
|
|
/// P: the parser that impl [CellBytesParser] for the CellBytes
|
2022-08-17 14:33:45 +08:00
|
|
|
pub struct GenericGroupController<C, T, G, P> {
|
2022-08-14 11:05:55 +08:00
|
|
|
pub field_id: String,
|
|
|
|
pub groups_map: IndexMap<String, Group>,
|
2022-08-13 23:26:42 +08:00
|
|
|
default_group: Group,
|
2022-08-12 16:05:56 +08:00
|
|
|
pub type_option: Option<T>,
|
|
|
|
pub configuration: Option<C>,
|
2022-08-12 10:41:46 +08:00
|
|
|
group_action_phantom: PhantomData<G>,
|
2022-08-13 23:26:42 +08:00
|
|
|
cell_parser_phantom: PhantomData<P>,
|
2022-08-12 09:49:07 +08:00
|
|
|
}
|
|
|
|
|
2022-08-13 23:26:42 +08:00
|
|
|
#[derive(Clone)]
|
2022-08-12 09:49:07 +08:00
|
|
|
pub struct Group {
|
2022-08-12 16:05:56 +08:00
|
|
|
pub id: String,
|
2022-08-18 15:02:44 +08:00
|
|
|
pub field_id: String,
|
2022-08-12 16:05:56 +08:00
|
|
|
pub desc: String,
|
2022-08-17 14:33:45 +08:00
|
|
|
rows: Vec<RowPB>,
|
2022-08-12 16:05:56 +08:00
|
|
|
pub content: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::convert::From<Group> for GroupPB {
|
|
|
|
fn from(group: Group) -> Self {
|
|
|
|
Self {
|
2022-08-19 11:56:47 +08:00
|
|
|
field_id: group.field_id,
|
2022-08-12 16:05:56 +08:00
|
|
|
group_id: group.id,
|
|
|
|
desc: group.desc,
|
|
|
|
rows: group.rows,
|
|
|
|
}
|
|
|
|
}
|
2022-08-12 09:49:07 +08:00
|
|
|
}
|
|
|
|
|
2022-08-17 14:33:45 +08:00
|
|
|
impl Group {
|
2022-08-18 15:02:44 +08:00
|
|
|
pub fn new(id: String, field_id: String, desc: String, content: String) -> Self {
|
2022-08-17 14:33:45 +08:00
|
|
|
Self {
|
|
|
|
id,
|
2022-08-18 15:02:44 +08:00
|
|
|
field_id,
|
2022-08-17 14:33:45 +08:00
|
|
|
desc,
|
|
|
|
rows: vec![],
|
|
|
|
content,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn contains_row(&self, row_id: &str) -> bool {
|
|
|
|
self.rows.iter().any(|row| row.id == row_id)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn remove_row(&mut self, row_id: &str) {
|
|
|
|
match self.rows.iter().position(|row| row.id == row_id) {
|
|
|
|
None => {}
|
|
|
|
Some(pos) => {
|
|
|
|
self.rows.remove(pos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_row(&mut self, row_pb: RowPB) {
|
|
|
|
match self.rows.iter().find(|row| row.id == row_pb.id) {
|
|
|
|
None => {
|
|
|
|
self.rows.push(row_pb);
|
|
|
|
}
|
|
|
|
Some(_) => {}
|
|
|
|
}
|
|
|
|
}
|
2022-08-17 19:29:14 +08:00
|
|
|
|
|
|
|
pub fn insert_row(&mut self, index: usize, row_pb: RowPB) {
|
|
|
|
if index < self.rows.len() {
|
|
|
|
self.rows.insert(index, row_pb);
|
|
|
|
} else {
|
|
|
|
tracing::error!("Insert row index:{} beyond the bounds:{},", index, self.rows.len());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn index_of_row(&self, row_id: &str) -> Option<usize> {
|
|
|
|
self.rows.iter().position(|row| row.id == row_id)
|
|
|
|
}
|
2022-08-18 23:12:26 +08:00
|
|
|
|
|
|
|
pub fn number_of_row(&self) -> usize {
|
|
|
|
self.rows.len()
|
|
|
|
}
|
2022-08-17 14:33:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<C, T, G, P> GenericGroupController<C, T, G, P>
|
2022-08-12 09:49:07 +08:00
|
|
|
where
|
|
|
|
C: TryFrom<Bytes, Error = protobuf::ProtobufError>,
|
|
|
|
T: TypeOptionDataDeserializer,
|
2022-08-12 16:05:56 +08:00
|
|
|
G: GroupGenerator<ConfigurationType = C, TypeOptionType = T>,
|
2022-08-12 09:49:07 +08:00
|
|
|
{
|
2022-08-15 20:07:01 +08:00
|
|
|
pub fn new(field_rev: &Arc<FieldRevision>, configuration: GroupConfigurationRevision) -> FlowyResult<Self> {
|
2022-08-19 19:59:09 +08:00
|
|
|
// let configuration = match configuration.content {
|
|
|
|
// None => None,
|
|
|
|
// Some(content) => Some(C::try_from(Bytes::from(content))?),
|
|
|
|
// };
|
|
|
|
|
|
|
|
let configuration = None;
|
2022-08-19 11:56:47 +08:00
|
|
|
let field_type_rev = field_rev.ty;
|
2022-08-12 09:49:07 +08:00
|
|
|
let type_option = field_rev.get_type_option_entry::<T>(field_type_rev);
|
2022-08-18 15:02:44 +08:00
|
|
|
let groups = G::generate_groups(&field_rev.id, &configuration, &type_option);
|
2022-08-13 16:23:44 +08:00
|
|
|
|
2022-08-17 14:33:45 +08:00
|
|
|
let default_group = Group::new(
|
|
|
|
DEFAULT_GROUP_ID.to_owned(),
|
2022-08-18 15:02:44 +08:00
|
|
|
field_rev.id.clone(),
|
2022-08-17 14:33:45 +08:00
|
|
|
format!("No {}", field_rev.name),
|
|
|
|
"".to_string(),
|
|
|
|
);
|
2022-08-13 16:23:44 +08:00
|
|
|
|
2022-08-12 09:49:07 +08:00
|
|
|
Ok(Self {
|
2022-08-14 11:05:55 +08:00
|
|
|
field_id: field_rev.id.clone(),
|
|
|
|
groups_map: groups.into_iter().map(|group| (group.id.clone(), group)).collect(),
|
2022-08-13 16:23:44 +08:00
|
|
|
default_group,
|
2022-08-12 09:49:07 +08:00
|
|
|
type_option,
|
|
|
|
configuration,
|
2022-08-12 10:41:46 +08:00
|
|
|
group_action_phantom: PhantomData,
|
|
|
|
cell_parser_phantom: PhantomData,
|
2022-08-12 09:49:07 +08:00
|
|
|
})
|
|
|
|
}
|
2022-08-17 14:33:45 +08:00
|
|
|
}
|
2022-08-12 16:05:56 +08:00
|
|
|
|
2022-08-17 14:33:45 +08:00
|
|
|
impl<C, T, G, P> GroupControllerSharedAction for GenericGroupController<C, T, G, P>
|
|
|
|
where
|
|
|
|
P: CellBytesParser,
|
|
|
|
Self: Groupable<CellDataType = P::Object>,
|
|
|
|
{
|
|
|
|
fn field_id(&self) -> &str {
|
|
|
|
&self.field_id
|
|
|
|
}
|
|
|
|
|
2022-08-18 21:43:05 +08:00
|
|
|
fn groups(&self) -> Vec<Group> {
|
2022-08-13 23:26:42 +08:00
|
|
|
let default_group = self.default_group.clone();
|
2022-08-14 11:05:55 +08:00
|
|
|
let mut groups: Vec<Group> = self.groups_map.values().cloned().collect();
|
2022-08-13 16:23:44 +08:00
|
|
|
if !default_group.rows.is_empty() {
|
|
|
|
groups.push(default_group);
|
|
|
|
}
|
|
|
|
groups
|
2022-08-12 16:05:56 +08:00
|
|
|
}
|
2022-08-12 09:49:07 +08:00
|
|
|
|
2022-08-17 14:33:45 +08:00
|
|
|
fn group_rows(&mut self, row_revs: &[Arc<RowRevision>], field_rev: &FieldRevision) -> FlowyResult<()> {
|
2022-08-12 09:49:07 +08:00
|
|
|
if self.configuration.is_none() {
|
2022-08-12 16:05:56 +08:00
|
|
|
return Ok(());
|
2022-08-12 09:49:07 +08:00
|
|
|
}
|
2022-08-14 11:05:55 +08:00
|
|
|
|
2022-08-17 14:33:45 +08:00
|
|
|
for row_rev in row_revs {
|
|
|
|
if let Some(cell_rev) = row_rev.cells.get(&self.field_id) {
|
2022-08-14 11:05:55 +08:00
|
|
|
let mut records: Vec<GroupRecord> = vec![];
|
2022-08-14 15:15:56 +08:00
|
|
|
let cell_bytes = decode_any_cell_data(cell_rev.data.clone(), field_rev);
|
2022-08-14 11:05:55 +08:00
|
|
|
let cell_data = cell_bytes.parser::<P>()?;
|
|
|
|
for group in self.groups_map.values() {
|
|
|
|
if self.can_group(&group.content, &cell_data) {
|
|
|
|
records.push(GroupRecord {
|
2022-08-17 14:33:45 +08:00
|
|
|
row: row_rev.into(),
|
2022-08-14 11:05:55 +08:00
|
|
|
group_id: group.id.clone(),
|
|
|
|
});
|
|
|
|
}
|
2022-08-12 16:05:56 +08:00
|
|
|
}
|
|
|
|
|
2022-08-14 11:05:55 +08:00
|
|
|
if records.is_empty() {
|
2022-08-17 14:33:45 +08:00
|
|
|
self.default_group.rows.push(row_rev.into());
|
2022-08-14 11:05:55 +08:00
|
|
|
} else {
|
|
|
|
for record in records {
|
|
|
|
if let Some(group) = self.groups_map.get_mut(&record.group_id) {
|
|
|
|
group.rows.push(record.row);
|
|
|
|
}
|
2022-08-12 16:05:56 +08:00
|
|
|
}
|
|
|
|
}
|
2022-08-14 11:05:55 +08:00
|
|
|
} else {
|
2022-08-17 14:33:45 +08:00
|
|
|
self.default_group.rows.push(row_rev.into());
|
2022-08-12 10:41:46 +08:00
|
|
|
}
|
2022-08-12 09:49:07 +08:00
|
|
|
}
|
2022-08-12 16:05:56 +08:00
|
|
|
|
|
|
|
Ok(())
|
2022-08-12 09:49:07 +08:00
|
|
|
}
|
2022-08-17 14:33:45 +08:00
|
|
|
|
|
|
|
fn did_update_row(
|
|
|
|
&mut self,
|
|
|
|
row_rev: &RowRevision,
|
|
|
|
field_rev: &FieldRevision,
|
|
|
|
) -> FlowyResult<Vec<GroupRowsChangesetPB>> {
|
|
|
|
if let Some(cell_rev) = row_rev.cells.get(&self.field_id) {
|
|
|
|
let cell_bytes = decode_any_cell_data(cell_rev.data.clone(), field_rev);
|
|
|
|
let cell_data = cell_bytes.parser::<P>()?;
|
2022-08-17 16:48:16 +08:00
|
|
|
Ok(self.add_row_if_match(row_rev, &cell_data))
|
|
|
|
} else {
|
|
|
|
Ok(vec![])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn did_delete_row(
|
|
|
|
&mut self,
|
|
|
|
row_rev: &RowRevision,
|
|
|
|
field_rev: &FieldRevision,
|
|
|
|
) -> FlowyResult<Vec<GroupRowsChangesetPB>> {
|
|
|
|
if let Some(cell_rev) = row_rev.cells.get(&self.field_id) {
|
|
|
|
let cell_bytes = decode_any_cell_data(cell_rev.data.clone(), field_rev);
|
|
|
|
let cell_data = cell_bytes.parser::<P>()?;
|
|
|
|
Ok(self.remove_row_if_match(row_rev, &cell_data))
|
2022-08-17 14:33:45 +08:00
|
|
|
} else {
|
|
|
|
Ok(vec![])
|
|
|
|
}
|
|
|
|
}
|
2022-08-17 19:29:14 +08:00
|
|
|
|
|
|
|
fn did_move_row(
|
|
|
|
&mut self,
|
|
|
|
row_rev: &RowRevision,
|
2022-08-18 15:02:44 +08:00
|
|
|
row_changeset: &mut RowChangeset,
|
2022-08-17 19:29:14 +08:00
|
|
|
field_rev: &FieldRevision,
|
|
|
|
to_row_id: &str,
|
|
|
|
) -> FlowyResult<Vec<GroupRowsChangesetPB>> {
|
|
|
|
if let Some(cell_rev) = row_rev.cells.get(&self.field_id) {
|
|
|
|
let cell_bytes = decode_any_cell_data(cell_rev.data.clone(), field_rev);
|
|
|
|
let cell_data = cell_bytes.parser::<P>()?;
|
2022-08-18 17:40:23 +08:00
|
|
|
tracing::trace!("Move row:{} to row:{}", row_rev.id, to_row_id);
|
2022-08-18 15:02:44 +08:00
|
|
|
Ok(self.move_row_if_match(field_rev, row_rev, row_changeset, &cell_data, to_row_id))
|
2022-08-17 19:29:14 +08:00
|
|
|
} else {
|
|
|
|
Ok(vec![])
|
|
|
|
}
|
|
|
|
}
|
2022-08-12 09:49:07 +08:00
|
|
|
}
|
2022-08-12 16:05:56 +08:00
|
|
|
|
|
|
|
struct GroupRecord {
|
|
|
|
row: RowPB,
|
|
|
|
group_id: String,
|
|
|
|
}
|