11use crate :: services:: group:: { default_group_configuration, Group } ;
22use flowy_error:: { FlowyError , FlowyResult } ;
33use flowy_grid_data_model:: revision:: {
4- FieldRevision , FieldTypeRevision , GroupConfigurationContent , GroupConfigurationRevision , GroupRecordRevision ,
4+ FieldRevision , FieldTypeRevision , GroupConfigurationContentSerde , GroupConfigurationRevision , GroupRecordRevision ,
55} ;
6+ use std:: marker:: PhantomData ;
67
78use indexmap:: IndexMap ;
89use lib_infra:: future:: AFFuture ;
@@ -20,52 +21,46 @@ pub trait GroupConfigurationWriter: Send + Sync + 'static {
2021 & self ,
2122 field_id : & str ,
2223 field_type : FieldTypeRevision ,
23- configuration_id : & str ,
24- content : String ,
24+ group_configuration : GroupConfigurationRevision ,
2525 ) -> AFFuture < FlowyResult < ( ) > > ;
2626}
2727
2828pub struct GenericGroupConfiguration < C > {
29- pub configuration : C ,
30- configuration_id : String ,
29+ pub configuration : Arc < GroupConfigurationRevision > ,
30+ configuration_content : PhantomData < C > ,
3131 field_rev : Arc < FieldRevision > ,
3232 groups_map : IndexMap < String , Group > ,
3333 writer : Arc < dyn GroupConfigurationWriter > ,
3434}
3535
3636impl < C > GenericGroupConfiguration < C >
3737where
38- C : GroupConfigurationContent ,
38+ C : GroupConfigurationContentSerde ,
3939{
40+ #[ tracing:: instrument( level = "trace" , skip_all, err) ]
4041 pub async fn new (
4142 field_rev : Arc < FieldRevision > ,
4243 reader : Arc < dyn GroupConfigurationReader > ,
4344 writer : Arc < dyn GroupConfigurationWriter > ,
4445 ) -> FlowyResult < Self > {
45- let configuration_rev = match reader. get_group_configuration ( field_rev. clone ( ) ) . await {
46+ let configuration = match reader. get_group_configuration ( field_rev. clone ( ) ) . await {
4647 None => {
4748 let default_group_configuration = default_group_configuration ( & field_rev) ;
4849 writer
49- . save_group_configuration (
50- & field_rev. id ,
51- field_rev. ty ,
52- & default_group_configuration. id ,
53- default_group_configuration. content . clone ( ) ,
54- )
50+ . save_group_configuration ( & field_rev. id , field_rev. ty , default_group_configuration. clone ( ) )
5551 . await ?;
5652 Arc :: new ( default_group_configuration)
5753 }
5854 Some ( configuration) => configuration,
5955 } ;
6056
61- let configuration_id = configuration_rev. id . clone ( ) ;
62- let configuration = C :: from_configuration_content ( & configuration_rev. content ) ?;
57+ // let configuration = C::from_configuration_content(&configuration_rev.content)?;
6358 Ok ( Self {
64- configuration_id,
6559 field_rev,
6660 groups_map : IndexMap :: new ( ) ,
6761 writer,
6862 configuration,
63+ configuration_content : PhantomData ,
6964 } )
7065 }
7166
@@ -78,11 +73,12 @@ where
7873 }
7974
8075 pub ( crate ) async fn merge_groups ( & mut self , groups : Vec < Group > ) -> FlowyResult < ( ) > {
81- let ( group_revs, groups) = merge_groups ( self . configuration . get_groups ( ) , groups) ;
82- self . configuration . set_groups ( group_revs) ;
83- let _ = self . save_configuration ( ) . await ?;
76+ let ( group_revs, groups) = merge_groups ( & self . configuration . groups , groups) ;
77+ self . mut_configuration ( move |configuration| {
78+ configuration. groups = group_revs;
79+ true
80+ } ) ?;
8481
85- tracing:: trace!( "merge new groups: {}" , groups. len( ) ) ;
8682 groups. into_iter ( ) . for_each ( |group| {
8783 self . groups_map . insert ( group. id . clone ( ) , group) ;
8884 } ) ;
@@ -91,19 +87,17 @@ where
9187
9288 #[ allow( dead_code) ]
9389 pub ( crate ) async fn hide_group ( & mut self , group_id : & str ) -> FlowyResult < ( ) > {
94- self . configuration . with_mut_group ( group_id, |group_rev| {
90+ self . mut_configuration_group ( group_id, |group_rev| {
9591 group_rev. visible = false ;
96- } ) ;
97- let _ = self . save_configuration ( ) . await ?;
92+ } ) ?;
9893 Ok ( ( ) )
9994 }
10095
10196 #[ allow( dead_code) ]
10297 pub ( crate ) async fn show_group ( & mut self , group_id : & str ) -> FlowyResult < ( ) > {
103- self . configuration . with_mut_group ( group_id, |group_rev| {
98+ self . mut_configuration_group ( group_id, |group_rev| {
10499 group_rev. visible = true ;
105- } ) ;
106- let _ = self . save_configuration ( ) . await ?;
100+ } ) ?;
107101 Ok ( ( ) )
108102 }
109103
@@ -123,7 +117,21 @@ where
123117 match ( from_group_index, to_group_index) {
124118 ( Some ( from_index) , Some ( to_index) ) => {
125119 self . groups_map . swap_indices ( from_index, to_index) ;
126- self . configuration . swap_group ( from_group_id, to_group_id) ;
120+
121+ self . mut_configuration ( |configuration| {
122+ let from_index = configuration
123+ . groups
124+ . iter ( )
125+ . position ( |group| group. group_id == from_group_id) ;
126+ let to_index = configuration
127+ . groups
128+ . iter ( )
129+ . position ( |group| group. group_id == to_group_id) ;
130+ if let ( Some ( from) , Some ( to) ) = ( from_index, to_index) {
131+ configuration. groups . swap ( from, to) ;
132+ }
133+ true
134+ } ) ?;
127135 Ok ( ( ) )
128136 }
129137 _ => Err ( FlowyError :: out_of_bounds ( ) ) ,
@@ -138,24 +146,54 @@ where
138146 }
139147 }
140148
141- pub async fn save_configuration ( & self ) -> FlowyResult < ( ) > {
142- let content = self . configuration . to_configuration_content ( ) ?;
143- let _ = self
144- . writer
145- . save_group_configuration ( & self . field_rev . id , self . field_rev . ty , & self . configuration_id , content)
146- . await ?;
149+ pub fn save_configuration ( & self ) -> FlowyResult < ( ) > {
150+ let configuration = ( & * self . configuration ) . clone ( ) ;
151+ let writer = self . writer . clone ( ) ;
152+ let field_id = self . field_rev . id . clone ( ) ;
153+ let field_type = self . field_rev . ty . clone ( ) ;
154+ tokio:: spawn ( async move {
155+ match writer
156+ . save_group_configuration ( & field_id, field_type, configuration)
157+ . await
158+ {
159+ Ok ( _) => { }
160+ Err ( e) => {
161+ tracing:: error!( "Save group configuration failed: {}" , e) ;
162+ }
163+ }
164+ } ) ;
165+
147166 Ok ( ( ) )
148167 }
149- }
150168
151- // impl<T> GroupConfigurationReader for Arc<T>
152- // where
153- // T: GroupConfigurationReader,
154- // {
155- // fn get_group_configuration(&self, field_rev: Arc<FieldRevision>) -> AFFuture<Arc<GroupConfigurationRevision>> {
156- // (**self).get_group_configuration(field_rev)
157- // }
158- // }
169+ fn mut_configuration_group (
170+ & mut self ,
171+ group_id : & str ,
172+ mut_groups_fn : impl Fn ( & mut GroupRecordRevision ) ,
173+ ) -> FlowyResult < ( ) > {
174+ self . mut_configuration ( |configuration| {
175+ match configuration. groups . iter_mut ( ) . find ( |group| group. group_id == group_id) {
176+ None => false ,
177+ Some ( group_rev) => {
178+ mut_groups_fn ( group_rev) ;
179+ true
180+ }
181+ }
182+ } )
183+ }
184+
185+ fn mut_configuration (
186+ & mut self ,
187+ mut_configuration_fn : impl FnOnce ( & mut GroupConfigurationRevision ) -> bool ,
188+ ) -> FlowyResult < ( ) > {
189+ let configuration = Arc :: make_mut ( & mut self . configuration ) ;
190+ let is_changed = mut_configuration_fn ( configuration) ;
191+ if is_changed {
192+ let _ = self . save_configuration ( ) ?;
193+ }
194+ Ok ( ( ) )
195+ }
196+ }
159197
160198fn merge_groups ( old_group_revs : & [ GroupRecordRevision ] , groups : Vec < Group > ) -> ( Vec < GroupRecordRevision > , Vec < Group > ) {
161199 tracing:: trace!( "Merge group: old: {}, new: {}" , old_group_revs. len( ) , groups. len( ) ) ;
@@ -172,6 +210,7 @@ fn merge_groups(old_group_revs: &[GroupRecordRevision], groups: Vec<Group>) -> (
172210 group_map. insert ( group. id . clone ( ) , group) ;
173211 } ) ;
174212
213+ // Inert
175214 let mut sorted_groups: Vec < Group > = vec ! [ ] ;
176215 for group_rev in old_group_revs {
177216 if let Some ( group) = group_map. remove ( & group_rev. group_id ) {
@@ -184,5 +223,6 @@ fn merge_groups(old_group_revs: &[GroupRecordRevision], groups: Vec<Group>) -> (
184223 . map ( |group| GroupRecordRevision :: new ( group. id . clone ( ) ) )
185224 . collect :: < Vec < GroupRecordRevision > > ( ) ;
186225
226+ tracing:: trace!( "group revs: {}, groups: {}" , new_group_revs. len( ) , sorted_groups. len( ) ) ;
187227 ( new_group_revs, sorted_groups)
188228}
0 commit comments