refactor: save default group order

This commit is contained in:
appflowy 2022-10-01 11:00:15 +08:00
parent 5763e289ef
commit 62d0a31a76
18 changed files with 154 additions and 127 deletions

View File

@ -36,21 +36,21 @@ class BoardBloc extends Bloc<BoardEvent, BoardState> {
super(BoardState.initial(view.id)) {
boardController = AppFlowyBoardController(
onMoveGroup: (
fromColumnId,
fromGroupId,
fromIndex,
toColumnId,
toGroupId,
toIndex,
) {
_moveGroup(fromColumnId, toColumnId);
_moveGroup(fromGroupId, toGroupId);
},
onMoveGroupItem: (
columnId,
groupId,
fromIndex,
toIndex,
) {
final fromRow = groupControllers[columnId]?.rowAtIndex(fromIndex);
final toRow = groupControllers[columnId]?.rowAtIndex(toIndex);
_moveRow(fromRow, columnId, toRow);
final fromRow = groupControllers[groupId]?.rowAtIndex(fromIndex);
final toRow = groupControllers[groupId]?.rowAtIndex(toIndex);
_moveRow(fromRow, groupId, toRow);
},
onMoveGroupItemToGroup: (
fromGroupId,

View File

@ -1,3 +1,5 @@
# 0.0.8
* Enable drag and drop group
# 0.0.7
* Rename some classes
* Add documentation
@ -7,7 +9,7 @@
# 0.0.5
* Optimize insert card animation
* Enable insert card at the end of the column
* Enable insert card at the end of the group
* Fix some bugs
# 0.0.4
@ -24,6 +26,5 @@
# 0.0.1
* Support drag and drop column
* Support drag and drop column items from one to another
* Support drag and drop group items from one to another

View File

@ -247,7 +247,6 @@ class ReorderFlexState extends State<ReorderFlex>
}
_animation.dispose();
widget.dragTargetKeys?.removeDragTarget(widget.reorderFlexId);
super.dispose();
}

View File

@ -1,6 +1,6 @@
name: appflowy_board
description: AppFlowyBoard is a board-style widget that consists of multi-groups. It supports drag and drop between different groups.
version: 0.0.7
version: 0.0.8
homepage: https://github.com/AppFlowy-IO/AppFlowy
repository: https://github.com/AppFlowy-IO/AppFlowy/tree/main/frontend/app_flowy/packages/appflowy_board

View File

@ -1444,9 +1444,9 @@ checksum = "eabb4a44450da02c90444cf74558da904edde8fb4e9035a9a6a4e15445af0bd7"
[[package]]
name = "hashbrown"
version = "0.11.2"
version = "0.12.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e"
checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888"
[[package]]
name = "heck"
@ -1610,9 +1610,9 @@ checksum = "ce23b50ad8242c51a442f3ff322d56b02f08852c77e4c0b4d3fd684abc89c683"
[[package]]
name = "indexmap"
version = "1.8.1"
version = "1.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0f647032dfaa1f8b6dc29bd3edb7bbef4861b8b8007ebb118d6db284fd59f6ee"
checksum = "10a35a97730320ffe8e2d410b5d3b69279b98d2c14bdb8b70ea89ecf7888d41e"
dependencies = [
"autocfg",
"hashbrown",

View File

@ -34,7 +34,7 @@ rayon = "1.5.2"
serde = { version = "1.0", features = ["derive"] }
serde_json = {version = "1.0"}
serde_repr = "0.1"
indexmap = {version = "1.8.1", features = ["serde"]}
indexmap = {version = "1.9.1", features = ["serde"]}
fancy-regex = "0.10.0"
regex = "1.5.6"
url = { version = "2"}

View File

@ -98,7 +98,7 @@ pub struct MoveGroupPayloadPB {
pub to_group_id: String,
}
#[derive(debug, Debug)]
#[derive(Debug)]
pub struct MoveGroupParams {
pub view_id: String,
pub from_group_id: String,

View File

@ -368,6 +368,7 @@ impl GridRevisionEditor {
Ok(row_pb)
}
#[tracing::instrument(level = "trace", skip_all, err)]
pub async fn move_group(&self, params: MoveGroupParams) -> FlowyResult<()> {
let _ = self.view_manager.move_group(params).await?;
Ok(())

View File

@ -173,7 +173,7 @@ impl GridViewRevisionEditor {
Ok(groups.into_iter().map(GroupPB::from).collect())
}
#[tracing::instrument(level = "trace", err)]
#[tracing::instrument(level = "trace", skip(self), err)]
pub(crate) async fn move_group(&self, params: MoveGroupParams) -> FlowyResult<()> {
let _ = self
.group_controller
@ -229,7 +229,11 @@ impl GridViewRevisionEditor {
let _ = self
.modify(|pad| {
let configuration = default_group_configuration(&field_rev);
let changeset = pad.insert_group(&params.field_id, &params.field_type_rev, configuration)?;
let changeset = pad.insert_or_update_group_configuration(
&params.field_id,
&params.field_type_rev,
configuration,
)?;
Ok(changeset)
})
.await?;
@ -497,10 +501,11 @@ impl GroupConfigurationWriter for GroupConfigurationWriterImpl {
let field_id = field_id.to_owned();
wrap_future(async move {
let changeset = view_pad
.write()
.await
.insert_group(&field_id, &field_type, group_configuration)?;
let changeset = view_pad.write().await.insert_or_update_group_configuration(
&field_id,
&field_type,
group_configuration,
)?;
if let Some(changeset) = changeset {
let _ = apply_change(&user_id, rev_manager, changeset).await?;

View File

@ -1,5 +1,5 @@
use crate::entities::{GroupPB, GroupViewChangesetPB};
use crate::services::group::{default_group_configuration, GeneratedGroup, Group};
use crate::services::group::{default_group_configuration, make_default_group, GeneratedGroup, Group};
use flowy_error::{FlowyError, FlowyResult};
use flowy_grid_data_model::revision::{
FieldRevision, FieldTypeRevision, GroupConfigurationContentSerde, GroupConfigurationRevision, GroupRevision,
@ -29,10 +29,7 @@ impl<T> std::fmt::Display for GroupContext<T> {
self.groups_map.iter().for_each(|(_, group)| {
let _ = f.write_fmt(format_args!("Group:{} has {} rows \n", group.id, group.rows.len()));
});
let _ = f.write_fmt(format_args!(
"Default group has {} rows \n",
self.default_group.rows.len()
));
Ok(())
}
}
@ -44,7 +41,7 @@ pub struct GroupContext<C> {
field_rev: Arc<FieldRevision>,
groups_map: IndexMap<String, Group>,
/// default_group is used to store the rows that don't belong to any groups.
default_group: Group,
// default_group: Group,
writer: Arc<dyn GroupConfigurationWriter>,
}
@ -59,16 +56,6 @@ where
reader: Arc<dyn GroupConfigurationReader>,
writer: Arc<dyn GroupConfigurationWriter>,
) -> FlowyResult<Self> {
let default_group_id = format!("{}_default_group", view_id);
let default_group = Group {
id: default_group_id,
field_id: field_rev.id.clone(),
name: format!("No {}", field_rev.name),
is_default: true,
is_visible: true,
rows: vec![],
filter_content: "".to_string(),
};
let configuration = match reader.get_configuration().await {
None => {
let default_configuration = default_group_configuration(&field_rev);
@ -80,24 +67,22 @@ where
Some(configuration) => configuration,
};
// let configuration = C::from_configuration_content(&configuration_rev.content)?;
Ok(Self {
view_id,
field_rev,
groups_map: IndexMap::new(),
default_group,
writer,
configuration,
configuration_content: PhantomData,
})
}
pub(crate) fn get_default_group(&self) -> &Group {
&self.default_group
pub(crate) fn get_default_group(&self) -> Option<&Group> {
self.groups_map.get(&self.field_rev.id)
}
pub(crate) fn get_mut_default_group(&mut self) -> &mut Group {
&mut self.default_group
pub(crate) fn get_mut_default_group(&mut self) -> Option<&mut Group> {
self.groups_map.get_mut(&self.field_rev.id)
}
/// Returns the groups without the default group
@ -122,8 +107,6 @@ where
self.groups_map.iter_mut().for_each(|(_, group)| {
each(group);
});
each(&mut self.default_group);
}
pub(crate) fn move_group(&mut self, from_id: &str, to_id: &str) -> FlowyResult<()> {
@ -131,19 +114,23 @@ where
let to_index = self.groups_map.get_index_of(to_id);
match (from_index, to_index) {
(Some(from_index), Some(to_index)) => {
self.groups_map.swap_indices(from_index, to_index);
self.groups_map.move_index(from_index, to_index);
self.mut_configuration(|configuration| {
let from_index = configuration.groups.iter().position(|group| group.id == from_id);
let to_index = configuration.groups.iter().position(|group| group.id == to_id);
tracing::trace!("Swap group index:{:?} with index:{:?}", from_index, to_index);
if let (Some(from), Some(to)) = (from_index, to_index) {
configuration.groups.swap(from, to);
tracing::info!("Configuration groups: {:?} ", configuration.groups);
if let (Some(from), Some(to)) = &(from_index, to_index) {
tracing::trace!("Move group from index:{:?} to index:{:?}", from_index, to_index);
let group = configuration.groups.remove(*from);
configuration.groups.insert(*to, group);
}
true
from_index.is_some() && to_index.is_some()
})?;
Ok(())
}
_ => Err(FlowyError::out_of_bounds()),
_ => Err(FlowyError::record_not_found().context("Moving group failed. Groups are not exist")),
}
}
@ -151,7 +138,6 @@ where
pub(crate) fn init_groups(
&mut self,
generated_groups: Vec<GeneratedGroup>,
reset: bool,
) -> FlowyResult<Option<GroupViewChangesetPB>> {
let mut new_groups = vec![];
let mut filter_content_map = HashMap::new();
@ -160,16 +146,17 @@ where
new_groups.push(generate_group.group_rev);
});
let mut old_groups = self.configuration.groups.clone();
if !old_groups.iter().any(|group| group.id == self.field_rev.id) {
old_groups.push(make_default_group(&self.field_rev));
}
let MergeGroupResult {
mut all_group_revs,
new_group_revs,
updated_group_revs: _,
deleted_group_revs,
} = if reset {
merge_groups(&[], new_groups)
} else {
merge_groups(&self.configuration.groups, new_groups)
};
} = merge_groups(old_groups, new_groups);
let deleted_group_ids = deleted_group_revs
.into_iter()
@ -198,31 +185,23 @@ where
Some(pos) => {
let mut old_group = configuration.groups.remove(pos);
group_rev.update_with_other(&old_group);
is_changed = is_group_changed(group_rev, &old_group);
// Take the GroupRevision if the name has changed
if is_group_changed(group_rev, &old_group) {
old_group.name = group_rev.name.clone();
is_changed = true;
configuration.groups.insert(pos, old_group);
}
old_group.name = group_rev.name.clone();
configuration.groups.insert(pos, old_group);
}
}
}
is_changed
})?;
// The len of the filter_content_map should equal to the len of the all_group_revs
debug_assert_eq!(filter_content_map.len(), all_group_revs.len());
all_group_revs.into_iter().for_each(|group_rev| {
if let Some(filter_content) = filter_content_map.get(&group_rev.id) {
let group = Group::new(
group_rev.id,
self.field_rev.id.clone(),
group_rev.name,
filter_content.clone(),
);
self.groups_map.insert(group.id.clone(), group);
}
let filter_content = filter_content_map
.get(&group_rev.id)
.cloned()
.unwrap_or_else(|| "".to_owned());
let group = Group::new(group_rev.id, self.field_rev.id.clone(), group_rev.name, filter_content);
self.groups_map.insert(group.id.clone(), group);
});
let new_groups = new_group_revs
@ -270,6 +249,7 @@ where
Ok(())
}
#[tracing::instrument(level = "trace", skip_all, err)]
pub fn save_configuration(&self) -> FlowyResult<()> {
let configuration = (&*self.configuration).clone();
let writer = self.writer.clone();
@ -312,13 +292,14 @@ where
}
}
fn merge_groups(old_groups: &[GroupRevision], new_groups: Vec<GroupRevision>) -> MergeGroupResult {
fn merge_groups(old_groups: Vec<GroupRevision>, new_groups: Vec<GroupRevision>) -> MergeGroupResult {
let mut merge_result = MergeGroupResult::new();
if old_groups.is_empty() {
merge_result.all_group_revs = new_groups.clone();
merge_result.new_group_revs = new_groups;
return merge_result;
}
// if old_groups.is_empty() {
// merge_result.all_group_revs.extend(new_groups.clone());
// merge_result.all_group_revs.push(default_group);
// merge_result.new_group_revs = new_groups;
// return merge_result;
// }
// group_map is a helper map is used to filter out the new groups.
let mut new_group_map: IndexMap<String, GroupRevision> = IndexMap::new();
@ -330,19 +311,20 @@ fn merge_groups(old_groups: &[GroupRevision], new_groups: Vec<GroupRevision>) ->
for old in old_groups {
if let Some(new) = new_group_map.remove(&old.id) {
merge_result.all_group_revs.push(new.clone());
if is_group_changed(&new, old) {
if is_group_changed(&new, &old) {
merge_result.updated_group_revs.push(new);
}
} else {
merge_result.deleted_group_revs.push(old.clone());
merge_result.all_group_revs.push(old);
}
}
// Find out the new groups
new_group_map.reverse();
let new_groups = new_group_map.into_values();
for (_, group) in new_groups.into_iter().enumerate() {
merge_result.all_group_revs.push(group.clone());
merge_result.new_group_revs.push(group);
merge_result.all_group_revs.insert(0, group.clone());
merge_result.new_group_revs.insert(0, group);
}
merge_result
}

View File

@ -88,7 +88,7 @@ where
pub async fn new(field_rev: &Arc<FieldRevision>, mut configuration: GroupContext<C>) -> FlowyResult<Self> {
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)?;
let _ = configuration.init_groups(groups)?;
Ok(Self {
field_id: field_rev.id.clone(),
@ -105,8 +105,8 @@ where
&mut self,
row_rev: &RowRevision,
other_group_changesets: &[GroupChangesetPB],
) -> GroupChangesetPB {
let default_group = self.group_ctx.get_mut_default_group();
) -> Option<GroupChangesetPB> {
let default_group = self.group_ctx.get_mut_default_group()?;
// [other_group_inserted_row] contains all the inserted rows except the default group.
let other_group_inserted_row = other_group_changesets
@ -163,7 +163,7 @@ where
}
default_group.rows.retain(|row| !deleted_row_ids.contains(&row.id));
changeset.deleted_rows.extend(deleted_row_ids);
changeset
Some(changeset)
}
}
@ -182,11 +182,14 @@ where
fn groups(&self) -> Vec<Group> {
if self.use_default_group() {
let mut groups: Vec<Group> = self.group_ctx.groups().into_iter().cloned().collect();
groups.push(self.group_ctx.get_default_group().clone());
groups
} else {
self.group_ctx.groups().into_iter().cloned().collect()
} else {
self.group_ctx
.groups()
.into_iter()
.filter(|group| group.id != self.field_id)
.cloned()
.collect::<Vec<_>>()
}
}
@ -216,17 +219,18 @@ where
}
}
if grouped_rows.is_empty() {
self.group_ctx.get_mut_default_group().add_row(row_rev.into());
} else {
if !grouped_rows.is_empty() {
for group_row in grouped_rows {
if let Some(group) = self.group_ctx.get_mut_group(&group_row.group_id) {
group.add_row(group_row.row);
}
}
continue;
}
} else {
self.group_ctx.get_mut_default_group().add_row(row_rev.into());
}
match self.group_ctx.get_mut_default_group() {
None => {}
Some(default_group) => default_group.add_row(row_rev.into()),
}
}
@ -247,10 +251,11 @@ where
let cell_bytes = decode_any_cell_data(cell_rev.data.clone(), field_rev).1;
let cell_data = cell_bytes.parser::<P>()?;
let mut changesets = self.add_row_if_match(row_rev, &cell_data);
let default_group_changeset = self.update_default_group(row_rev, &changesets);
tracing::trace!("default_group_changeset: {}", default_group_changeset);
if !default_group_changeset.is_empty() {
changesets.push(default_group_changeset);
if let Some(default_group_changeset) = self.update_default_group(row_rev, &changesets) {
tracing::trace!("default_group_changeset: {}", default_group_changeset);
if !default_group_changeset.is_empty() {
changesets.push(default_group_changeset);
}
}
Ok(changesets)
} else {
@ -268,12 +273,13 @@ where
let cell_bytes = decode_any_cell_data(cell_rev.data.clone(), field_rev).1;
let cell_data = cell_bytes.parser::<P>()?;
Ok(self.remove_row_if_match(row_rev, &cell_data))
} else {
let group = self.group_ctx.get_default_group();
} else if let Some(group) = self.group_ctx.get_default_group() {
Ok(vec![GroupChangesetPB::delete(
group.id.clone(),
vec![row_rev.id.clone()],
)])
} else {
Ok(vec![])
}
}
@ -297,7 +303,7 @@ where
fn did_update_field(&mut self, field_rev: &FieldRevision) -> FlowyResult<Option<GroupViewChangesetPB>> {
let type_option = field_rev.get_type_option::<T>(field_rev.ty);
let groups = G::generate_groups(&field_rev.id, &self.group_ctx, &type_option);
let changeset = self.group_ctx.init_groups(groups, false)?;
let changeset = self.group_ctx.init_groups(groups)?;
Ok(changeset)
}
}

View File

@ -9,16 +9,17 @@ pub struct Group {
pub is_visible: bool,
pub(crate) rows: Vec<RowPB>,
/// [content] is used to determine which group the cell belongs to.
/// [filter_content] is used to determine which group the cell belongs to.
pub filter_content: String,
}
impl Group {
pub fn new(id: String, field_id: String, name: String, filter_content: String) -> Self {
let is_default = id == field_id;
Self {
id,
field_id,
is_default: false,
is_default,
is_visible: true,
name,
rows: vec![],

View File

@ -8,8 +8,8 @@ use crate::services::group::{
use flowy_error::FlowyResult;
use flowy_grid_data_model::revision::{
CheckboxGroupConfigurationRevision, DateGroupConfigurationRevision, FieldRevision, GroupConfigurationRevision,
LayoutRevision, NumberGroupConfigurationRevision, RowRevision, SelectOptionGroupConfigurationRevision,
TextGroupConfigurationRevision, UrlGroupConfigurationRevision,
GroupRevision, LayoutRevision, NumberGroupConfigurationRevision, RowRevision,
SelectOptionGroupConfigurationRevision, TextGroupConfigurationRevision, UrlGroupConfigurationRevision,
};
use std::sync::Arc;
@ -79,7 +79,7 @@ pub fn default_group_configuration(field_rev: &FieldRevision) -> GroupConfigurat
let field_id = field_rev.id.clone();
let field_type_rev = field_rev.ty;
let field_type: FieldType = field_rev.ty.into();
match field_type {
let mut group_configuration_rev = match field_type {
FieldType::RichText => {
GroupConfigurationRevision::new(field_id, field_type_rev, TextGroupConfigurationRevision::default())
.unwrap()
@ -112,5 +112,23 @@ pub fn default_group_configuration(field_rev: &FieldRevision) -> GroupConfigurat
FieldType::URL => {
GroupConfigurationRevision::new(field_id, field_type_rev, UrlGroupConfigurationRevision::default()).unwrap()
}
};
// Append the no `status` group
let default_group_rev = GroupRevision {
id: field_rev.id.clone(),
name: format!("No {}", field_rev.name),
visible: true,
};
group_configuration_rev.groups.push(default_group_rev);
group_configuration_rev
}
pub fn make_default_group(field_rev: &FieldRevision) -> GroupRevision {
GroupRevision {
id: field_rev.id.clone(),
name: format!("No {}", field_rev.name),
visible: true,
}
}

View File

@ -370,6 +370,28 @@ async fn group_move_group_test() {
test.run_scripts(scripts).await;
}
#[tokio::test]
async fn group_default_move_group_test() {
let mut test = GridGroupTest::new().await;
let group_0 = test.group_at_index(0).await;
let group_3 = test.group_at_index(3).await;
let scripts = vec![
MoveGroup {
from_group_index: 3,
to_group_index: 0,
},
AssertGroup {
group_index: 0,
expected_group: group_3,
},
AssertGroup {
group_index: 1,
expected_group: group_0,
},
];
test.run_scripts(scripts).await;
}
#[tokio::test]
async fn group_insert_single_select_option_test() {
let mut test = GridGroupTest::new().await;
@ -402,7 +424,7 @@ async fn group_group_by_other_field() {
group_index: 1,
row_count: 2,
},
AssertGroupCount(4),
AssertGroupCount(5),
];
test.run_scripts(scripts).await;
}

8
shared-lib/Cargo.lock generated
View File

@ -650,9 +650,9 @@ dependencies = [
[[package]]
name = "hashbrown"
version = "0.11.2"
version = "0.12.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e"
checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888"
[[package]]
name = "heck"
@ -732,9 +732,9 @@ dependencies = [
[[package]]
name = "indexmap"
version = "1.8.1"
version = "1.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0f647032dfaa1f8b6dc29bd3edb7bbef4861b8b8007ebb118d6db284fd59f6ee"
checksum = "10a35a97730320ffe8e2d410b5d3b69279b98d2c14bdb8b70ea89ecf7888d41e"
dependencies = [
"autocfg",
"hashbrown",

View File

@ -12,7 +12,7 @@ serde_json = {version = "1.0"}
serde_repr = "0.1"
nanoid = "0.4.0"
flowy-error-code = { path = "../flowy-error-code"}
indexmap = {version = "1.8.1", features = ["serde"]}
indexmap = {version = "1.9.1", features = ["serde"]}
tracing = { version = "0.1", features = ["log"] }
[build-dependencies]

View File

@ -128,14 +128,6 @@ impl GroupRevision {
}
}
pub fn default_group(id: String, group_name: String) -> Self {
Self {
id,
name: group_name,
visible: true,
}
}
pub fn update_with_other(&mut self, other: &GroupRevision) {
self.visible = other.visible
}

View File

@ -66,7 +66,7 @@ impl GridViewRevisionPad {
}
#[tracing::instrument(level = "trace", skip_all, err)]
pub fn insert_group(
pub fn insert_or_update_group_configuration(
&mut self,
field_id: &str,
field_type: &FieldTypeRevision,