Skip to content

Commit fa0a485

Browse files
committed
fix: move checkbox card fail
1 parent 55be554 commit fa0a485

File tree

8 files changed

+81
-38
lines changed

8 files changed

+81
-38
lines changed

frontend/rust-lib/flowy-grid/src/dart_notification.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,3 @@ pub fn send_dart_notification(id: &str, ty: GridNotification) -> DartNotifyBuild
3434
DartNotifyBuilder::new(id, ty, OBSERVABLE_CATEGORY)
3535
}
3636

37-
#[tracing::instrument(level = "trace")]
38-
pub fn send_anonymous_dart_notification(ty: GridNotification) -> DartNotifyBuilder {
39-
DartNotifyBuilder::new("", ty, OBSERVABLE_CATEGORY)
40-
}

frontend/rust-lib/flowy-grid/src/services/cell/any_cell_data.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ impl std::str::FromStr for AnyCellData {
1919
type Err = FlowyError;
2020

2121
fn from_str(s: &str) -> Result<Self, Self::Err> {
22-
let type_option_cell_data: AnyCellData = serde_json::from_str(s)?;
22+
let type_option_cell_data: AnyCellData = serde_json::from_str(s).map_err(|err| {
23+
let msg = format!("Deserialize {} to any cell data failed. Serde error: {}", s, err);
24+
FlowyError::internal().context(msg)
25+
})?;
2326
Ok(type_option_cell_data)
2427
}
2528
}

frontend/rust-lib/flowy-grid/src/services/cell/cell_operation.rs

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::entities::FieldType;
22
use crate::services::cell::{AnyCellData, CellBytes};
33
use crate::services::field::*;
4+
use std::fmt::Debug;
45

56
use flowy_error::{ErrorCode, FlowyError, FlowyResult};
67
use flowy_grid_data_model::revision::{CellRevision, FieldRevision, FieldTypeRevision};
@@ -73,20 +74,30 @@ pub fn apply_cell_data_changeset<C: ToString, T: AsRef<FieldRevision>>(
7374
Ok(AnyCellData::new(s, field_type).json())
7475
}
7576

