fix: move checkbox card fail

This commit is contained in:
appflowy 2022-09-24 15:57:40 +08:00 committed by nathan
parent 55be554cc7
commit fa0a485c85
8 changed files with 81 additions and 38 deletions

View File

@ -34,7 +34,3 @@ pub fn send_dart_notification(id: &str, ty: GridNotification) -> DartNotifyBuild
DartNotifyBuilder::new(id, ty, OBSERVABLE_CATEGORY)
}
#[tracing::instrument(level = "trace")]
pub fn send_anonymous_dart_notification(ty: GridNotification) -> DartNotifyBuilder {
DartNotifyBuilder::new("", ty, OBSERVABLE_CATEGORY)
}

View File

@ -19,7 +19,10 @@ impl std::str::FromStr for AnyCellData {
type Err = FlowyError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let type_option_cell_data: AnyCellData = serde_json::from_str(s)?;
let type_option_cell_data: AnyCellData = serde_json::from_str(s).map_err(|err| {
let msg = format!("Deserialize {} to any cell data failed. Serde error: {}", s, err);
FlowyError::internal().context(msg)
})?;
Ok(type_option_cell_data)
}
}

View File

@ -1,6 +1,7 @@
use crate::entities::FieldType;
use crate::services::cell::{AnyCellData, CellBytes};
use crate::services::field::*;
use std::fmt::Debug;
use flowy_error::{ErrorCode, FlowyError, FlowyResult};
use flowy_grid_data_model::revision::{CellRevision, FieldRevision, FieldTypeRevision};
@ -73,20 +74,30 @@ pub fn apply_cell_data_changeset<C: ToString, T: AsRef<FieldRevision>>(
Ok(AnyCellData::new(s, field_type).json())
}
pub fn decode_any_cell_data<T: TryInto<AnyCellData>>(data: T, field_rev: &FieldRevision) -> CellBytes {
if let Ok(any_cell_data) = data.try_into() {
let AnyCellData { data, field_type } = any_cell_data;
let to_field_type = field_rev.ty.into();
match try_decode_cell_data(data.into(), field_rev, &field_type, &to_field_type) {
Ok(cell_bytes) => cell_bytes,
Err(e) => {
tracing::error!("Decode cell data failed, {:?}", e);
CellBytes::default()
pub fn decode_any_cell_data<T: TryInto<AnyCellData, Error = FlowyError> + Debug>(
data: T,
field_rev: &FieldRevision,
) -> CellBytes {
match data.try_into() {
Ok(any_cell_data) => {
let AnyCellData { data, field_type } = any_cell_data;
let to_field_type = field_rev.ty.into();
match try_decode_cell_data(data.into(), field_rev, &field_type, &to_field_type) {
Ok(cell_bytes) => cell_bytes,
Err(e) => {
tracing::error!("Decode cell data failed, {:?}", e);
CellBytes::default()
}
}
}
} else {
tracing::error!("Decode type option data failed");
CellBytes::default()
Err(err) => {
tracing::error!(
"Decode type option data to type: {} failed: {:?}",
std::any::type_name::<T>(),
err,
);
CellBytes::default()
}
}
}

View File

@ -14,5 +14,6 @@ pub trait GroupAction: Send + Sync {
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<GroupChangesetPB>;
fn remove_row_if_match(&mut self, row_rev: &RowRevision, cell_data: &Self::CellDataType) -> Vec<GroupChangesetPB>;
// Move row from one group to another
fn move_row(&mut self, cell_data: &Self::CellDataType, context: MoveGroupRowContext) -> Vec<GroupChangesetPB>;
}

View File

@ -1,12 +1,10 @@
use crate::entities::{GroupChangesetPB, GroupViewChangesetPB, InsertedRowPB, RowPB};
use crate::services::cell::{decode_any_cell_data, CellBytesParser};
use crate::entities::{ GroupChangesetPB, GroupViewChangesetPB, InsertedRowPB, RowPB};
use crate::services::cell::{decode_any_cell_data, CellBytesParser,};
use crate::services::group::action::GroupAction;
use crate::services::group::configuration::GroupContext;
use crate::services::group::entities::Group;
use flowy_error::FlowyResult;
use flowy_grid_data_model::revision::{
FieldRevision, GroupConfigurationContentSerde, GroupRevision, RowChangeset, RowRevision, TypeOptionDataDeserializer,
};
use flowy_grid_data_model::revision::{ FieldRevision, GroupConfigurationContentSerde, GroupRevision, RowChangeset, RowRevision, TypeOptionDataDeserializer};
use std::marker::PhantomData;
use std::sync::Arc;
@ -86,8 +84,7 @@ where
G: GroupGenerator<Context = GroupContext<C>, TypeOptionType = T>,
{
pub async fn new(field_rev: &Arc<FieldRevision>, mut configuration: GroupContext<C>) -> FlowyResult<Self> {
let field_type_rev = field_rev.ty;
let type_option = field_rev.get_type_option::<T>(field_type_rev);
let type_option = field_rev.get_type_option::<T>(field_rev.ty);
let groups = G::generate_groups(&field_rev.id, &configuration, &type_option);
let _ = configuration.init_groups(groups, true)?;
@ -278,12 +275,19 @@ where
}
}
#[tracing::instrument(level = "trace", skip_all, err)]
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_rev = match context.row_rev.cells.get(&self.field_id) {
Some(cell_rev) => Some(cell_rev.clone()),
None => self.default_cell_rev(),
};
if let Some(cell_rev) = cell_rev {
let cell_bytes = decode_any_cell_data(cell_rev.data, context.field_rev);
let cell_data = cell_bytes.parser::<P>()?;
Ok(self.move_row(&cell_data, context))
} else {
tracing::warn!("Unexpected moving group row, changes should not be empty");
Ok(vec![])
}
}

View File

@ -80,7 +80,7 @@ impl GroupAction for CheckboxGroupController {
fn move_row(&mut self, _cell_data: &Self::CellDataType, mut context: MoveGroupRowContext) -> Vec<GroupChangesetPB> {
let mut group_changeset = vec![];
self.group_ctx.iter_mut_all_groups(|group| {
if let Some(changeset) = move_group_row(group, &mut context) {
if let Some(changeset) = move_group_row(group, &mut context) {
group_changeset.push(changeset);
}
});

View File

@ -49,7 +49,7 @@ impl GroupAction for SingleSelectGroupController {
fn move_row(&mut self, _cell_data: &Self::CellDataType, mut context: MoveGroupRowContext) -> Vec<GroupChangesetPB> {
let mut group_changeset = vec![];
self.group_ctx.iter_mut_all_groups(|group| {
if let Some(changeset) = move_group_row(group, &mut context) {
if let Some(changeset) = move_group_row(group, &mut context) {
group_changeset.push(changeset);
}
});

View File

@ -1,11 +1,12 @@
use crate::entities::{GroupChangesetPB, InsertedRowPB, RowPB};
use crate::services::cell::insert_select_option_cell;
use crate::services::field::{SelectOptionCellDataPB, SelectOptionPB};
use crate::entities::{FieldType, GroupChangesetPB, InsertedRowPB, RowPB};
use crate::services::cell::{insert_checkbox_cell, insert_select_option_cell};
use crate::services::field::{SelectOptionCellDataPB, SelectOptionPB, CHECK};
use crate::services::group::configuration::GroupContext;
use crate::services::group::{GeneratedGroup, Group};
use crate::services::group::controller::MoveGroupRowContext;
use flowy_grid_data_model::revision::{GroupRevision, RowRevision, SelectOptionGroupConfigurationRevision};
use crate::services::group::{GeneratedGroup, Group};
use flowy_grid_data_model::revision::{
CellRevision, FieldRevision, GroupRevision, RowRevision, SelectOptionGroupConfigurationRevision,
};
pub type SelectOptionGroupContext = GroupContext<SelectOptionGroupConfigurationRevision>;
@ -109,10 +110,16 @@ pub fn move_group_row(group: &mut Group, context: &mut MoveGroupRowContext) -> O
// Update the corresponding row's cell content.
if from_index.is_none() {
tracing::debug!("Mark row:{} belong to group:{}", row_rev.id, group.id);
let cell_rev = insert_select_option_cell(group.id.clone(), field_rev);
row_changeset.cell_by_field_id.insert(field_rev.id.clone(), cell_rev);
changeset.updated_rows.push(RowPB::from(*row_rev));
let cell_rev = make_inserted_cell_rev(&group.id, field_rev);
if let Some(cell_rev) = cell_rev {
tracing::debug!(
"Update content of the cell in the row:{} to group:{}",
row_rev.id,
group.id
);
row_changeset.cell_by_field_id.insert(field_rev.id.clone(), cell_rev);
changeset.updated_rows.push(RowPB::from(*row_rev));
}
}
}
if changeset.is_empty() {
@ -122,6 +129,27 @@ pub fn move_group_row(group: &mut Group, context: &mut MoveGroupRowContext) -> O
}
}
pub fn make_inserted_cell_rev(group_id: &str, field_rev: &FieldRevision) -> Option<CellRevision> {
let field_type: FieldType = field_rev.ty.into();
match field_type {
FieldType::SingleSelect => {
let cell_rev = insert_select_option_cell(group_id.to_owned(), field_rev);
Some(cell_rev)
}
FieldType::MultiSelect => {
let cell_rev = insert_select_option_cell(group_id.to_owned(), field_rev);
Some(cell_rev)
}
FieldType::Checkbox => {
let cell_rev = insert_checkbox_cell(group_id == CHECK, field_rev);
Some(cell_rev)
}
_ => {
tracing::warn!("Unknown field type: {:?}", field_type);
None
}
}
}
pub fn generate_select_option_groups(
_field_id: &str,
_group_ctx: &SelectOptionGroupContext,