@@ -93,16 +93,47 @@ where
9393 } )
9494 }
9595
96- pub ( crate ) fn groups ( & self ) -> Vec < & Group > {
96+ /// Returns the groups without the default group
97+ pub ( crate ) fn concrete_groups ( & self ) -> Vec < & Group > {
9798 self . groups_map . values ( ) . collect ( )
9899 }
99100
101+ /// Returns the all the groups that contain the default group.
100102 pub ( crate ) fn clone_groups ( & self ) -> Vec < Group > {
101103 let mut groups: Vec < Group > = self . groups_map . values ( ) . cloned ( ) . collect ( ) ;
102104 groups. push ( self . default_group . clone ( ) ) ;
103105 groups
104106 }
105107
108+ /// Iterate mut the groups. The default group will be the last one that get mutated.
109+ pub ( crate ) fn iter_mut_groups ( & mut self , mut each : impl FnMut ( & mut Group ) ) {
110+ self . groups_map . iter_mut ( ) . for_each ( |( _, group) | {
111+ each ( group) ;
112+ } ) ;
113+
114+ each ( & mut self . default_group ) ;
115+ }
116+
117+ pub ( crate ) fn move_group ( & mut self , from_id : & str , to_id : & str ) -> FlowyResult < ( ) > {
118+ let from_index = self . groups_map . get_index_of ( from_id) ;
119+ let to_index = self . groups_map . get_index_of ( to_id) ;
120+ match ( from_index, to_index) {
121+ ( Some ( from_index) , Some ( to_index) ) => {
122+ self . groups_map . swap_indices ( from_index, to_index) ;
123+ self . mut_configuration ( |configuration| {
124+ let from_index = configuration. groups . iter ( ) . position ( |group| group. id == from_id) ;
125+ let to_index = configuration. groups . iter ( ) . position ( |group| group. id == to_id) ;
126+ if let ( Some ( from) , Some ( to) ) = ( from_index, to_index) {
127+ configuration. groups . swap ( from, to) ;
128+ }
129+ true
130+ } ) ?;
131+ Ok ( ( ) )
132+ }
133+ _ => Err ( FlowyError :: out_of_bounds ( ) ) ,
134+ }
135+ }
136+
106137 pub ( crate ) fn merge_groups ( & mut self , groups : Vec < Group > ) -> FlowyResult < Option < GroupViewChangesetPB > > {
107138 let MergeGroupResult {
108139 groups,
@@ -154,53 +185,26 @@ where
154185
155186 #[ allow( dead_code) ]
156187 pub ( crate ) async fn hide_group ( & mut self , group_id : & str ) -> FlowyResult < ( ) > {
157- self . mut_configuration_group ( group_id, |group_rev| {
188+ self . mut_group_rev ( group_id, |group_rev| {
158189 group_rev. visible = false ;
159190 } ) ?;
160191 Ok ( ( ) )
161192 }
162193
163194 #[ allow( dead_code) ]
164195 pub ( crate ) async fn show_group ( & mut self , group_id : & str ) -> FlowyResult < ( ) > {
165- self . mut_configuration_group ( group_id, |group_rev| {
196+ self . mut_group_rev ( group_id, |group_rev| {
166197 group_rev. visible = true ;
167198 } ) ?;
168199 Ok ( ( ) )
169200 }
170201
171- pub ( crate ) fn iter_mut_groups ( & mut self , mut each : impl FnMut ( & mut Group ) ) {
172- self . groups_map . iter_mut ( ) . for_each ( |( _, group) | {
173- each ( group) ;
174- } )
175- }
176-
177- pub ( crate ) fn get_mut_group ( & mut self , group_id : & str ) -> Option < & mut Group > {
178- self . groups_map . get_mut ( group_id)
179- }
180-
181202 pub ( crate ) fn get_mut_default_group ( & mut self ) -> & mut Group {
182203 & mut self . default_group
183204 }
184205
185- pub ( crate ) fn move_group ( & mut self , from_id : & str , to_id : & str ) -> FlowyResult < ( ) > {
186- let from_index = self . groups_map . get_index_of ( from_id) ;
187- let to_index = self . groups_map . get_index_of ( to_id) ;
188- match ( from_index, to_index) {
189- ( Some ( from_index) , Some ( to_index) ) => {
190- self . groups_map . swap_indices ( from_index, to_index) ;
191-
192- self . mut_configuration ( |configuration| {
193- let from_index = configuration. groups . iter ( ) . position ( |group| group. id == from_id) ;
194- let to_index = configuration. groups . iter ( ) . position ( |group| group. id == to_id) ;
195- if let ( Some ( from) , Some ( to) ) = ( from_index, to_index) {
196- configuration. groups . swap ( from, to) ;
197- }
198- true
199- } ) ?;
200- Ok ( ( ) )
201- }
202- _ => Err ( FlowyError :: out_of_bounds ( ) ) ,
203- }
206+ pub ( crate ) fn get_mut_group ( & mut self , group_id : & str ) -> Option < & mut Group > {
207+ self . groups_map . get_mut ( group_id)
204208 }
205209
206210 // Returns the index and group specified by the group_id
@@ -231,11 +235,19 @@ where
231235 Ok ( ( ) )
232236 }
233237
234- fn mut_configuration_group (
238+ fn mut_configuration (
235239 & mut self ,
236- group_id : & str ,
237- mut_groups_fn : impl Fn ( & mut GroupRevision ) ,
240+ mut_configuration_fn : impl FnOnce ( & mut GroupConfigurationRevision ) -> bool ,
238241 ) -> FlowyResult < ( ) > {
242+ let configuration = Arc :: make_mut ( & mut self . configuration ) ;
243+ let is_changed = mut_configuration_fn ( configuration) ;
244+ if is_changed {
245+ let _ = self . save_configuration ( ) ?;
246+ }
247+ Ok ( ( ) )
248+ }
249+
250+ fn mut_group_rev ( & mut self , group_id : & str , mut_groups_fn : impl Fn ( & mut GroupRevision ) ) -> FlowyResult < ( ) > {
239251 self . mut_configuration ( |configuration| {
240252 match configuration. groups . iter_mut ( ) . find ( |group| group. id == group_id) {
241253 None => false ,
@@ -246,18 +258,6 @@ where
246258 }
247259 } )
248260 }
249-
250- fn mut_configuration (
251- & mut self ,
252- mut_configuration_fn : impl FnOnce ( & mut GroupConfigurationRevision ) -> bool ,
253- ) -> FlowyResult < ( ) > {
254- let configuration = Arc :: make_mut ( & mut self . configuration ) ;
255- let is_changed = mut_configuration_fn ( configuration) ;
256- if is_changed {
257- let _ = self . save_configuration ( ) ?;
258- }
259- Ok ( ( ) )
260- }
261261}
262262
263263fn merge_groups ( old_groups : & [ GroupRevision ] , groups : Vec < Group > ) -> MergeGroupResult {
0 commit comments