feat: allow hiding ungrouped stack (#3752)

* feat: allow hiding ungrouped stack

* chore: add notifications and listeners

* chore: implement UI

* fix: field info update

* chore: more responsive notification

* chore: read the right configurations

* feat: add ungrouped button

* fix: new board not getting isGroupField

* feat: refresh the counter

* fix: item count update

* chore: apply code suggestions from Mathias

* chore: yolo through tests

* chore: UI fix

* chore: code cleanup

* chore: ungrouped item count fix

* chore: same as above
This commit is contained in:
Richard Shiue
2023-10-26 11:48:58 +08:00
committed by GitHub
parent 1883dd6d7c
commit 8c3984d21a
31 changed files with 895 additions and 118 deletions

View File

@ -5,7 +5,7 @@ use flowy_error::ErrorCode;
use crate::entities::parser::NotEmptyStr;
use crate::entities::{FieldType, RowMetaPB};
use crate::services::group::{GroupChangeset, GroupData, GroupSetting};
use crate::services::group::{GroupChangeset, GroupData, GroupSetting, GroupSettingChangeset};
#[derive(Eq, PartialEq, ProtoBuf, Debug, Default, Clone)]
pub struct GroupSettingPB {
@ -14,6 +14,9 @@ pub struct GroupSettingPB {
#[pb(index = 2)]
pub field_id: String,
#[pb(index = 3)]
pub hide_ungrouped: bool,
}
impl std::convert::From<&GroupSetting> for GroupSettingPB {
@ -21,6 +24,7 @@ impl std::convert::From<&GroupSetting> for GroupSettingPB {
GroupSettingPB {
id: rev.id.clone(),
field_id: rev.field_id.clone(),
hide_ungrouped: rev.hide_ungrouped,
}
}
}
@ -48,6 +52,26 @@ impl std::convert::From<Vec<GroupSetting>> for RepeatedGroupSettingPB {
}
}
#[derive(Debug, Default, ProtoBuf)]
pub struct GroupSettingChangesetPB {
#[pb(index = 1)]
pub view_id: String,
#[pb(index = 2)]
pub group_configuration_id: String,
#[pb(index = 3, one_of)]
pub hide_ungrouped: Option<bool>,
}
impl From<GroupSettingChangesetPB> for GroupSettingChangeset {
fn from(value: GroupSettingChangesetPB) -> Self {
Self {
hide_ungrouped: value.hide_ungrouped,
}
}
}
#[derive(ProtoBuf, Debug, Default, Clone)]
pub struct RepeatedGroupPB {
#[pb(index = 1)]

View File

@ -15,7 +15,7 @@ use crate::services::field::{
type_option_data_from_pb_or_default, DateCellChangeset, SelectOptionCellChangeset,
};
use crate::services::field_settings::FieldSettingsChangesetParams;
use crate::services::group::{GroupChangeset, GroupSettingChangeset};
use crate::services::group::{GroupChangeset, GroupChangesets};
use crate::services::share::csv::CSVFormat;
fn upgrade_manager(
@ -645,6 +645,36 @@ pub(crate) async fn update_date_cell_handler(
Ok(())
}
#[tracing::instrument(level = "trace", skip_all, err)]
pub(crate) async fn get_group_configurations_handler(
data: AFPluginData<DatabaseViewIdPB>,
manager: AFPluginState<Weak<DatabaseManager>>,
) -> DataResult<RepeatedGroupSettingPB, FlowyError> {
let manager = upgrade_manager(manager)?;
let params = data.into_inner();
let database_editor = manager.get_database_with_view_id(params.as_ref()).await?;
let group_configs = database_editor
.get_group_configuration_settings(params.as_ref())
.await?;
data_result_ok(group_configs.into())
}
#[tracing::instrument(level = "trace", skip_all, err)]
pub(crate) async fn update_group_configuration_handler(
data: AFPluginData<GroupSettingChangesetPB>,
manager: AFPluginState<Weak<DatabaseManager>>,
) -> Result<(), FlowyError> {
let manager = upgrade_manager(manager)?;
let params = data.into_inner();
let view_id = params.view_id.clone();
let database_editor = manager.get_database_with_view_id(&view_id).await?;
database_editor
.update_group_configuration_setting(&view_id, params.into())
.await?;
Ok(())
}
#[tracing::instrument(level = "trace", skip_all, err)]
pub(crate) async fn get_groups_handler(
data: AFPluginData<DatabaseViewIdPB>,
@ -701,7 +731,7 @@ pub(crate) async fn update_group_handler(
database_editor
.update_group_setting(
&view_id,
GroupSettingChangeset {
GroupChangesets {
update_groups: vec![group_changeset],
},
)

View File

@ -54,11 +54,13 @@ pub fn init(database_manager: Weak<DatabaseManager>) -> AFPlugin {
// Date
.event(DatabaseEvent::UpdateDateCell, update_date_cell_handler)
// Group
.event(DatabaseEvent::GetGroupConfigurations, get_group_configurations_handler)
.event(DatabaseEvent::UpdateGroupConfiguration, update_group_configuration_handler)
.event(DatabaseEvent::SetGroupByField, set_group_by_field_handler)
.event(DatabaseEvent::MoveGroup, move_group_handler)
.event(DatabaseEvent::MoveGroupRow, move_group_row_handler)
.event(DatabaseEvent::GetGroups, get_groups_handler)
.event(DatabaseEvent::GetGroup, get_group_handler)
.event(DatabaseEvent::SetGroupByField, set_group_by_field_handler)
.event(DatabaseEvent::UpdateGroup, update_group_handler)
// Database
.event(DatabaseEvent::GetDatabases, get_databases_handler)
@ -264,6 +266,15 @@ pub enum DatabaseEvent {
#[event(input = "DateChangesetPB")]
UpdateDateCell = 80,
#[event(input = "DatabaseViewIdPB", output = "RepeatedGroupSettingPB")]
GetGroupConfigurations = 90,
#[event(input = "GroupSettingChangesetPB")]
UpdateGroupConfiguration = 91,
#[event(input = "GroupByFieldPayloadPB")]
SetGroupByField = 92,
#[event(input = "DatabaseViewIdPB", output = "RepeatedGroupPB")]
GetGroups = 100,
@ -276,11 +287,8 @@ pub enum DatabaseEvent {
#[event(input = "MoveGroupRowPayloadPB")]
MoveGroupRow = 112,
#[event(input = "GroupByFieldPayloadPB")]
SetGroupByField = 113,
#[event(input = "UpdateGroupPB")]
UpdateGroup = 114,
UpdateGroup = 113,
/// Returns all the databases
#[event(output = "RepeatedDatabaseDescriptionPB")]

View File

@ -20,6 +20,8 @@ pub enum DatabaseNotification {
DidUpdateCell = 40,
/// Trigger after editing a field properties including rename,update type option, etc
DidUpdateField = 50,
/// Trigger after the group configuration is changed
DidUpdateGroupConfiguration = 59,
/// Trigger after the number of groups is changed
DidUpdateNumOfGroups = 60,
/// Trigger after inserting/deleting/updating/moving a row
@ -69,6 +71,7 @@ impl std::convert::From<i32> for DatabaseNotification {
22 => DatabaseNotification::DidUpdateFields,
40 => DatabaseNotification::DidUpdateCell,
50 => DatabaseNotification::DidUpdateField,
59 => DatabaseNotification::DidUpdateGroupConfiguration,
60 => DatabaseNotification::DidUpdateNumOfGroups,
61 => DatabaseNotification::DidUpdateGroupRow,
62 => DatabaseNotification::DidGroupByField,

View File

@ -32,7 +32,8 @@ use crate::services::field_settings::{
};
use crate::services::filter::Filter;
use crate::services::group::{
default_group_setting, GroupChangeset, GroupSetting, GroupSettingChangeset, RowChangeset,
default_group_setting, GroupChangeset, GroupChangesets, GroupSetting, GroupSettingChangeset,
RowChangeset,
};
use crate::services::share::csv::{CSVExport, CSVFormat};
use crate::services::sort::Sort;
@ -179,11 +180,11 @@ impl DatabaseEditor {
pub async fn update_group_setting(
&self,
view_id: &str,
group_setting_changeset: GroupSettingChangeset,
group_setting_changeset: GroupChangesets,
) -> FlowyResult<()> {
let view_editor = self.database_views.get_view_editor(view_id).await?;
view_editor
.update_group_setting(group_setting_changeset)
.v_update_group_setting(group_setting_changeset)
.await?;
Ok(())
}
@ -907,6 +908,40 @@ impl DatabaseEditor {
Ok(())
}
pub async fn get_group_configuration_settings(
&self,
view_id: &str,
) -> FlowyResult<Vec<GroupSettingPB>> {
let view = self.database_views.get_view_editor(view_id).await?;
let group_settings = view
.v_get_group_configuration_settings()
.await
.into_iter()
.map(|value| GroupSettingPB::from(&value))
.collect::<Vec<GroupSettingPB>>();
Ok(group_settings)
}
pub async fn update_group_configuration_setting(
&self,
view_id: &str,
changeset: GroupSettingChangeset,
) -> FlowyResult<()> {
let view = self.database_views.get_view_editor(view_id).await?;
let group_configuration = view.v_update_group_configuration_setting(changeset).await?;
if let Some(configuration) = group_configuration {
let payload: RepeatedGroupSettingPB = vec![configuration].into();
send_notification(view_id, DatabaseNotification::DidUpdateGroupConfiguration)
.payload(payload)
.send();
}
Ok(())
}
#[tracing::instrument(level = "trace", skip_all, err)]
pub async fn load_groups(&self, view_id: &str) -> FlowyResult<RepeatedGroupPB> {
let view = self.database_views.get_view_editor(view_id).await?;

View File

@ -37,8 +37,8 @@ use crate::services::filter::{
Filter, FilterChangeset, FilterController, FilterType, UpdatedFilterType,
};
use crate::services::group::{
GroupChangeset, GroupController, GroupSetting, GroupSettingChangeset, MoveGroupRowContext,
RowChangeset,
GroupChangeset, GroupChangesets, GroupController, GroupSetting, GroupSettingChangeset,
MoveGroupRowContext, RowChangeset,
};
use crate::services::setting::CalendarLayoutSetting;
use crate::services::sort::{DeletedSortType, Sort, SortChangeset, SortController, SortType};
@ -407,6 +407,7 @@ impl DatabaseViewEditor {
}
}
}
/// Only call once after database view editor initialized
#[tracing::instrument(level = "trace", skip(self))]
pub async fn v_load_groups(&self) -> Option<Vec<GroupPB>> {
@ -471,7 +472,20 @@ impl DatabaseViewEditor {
Ok(())
}
pub async fn update_group_setting(&self, changeset: GroupSettingChangeset) -> FlowyResult<()> {
pub async fn v_update_group_configuration_setting(
&self,
changeset: GroupSettingChangeset,
) -> FlowyResult<Option<GroupSetting>> {
let result = self
.mut_group_controller(|group_controller, _| {
group_controller.apply_group_configuration_setting_changeset(changeset)
})
.await;
Ok(result.flatten())
}
pub async fn v_update_group_setting(&self, changeset: GroupChangesets) -> FlowyResult<()> {
self
.mut_group_controller(|group_controller, _| {
group_controller.apply_group_setting_changeset(changeset)
@ -480,6 +494,10 @@ impl DatabaseViewEditor {
Ok(())
}
pub async fn v_get_group_configuration_settings(&self) -> Vec<GroupSetting> {
self.delegate.get_group_setting(&self.view_id)
}
pub async fn update_group(
&self,
changeset: GroupChangeset,

View File

@ -6,7 +6,8 @@ use flowy_error::FlowyResult;
use crate::entities::{GroupChangesPB, GroupPB, GroupRowsNotificationPB, InsertedGroupPB};
use crate::services::cell::DecodedCellData;
use crate::services::group::controller::MoveGroupRowContext;
use crate::services::group::{GroupData, GroupSettingChangeset};
use crate::services::group::entities::GroupSetting;
use crate::services::group::{GroupChangesets, GroupData, GroupSettingChangeset};
/// Using polymorphism to provides the customs action for different group controller.
///
@ -103,7 +104,12 @@ pub trait GroupControllerOperation: Send + Sync {
/// Update the group if the corresponding field is changed
fn did_update_group_field(&mut self, field: &Field) -> FlowyResult<Option<GroupChangesPB>>;
fn apply_group_setting_changeset(&mut self, changeset: GroupSettingChangeset) -> FlowyResult<()>;
fn apply_group_setting_changeset(&mut self, changeset: GroupChangesets) -> FlowyResult<()>;
fn apply_group_configuration_setting_changeset(
&mut self,
changeset: GroupSettingChangeset,
) -> FlowyResult<Option<GroupSetting>>;
}
#[derive(Debug)]

View File

@ -15,6 +15,7 @@ use crate::entities::{GroupChangesPB, GroupPB, InsertedGroupPB};
use crate::services::field::RowSingleCellData;
use crate::services::group::{
default_group_setting, GeneratedGroups, Group, GroupChangeset, GroupData, GroupSetting,
GroupSettingChangeset,
};
pub trait GroupSettingReader: Send + Sync + 'static {
@ -374,6 +375,20 @@ where
Ok(())
}
pub(crate) fn update_configuration(
&mut self,
changeset: GroupSettingChangeset,
) -> FlowyResult<Option<GroupSetting>> {
self.mut_configuration(|configuration| match changeset.hide_ungrouped {
Some(value) if value != configuration.hide_ungrouped => {
configuration.hide_ungrouped = value;
true
},
_ => false,
})?;
Ok(Some(GroupSetting::clone(&self.setting)))
}
pub(crate) async fn get_all_cells(&self) -> Vec<RowSingleCellData> {
self
.reader
@ -402,7 +417,9 @@ where
let view_id = self.view_id.clone();
tokio::spawn(async move {
match writer.save_configuration(&view_id, configuration).await {
Ok(_) => {},
Ok(_) => {
tracing::trace!("SUCCESSFULLY SAVED CONFIGURATION"); // TODO(richard): remove this
},
Err(e) => {
tracing::error!("Save group configuration failed: {}", e);
},

View File

@ -17,8 +17,8 @@ use crate::services::group::action::{
DidMoveGroupRowResult, DidUpdateGroupRowResult, GroupControllerOperation, GroupCustomize,
};
use crate::services::group::configuration::GroupContext;
use crate::services::group::entities::GroupData;
use crate::services::group::{Group, GroupSettingChangeset};
use crate::services::group::entities::{GroupData, GroupSetting};
use crate::services::group::{Group, GroupChangesets, GroupSettingChangeset};
// use collab_database::views::Group;
@ -137,8 +137,6 @@ where
})
}
// https://stackoverflow.com/questions/69413164/how-to-fix-this-clippy-warning-needless-collect
#[allow(clippy::needless_collect)]
fn update_no_status_group(
&mut self,
row_detail: &RowDetail,
@ -382,7 +380,7 @@ where
Ok(None)
}
fn apply_group_setting_changeset(&mut self, changeset: GroupSettingChangeset) -> FlowyResult<()> {
fn apply_group_setting_changeset(&mut self, changeset: GroupChangesets) -> FlowyResult<()> {
for group_changeset in changeset.update_groups {
if let Err(e) = self.context.update_group(group_changeset) {
tracing::error!("Failed to update group: {:?}", e);
@ -390,6 +388,13 @@ where
}
Ok(())
}
fn apply_group_configuration_setting_changeset(
&mut self,
changeset: GroupSettingChangeset,
) -> FlowyResult<Option<GroupSetting>> {
self.context.update_configuration(changeset)
}
}
struct GroupedRow {

View File

@ -10,7 +10,8 @@ use crate::services::group::action::{
DidMoveGroupRowResult, DidUpdateGroupRowResult, GroupControllerOperation,
};
use crate::services::group::{
GroupController, GroupData, GroupSettingChangeset, MoveGroupRowContext,
GroupChangesets, GroupController, GroupData, GroupSetting, GroupSettingChangeset,
MoveGroupRowContext,
};
/// A [DefaultGroupController] is used to handle the group actions for the [FieldType] that doesn't
@ -101,11 +102,15 @@ impl GroupControllerOperation for DefaultGroupController {
Ok(None)
}
fn apply_group_setting_changeset(
fn apply_group_setting_changeset(&mut self, _changeset: GroupChangesets) -> FlowyResult<()> {
Ok(())
}
fn apply_group_configuration_setting_changeset(
&mut self,
_changeset: GroupSettingChangeset,
) -> FlowyResult<()> {
Ok(())
) -> FlowyResult<Option<GroupSetting>> {
Ok(None)
}
}

View File

@ -12,9 +12,14 @@ pub struct GroupSetting {
pub field_type: i64,
pub groups: Vec<Group>,
pub content: String,
pub hide_ungrouped: bool,
}
pub struct GroupSettingChangeset {
pub hide_ungrouped: Option<bool>,
}
pub struct GroupChangesets {
pub update_groups: Vec<GroupChangeset>,
}
@ -27,13 +32,14 @@ pub struct GroupChangeset {
}
impl GroupSetting {
pub fn new(field_id: String, field_type: i64, content: String) -> Self {
pub fn new(field_id: String, field_type: i64, content: String, hide_ungrouped: bool) -> Self {
Self {
id: gen_database_group_id(),
field_id,
field_type,
groups: vec![],
content,
hide_ungrouped,
}
}
}
@ -43,6 +49,7 @@ const FIELD_ID: &str = "field_id";
const FIELD_TYPE: &str = "ty";
const GROUPS: &str = "groups";
const CONTENT: &str = "content";
const HIDE_UNGROUPED: &str = "hide_ungrouped";
impl TryFrom<GroupSettingMap> for GroupSetting {
type Error = anyhow::Error;
@ -52,8 +59,9 @@ impl TryFrom<GroupSettingMap> for GroupSetting {
value.get_str_value(GROUP_ID),
value.get_str_value(FIELD_ID),
value.get_i64_value(FIELD_TYPE),
value.get_bool_value(HIDE_UNGROUPED),
) {
(Some(id), Some(field_id), Some(field_type)) => {
(Some(id), Some(field_id), Some(field_type), Some(hide_ungrouped)) => {
let content = value.get_str_value(CONTENT).unwrap_or_default();
let groups = value.try_get_array(GROUPS);
Ok(Self {
@ -62,6 +70,7 @@ impl TryFrom<GroupSettingMap> for GroupSetting {
field_type,
groups,
content,
hide_ungrouped,
})
},
_ => {
@ -79,6 +88,7 @@ impl From<GroupSetting> for GroupSettingMap {
.insert_i64_value(FIELD_TYPE, setting.field_type)
.insert_maps(GROUPS, setting.groups)
.insert_str_value(CONTENT, setting.content)
.insert_bool_value(HIDE_UNGROUPED, setting.hide_ungrouped)
.build()
}
}

View File

@ -154,7 +154,7 @@ pub fn find_new_grouping_field(
///
pub fn default_group_setting(field: &Field) -> GroupSetting {
let field_id = field.id.clone();
GroupSetting::new(field_id, field.field_type, "".to_owned())
GroupSetting::new(field_id, field.field_type, "".to_owned(), false)
}
pub fn make_no_status_group(field: &Field) -> Group {

View File

@ -8,5 +8,5 @@ mod group_builder;
pub(crate) use configuration::*;
pub(crate) use controller::*;
pub(crate) use controller_impls::*;
pub(crate) use entities::*;
pub use entities::*;
pub(crate) use group_builder::*;