|
@@ -93,16 +93,47 @@ where
|
|
|
})
|
|
|
}
|
|
|
|
|
|
- pub(crate) fn groups(&self) -> Vec<&Group> {
|
|
|
+ /// Returns the groups without the default group
|
|
|
+ pub(crate) fn concrete_groups(&self) -> Vec<&Group> {
|
|
|
self.groups_map.values().collect()
|
|
|
}
|
|
|
|
|
|
+ /// Returns the all the groups that contain the default group.
|
|
|
pub(crate) fn clone_groups(&self) -> Vec<Group> {
|
|
|
let mut groups: Vec<Group> = self.groups_map.values().cloned().collect();
|
|
|
groups.push(self.default_group.clone());
|
|
|
groups
|
|
|
}
|
|
|
|
|
|
+ /// Iterate mut the groups. The default group will be the last one that get mutated.
|
|
|
+ pub(crate) fn iter_mut_groups(&mut self, mut each: impl FnMut(&mut Group)) {
|
|
|
+ self.groups_map.iter_mut().for_each(|(_, group)| {
|
|
|
+ each(group);
|
|
|
+ });
|
|
|
+
|
|
|
+ each(&mut self.default_group);
|
|
|
+ }
|
|
|
+
|
|
|
+ pub(crate) fn move_group(&mut self, from_id: &str, to_id: &str) -> FlowyResult<()> {
|
|
|
+ let from_index = self.groups_map.get_index_of(from_id);
|
|
|
+ let to_index = self.groups_map.get_index_of(to_id);
|
|
|
+ match (from_index, to_index) {
|
|
|
+ (Some(from_index), Some(to_index)) => {
|
|
|
+ self.groups_map.swap_indices(from_index, to_index);
|
|
|
+ self.mut_configuration(|configuration| {
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ true
|
|
|
+ })?;
|
|
|
+ Ok(())
|
|
|
+ }
|
|
|
+ _ => Err(FlowyError::out_of_bounds()),
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
pub(crate) fn merge_groups(&mut self, groups: Vec<Group>) -> FlowyResult<Option<GroupViewChangesetPB>> {
|
|
|
let MergeGroupResult {
|
|
|
groups,
|
|
@@ -154,7 +185,7 @@ where
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
pub(crate) async fn hide_group(&mut self, group_id: &str) -> FlowyResult<()> {
|
|
|
- self.mut_configuration_group(group_id, |group_rev| {
|
|
|
+ self.mut_group_rev(group_id, |group_rev| {
|
|
|
group_rev.visible = false;
|
|
|
})?;
|
|
|
Ok(())
|
|
@@ -162,45 +193,18 @@ where
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
pub(crate) async fn show_group(&mut self, group_id: &str) -> FlowyResult<()> {
|
|
|
- self.mut_configuration_group(group_id, |group_rev| {
|
|
|
+ self.mut_group_rev(group_id, |group_rev| {
|
|
|
group_rev.visible = true;
|
|
|
})?;
|
|
|
Ok(())
|
|
|
}
|
|
|
|
|
|
- pub(crate) fn iter_mut_groups(&mut self, mut each: impl FnMut(&mut Group)) {
|
|
|
- self.groups_map.iter_mut().for_each(|(_, group)| {
|
|
|
- each(group);
|
|
|
- })
|
|
|
- }
|
|
|
-
|
|
|
- pub(crate) fn get_mut_group(&mut self, group_id: &str) -> Option<&mut Group> {
|
|
|
- self.groups_map.get_mut(group_id)
|
|
|
- }
|
|
|
-
|
|
|
pub(crate) fn get_mut_default_group(&mut self) -> &mut Group {
|
|
|
&mut self.default_group
|
|
|
}
|
|
|
|
|
|
- pub(crate) fn move_group(&mut self, from_id: &str, to_id: &str) -> FlowyResult<()> {
|
|
|
- let from_index = self.groups_map.get_index_of(from_id);
|
|
|
- let to_index = self.groups_map.get_index_of(to_id);
|
|
|
- match (from_index, to_index) {
|
|
|
- (Some(from_index), Some(to_index)) => {
|
|
|
- self.groups_map.swap_indices(from_index, to_index);
|
|
|
-
|
|
|
- self.mut_configuration(|configuration| {
|
|
|
- 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);
|
|
|
- }
|
|
|
- true
|
|
|
- })?;
|
|
|
- Ok(())
|
|
|
- }
|
|
|
- _ => Err(FlowyError::out_of_bounds()),
|
|
|
- }
|
|
|
+ pub(crate) fn get_mut_group(&mut self, group_id: &str) -> Option<&mut Group> {
|
|
|
+ self.groups_map.get_mut(group_id)
|
|
|
}
|
|
|
|
|
|
// Returns the index and group specified by the group_id
|
|
@@ -231,11 +235,19 @@ where
|
|
|
Ok(())
|
|
|
}
|
|
|
|
|
|
- fn mut_configuration_group(
|
|
|
+ fn mut_configuration(
|
|
|
&mut self,
|
|
|
- group_id: &str,
|
|
|
- mut_groups_fn: impl Fn(&mut GroupRevision),
|
|
|
+ mut_configuration_fn: impl FnOnce(&mut GroupConfigurationRevision) -> bool,
|
|
|
) -> FlowyResult<()> {
|
|
|
+ let configuration = Arc::make_mut(&mut self.configuration);
|
|
|
+ let is_changed = mut_configuration_fn(configuration);
|
|
|
+ if is_changed {
|
|
|
+ let _ = self.save_configuration()?;
|
|
|
+ }
|
|
|
+ Ok(())
|
|
|
+ }
|
|
|
+
|
|
|
+ fn mut_group_rev(&mut self, group_id: &str, mut_groups_fn: impl Fn(&mut GroupRevision)) -> FlowyResult<()> {
|
|
|
self.mut_configuration(|configuration| {
|
|
|
match configuration.groups.iter_mut().find(|group| group.id == group_id) {
|
|
|
None => false,
|
|
@@ -246,18 +258,6 @@ where
|
|
|
}
|
|
|
})
|
|
|
}
|
|
|
-
|
|
|
- fn mut_configuration(
|
|
|
- &mut self,
|
|
|
- mut_configuration_fn: impl FnOnce(&mut GroupConfigurationRevision) -> bool,
|
|
|
- ) -> FlowyResult<()> {
|
|
|
- let configuration = Arc::make_mut(&mut self.configuration);
|
|
|
- let is_changed = mut_configuration_fn(configuration);
|
|
|
- if is_changed {
|
|
|
- let _ = self.save_configuration()?;
|
|
|
- }
|
|
|
- Ok(())
|
|
|
- }
|
|
|
}
|
|
|
|
|
|
fn merge_groups(old_groups: &[GroupRevision], groups: Vec<Group>) -> MergeGroupResult {
|