76-
pub fn decode_any_cell_data<T: TryInto<AnyCellData>>(data: T, field_rev: &FieldRevision) -> CellBytes {
77-
if let Ok(any_cell_data) = data.try_into() {
78-
let AnyCellData { data, field_type } = any_cell_data;
79-
let to_field_type = field_rev.ty.into();
80-
match try_decode_cell_data(data.into(), field_rev, &field_type, &to_field_type) {
81-
Ok(cell_bytes) => cell_bytes,
82-
Err(e) => {
83-
tracing::error!("Decode cell data failed, {:?}", e);
84-
CellBytes::default()
77+
pub fn decode_any_cell_data<T: TryInto<AnyCellData, Error = FlowyError> + Debug>(
78+
data: T,
79+
field_rev: &FieldRevision,
80+
) -> CellBytes {
81+
match data.try_into() {
82+
Ok(any_cell_data) => {
83+
let AnyCellData { data, field_type } = any_cell_data;
84+
let to_field_type = field_rev.ty.into();
85+
match try_decode_cell_data(data.into(), field_rev, &field_type, &to_field_type) {
86+
Ok(cell_bytes) => cell_bytes,
87+
Err(e) => {
88+
tracing::error!("Decode cell data failed, {:?}", e);
89+
CellBytes::default()
90+
}
8591
}
8692
}
87-
} else {
88-
tracing::error!("Decode type option data failed");
89-
CellBytes::default()
93+
Err(err) => {
94+
tracing::error!(
95+
"Decode type option data to type: {} failed: {:?}",
96+
std::any::type_name::<T>(),
97+
err,
98+
);
99+
CellBytes::default()
100+
}
90101
}
91102
}
92103

frontend/rust-lib/flowy-grid/src/services/group/action.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@ pub trait GroupAction: Send + Sync {
1414
fn can_group(&self, content: &str, cell_data: &Self::CellDataType) -> bool;
1515
fn add_row_if_match(&mut self, row_rev: &RowRevision, cell_data: &Self::CellDataType) -> Vec<GroupChangesetPB>;
1616
fn remove_row_if_match(&mut self, row_rev: &RowRevision, cell_data: &Self::CellDataType) -> Vec<GroupChangesetPB>;
17+
// Move row from one group to another
1718
fn move_row(&mut self, cell_data: &Self::CellDataType, context: MoveGroupRowContext) -> Vec<GroupChangesetPB>;
1819
}

frontend/rust-lib/flowy-grid/src/services/group/controller.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
use crate::entities::{GroupChangesetPB, GroupViewChangesetPB, InsertedRowPB, RowPB};
2-
use crate::services::cell::{decode_any_cell_data, CellBytesParser};
1+
use crate::entities::{ GroupChangesetPB, GroupViewChangesetPB, InsertedRowPB, RowPB};
2+
use crate::services::cell::{decode_any_cell_data, CellBytesParser,};
33
use crate::services::group::action::GroupAction;
44
use crate::services::group::configuration::GroupContext;
55
use crate::services::group::entities::Group;
66
use flowy_error::FlowyResult;
7-
use flowy_grid_data_model::revision::{
8-
FieldRevision, GroupConfigurationContentSerde, GroupRevision, RowChangeset, RowRevision, TypeOptionDataDeserializer,
9-
};
7+
use flowy_grid_data_model::revision::{ FieldRevision, GroupConfigurationContentSerde, GroupRevision, RowChangeset, RowRevision, TypeOptionDataDeserializer};
108
use std::marker::PhantomData;
119
use std::sync::Arc;
1210

@@ -86,8 +84,7 @@ where
8684
G: GroupGenerator<Context = GroupContext<C>, TypeOptionType = T>,
8785
{
8886
pub async fn new(field_rev: &Arc<FieldRevision>, mut configuration: GroupContext<C>) -> FlowyResult<Self> {
89-
let field_type_rev = field_rev.ty;
90-
let type_option = field_rev.get_type_option::<T>(field_type_rev);
87+
let type_option = field_rev.get_type_option::<T>(field_rev.ty);
9188
let groups = G::generate_groups(&field_rev.id, &configuration, &type_option);
9289
let _ = configuration.init_groups(groups, true)?;
9390

@@ -278,12 +275,19 @@ where
278275
}
279276
}
280277

278+
#[tracing::instrument(level = "trace", skip_all, err)]
281279
fn move_group_row(&mut self, context: MoveGroupRowContext) -> FlowyResult<Vec<GroupChangesetPB>> {
282-
if let Some(cell_rev) = context.row_rev.cells.get(&self.field_id) {
283-
let cell_bytes = decode_any_cell_data(cell_rev.data.clone(), context.field_rev);
280+
let cell_rev = match context.row_rev.cells.get(&self.field_id) {
281+
Some(cell_rev) => Some(cell_rev.clone()),
282+
None => self.default_cell_rev(),
283+
};
284+
285+
if let Some(cell_rev) = cell_rev {
286+
let cell_bytes = decode_any_cell_data(cell_rev.data, context.field_rev);
284287
let cell_data = cell_bytes.parser::<P>()?;
285288
Ok(self.move_row(&cell_data, context))
286289
} else {
290+
tracing::warn!("Unexpected moving group row, changes should not be empty");
287291
Ok(vec![])
288292
}
289293
}

frontend/rust-lib/flowy-grid/src/services/group/controller_impls/checkbox_controller.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ impl GroupAction for CheckboxGroupController {
8080
fn move_row(&mut self, _cell_data: &Self::CellDataType, mut context: MoveGroupRowContext) -> Vec<GroupChangesetPB> {
8181
let mut group_changeset = vec![];
8282
self.group_ctx.iter_mut_all_groups(|group| {
83-
if let Some(changeset) = move_group_row(group, &mut context) {
83+
if let Some(changeset) = move_group_row(group, &mut context) {
8484
group_changeset.push(changeset);
8585
}
8686
});

frontend/rust-lib/flowy-grid/src/services/group/controller_impls/select_option_controller/single_select_controller.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ impl GroupAction for SingleSelectGroupController {
4949
fn move_row(&mut self, _cell_data: &Self::CellDataType, mut context: MoveGroupRowContext) -> Vec<GroupChangesetPB> {
5050
let mut group_changeset = vec![];
5151
self.group_ctx.iter_mut_all_groups(|group| {
52-
if let Some(changeset) = move_group_row(group, &mut context) {
52+
if let Some(changeset) = move_group_row(group, &mut context) {
5353
group_changeset.push(changeset);
5454
}
5555
});

frontend/rust-lib/flowy-grid/src/services/group/controller_impls/select_option_controller/util.rs

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
use crate::entities::{GroupChangesetPB, InsertedRowPB, RowPB};
2-
use crate::services::cell::insert_select_option_cell;
3-
use crate::services::field::{SelectOptionCellDataPB, SelectOptionPB};
1+
use crate::entities::{FieldType, GroupChangesetPB, InsertedRowPB, RowPB};
2+
use crate::services::cell::{insert_checkbox_cell, insert_select_option_cell};
3+
use crate::services::field::{SelectOptionCellDataPB, SelectOptionPB, CHECK};
44
use crate::services::group::configuration::GroupContext;
5-
use crate::services::group::{GeneratedGroup, Group};
6-
75
use crate::services::group::controller::MoveGroupRowContext;
8-
use flowy_grid_data_model::revision::{GroupRevision, RowRevision, SelectOptionGroupConfigurationRevision};
6+
use crate::services::group::{GeneratedGroup, Group};
7+
use flowy_grid_data_model::revision::{
8+
CellRevision, FieldRevision, GroupRevision, RowRevision, SelectOptionGroupConfigurationRevision,
9+
};
910

1011
pub type SelectOptionGroupContext = GroupContext<SelectOptionGroupConfigurationRevision>;
1112

@@ -109,10 +110,16 @@ pub fn move_group_row(group: &mut Group, context: &mut MoveGroupRowContext) -> O
109110

110111
// Update the corresponding row's cell content.
111112
if from_index.is_none() {
112-
tracing::debug!("Mark row:{} belong to group:{}", row_rev.id, group.id);
113-
let cell_rev = insert_select_option_cell(group.id.clone(), field_rev);
114-
row_changeset.cell_by_field_id.insert(field_rev.id.clone(), cell_rev);
115-
changeset.updated_rows.push(RowPB::from(*row_rev));
113+
let cell_rev = make_inserted_cell_rev(&group.id, field_rev);
114+
if let Some(cell_rev) = cell_rev {
115+
tracing::debug!(
116+
"Update content of the cell in the row:{} to group:{}",
117+
row_rev.id,
118+
group.id
119+
);
120+
row_changeset.cell_by_field_id.insert(field_rev.id.clone(), cell_rev);
121+
changeset.updated_rows.push(RowPB::from(*row_rev));
122+
}
116123
}
117124
}
118125
if changeset.is_empty() {
@@ -122,6 +129,27 @@ pub fn move_group_row(group: &mut Group, context: &mut MoveGroupRowContext) -> O
122129
}
123130
}
124131

132+
pub fn make_inserted_cell_rev(group_id: &str, field_rev: &FieldRevision) -> Option<CellRevision> {
133+
let field_type: FieldType = field_rev.ty.into();
134+
match field_type {
135+
FieldType::SingleSelect => {
136+
let cell_rev = insert_select_option_cell(group_id.to_owned(), field_rev);
137+
Some(cell_rev)
138+
}
139+
FieldType::MultiSelect => {
140+
let cell_rev = insert_select_option_cell(group_id.to_owned(), field_rev);
141+
Some(cell_rev)
142+
}
143+
FieldType::Checkbox => {
144+
let cell_rev = insert_checkbox_cell(group_id == CHECK, field_rev);
145+
Some(cell_rev)
146+
}
147+
_ => {
148+
tracing::warn!("Unknown field type: {:?}", field_type);
149+
None
150+
}
151+
}
152+
}
125153
pub fn generate_select_option_groups(
126154
_field_id: &str,
127155
_group_ctx: &SelectOptionGroupContext,

0 commit comments

Comments
 (0)