2023-04-28 14:08:53 +08:00
|
|
|
use std::marker::PhantomData;
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
2023-10-28 11:48:28 +08:00
|
|
|
use async_trait::async_trait;
|
2023-04-28 14:08:53 +08:00
|
|
|
use collab_database::fields::{Field, TypeOptionData};
|
2023-11-28 10:43:22 +08:00
|
|
|
use collab_database::rows::{Cells, Row, RowDetail, RowId};
|
2023-10-28 11:48:28 +08:00
|
|
|
use futures::executor::block_on;
|
2023-04-28 14:08:53 +08:00
|
|
|
use serde::de::DeserializeOwned;
|
|
|
|
use serde::Serialize;
|
|
|
|
|
|
|
|
use flowy_error::FlowyResult;
|
|
|
|
|
2023-06-14 22:16:33 +08:00
|
|
|
use crate::entities::{
|
2023-11-13 16:14:31 +08:00
|
|
|
FieldType, GroupChangesPB, GroupPB, GroupRowsNotificationPB, InsertedGroupPB, InsertedRowPB,
|
|
|
|
RowMetaPB,
|
2023-06-14 22:16:33 +08:00
|
|
|
};
|
2023-10-28 11:48:28 +08:00
|
|
|
use crate::services::cell::{get_cell_protobuf, CellProtobufBlobParser};
|
|
|
|
use crate::services::field::{default_type_option_data_from_type, TypeOption, TypeOptionCellData};
|
2023-04-28 14:08:53 +08:00
|
|
|
use crate::services::group::action::{
|
2023-05-28 22:54:03 +08:00
|
|
|
DidMoveGroupRowResult, DidUpdateGroupRowResult, GroupControllerOperation, GroupCustomize,
|
2023-04-28 14:08:53 +08:00
|
|
|
};
|
|
|
|
use crate::services::group::configuration::GroupContext;
|
2023-10-29 11:26:49 +08:00
|
|
|
use crate::services::group::entities::GroupData;
|
|
|
|
use crate::services::group::{GroupChangeset, GroupChangesets, GroupsBuilder, MoveGroupRowContext};
|
2023-04-28 14:08:53 +08:00
|
|
|
|
|
|
|
// use collab_database::views::Group;
|
|
|
|
|
|
|
|
/// The [GroupController] trait defines the group actions, including create/delete/move items
|
|
|
|
/// For example, the group will insert a item if the one of the new [RowRevision]'s [CellRevision]s
|
|
|
|
/// content match the group filter.
|
|
|
|
///
|
|
|
|
/// Different [FieldType] has a different controller that implements the [GroupController] trait.
|
|
|
|
/// If the [FieldType] doesn't implement its group controller, then the [DefaultGroupController] will
|
|
|
|
/// be used.
|
|
|
|
///
|
2023-05-28 22:54:03 +08:00
|
|
|
pub trait GroupController: GroupControllerOperation + Send + Sync {
|
|
|
|
/// Called when the type option of the [Field] was updated.
|
2023-10-28 11:48:28 +08:00
|
|
|
fn did_update_field_type_option(&mut self, field: &Field);
|
2023-05-28 22:54:03 +08:00
|
|
|
|
|
|
|
/// Called before the row was created.
|
2023-04-28 14:08:53 +08:00
|
|
|
fn will_create_row(&mut self, cells: &mut Cells, field: &Field, group_id: &str);
|
|
|
|
}
|
|
|
|
|
2023-10-28 11:48:28 +08:00
|
|
|
#[async_trait]
|
|
|
|
pub trait GroupOperationInterceptor {
|
|
|
|
type GroupTypeOption: TypeOption;
|
|
|
|
async fn type_option_from_group_changeset(
|
|
|
|
&self,
|
2023-11-13 16:14:31 +08:00
|
|
|
_changeset: &GroupChangeset,
|
|
|
|
_type_option: &Self::GroupTypeOption,
|
|
|
|
_view_id: &str,
|
|
|
|
) -> Option<TypeOptionData> {
|
|
|
|
None
|
|
|
|
}
|
2023-04-28 14:08:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// C: represents the group configuration that impl [GroupConfigurationSerde]
|
|
|
|
/// T: the type-option data deserializer that impl [TypeOptionDataDeserializer]
|
2023-05-28 22:54:03 +08:00
|
|
|
/// G: the group generator, [GroupsBuilder]
|
2023-04-28 14:08:53 +08:00
|
|
|
/// P: the parser that impl [CellProtobufBlobParser] for the CellBytes
|
2023-10-28 11:48:28 +08:00
|
|
|
pub struct BaseGroupController<C, T, G, P, I> {
|
2023-04-28 14:08:53 +08:00
|
|
|
pub grouping_field_id: String,
|
2023-10-28 11:48:28 +08:00
|
|
|
pub type_option: T,
|
2023-05-28 22:54:03 +08:00
|
|
|
pub context: GroupContext<C>,
|
2023-10-28 11:48:28 +08:00
|
|
|
group_builder_phantom: PhantomData<G>,
|
2023-04-28 14:08:53 +08:00
|
|
|
cell_parser_phantom: PhantomData<P>,
|
2023-10-28 11:48:28 +08:00
|
|
|
pub operation_interceptor: I,
|
2023-04-28 14:08:53 +08:00
|
|
|
}
|
|
|
|
|
2023-10-28 11:48:28 +08:00
|
|
|
impl<C, T, G, P, I> BaseGroupController<C, T, G, P, I>
|
2023-04-28 14:08:53 +08:00
|
|
|
where
|
|
|
|
C: Serialize + DeserializeOwned,
|
2023-10-28 11:48:28 +08:00
|
|
|
T: TypeOption + From<TypeOptionData> + Send + Sync,
|
|
|
|
G: GroupsBuilder<Context = GroupContext<C>, GroupTypeOption = T>,
|
|
|
|
I: GroupOperationInterceptor<GroupTypeOption = T> + Send + Sync,
|
2023-04-28 14:08:53 +08:00
|
|
|
{
|
|
|
|
pub async fn new(
|
|
|
|
grouping_field: &Arc<Field>,
|
|
|
|
mut configuration: GroupContext<C>,
|
2023-10-28 11:48:28 +08:00
|
|
|
operation_interceptor: I,
|
2023-04-28 14:08:53 +08:00
|
|
|
) -> FlowyResult<Self> {
|
|
|
|
let field_type = FieldType::from(grouping_field.field_type);
|
2023-10-28 11:48:28 +08:00
|
|
|
let type_option = grouping_field
|
|
|
|
.get_type_option::<T>(&field_type)
|
|
|
|
.unwrap_or_else(|| T::from(default_type_option_data_from_type(&field_type)));
|
|
|
|
|
|
|
|
// TODO(nathan): remove block_on
|
|
|
|
let generated_groups = block_on(G::build(grouping_field, &configuration, &type_option));
|
2023-05-28 22:54:03 +08:00
|
|
|
let _ = configuration.init_groups(generated_groups)?;
|
2023-04-28 14:08:53 +08:00
|
|
|
|
|
|
|
Ok(Self {
|
|
|
|
grouping_field_id: grouping_field.id.clone(),
|
|
|
|
type_option,
|
2023-05-28 22:54:03 +08:00
|
|
|
context: configuration,
|
2023-10-28 11:48:28 +08:00
|
|
|
group_builder_phantom: PhantomData,
|
2023-04-28 14:08:53 +08:00
|
|
|
cell_parser_phantom: PhantomData,
|
2023-10-28 11:48:28 +08:00
|
|
|
operation_interceptor,
|
2023-04-28 14:08:53 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn update_no_status_group(
|
|
|
|
&mut self,
|
2023-06-14 22:16:33 +08:00
|
|
|
row_detail: &RowDetail,
|
2023-04-28 14:08:53 +08:00
|
|
|
other_group_changesets: &[GroupRowsNotificationPB],
|
|
|
|
) -> Option<GroupRowsNotificationPB> {
|
2023-05-28 22:54:03 +08:00
|
|
|
let no_status_group = self.context.get_mut_no_status_group()?;
|
2023-04-28 14:08:53 +08:00
|
|
|
|
|
|
|
// [other_group_inserted_row] contains all the inserted rows except the default group.
|
|
|
|
let other_group_inserted_row = other_group_changesets
|
|
|
|
.iter()
|
|
|
|
.flat_map(|changeset| &changeset.inserted_rows)
|
|
|
|
.collect::<Vec<&InsertedRowPB>>();
|
|
|
|
|
|
|
|
// Calculate the inserted_rows of the default_group
|
|
|
|
let no_status_group_rows = other_group_changesets
|
|
|
|
.iter()
|
|
|
|
.flat_map(|changeset| &changeset.deleted_rows)
|
2023-12-30 01:22:06 +08:00
|
|
|
.filter(|&row_id| {
|
2023-04-28 14:08:53 +08:00
|
|
|
// if the [other_group_inserted_row] contains the row_id of the row
|
|
|
|
// which means the row should not move to the default group.
|
|
|
|
!other_group_inserted_row
|
|
|
|
.iter()
|
2023-06-14 22:16:33 +08:00
|
|
|
.any(|inserted_row| &inserted_row.row_meta.id == row_id)
|
2023-04-28 14:08:53 +08:00
|
|
|
})
|
2023-12-30 01:22:06 +08:00
|
|
|
.cloned()
|
2023-04-28 20:47:40 +08:00
|
|
|
.collect::<Vec<String>>();
|
2023-04-28 14:08:53 +08:00
|
|
|
|
|
|
|
let mut changeset = GroupRowsNotificationPB::new(no_status_group.id.clone());
|
|
|
|
if !no_status_group_rows.is_empty() {
|
2023-06-14 22:16:33 +08:00
|
|
|
changeset
|
|
|
|
.inserted_rows
|
2023-08-28 13:28:24 +08:00
|
|
|
.push(InsertedRowPB::new(RowMetaPB::from(row_detail)));
|
2023-06-14 22:16:33 +08:00
|
|
|
no_status_group.add_row(row_detail.clone());
|
2023-04-28 14:08:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// [other_group_delete_rows] contains all the deleted rows except the default group.
|
2023-04-28 20:47:40 +08:00
|
|
|
let other_group_delete_rows: Vec<String> = other_group_changesets
|
2023-04-28 14:08:53 +08:00
|
|
|
.iter()
|
|
|
|
.flat_map(|changeset| &changeset.deleted_rows)
|
|
|
|
.cloned()
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let default_group_deleted_rows = other_group_changesets
|
|
|
|
.iter()
|
|
|
|
.flat_map(|changeset| &changeset.inserted_rows)
|
|
|
|
.filter(|inserted_row| {
|
|
|
|
// if the [other_group_delete_rows] contain the inserted_row, which means this row should move
|
|
|
|
// out from the default_group.
|
|
|
|
!other_group_delete_rows
|
|
|
|
.iter()
|
2023-06-14 22:16:33 +08:00
|
|
|
.any(|row_id| &inserted_row.row_meta.id == row_id)
|
2023-04-28 14:08:53 +08:00
|
|
|
})
|
|
|
|
.collect::<Vec<&InsertedRowPB>>();
|
|
|
|
|
|
|
|
let mut deleted_row_ids = vec![];
|
2023-06-14 22:16:33 +08:00
|
|
|
for row_detail in &no_status_group.rows {
|
2023-08-28 13:28:24 +08:00
|
|
|
let row_id = row_detail.row.id.to_string();
|
2023-04-28 14:08:53 +08:00
|
|
|
if default_group_deleted_rows
|
|
|
|
.iter()
|
2023-06-14 22:16:33 +08:00
|
|
|
.any(|deleted_row| deleted_row.row_meta.id == row_id)
|
2023-04-28 14:08:53 +08:00
|
|
|
{
|
2023-04-28 20:47:40 +08:00
|
|
|
deleted_row_ids.push(row_id);
|
2023-04-28 14:08:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
no_status_group
|
|
|
|
.rows
|
2023-08-28 13:28:24 +08:00
|
|
|
.retain(|row_detail| !deleted_row_ids.contains(&row_detail.row.id));
|
2023-04-28 20:47:40 +08:00
|
|
|
changeset.deleted_rows.extend(deleted_row_ids);
|
2023-04-28 14:08:53 +08:00
|
|
|
Some(changeset)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-28 11:48:28 +08:00
|
|
|
#[async_trait]
|
|
|
|
impl<C, T, G, P, I> GroupControllerOperation for BaseGroupController<C, T, G, P, I>
|
2023-04-28 14:08:53 +08:00
|
|
|
where
|
2023-10-28 11:48:28 +08:00
|
|
|
P: CellProtobufBlobParser<Object = <T as TypeOption>::CellProtobufType>,
|
|
|
|
C: Serialize + DeserializeOwned + Sync + Send,
|
|
|
|
T: TypeOption + From<TypeOptionData> + Send + Sync,
|
|
|
|
G: GroupsBuilder<Context = GroupContext<C>, GroupTypeOption = T>,
|
|
|
|
I: GroupOperationInterceptor<GroupTypeOption = T> + Send + Sync,
|
|
|
|
Self: GroupCustomize<GroupTypeOption = T>,
|
2023-04-28 14:08:53 +08:00
|
|
|
{
|
|
|
|
fn field_id(&self) -> &str {
|
|
|
|
&self.grouping_field_id
|
|
|
|
}
|
|
|
|
|
2023-11-06 16:17:05 +08:00
|
|
|
fn get_all_groups(&self) -> Vec<&GroupData> {
|
2023-05-28 22:54:03 +08:00
|
|
|
self.context.groups()
|
2023-04-28 14:08:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn get_group(&self, group_id: &str) -> Option<(usize, GroupData)> {
|
2023-05-28 22:54:03 +08:00
|
|
|
let group = self.context.get_group(group_id)?;
|
2023-04-28 14:08:53 +08:00
|
|
|
Some((group.0, group.1.clone()))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tracing::instrument(level = "trace", skip_all, fields(row_count=%rows.len(), group_result))]
|
2023-10-28 11:48:28 +08:00
|
|
|
fn fill_groups(&mut self, rows: &[&RowDetail], _field: &Field) -> FlowyResult<()> {
|
2023-06-14 22:16:33 +08:00
|
|
|
for row_detail in rows {
|
|
|
|
let cell = match row_detail.row.cells.get(&self.grouping_field_id) {
|
2023-04-28 14:08:53 +08:00
|
|
|
None => self.placeholder_cell(),
|
|
|
|
Some(cell) => Some(cell.clone()),
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(cell) = cell {
|
|
|
|
let mut grouped_rows: Vec<GroupedRow> = vec![];
|
2023-10-28 11:48:28 +08:00
|
|
|
let cell_data = <T as TypeOption>::CellData::from(&cell);
|
2023-05-28 22:54:03 +08:00
|
|
|
for group in self.context.groups() {
|
2023-04-28 14:08:53 +08:00
|
|
|
if self.can_group(&group.filter_content, &cell_data) {
|
|
|
|
grouped_rows.push(GroupedRow {
|
2023-06-14 22:16:33 +08:00
|
|
|
row_detail: (*row_detail).clone(),
|
2023-04-28 14:08:53 +08:00
|
|
|
group_id: group.id.clone(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !grouped_rows.is_empty() {
|
|
|
|
for group_row in grouped_rows {
|
2023-05-28 22:54:03 +08:00
|
|
|
if let Some(group) = self.context.get_mut_group(&group_row.group_id) {
|
2023-06-14 22:16:33 +08:00
|
|
|
group.add_row(group_row.row_detail);
|
2023-04-28 14:08:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2023-06-01 20:23:27 +08:00
|
|
|
|
2023-05-28 22:54:03 +08:00
|
|
|
match self.context.get_mut_no_status_group() {
|
2023-04-28 14:08:53 +08:00
|
|
|
None => {},
|
2023-06-14 22:16:33 +08:00
|
|
|
Some(no_status_group) => no_status_group.add_row((*row_detail).clone()),
|
2023-04-28 14:08:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-28 22:54:03 +08:00
|
|
|
tracing::Span::current().record("group_result", format!("{},", self.context,).as_str());
|
2023-04-28 14:08:53 +08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-11-06 16:17:05 +08:00
|
|
|
fn create_group(
|
|
|
|
&mut self,
|
|
|
|
name: String,
|
|
|
|
) -> FlowyResult<(Option<TypeOptionData>, Option<InsertedGroupPB>)> {
|
|
|
|
self.generate_new_group(name)
|
|
|
|
}
|
|
|
|
|
2023-04-28 14:08:53 +08:00
|
|
|
fn move_group(&mut self, from_group_id: &str, to_group_id: &str) -> FlowyResult<()> {
|
2023-05-28 22:54:03 +08:00
|
|
|
self.context.move_group(from_group_id, to_group_id)
|
2023-04-28 14:08:53 +08:00
|
|
|
}
|
|
|
|
|
2023-11-06 16:17:05 +08:00
|
|
|
fn did_create_row(
|
|
|
|
&mut self,
|
|
|
|
row_detail: &RowDetail,
|
|
|
|
index: usize,
|
|
|
|
) -> Vec<GroupRowsNotificationPB> {
|
|
|
|
let cell = match row_detail.row.cells.get(&self.grouping_field_id) {
|
|
|
|
None => self.placeholder_cell(),
|
|
|
|
Some(cell) => Some(cell.clone()),
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut changesets: Vec<GroupRowsNotificationPB> = vec![];
|
|
|
|
if let Some(cell) = cell {
|
|
|
|
let cell_data = <T as TypeOption>::CellData::from(&cell);
|
|
|
|
|
|
|
|
let mut suitable_group_ids = vec![];
|
|
|
|
|
|
|
|
for group in self.get_all_groups() {
|
|
|
|
if self.can_group(&group.filter_content, &cell_data) {
|
|
|
|
suitable_group_ids.push(group.id.clone());
|
|
|
|
let changeset = GroupRowsNotificationPB::insert(
|
|
|
|
group.id.clone(),
|
|
|
|
vec![InsertedRowPB {
|
|
|
|
row_meta: row_detail.into(),
|
|
|
|
index: Some(index as i32),
|
|
|
|
is_new: true,
|
|
|
|
}],
|
|
|
|
);
|
|
|
|
changesets.push(changeset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !suitable_group_ids.is_empty() {
|
|
|
|
for group_id in suitable_group_ids.iter() {
|
|
|
|
if let Some(group) = self.context.get_mut_group(group_id) {
|
|
|
|
group.add_row(row_detail.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if let Some(no_status_group) = self.context.get_mut_no_status_group() {
|
|
|
|
no_status_group.add_row(row_detail.clone());
|
|
|
|
let changeset = GroupRowsNotificationPB::insert(
|
|
|
|
no_status_group.id.clone(),
|
|
|
|
vec![InsertedRowPB {
|
|
|
|
row_meta: row_detail.into(),
|
|
|
|
index: Some(index as i32),
|
|
|
|
is_new: true,
|
|
|
|
}],
|
|
|
|
);
|
|
|
|
changesets.push(changeset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
changesets
|
|
|
|
}
|
|
|
|
|
2023-04-28 14:08:53 +08:00
|
|
|
fn did_update_group_row(
|
|
|
|
&mut self,
|
2023-06-14 22:16:33 +08:00
|
|
|
old_row_detail: &Option<RowDetail>,
|
|
|
|
row_detail: &RowDetail,
|
2023-04-28 14:08:53 +08:00
|
|
|
field: &Field,
|
|
|
|
) -> FlowyResult<DidUpdateGroupRowResult> {
|
|
|
|
// let cell_data = row_rev.cells.get(&self.field_id).and_then(|cell_rev| {
|
|
|
|
// let cell_data: Option<P> = get_type_cell_data(cell_rev, field_rev, None);
|
|
|
|
// cell_data
|
|
|
|
// });
|
|
|
|
let mut result = DidUpdateGroupRowResult {
|
|
|
|
inserted_group: None,
|
|
|
|
deleted_group: None,
|
|
|
|
row_changesets: vec![],
|
|
|
|
};
|
|
|
|
|
2023-06-14 22:16:33 +08:00
|
|
|
if let Some(cell_data) = get_cell_data_from_row::<P>(Some(&row_detail.row), field) {
|
|
|
|
let _old_row = old_row_detail.as_ref();
|
|
|
|
let old_cell_data =
|
|
|
|
get_cell_data_from_row::<P>(old_row_detail.as_ref().map(|detail| &detail.row), field);
|
|
|
|
if let Ok((insert, delete)) = self.create_or_delete_group_when_cell_changed(
|
|
|
|
row_detail,
|
|
|
|
old_cell_data.as_ref(),
|
|
|
|
&cell_data,
|
|
|
|
) {
|
2023-04-28 14:08:53 +08:00
|
|
|
result.inserted_group = insert;
|
|
|
|
result.deleted_group = delete;
|
|
|
|
}
|
|
|
|
|
2023-06-14 22:16:33 +08:00
|
|
|
let mut changesets = self.add_or_remove_row_when_cell_changed(row_detail, &cell_data);
|
|
|
|
if let Some(changeset) = self.update_no_status_group(row_detail, &changesets) {
|
2023-04-28 14:08:53 +08:00
|
|
|
if !changeset.is_empty() {
|
|
|
|
changesets.push(changeset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result.row_changesets = changesets;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(result)
|
|
|
|
}
|
|
|
|
|
2023-11-06 16:17:05 +08:00
|
|
|
fn did_delete_row(&mut self, row: &Row) -> FlowyResult<DidMoveGroupRowResult> {
|
2023-04-28 14:08:53 +08:00
|
|
|
let mut result = DidMoveGroupRowResult {
|
|
|
|
deleted_group: None,
|
|
|
|
row_changesets: vec![],
|
|
|
|
};
|
2023-11-06 16:17:05 +08:00
|
|
|
// early return if the row is not in the default group
|
2023-04-28 14:08:53 +08:00
|
|
|
if let Some(cell) = row.cells.get(&self.grouping_field_id) {
|
2023-10-28 11:48:28 +08:00
|
|
|
let cell_data = <T as TypeOption>::CellData::from(cell);
|
|
|
|
if !cell_data.is_cell_empty() {
|
2023-11-06 16:17:05 +08:00
|
|
|
(result.deleted_group, result.row_changesets) = self.delete_row(row, &cell_data);
|
2023-04-28 14:08:53 +08:00
|
|
|
return Ok(result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-06 16:17:05 +08:00
|
|
|
match self.context.get_mut_no_status_group() {
|
2023-04-28 14:08:53 +08:00
|
|
|
None => {
|
|
|
|
tracing::error!("Unexpected None value. It should have the no status group");
|
|
|
|
},
|
|
|
|
Some(no_status_group) => {
|
2023-04-28 20:47:40 +08:00
|
|
|
if !no_status_group.contains_row(&row.id) {
|
2023-04-28 14:08:53 +08:00
|
|
|
tracing::error!("The row: {:?} should be in the no status group", row.id);
|
|
|
|
}
|
2023-11-06 16:17:05 +08:00
|
|
|
no_status_group.remove_row(&row.id);
|
2023-04-28 14:08:53 +08:00
|
|
|
result.row_changesets = vec![GroupRowsNotificationPB::delete(
|
|
|
|
no_status_group.id.clone(),
|
2023-04-28 20:47:40 +08:00
|
|
|
vec![row.id.clone().into_inner()],
|
2023-04-28 14:08:53 +08:00
|
|
|
)];
|
|
|
|
},
|
|
|
|
}
|
|
|
|
Ok(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tracing::instrument(level = "trace", skip_all, err)]
|
|
|
|
fn move_group_row(&mut self, context: MoveGroupRowContext) -> FlowyResult<DidMoveGroupRowResult> {
|
|
|
|
let mut result = DidMoveGroupRowResult {
|
|
|
|
deleted_group: None,
|
|
|
|
row_changesets: vec![],
|
|
|
|
};
|
2023-06-14 22:16:33 +08:00
|
|
|
let cell = match context.row_detail.row.cells.get(&self.grouping_field_id) {
|
2023-06-01 20:23:27 +08:00
|
|
|
Some(cell) => Some(cell.clone()),
|
2023-04-28 14:08:53 +08:00
|
|
|
None => self.placeholder_cell(),
|
|
|
|
};
|
|
|
|
|
2023-06-01 20:23:27 +08:00
|
|
|
if let Some(cell) = cell {
|
2023-05-25 23:22:23 +08:00
|
|
|
let cell_bytes = get_cell_protobuf(&cell, context.field, None);
|
2023-04-28 14:08:53 +08:00
|
|
|
let cell_data = cell_bytes.parser::<P>()?;
|
2023-06-14 22:16:33 +08:00
|
|
|
result.deleted_group = self.delete_group_when_move_row(&context.row_detail.row, &cell_data);
|
2023-04-28 14:08:53 +08:00
|
|
|
result.row_changesets = self.move_row(&cell_data, context);
|
|
|
|
} else {
|
|
|
|
tracing::warn!("Unexpected moving group row, changes should not be empty");
|
|
|
|
}
|
|
|
|
Ok(result)
|
|
|
|
}
|
|
|
|
|
2023-05-28 22:54:03 +08:00
|
|
|
fn did_update_group_field(&mut self, _field: &Field) -> FlowyResult<Option<GroupChangesPB>> {
|
2023-04-28 14:08:53 +08:00
|
|
|
Ok(None)
|
|
|
|
}
|
2023-05-28 22:54:03 +08:00
|
|
|
|
2023-11-28 10:43:22 +08:00
|
|
|
fn delete_group(&mut self, group_id: &str) -> FlowyResult<(Vec<RowId>, Option<TypeOptionData>)> {
|
|
|
|
let group = if group_id != self.field_id() {
|
|
|
|
self.get_group(group_id)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
match group {
|
|
|
|
Some((_index, group_data)) => {
|
|
|
|
let row_ids = group_data
|
|
|
|
.rows
|
|
|
|
.iter()
|
|
|
|
.map(|row| row.row.id.clone())
|
|
|
|
.collect();
|
|
|
|
let type_option_data = self.delete_group_custom(group_id)?;
|
|
|
|
Ok((row_ids, type_option_data))
|
|
|
|
},
|
|
|
|
None => Ok((vec![], None)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-28 11:48:28 +08:00
|
|
|
async fn apply_group_changeset(
|
|
|
|
&mut self,
|
|
|
|
changeset: &GroupChangesets,
|
2023-11-13 16:14:31 +08:00
|
|
|
) -> FlowyResult<(Vec<GroupPB>, TypeOptionData)> {
|
2023-10-28 11:48:28 +08:00
|
|
|
for group_changeset in changeset.changesets.iter() {
|
|
|
|
self.context.update_group(group_changeset)?;
|
|
|
|
}
|
|
|
|
let mut type_option_data = TypeOptionData::new();
|
|
|
|
for group_changeset in changeset.changesets.iter() {
|
|
|
|
if let Some(new_type_option_data) = self
|
|
|
|
.operation_interceptor
|
|
|
|
.type_option_from_group_changeset(group_changeset, &self.type_option, &self.context.view_id)
|
|
|
|
.await
|
|
|
|
{
|
|
|
|
type_option_data.extend(new_type_option_data);
|
2023-05-28 22:54:03 +08:00
|
|
|
}
|
|
|
|
}
|
2023-11-13 16:14:31 +08:00
|
|
|
let updated_groups = changeset
|
|
|
|
.changesets
|
|
|
|
.iter()
|
|
|
|
.filter_map(|changeset| {
|
|
|
|
self
|
|
|
|
.get_group(&changeset.group_id)
|
|
|
|
.map(|(_, group)| GroupPB::from(group))
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
Ok((updated_groups, type_option_data))
|
2023-05-28 22:54:03 +08:00
|
|
|
}
|
2023-04-28 14:08:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
struct GroupedRow {
|
2023-06-14 22:16:33 +08:00
|
|
|
row_detail: RowDetail,
|
2023-04-28 14:08:53 +08:00
|
|
|
group_id: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_cell_data_from_row<P: CellProtobufBlobParser>(
|
|
|
|
row: Option<&Row>,
|
|
|
|
field: &Field,
|
|
|
|
) -> Option<P::Object> {
|
|
|
|
let cell = row.and_then(|row| row.cells.get(&field.id))?;
|
2023-05-25 23:22:23 +08:00
|
|
|
let cell_bytes = get_cell_protobuf(cell, field, None);
|
2023-04-28 14:08:53 +08:00
|
|
|
cell_bytes.parser::<P>().ok()
|
|
|
|
}
|