chore: insert default group at index 0

This commit is contained in:
appflowy 2022-08-25 13:44:58 +08:00
parent c827f9b156
commit 14874772b1
6 changed files with 104 additions and 38 deletions

View File

@ -5,8 +5,6 @@ import 'package:flowy_infra_ui/style_widget/icon_button.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'define.dart';
class BoardCheckboxCell extends StatefulWidget {
final GridCellControllerBuilder cellControllerBuilder;

View File

@ -1,5 +1,5 @@
use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
use flowy_grid_data_model::revision::{GroupRecordRevision, SelectOptionGroupConfigurationRevision};
use flowy_grid_data_model::revision::{GroupRevision, SelectOptionGroupConfigurationRevision};
#[derive(Eq, PartialEq, ProtoBuf, Debug, Default, Clone)]
pub struct UrlGroupConfigurationPB {
@ -36,10 +36,10 @@ pub struct GroupRecordPB {
visible: bool,
}
impl std::convert::From<GroupRecordRevision> for GroupRecordPB {
fn from(rev: GroupRecordRevision) -> Self {
impl std::convert::From<GroupRevision> for GroupRecordPB {
fn from(rev: GroupRevision) -> Self {
Self {
group_id: rev.group_id,
group_id: rev.id,
visible: rev.visible,
}
}

View File

@ -2,10 +2,11 @@ use crate::entities::{GroupPB, GroupViewChangesetPB, InsertedGroupPB};
use crate::services::group::{default_group_configuration, Group};
use flowy_error::{FlowyError, FlowyResult};
use flowy_grid_data_model::revision::{
FieldRevision, FieldTypeRevision, GroupConfigurationContentSerde, GroupConfigurationRevision, GroupRecordRevision,
FieldRevision, FieldTypeRevision, GroupConfigurationContentSerde, GroupConfigurationRevision, GroupRevision,
};
use indexmap::IndexMap;
use lib_infra::future::AFFuture;
use std::fmt::Formatter;
use std::marker::PhantomData;
use std::sync::Arc;
@ -25,6 +26,15 @@ pub trait GroupConfigurationWriter: Send + Sync + 'static {
) -> AFFuture<FlowyResult<()>>;
}
impl<T> std::fmt::Display for GenericGroupConfiguration<T> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
self.groups_map.iter().for_each(|(_, group)| {
let _ = f.write_fmt(format_args!("Group:{} has {} rows \n", group.id, group.rows.len()));
});
Ok(())
}
}
pub struct GenericGroupConfiguration<C> {
view_id: String,
pub configuration: Arc<GroupConfigurationRevision>,
@ -84,12 +94,31 @@ where
let group_revs = groups
.iter()
.map(|group| GroupRecordRevision::new(group.id.clone(), group.name.clone()))
.collect();
.map(|group| GroupRevision::new(group.id.clone(), group.name.clone()))
.collect::<Vec<GroupRevision>>();
self.mut_configuration(move |configuration| {
configuration.groups = group_revs;
true
let mut is_changed = false;
for new_group_rev in group_revs {
match configuration
.groups
.iter()
.position(|group_rev| group_rev.id == new_group_rev.id)
{
None => {
configuration.groups.push(new_group_rev);
is_changed = true;
}
Some(pos) => {
let removed_group = configuration.groups.remove(pos);
if removed_group != new_group_rev {
is_changed = true;
}
configuration.groups.insert(pos, new_group_rev);
}
}
}
is_changed
})?;
groups.into_iter().for_each(|group| {
@ -139,8 +168,8 @@ where
self.groups_map.swap_indices(from_index, to_index);
self.mut_configuration(|configuration| {
let from_index = configuration.groups.iter().position(|group| group.group_id == from_id);
let to_index = configuration.groups.iter().position(|group| group.group_id == to_id);
let from_index = configuration.groups.iter().position(|group| group.id == from_id);
let to_index = configuration.groups.iter().position(|group| group.id == to_id);
if let (Some(from), Some(to)) = (from_index, to_index) {
configuration.groups.swap(from, to);
}
@ -183,10 +212,10 @@ where
fn mut_configuration_group(
&mut self,
group_id: &str,
mut_groups_fn: impl Fn(&mut GroupRecordRevision),
mut_groups_fn: impl Fn(&mut GroupRevision),
) -> FlowyResult<()> {
self.mut_configuration(|configuration| {
match configuration.groups.iter_mut().find(|group| group.group_id == group_id) {
match configuration.groups.iter_mut().find(|group| group.id == group_id) {
None => false,
Some(group_rev) => {
mut_groups_fn(group_rev);
@ -209,7 +238,7 @@ where
}
}
fn merge_groups(old_groups: &[GroupRecordRevision], groups: Vec<Group>) -> MergeGroupResult {
fn merge_groups(old_groups: &[GroupRevision], groups: Vec<Group>) -> MergeGroupResult {
let mut merge_result = MergeGroupResult::new();
if old_groups.is_empty() {
merge_result.groups = groups;
@ -224,7 +253,7 @@ fn merge_groups(old_groups: &[GroupRecordRevision], groups: Vec<Group>) -> Merge
// The group is ordered in old groups. Add them before adding the new groups
for group_rev in old_groups {
if let Some(group) = group_map.remove(&group_rev.group_id) {
if let Some(group) = group_map.remove(&group_rev.id) {
if group.name == group_rev.name {
merge_result.add_group(group);
} else {

View File

@ -124,7 +124,11 @@ where
}
fn groups(&self) -> Vec<Group> {
self.configuration.clone_groups()
let mut groups = self.configuration.clone_groups();
if self.default_group.is_empty() == false {
groups.insert(0, self.default_group.clone());
}
groups
}
fn get_group(&self, group_id: &str) -> Option<(usize, Group)> {
@ -132,25 +136,28 @@ where
Some((group.0, group.1.clone()))
}
#[tracing::instrument(level = "trace", skip_all, fields(row_count=%row_revs.len(), group_result))]
fn fill_groups(&mut self, row_revs: &[Arc<RowRevision>], field_rev: &FieldRevision) -> FlowyResult<Vec<Group>> {
// let mut ungrouped_rows = vec![];
for row_rev in row_revs {
if let Some(cell_rev) = row_rev.cells.get(&self.field_id) {
let mut group_rows: Vec<GroupRow> = vec![];
let mut grouped_rows: Vec<GroupedRow> = vec![];
let cell_bytes = decode_any_cell_data(cell_rev.data.clone(), field_rev);
let cell_data = cell_bytes.parser::<P>()?;
for group in self.configuration.groups() {
if self.can_group(&group.content, &cell_data) {
group_rows.push(GroupRow {
grouped_rows.push(GroupedRow {
row: row_rev.into(),
group_id: group.id.clone(),
});
}
}
if group_rows.is_empty() {
if grouped_rows.is_empty() {
// ungrouped_rows.push(RowPB::from(row_rev));
self.default_group.add_row(row_rev.into());
} else {
for group_row in group_rows {
for group_row in grouped_rows {
if let Some(group) = self.configuration.get_mut_group(&group_row.group_id) {
group.add_row(group_row.row);
}
@ -161,13 +168,27 @@ where
}
}
let default_group = self.default_group.clone();
let mut groups: Vec<Group> = self.configuration.clone_groups();
if !default_group.number_of_row() == 0 {
groups.push(default_group);
}
// if !ungrouped_rows.is_empty() {
// let default_group_rev = GroupRevision::default_group(gen_grid_group_id(), format!("No {}", field_rev.name));
// let default_group = Group::new(
// default_group_rev.id.clone(),
// field_rev.id.clone(),
// default_group_rev.name.clone(),
// "".to_owned(),
// );
// }
Ok(groups)
tracing::Span::current().record(
"group_result",
&format!(
"{}, default_group has {} rows",
self.configuration,
self.default_group.rows.len()
)
.as_str(),
);
Ok(self.groups())
}
fn move_group(&mut self, from_group_id: &str, to_group_id: &str) -> FlowyResult<()> {
@ -222,7 +243,7 @@ where
}
}
struct GroupRow {
struct GroupedRow {
row: RowPB,
group_id: String,
}

View File

@ -59,4 +59,8 @@ impl Group {
pub fn number_of_row(&self) -> usize {
self.rows.len()
}
pub fn is_empty(&self) -> bool {
self.rows.is_empty()
}
}

View File

@ -14,7 +14,7 @@ pub struct GroupConfigurationRevision {
pub id: String,
pub field_id: String,
pub field_type_rev: FieldTypeRevision,
pub groups: Vec<GroupRecordRevision>,
pub groups: Vec<GroupRevision>,
pub content: String,
}
@ -106,24 +106,38 @@ impl GroupConfigurationContentSerde for SelectOptionGroupConfigurationRevision {
}
}
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct GroupRecordRevision {
pub group_id: String,
#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq, Eq)]
pub struct GroupRevision {
pub id: String,
#[serde(default)]
pub name: String,
#[serde(default = "DEFAULT_GROUP_RECORD_VISIBILITY")]
#[serde(skip, default = "IS_DEFAULT_GROUP")]
pub is_default: bool,
#[serde(default = "GROUP_REV_VISIBILITY")]
pub visible: bool,
}
const DEFAULT_GROUP_RECORD_VISIBILITY: fn() -> bool = || true;
const GROUP_REV_VISIBILITY: fn() -> bool = || true;
const IS_DEFAULT_GROUP: fn() -> bool = || false;
impl GroupRecordRevision {
pub fn new(group_id: String, group_name: String) -> Self {
impl GroupRevision {
pub fn new(id: String, group_name: String) -> Self {
Self {
group_id,
id,
name: group_name,
is_default: false,
visible: true,
}
}
pub fn default_group(id: String, group_name: String) -> Self {
Self {
id,
name: group_name,
is_default: true,
visible: true,
}
}