chore: update column name when field was changed

This commit is contained in:
appflowy 2022-08-23 20:36:28 +08:00
parent c7b671ca07
commit f5f3f51cca
11 changed files with 91 additions and 80 deletions

View File

@ -31,7 +31,7 @@ class BoardPluginBuilder implements PluginBuilder {
class BoardPluginConfig implements PluginConfig {
@override
bool get creatable => false;
bool get creatable => true;
}
class BoardPlugin extends Plugin {

View File

@ -1,25 +1,29 @@
use crate::entities::{GroupPB, InsertedRowPB, RowPB};
use diesel::insertable::ColumnInsertValue::Default;
use flowy_derive::ProtoBuf;
use flowy_error::ErrorCode;
use flowy_grid_data_model::parser::NotEmptyStr;
use std::fmt::Formatter;
#[derive(Debug, Default, ProtoBuf)]
pub struct GroupRowsChangesetPB {
pub struct GroupChangesetPB {
#[pb(index = 1)]
pub group_id: String,
#[pb(index = 2)]
pub inserted_rows: Vec<InsertedRowPB>,
#[pb(index = 2, one_of)]
pub group_name: Option<String>,
#[pb(index = 3)]
pub deleted_rows: Vec<String>,
pub inserted_rows: Vec<InsertedRowPB>,
#[pb(index = 4)]
pub deleted_rows: Vec<String>,
#[pb(index = 5)]
pub updated_rows: Vec<RowPB>,
}
impl std::fmt::Display for GroupRowsChangesetPB {
impl std::fmt::Display for GroupChangesetPB {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
for inserted_row in &self.inserted_rows {
let _ = f.write_fmt(format_args!(
@ -36,10 +40,19 @@ impl std::fmt::Display for GroupRowsChangesetPB {
}
}
impl GroupRowsChangesetPB {
impl GroupChangesetPB {
pub fn is_empty(&self) -> bool {
self.inserted_rows.is_empty() && self.deleted_rows.is_empty() && self.updated_rows.is_empty()
}
pub fn name(group_id: String, name: &str) -> Self {
Self {
group_id,
group_name: Some(name.to_owned()),
..Default::default()
}
}
pub fn insert(group_id: String, inserted_rows: Vec<InsertedRowPB>) -> Self {
Self {
group_id,

View File

@ -1,8 +1,8 @@
use crate::dart_notification::{send_dart_notification, GridNotification};
use crate::entities::{
CreateFilterParams, CreateRowParams, DeleteFilterParams, GridFilterConfiguration, GridLayout, GridLayoutPB,
GridSettingPB, GroupPB, GroupRowsChangesetPB, GroupViewChangesetPB, InsertedGroupPB, InsertedRowPB,
MoveGroupParams, RepeatedGridConfigurationFilterPB, RepeatedGridGroupConfigurationPB, RowPB,
GridSettingPB, GroupChangesetPB, GroupPB, GroupViewChangesetPB, InsertedGroupPB, InsertedRowPB, MoveGroupParams,
RepeatedGridConfigurationFilterPB, RepeatedGridGroupConfigurationPB, RowPB,
};
use crate::services::grid_editor_task::GridServiceTaskScheduler;
use crate::services::grid_view_manager::{GridViewFieldDelegate, GridViewRowDelegate};
@ -99,8 +99,8 @@ impl GridViewRevisionEditor {
row: row_pb.clone(),
index: None,
};
let changeset = GroupRowsChangesetPB::insert(group_id.clone(), vec![inserted_row]);
self.notify_did_update_group_rows(changeset).await;
let changeset = GroupChangesetPB::insert(group_id.clone(), vec![inserted_row]);
self.notify_did_update_group(changeset).await;
}
}
}
@ -115,7 +115,7 @@ impl GridViewRevisionEditor {
.await
{
for changeset in changesets {
self.notify_did_update_group_rows(changeset).await;
self.notify_did_update_group(changeset).await;
}
}
}
@ -129,7 +129,7 @@ impl GridViewRevisionEditor {
.await
{
for changeset in changesets {
self.notify_did_update_group_rows(changeset).await;
self.notify_did_update_group(changeset).await;
}
}
}
@ -151,7 +151,7 @@ impl GridViewRevisionEditor {
.await
{
for changeset in changesets {
self.notify_did_update_group_rows(changeset).await;
self.notify_did_update_group(changeset).await;
}
}
}
@ -253,7 +253,14 @@ impl GridViewRevisionEditor {
.await
}
async fn notify_did_update_group_rows(&self, changeset: GroupRowsChangesetPB) {
pub(crate) async fn did_update_field(&self, field_id: &str) -> FlowyResult<()> {
if let Some(field_rev) = self.field_delegate.get_field_rev(&field_id).await {
let _ = self.group_service.write().await.did_update_field(&field_rev).await?;
}
Ok(())
}
async fn notify_did_update_group(&self, changeset: GroupChangesetPB) {
send_dart_notification(&changeset.group_id, GridNotification::DidUpdateGroup)
.payload(changeset)
.send();

View File

@ -149,6 +149,12 @@ impl GridViewManager {
}
}
pub(crate) async fn did_update_field(&self, field_id: &str) -> FlowyResult<()> {
let view_editor = self.get_default_view_editor().await?;
let _ = view_editor.did_update_field(field_id).await?;
Ok(())
}
pub(crate) async fn get_view_editor(&self, view_id: &str) -> FlowyResult<Arc<GridViewRevisionEditor>> {
debug_assert!(!view_id.is_empty());
match self.view_editors.get(view_id) {

View File

@ -1,4 +1,4 @@
use crate::entities::GroupRowsChangesetPB;
use crate::entities::GroupChangesetPB;
use crate::services::group::controller::MoveGroupRowContext;
use flowy_grid_data_model::revision::RowRevision;
@ -6,12 +6,8 @@ use flowy_grid_data_model::revision::RowRevision;
pub trait GroupAction: Send + Sync {
type CellDataType;
fn can_group(&self, content: &str, cell_data: &Self::CellDataType) -> bool;
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>;
fn add_row_if_match(&mut self, row_rev: &RowRevision, cell_data: &Self::CellDataType) -> Vec<GroupChangesetPB>;
fn remove_row_if_match(&mut self, row_rev: &RowRevision, cell_data: &Self::CellDataType) -> Vec<GroupChangesetPB>;
fn move_row(&mut self, cell_data: &Self::CellDataType, context: MoveGroupRowContext) -> Vec<GroupRowsChangesetPB>;
fn move_row(&mut self, cell_data: &Self::CellDataType, context: MoveGroupRowContext) -> Vec<GroupChangesetPB>;
}

View File

@ -1,4 +1,4 @@
use crate::entities::{GroupRowsChangesetPB, RowPB};
use crate::entities::{GroupChangesetPB, RowPB};
use crate::services::cell::{decode_any_cell_data, CellBytesParser};
use crate::services::group::action::GroupAction;
use crate::services::group::configuration::GenericGroupConfiguration;
@ -51,15 +51,17 @@ pub trait GroupControllerSharedOperation: Send + Sync {
&mut self,
row_rev: &RowRevision,
field_rev: &FieldRevision,
) -> FlowyResult<Vec<GroupRowsChangesetPB>>;
) -> FlowyResult<Vec<GroupChangesetPB>>;
fn did_delete_row(
&mut self,
row_rev: &RowRevision,
field_rev: &FieldRevision,
) -> FlowyResult<Vec<GroupRowsChangesetPB>>;
) -> FlowyResult<Vec<GroupChangesetPB>>;
fn move_group_row(&mut self, context: MoveGroupRowContext) -> FlowyResult<Vec<GroupRowsChangesetPB>>;
fn move_group_row(&mut self, context: MoveGroupRowContext) -> FlowyResult<Vec<GroupChangesetPB>>;
fn did_update_field(&mut self, field_rev: &FieldRevision) -> FlowyResult<()>;
}
/// C: represents the group configuration that impl [GroupConfigurationSerde]
@ -173,7 +175,7 @@ where
&mut self,
row_rev: &RowRevision,
field_rev: &FieldRevision,
) -> FlowyResult<Vec<GroupRowsChangesetPB>> {
) -> FlowyResult<Vec<GroupChangesetPB>> {
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>()?;
@ -187,7 +189,7 @@ where
&mut self,
row_rev: &RowRevision,
field_rev: &FieldRevision,
) -> FlowyResult<Vec<GroupRowsChangesetPB>> {
) -> FlowyResult<Vec<GroupChangesetPB>> {
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>()?;
@ -197,7 +199,7 @@ where
}
}
fn move_group_row(&mut self, context: MoveGroupRowContext) -> FlowyResult<Vec<GroupRowsChangesetPB>> {
fn move_group_row(&mut self, context: MoveGroupRowContext) -> FlowyResult<Vec<GroupChangesetPB>> {
if let Some(cell_rev) = context.row_rev.cells.get(&self.field_id) {
let cell_bytes = decode_any_cell_data(cell_rev.data.clone(), context.field_rev);
let cell_data = cell_bytes.parser::<P>()?;
@ -206,6 +208,10 @@ where
Ok(vec![])
}
}
fn did_update_field(&mut self, field_rev: &FieldRevision) -> FlowyResult<()> {
todo!()
}
}
struct GroupRow {

View File

@ -1,4 +1,4 @@
use crate::entities::GroupRowsChangesetPB;
use crate::entities::GroupChangesetPB;
use crate::services::field::{CheckboxCellData, CheckboxCellDataParser, CheckboxTypeOptionPB, CHECK, UNCHECK};
use crate::services::group::action::GroupAction;
use crate::services::group::configuration::GenericGroupConfiguration;
@ -24,11 +24,7 @@ impl GroupAction for CheckboxGroupController {
false
}
fn add_row_if_match(
&mut self,
_row_rev: &RowRevision,
_cell_data: &Self::CellDataType,
) -> Vec<GroupRowsChangesetPB> {
fn add_row_if_match(&mut self, _row_rev: &RowRevision, _cell_data: &Self::CellDataType) -> Vec<GroupChangesetPB> {
todo!()
}
@ -36,15 +32,11 @@ impl GroupAction for CheckboxGroupController {
&mut self,
_row_rev: &RowRevision,
_cell_data: &Self::CellDataType,
) -> Vec<GroupRowsChangesetPB> {
) -> Vec<GroupChangesetPB> {
todo!()
}
fn move_row(
&mut self,
_cell_data: &Self::CellDataType,
_context: MoveGroupRowContext,
) -> Vec<GroupRowsChangesetPB> {
fn move_row(&mut self, _cell_data: &Self::CellDataType, _context: MoveGroupRowContext) -> Vec<GroupChangesetPB> {
todo!()
}
}

View File

@ -1,4 +1,4 @@
use crate::entities::GroupRowsChangesetPB;
use crate::entities::GroupChangesetPB;
use crate::services::cell::insert_select_option_cell;
use crate::services::field::{MultiSelectTypeOptionPB, SelectOptionCellDataPB, SelectOptionCellDataParser};
use crate::services::group::action::GroupAction;
@ -25,7 +25,7 @@ impl GroupAction for MultiSelectGroupController {
cell_data.select_options.iter().any(|option| option.id == content)
}
fn add_row_if_match(&mut self, row_rev: &RowRevision, cell_data: &Self::CellDataType) -> Vec<GroupRowsChangesetPB> {
fn add_row_if_match(&mut self, row_rev: &RowRevision, cell_data: &Self::CellDataType) -> Vec<GroupChangesetPB> {
let mut changesets = vec![];
self.configuration.with_mut_groups(|group| {
add_row(group, &mut changesets, cell_data, row_rev);
@ -33,11 +33,7 @@ impl GroupAction for MultiSelectGroupController {
changesets
}
fn remove_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<GroupChangesetPB> {
let mut changesets = vec![];
self.configuration.with_mut_groups(|group| {
remove_row(group, &mut changesets, cell_data, row_rev);
@ -45,11 +41,7 @@ impl GroupAction for MultiSelectGroupController {
changesets
}
fn move_row(
&mut self,
cell_data: &Self::CellDataType,
mut context: MoveGroupRowContext,
) -> Vec<GroupRowsChangesetPB> {
fn move_row(&mut self, cell_data: &Self::CellDataType, mut context: MoveGroupRowContext) -> Vec<GroupChangesetPB> {
let mut group_changeset = vec![];
self.configuration.with_mut_groups(|group| {
move_select_option_row(group, &mut group_changeset, cell_data, &mut context);

View File

@ -1,4 +1,4 @@
use crate::entities::{GroupRowsChangesetPB, RowPB};
use crate::entities::{GroupChangesetPB, RowPB};
use crate::services::cell::insert_select_option_cell;
use crate::services::field::{SelectOptionCellDataPB, SelectOptionCellDataParser, SingleSelectTypeOptionPB};
use crate::services::group::action::GroupAction;
@ -25,7 +25,7 @@ impl GroupAction for SingleSelectGroupController {
cell_data.select_options.iter().any(|option| option.id == content)
}
fn add_row_if_match(&mut self, row_rev: &RowRevision, cell_data: &Self::CellDataType) -> Vec<GroupRowsChangesetPB> {
fn add_row_if_match(&mut self, row_rev: &RowRevision, cell_data: &Self::CellDataType) -> Vec<GroupChangesetPB> {
let mut changesets = vec![];
self.configuration.with_mut_groups(|group| {
add_row(group, &mut changesets, cell_data, row_rev);
@ -33,11 +33,7 @@ impl GroupAction for SingleSelectGroupController {
changesets
}
fn remove_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<GroupChangesetPB> {
let mut changesets = vec![];
self.configuration.with_mut_groups(|group| {
remove_row(group, &mut changesets, cell_data, row_rev);
@ -45,11 +41,7 @@ impl GroupAction for SingleSelectGroupController {
changesets
}
fn move_row(
&mut self,
cell_data: &Self::CellDataType,
mut context: MoveGroupRowContext,
) -> Vec<GroupRowsChangesetPB> {
fn move_row(&mut self, cell_data: &Self::CellDataType, mut context: MoveGroupRowContext) -> Vec<GroupChangesetPB> {
let mut group_changeset = vec![];
self.configuration.with_mut_groups(|group| {
move_select_option_row(group, &mut group_changeset, cell_data, &mut context);

View File

@ -1,4 +1,4 @@
use crate::entities::{GroupRowsChangesetPB, InsertedRowPB, RowPB};
use crate::entities::{GroupChangesetPB, InsertedRowPB, RowPB};
use crate::services::cell::insert_select_option_cell;
use crate::services::field::SelectOptionCellDataPB;
use crate::services::group::configuration::GenericGroupConfiguration;
@ -11,7 +11,7 @@ pub type SelectOptionGroupConfiguration = GenericGroupConfiguration<SelectOption
pub fn add_row(
group: &mut Group,
changesets: &mut Vec<GroupRowsChangesetPB>,
changesets: &mut Vec<GroupChangesetPB>,
cell_data: &SelectOptionCellDataPB,
row_rev: &RowRevision,
) {
@ -19,14 +19,14 @@ pub fn add_row(
if option.id == group.id {
if !group.contains_row(&row_rev.id) {
let row_pb = RowPB::from(row_rev);
changesets.push(GroupRowsChangesetPB::insert(
changesets.push(GroupChangesetPB::insert(
group.id.clone(),
vec![InsertedRowPB::new(row_pb.clone())],
));
group.add_row(row_pb);
}
} else if group.contains_row(&row_rev.id) {
changesets.push(GroupRowsChangesetPB::delete(group.id.clone(), vec![row_rev.id.clone()]));
changesets.push(GroupChangesetPB::delete(group.id.clone(), vec![row_rev.id.clone()]));
group.remove_row(&row_rev.id);
}
});
@ -34,13 +34,13 @@ pub fn add_row(
pub fn remove_row(
group: &mut Group,
changesets: &mut Vec<GroupRowsChangesetPB>,
changesets: &mut Vec<GroupChangesetPB>,
cell_data: &SelectOptionCellDataPB,
row_rev: &RowRevision,
) {
cell_data.select_options.iter().for_each(|option| {
if option.id == group.id && group.contains_row(&row_rev.id) {
changesets.push(GroupRowsChangesetPB::delete(group.id.clone(), vec![row_rev.id.clone()]));
changesets.push(GroupChangesetPB::delete(group.id.clone(), vec![row_rev.id.clone()]));
group.remove_row(&row_rev.id);
}
});
@ -48,7 +48,7 @@ pub fn remove_row(
pub fn move_select_option_row(
group: &mut Group,
group_changeset: &mut Vec<GroupRowsChangesetPB>,
group_changeset: &mut Vec<GroupChangesetPB>,
_cell_data: &SelectOptionCellDataPB,
context: &mut MoveGroupRowContext,
) {
@ -68,7 +68,7 @@ pub fn move_select_option_row(
// Remove the row in which group contains it
if from_index.is_some() {
group_changeset.push(GroupRowsChangesetPB::delete(group.id.clone(), vec![row_rev.id.clone()]));
group_changeset.push(GroupChangesetPB::delete(group.id.clone(), vec![row_rev.id.clone()]));
tracing::debug!("Group:{} remove row:{}", group.id, row_rev.id);
group.remove_row(&row_rev.id);
}
@ -78,7 +78,7 @@ pub fn move_select_option_row(
let mut inserted_row = InsertedRowPB::new(row_pb.clone());
match to_index {
None => {
group_changeset.push(GroupRowsChangesetPB::insert(group.id.clone(), vec![inserted_row]));
group_changeset.push(GroupChangesetPB::insert(group.id.clone(), vec![inserted_row]));
tracing::debug!("Group:{} append row:{}", group.id, row_rev.id);
group.add_row(row_pb);
}
@ -91,7 +91,7 @@ pub fn move_select_option_row(
tracing::debug!("Group:{} append row:{}", group.id, row_rev.id);
group.add_row(row_pb);
}
group_changeset.push(GroupRowsChangesetPB::insert(group.id.clone(), vec![inserted_row]));
group_changeset.push(GroupChangesetPB::insert(group.id.clone(), vec![inserted_row]));
}
}

View File

@ -1,4 +1,4 @@
use crate::entities::{FieldType, GroupRowsChangesetPB};
use crate::entities::{FieldType, GroupChangesetPB};
use crate::services::group::configuration::GroupConfigurationReader;
use crate::services::group::controller::{GroupController, MoveGroupRowContext};
use crate::services::group::{
@ -86,7 +86,7 @@ impl GroupService {
&mut self,
row_rev: &RowRevision,
get_field_fn: F,
) -> Option<Vec<GroupRowsChangesetPB>>
) -> Option<Vec<GroupChangesetPB>>
where
F: FnOnce(String) -> O,
O: Future<Output = Option<Arc<FieldRevision>>> + Send + Sync + 'static,
@ -111,7 +111,7 @@ impl GroupService {
to_group_id: &str,
to_row_id: Option<String>,
get_field_fn: F,
) -> Option<Vec<GroupRowsChangesetPB>>
) -> Option<Vec<GroupChangesetPB>>
where
F: FnOnce(String) -> O,
O: Future<Output = Option<Arc<FieldRevision>>> + Send + Sync + 'static,
@ -141,7 +141,7 @@ impl GroupService {
&mut self,
row_rev: &RowRevision,
get_field_fn: F,
) -> Option<Vec<GroupRowsChangesetPB>>
) -> Option<Vec<GroupChangesetPB>>
where
F: FnOnce(String) -> O,
O: Future<Output = Option<Arc<FieldRevision>>> + Send + Sync + 'static,
@ -170,6 +170,13 @@ impl GroupService {
}
}
pub(crate) async fn did_update_field(&mut self, field_rev: &FieldRevision) -> FlowyResult<()> {
match self.group_controller.as_mut() {
None => Ok(()),
Some(group_controller) => group_controller.did_update_field(field_rev),
}
}
#[tracing::instrument(level = "trace", skip(self, field_rev), err)]
async fn make_group_controller(
&self,