11use crate :: dart_notification:: { send_dart_notification, GridNotification } ;
22use crate :: entities:: {
33 CreateFilterParams , CreateRowParams , DeleteFilterParams , GridFilterConfiguration , GridLayout , GridLayoutPB ,
4- GridSettingChangesetParams , GridSettingPB , GroupPB , GroupRowsChangesetPB , GroupViewChangesetPB , InsertedRowPB ,
4+ GridSettingPB , GroupPB , GroupRowsChangesetPB , GroupViewChangesetPB , InsertedGroupPB , InsertedRowPB ,
55 MoveGroupParams , RepeatedGridConfigurationFilterPB , RepeatedGridGroupConfigurationPB , RowPB ,
66} ;
77use crate :: services:: grid_editor_task:: GridServiceTaskScheduler ;
88use crate :: services:: grid_view_manager:: { GridViewFieldDelegate , GridViewRowDelegate } ;
9- use crate :: services:: group:: {
10- default_group_configuration, GroupConfigurationReader , GroupConfigurationWriter , GroupService ,
11- } ;
9+ use crate :: services:: group:: { GroupConfigurationReader , GroupConfigurationWriter , GroupService } ;
1210use flowy_error:: { FlowyError , FlowyResult } ;
1311use flowy_grid_data_model:: revision:: {
1412 gen_grid_filter_id, FieldRevision , FieldTypeRevision , FilterConfigurationRevision , GroupConfigurationRevision ,
@@ -19,6 +17,7 @@ use flowy_sync::client_grid::{GridViewRevisionChangeset, GridViewRevisionPad};
1917use flowy_sync:: entities:: revision:: Revision ;
2018use lib_infra:: future:: { wrap_future, AFFuture , FutureResult } ;
2119use std:: collections:: HashMap ;
20+
2221use std:: sync:: atomic:: { AtomicBool , Ordering } ;
2322use std:: sync:: Arc ;
2423use tokio:: sync:: RwLock ;
@@ -37,6 +36,7 @@ pub struct GridViewRevisionEditor {
3736}
3837
3938impl GridViewRevisionEditor {
39+ #[ tracing:: instrument( level = "trace" , skip_all, err) ]
4040 pub ( crate ) async fn new (
4141 user_id : & str ,
4242 token : & str ,
@@ -109,7 +109,7 @@ impl GridViewRevisionEditor {
109109 // Send the group notification if the current view has groups;
110110 if let Some ( changesets) = self
111111 . group_service
112- . write ( )
112+ . read ( )
113113 . await
114114 . did_delete_row ( row_rev, |field_id| self . field_delegate . get_field_rev ( & field_id) )
115115 . await
@@ -123,7 +123,7 @@ impl GridViewRevisionEditor {
123123 pub ( crate ) async fn did_update_row ( & self , row_rev : & RowRevision ) {
124124 if let Some ( changesets) = self
125125 . group_service
126- . write ( )
126+ . read ( )
127127 . await
128128 . did_update_row ( row_rev, |field_id| self . field_delegate . get_field_rev ( & field_id) )
129129 . await
@@ -142,7 +142,7 @@ impl GridViewRevisionEditor {
142142 ) {
143143 if let Some ( changesets) = self
144144 . group_service
145- . write ( )
145+ . read ( )
146146 . await
147147 . did_move_row ( row_rev, row_changeset, upper_row_id, |field_id| {
148148 self . field_delegate . get_field_rev ( & field_id)
@@ -156,11 +156,13 @@ impl GridViewRevisionEditor {
156156 }
157157 }
158158
159+ #[ tracing:: instrument( level = "trace" , skip( self ) ) ]
159160 pub ( crate ) async fn load_groups ( & self ) -> FlowyResult < Vec < GroupPB > > {
160161 let groups = if !self . did_load_group . load ( Ordering :: SeqCst ) {
161162 self . did_load_group . store ( true , Ordering :: SeqCst ) ;
162163 let field_revs = self . field_delegate . get_field_revs ( ) . await ;
163164 let row_revs = self . row_delegate . gv_row_revs ( ) . await ;
165+
164166 match self
165167 . group_service
166168 . write ( )
@@ -174,11 +176,37 @@ impl GridViewRevisionEditor {
174176 } else {
175177 self . group_service . read ( ) . await . groups ( ) . await
176178 } ;
179+
180+ tracing:: trace!( "Number of groups: {}" , groups. len( ) ) ;
177181 Ok ( groups. into_iter ( ) . map ( GroupPB :: from) . collect ( ) )
178182 }
179183
180184 pub ( crate ) async fn move_group ( & self , params : MoveGroupParams ) -> FlowyResult < ( ) > {
181- todo ! ( )
185+ let _ = self
186+ . group_service
187+ . read ( )
188+ . await
189+ . move_group ( & params. from_group_id , & params. to_group_id )
190+ . await ?;
191+
192+ match self . group_service . read ( ) . await . get_group ( & params. from_group_id ) . await {
193+ None => { }
194+ Some ( ( index, group) ) => {
195+ let inserted_group = InsertedGroupPB {
196+ group : GroupPB :: from ( group) ,
197+ index : index as i32 ,
198+ } ;
199+
200+ let changeset = GroupViewChangesetPB {
201+ view_id : "" . to_string ( ) ,
202+ inserted_groups : vec ! [ inserted_group] ,
203+ deleted_groups : vec ! [ params. from_group_id. clone( ) ] ,
204+ } ;
205+
206+ self . notify_did_update_view ( changeset) . await ;
207+ }
208+ }
209+ Ok ( ( ) )
182210 }
183211
184212 pub ( crate ) async fn get_setting ( & self ) -> GridSettingPB {
@@ -291,17 +319,19 @@ impl RevisionObjectBuilder for GridViewRevisionPadBuilder {
291319struct GroupConfigurationReaderImpl ( Arc < RwLock < GridViewRevisionPad > > ) ;
292320
293321impl GroupConfigurationReader for GroupConfigurationReaderImpl {
294- fn get_group_configuration ( & self , field_rev : Arc < FieldRevision > ) -> AFFuture < Arc < GroupConfigurationRevision > > {
322+ fn get_group_configuration (
323+ & self ,
324+ field_rev : Arc < FieldRevision > ,
325+ ) -> AFFuture < Option < Arc < GroupConfigurationRevision > > > {
295326 let view_pad = self . 0 . clone ( ) ;
296327 wrap_future ( async move {
297- let view_pad = view_pad. read ( ) . await ;
298- let configurations = view_pad. get_groups ( & field_rev. id , & field_rev. ty ) ;
299- match configurations {
300- None => {
301- let default_configuration = default_group_configuration ( & field_rev) ;
302- Arc :: new ( default_configuration)
303- }
304- Some ( configuration) => configuration,
328+ let mut groups = view_pad. read ( ) . await . groups . get_objects ( & field_rev. id , & field_rev. ty ) ?;
329+
330+ if groups. is_empty ( ) {
331+ None
332+ } else {
333+ debug_assert_eq ! ( groups. len( ) , 1 ) ;
334+ Some ( groups. pop ( ) . unwrap ( ) )
305335 }
306336 } )
307337 }
@@ -328,17 +358,25 @@ impl GroupConfigurationWriter for GroupConfigurationWriterImpl {
328358 let field_id = field_id. to_owned ( ) ;
329359
330360 wrap_future ( async move {
331- match view_pad. write ( ) . await . get_mut_group (
332- & field_id,
333- & field_type,
334- & configuration_id,
335- |group_configuration| {
336- group_configuration. content = content;
337- } ,
338- ) ? {
339- None => Ok ( ( ) ) ,
340- Some ( changeset) => apply_change ( & user_id, rev_manager, changeset) . await ,
361+ let is_contained = view_pad. read ( ) . await . contains_group ( & field_id, & field_type) ;
362+ let changeset = if is_contained {
363+ view_pad. write ( ) . await . with_mut_group (
364+ & field_id,
365+ & field_type,
366+ & configuration_id,
367+ |group_configuration| {
368+ group_configuration. content = content;
369+ } ,
370+ ) ?
371+ } else {
372+ let group_rev = GroupConfigurationRevision :: new ( field_id. clone ( ) , field_type, content) ?;
373+ view_pad. write ( ) . await . insert_group ( & field_id, & field_type, group_rev) ?
374+ } ;
375+
376+ if let Some ( changeset) = changeset {
377+ let _ = apply_change ( & user_id, rev_manager, changeset) . await ?;
341378 }
379+ Ok ( ( ) )
342380 } )
343381 }
344382}
@@ -371,3 +409,17 @@ pub fn make_grid_setting(view_pad: &GridViewRevisionPad, field_revs: &[Arc<Field
371409 group_configuration_by_field_id : groups_by_field_id,
372410 }
373411}
412+
413+ #[ cfg( test) ]
414+ mod tests {
415+ use lib_ot:: core:: TextDelta ;
416+
417+ #[ test]
418+ fn test ( ) {
419+ let s1 = r#"[{"insert":"{\"view_id\":\"fTURELffPr\",\"grid_id\":\"fTURELffPr\",\"layout\":0,\"filters\":[],\"groups\":[]}"}]"# ;
420+ let _delta_1 = TextDelta :: from_json ( s1) . unwrap ( ) ;
421+
422+ let s2 = r#"[{"retain":195},{"insert":"{\\\"group_id\\\":\\\"wD9i\\\",\\\"visible\\\":true},{\\\"group_id\\\":\\\"xZtv\\\",\\\"visible\\\":true},{\\\"group_id\\\":\\\"tFV2\\\",\\\"visible\\\":true}"},{"retain":10}]"# ;
423+ let _delta_2 = TextDelta :: from_json ( s2) . unwrap ( ) ;
424+ }
425+ }
0 commit comments