1- use crate :: entities:: { GroupChangesetPB , GroupViewChangesetPB , RowPB } ;
1+ use crate :: entities:: { GroupChangesetPB , GroupViewChangesetPB , InsertedRowPB , RowPB } ;
22use crate :: services:: cell:: { decode_any_cell_data, CellBytesParser } ;
33use crate :: services:: group:: action:: GroupAction ;
44use crate :: services:: group:: configuration:: GenericGroupConfiguration ;
@@ -11,8 +11,6 @@ use flowy_grid_data_model::revision::{
1111use std:: marker:: PhantomData ;
1212use std:: sync:: Arc ;
1313
14- const DEFAULT_GROUP_ID : & str = "default_group" ;
15-
1614// Each kind of group must implement this trait to provide custom group
1715// operations. For example, insert cell data to the row_rev when creating
1816// a new row.
@@ -72,8 +70,6 @@ pub struct GenericGroupController<C, T, G, P> {
7270 pub field_id : String ,
7371 pub type_option : Option < T > ,
7472 pub configuration : GenericGroupConfiguration < C > ,
75- /// default_group is used to store the rows that don't belong to any groups.
76- default_group : Group ,
7773 group_action_phantom : PhantomData < G > ,
7874 cell_parser_phantom : PhantomData < P > ,
7975}
@@ -92,22 +88,85 @@ where
9288 let type_option = field_rev. get_type_option_entry :: < T > ( field_type_rev) ;
9389 let groups = G :: generate_groups ( & field_rev. id , & configuration, & type_option) ;
9490 let _ = configuration. merge_groups ( groups) ?;
95- let default_group = Group :: new (
96- DEFAULT_GROUP_ID . to_owned ( ) ,
97- field_rev. id . clone ( ) ,
98- format ! ( "No {}" , field_rev. name) ,
99- "" . to_string ( ) ,
100- ) ;
10191
10292 Ok ( Self {
10393 field_id : field_rev. id . clone ( ) ,
104- default_group,
10594 type_option,
10695 configuration,
10796 group_action_phantom : PhantomData ,
10897 cell_parser_phantom : PhantomData ,
10998 } )
11099 }
100+
101+ fn update_default_group (
102+ & mut self ,
103+ row_rev : & RowRevision ,
104+ other_group_changesets : & Vec < GroupChangesetPB > ,
105+ ) -> GroupChangesetPB {
106+ let default_group = self . configuration . get_mut_default_group ( ) ;
107+
108+ // [other_group_inserted_row] contains all the inserted rows except the default group.
109+ let other_group_inserted_row = other_group_changesets
110+ . iter ( )
111+ . flat_map ( |changeset| & changeset. inserted_rows )
112+ . collect :: < Vec < & InsertedRowPB > > ( ) ;
113+
114+ // Calculate the inserted_rows of the default_group
115+ let default_group_inserted_row = other_group_changesets
116+ . iter ( )
117+ . flat_map ( |changeset| & changeset. deleted_rows )
118+ . cloned ( )
119+ . filter ( |row_id| {
120+ // if the [other_group_inserted_row] contains the row_id of the row
121+ // which means the row should not move to the default group.
122+ other_group_inserted_row
123+ . iter ( )
124+ . find ( |inserted_row| & inserted_row. row . id == row_id)
125+ . is_none ( )
126+ } )
127+ . collect :: < Vec < String > > ( ) ;
128+
129+ let mut changeset = GroupChangesetPB :: new ( default_group. id . clone ( ) ) ;
130+ if default_group_inserted_row. is_empty ( ) == false {
131+ changeset. inserted_rows . push ( InsertedRowPB :: new ( row_rev. into ( ) ) ) ;
132+ default_group. add_row ( row_rev. into ( ) ) ;
133+ }
134+
135+ // [other_group_delete_rows] contains all the deleted rows except the default group.
136+ let other_group_delete_rows: Vec < String > = other_group_changesets
137+ . iter ( )
138+ . flat_map ( |changeset| & changeset. deleted_rows )
139+ . cloned ( )
140+ . collect ( ) ;
141+
142+ let default_group_deleted_rows = other_group_changesets
143+ . iter ( )
144+ . flat_map ( |changeset| & changeset. inserted_rows )
145+ . filter ( |inserted_row| {
146+ // if the [other_group_delete_rows] contain the inserted_row, which means this row should move
147+ // out from the default_group.
148+ let inserted_row_id = & inserted_row. row . id ;
149+ other_group_delete_rows
150+ . iter ( )
151+ . find ( |row_id| inserted_row_id == row_id. clone ( ) )
152+ . is_none ( )
153+ } )
154+ . collect :: < Vec < & InsertedRowPB > > ( ) ;
155+
156+ let mut deleted_row_ids = vec ! [ ] ;
157+ for row in & default_group. rows {
158+ if default_group_deleted_rows
159+ . iter ( )
160+ . find ( |deleted_row| deleted_row. row . id == row. id )
161+ . is_some ( )
162+ {
163+ deleted_row_ids. push ( row. id . clone ( ) ) ;
164+ }
165+ }
166+ default_group. rows . retain ( |row| !deleted_row_ids. contains ( & row. id ) ) ;
167+ changeset. deleted_rows . extend ( deleted_row_ids) ;
168+ changeset
169+ }
111170}
112171
113172impl < C , T , G , P > GroupControllerSharedOperation for GenericGroupController < C , T , G , P >
@@ -124,11 +183,7 @@ where
124183 }
125184
126185 fn groups ( & self ) -> Vec < Group > {
127- let mut groups = self . configuration . clone_groups ( ) ;
128- if self . default_group . is_empty ( ) == false {
129- groups. insert ( 0 , self . default_group . clone ( ) ) ;
130- }
131- groups
186+ self . configuration . clone_groups ( )
132187 }
133188
134189 fn get_group ( & self , group_id : & str ) -> Option < ( usize , Group ) > {
@@ -138,7 +193,6 @@ where
138193
139194 #[ tracing:: instrument( level = "trace" , skip_all, fields( row_count=%row_revs. len( ) , group_result) ) ]
140195 fn fill_groups ( & mut self , row_revs : & [ Arc < RowRevision > ] , field_rev : & FieldRevision ) -> FlowyResult < Vec < Group > > {
141- // let mut ungrouped_rows = vec![];
142196 for row_rev in row_revs {
143197 if let Some ( cell_rev) = row_rev. cells . get ( & self . field_id ) {
144198 let mut grouped_rows: Vec < GroupedRow > = vec ! [ ] ;
@@ -154,8 +208,7 @@ where
154208 }
155209
156210 if grouped_rows. is_empty ( ) {
157- // ungrouped_rows.push(RowPB::from(row_rev));
158- self . default_group . add_row ( row_rev. into ( ) ) ;
211+ self . configuration . get_mut_default_group ( ) . add_row ( row_rev. into ( ) ) ;
159212 } else {
160213 for group_row in grouped_rows {
161214 if let Some ( group) = self . configuration . get_mut_group ( & group_row. group_id ) {
@@ -164,30 +217,11 @@ where
164217 }
165218 }
166219 } else {
167- self . default_group . add_row ( row_rev. into ( ) ) ;
220+ self . configuration . get_mut_default_group ( ) . add_row ( row_rev. into ( ) ) ;
168221 }
169222 }
170223
171- // if !ungrouped_rows.is_empty() {
172- // let default_group_rev = GroupRevision::default_group(gen_grid_group_id(), format!("No {}", field_rev.name));
173- // let default_group = Group::new(
174- // default_group_rev.id.clone(),
175- // field_rev.id.clone(),
176- // default_group_rev.name.clone(),
177- // "".to_owned(),
178- // );
179- // }
180-
181- tracing:: Span :: current ( ) . record (
182- "group_result" ,
183- & format ! (
184- "{}, default_group has {} rows" ,
185- self . configuration,
186- self . default_group. rows. len( )
187- )
188- . as_str ( ) ,
189- ) ;
190-
224+ tracing:: Span :: current ( ) . record ( "group_result" , & format ! ( "{}," , self . configuration, ) . as_str ( ) ) ;
191225 Ok ( self . groups ( ) )
192226 }
193227
@@ -203,7 +237,12 @@ where
203237 if let Some ( cell_rev) = row_rev. cells . get ( & self . field_id ) {
204238 let cell_bytes = decode_any_cell_data ( cell_rev. data . clone ( ) , field_rev) ;
205239 let cell_data = cell_bytes. parser :: < P > ( ) ?;
206- let changesets = self . add_row_if_match ( row_rev, & cell_data) ;
240+ let mut changesets = self . add_row_if_match ( row_rev, & cell_data) ;
241+ let default_group_changeset = self . update_default_group ( row_rev, & changesets) ;
242+ tracing:: info!( "default_group_changeset: {}" , default_group_changeset) ;
243+ if !default_group_changeset. is_empty ( ) {
244+ changesets. push ( default_group_changeset) ;
245+ }
207246 Ok ( changesets)
208247 } else {
209248 Ok ( vec ! [ ] )
0 commit comments