mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
chore: clean up sort and filter code (#4585)
* refactor: port away from extra SortType struct * refactor: add validator to flowy_database and clean up unused structs * refactor: port away from extra FilterType struct * chore: analysis options * fix: clippy and dart/ts compile * fix: tauri build --------- Co-authored-by: nathan <nathan@appflowy.io>
This commit is contained in:
@ -6,6 +6,7 @@ use collab_database::fields::Field;
|
||||
|
||||
use flowy_derive::ProtoBuf;
|
||||
use flowy_error::ErrorCode;
|
||||
use validator::Validate;
|
||||
|
||||
use crate::entities::parser::NotEmptyStr;
|
||||
use crate::entities::{
|
||||
@ -13,7 +14,7 @@ use crate::entities::{
|
||||
NumberFilterPB, SelectOptionFilterPB, TextFilterPB,
|
||||
};
|
||||
use crate::services::field::SelectOptionIds;
|
||||
use crate::services::filter::{Filter, FilterType};
|
||||
use crate::services::filter::Filter;
|
||||
|
||||
#[derive(Eq, PartialEq, ProtoBuf, Debug, Default, Clone)]
|
||||
pub struct FilterPB {
|
||||
@ -73,60 +74,28 @@ impl std::convert::From<Vec<FilterPB>> for RepeatedFilterPB {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(ProtoBuf, Debug, Default, Clone)]
|
||||
#[derive(ProtoBuf, Debug, Default, Clone, Validate)]
|
||||
pub struct DeleteFilterPayloadPB {
|
||||
#[pb(index = 1)]
|
||||
#[validate(custom = "lib_infra::validator_fn::required_not_empty_str")]
|
||||
pub field_id: String,
|
||||
|
||||
#[pb(index = 2)]
|
||||
pub field_type: FieldType,
|
||||
|
||||
#[pb(index = 3)]
|
||||
#[validate(custom = "lib_infra::validator_fn::required_not_empty_str")]
|
||||
pub filter_id: String,
|
||||
|
||||
#[pb(index = 4)]
|
||||
#[validate(custom = "lib_infra::validator_fn::required_not_empty_str")]
|
||||
pub view_id: String,
|
||||
}
|
||||
|
||||
impl TryInto<DeleteFilterParams> for DeleteFilterPayloadPB {
|
||||
type Error = ErrorCode;
|
||||
|
||||
fn try_into(self) -> Result<DeleteFilterParams, Self::Error> {
|
||||
let view_id = NotEmptyStr::parse(self.view_id)
|
||||
.map_err(|_| ErrorCode::DatabaseViewIdIsEmpty)?
|
||||
.0;
|
||||
let field_id = NotEmptyStr::parse(self.field_id)
|
||||
.map_err(|_| ErrorCode::FieldIdIsEmpty)?
|
||||
.0;
|
||||
|
||||
let filter_id = NotEmptyStr::parse(self.filter_id)
|
||||
.map_err(|_| ErrorCode::UnexpectedEmpty)?
|
||||
.0;
|
||||
|
||||
let filter_type = FilterType {
|
||||
filter_id: filter_id.clone(),
|
||||
field_id,
|
||||
field_type: self.field_type,
|
||||
};
|
||||
|
||||
Ok(DeleteFilterParams {
|
||||
view_id,
|
||||
filter_id,
|
||||
filter_type,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct DeleteFilterParams {
|
||||
pub view_id: String,
|
||||
pub filter_id: String,
|
||||
pub filter_type: FilterType,
|
||||
}
|
||||
|
||||
#[derive(ProtoBuf, Debug, Default, Clone)]
|
||||
#[derive(ProtoBuf, Debug, Default, Clone, Validate)]
|
||||
pub struct UpdateFilterPayloadPB {
|
||||
#[pb(index = 1)]
|
||||
#[validate(custom = "lib_infra::validator_fn::required_not_empty_str")]
|
||||
pub field_id: String,
|
||||
|
||||
#[pb(index = 2)]
|
||||
@ -134,12 +103,14 @@ pub struct UpdateFilterPayloadPB {
|
||||
|
||||
/// Create a new filter if the filter_id is None
|
||||
#[pb(index = 3, one_of)]
|
||||
#[validate(custom = "lib_infra::validator_fn::required_not_empty_str")]
|
||||
pub filter_id: Option<String>,
|
||||
|
||||
#[pb(index = 4)]
|
||||
pub data: Vec<u8>,
|
||||
|
||||
#[pb(index = 5)]
|
||||
#[validate(custom = "lib_infra::validator_fn::required_not_empty_str")]
|
||||
pub view_id: String,
|
||||
}
|
||||
|
||||
|
@ -2,6 +2,7 @@ use std::convert::TryInto;
|
||||
|
||||
use flowy_derive::ProtoBuf;
|
||||
use flowy_error::ErrorCode;
|
||||
use validator::Validate;
|
||||
|
||||
use crate::entities::parser::NotEmptyStr;
|
||||
use crate::entities::RowMetaPB;
|
||||
@ -130,15 +131,18 @@ pub struct GroupByFieldParams {
|
||||
pub view_id: String,
|
||||
}
|
||||
|
||||
#[derive(Eq, PartialEq, ProtoBuf, Debug, Default, Clone)]
|
||||
#[derive(Eq, PartialEq, ProtoBuf, Debug, Default, Clone, Validate)]
|
||||
pub struct UpdateGroupPB {
|
||||
#[pb(index = 1)]
|
||||
#[validate(custom = "lib_infra::validator_fn::required_not_empty_str")]
|
||||
pub view_id: String,
|
||||
|
||||
#[pb(index = 2)]
|
||||
#[validate(custom = "lib_infra::validator_fn::required_not_empty_str")]
|
||||
pub group_id: String,
|
||||
|
||||
#[pb(index = 3)]
|
||||
#[validate(custom = "lib_infra::validator_fn::required_not_empty_str")]
|
||||
pub field_id: String,
|
||||
|
||||
#[pb(index = 4, one_of)]
|
||||
|
@ -5,12 +5,12 @@ use strum_macros::EnumIter;
|
||||
|
||||
use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
|
||||
use flowy_error::ErrorCode;
|
||||
use validator::Validate;
|
||||
|
||||
use crate::entities::parser::NotEmptyStr;
|
||||
use crate::entities::{
|
||||
CalendarLayoutSettingPB, DeleteFilterParams, DeleteFilterPayloadPB, DeleteSortParams,
|
||||
DeleteSortPayloadPB, RepeatedFieldSettingsPB, RepeatedFilterPB, RepeatedGroupSettingPB,
|
||||
RepeatedSortPB, UpdateFilterParams, UpdateFilterPayloadPB, UpdateGroupPB, UpdateSortParams,
|
||||
CalendarLayoutSettingPB, DeleteFilterPayloadPB, DeleteSortPayloadPB, RepeatedFieldSettingsPB,
|
||||
RepeatedFilterPB, RepeatedGroupSettingPB, RepeatedSortPB, UpdateFilterPayloadPB, UpdateGroupPB,
|
||||
UpdateSortPayloadPB,
|
||||
};
|
||||
use crate::services::setting::{BoardLayoutSetting, CalendarLayoutSetting};
|
||||
@ -69,84 +69,36 @@ impl std::convert::From<DatabaseLayoutPB> for DatabaseLayout {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default, ProtoBuf)]
|
||||
#[derive(Default, Validate, ProtoBuf)]
|
||||
pub struct DatabaseSettingChangesetPB {
|
||||
#[pb(index = 1)]
|
||||
#[validate(custom = "lib_infra::validator_fn::required_not_empty_str")]
|
||||
pub view_id: String,
|
||||
|
||||
#[pb(index = 2, one_of)]
|
||||
pub layout_type: Option<DatabaseLayoutPB>,
|
||||
|
||||
#[pb(index = 3, one_of)]
|
||||
#[validate]
|
||||
pub update_filter: Option<UpdateFilterPayloadPB>,
|
||||
|
||||
#[pb(index = 4, one_of)]
|
||||
#[validate]
|
||||
pub delete_filter: Option<DeleteFilterPayloadPB>,
|
||||
|
||||
#[pb(index = 5, one_of)]
|
||||
#[validate]
|
||||
pub update_group: Option<UpdateGroupPB>,
|
||||
|
||||
#[pb(index = 6, one_of)]
|
||||
#[validate]
|
||||
pub update_sort: Option<UpdateSortPayloadPB>,
|
||||
|
||||
#[pb(index = 7, one_of)]
|
||||
#[validate]
|
||||
pub delete_sort: Option<DeleteSortPayloadPB>,
|
||||
}
|
||||
|
||||
impl TryInto<DatabaseSettingChangesetParams> for DatabaseSettingChangesetPB {
|
||||
type Error = ErrorCode;
|
||||
|
||||
fn try_into(self) -> Result<DatabaseSettingChangesetParams, Self::Error> {
|
||||
let view_id = NotEmptyStr::parse(self.view_id)
|
||||
.map_err(|_| ErrorCode::ViewIdIsInvalid)?
|
||||
.0;
|
||||
|
||||
let insert_filter = match self.update_filter {
|
||||
None => None,
|
||||
Some(payload) => Some(payload.try_into()?),
|
||||
};
|
||||
|
||||
let delete_filter = match self.delete_filter {
|
||||
None => None,
|
||||
Some(payload) => Some(payload.try_into()?),
|
||||
};
|
||||
|
||||
let alert_sort = match self.update_sort {
|
||||
None => None,
|
||||
Some(payload) => Some(payload.try_into()?),
|
||||
};
|
||||
|
||||
let delete_sort = match self.delete_sort {
|
||||
None => None,
|
||||
Some(payload) => Some(payload.try_into()?),
|
||||
};
|
||||
|
||||
Ok(DatabaseSettingChangesetParams {
|
||||
view_id,
|
||||
layout_type: self.layout_type.map(|ty| ty.into()),
|
||||
insert_filter,
|
||||
delete_filter,
|
||||
alert_sort,
|
||||
delete_sort,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub struct DatabaseSettingChangesetParams {
|
||||
pub view_id: String,
|
||||
pub layout_type: Option<DatabaseLayout>,
|
||||
pub insert_filter: Option<UpdateFilterParams>,
|
||||
pub delete_filter: Option<DeleteFilterParams>,
|
||||
pub alert_sort: Option<UpdateSortParams>,
|
||||
pub delete_sort: Option<DeleteSortParams>,
|
||||
}
|
||||
|
||||
impl DatabaseSettingChangesetParams {
|
||||
pub fn is_filter_changed(&self) -> bool {
|
||||
self.insert_filter.is_some() || self.delete_filter.is_some()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Eq, PartialEq, Default, ProtoBuf, Clone)]
|
||||
pub struct DatabaseLayoutSettingPB {
|
||||
#[pb(index = 1)]
|
||||
|
@ -1,9 +1,8 @@
|
||||
use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
|
||||
use flowy_error::ErrorCode;
|
||||
use validator::Validate;
|
||||
|
||||
use crate::entities::parser::NotEmptyStr;
|
||||
use crate::entities::FieldType;
|
||||
use crate::services::sort::{Sort, SortCondition, SortType};
|
||||
use crate::services::sort::{Sort, SortCondition};
|
||||
|
||||
#[derive(Eq, PartialEq, ProtoBuf, Debug, Default, Clone)]
|
||||
pub struct SortPB {
|
||||
@ -91,12 +90,14 @@ impl std::convert::From<SortConditionPB> for SortCondition {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(ProtoBuf, Debug, Default, Clone)]
|
||||
#[derive(ProtoBuf, Debug, Default, Clone, Validate)]
|
||||
pub struct UpdateSortPayloadPB {
|
||||
#[pb(index = 1)]
|
||||
#[validate(custom = "lib_infra::validator_fn::required_not_empty_str")]
|
||||
pub view_id: String,
|
||||
|
||||
#[pb(index = 2)]
|
||||
#[validate(custom = "lib_infra::validator_fn::required_not_empty_str")]
|
||||
pub field_id: String,
|
||||
|
||||
#[pb(index = 3)]
|
||||
@ -110,95 +111,14 @@ pub struct UpdateSortPayloadPB {
|
||||
pub condition: SortConditionPB,
|
||||
}
|
||||
|
||||
impl TryInto<UpdateSortParams> for UpdateSortPayloadPB {
|
||||
type Error = ErrorCode;
|
||||
|
||||
fn try_into(self) -> Result<UpdateSortParams, Self::Error> {
|
||||
let view_id = NotEmptyStr::parse(self.view_id)
|
||||
.map_err(|_| ErrorCode::DatabaseViewIdIsEmpty)?
|
||||
.0;
|
||||
|
||||
let field_id = NotEmptyStr::parse(self.field_id)
|
||||
.map_err(|_| ErrorCode::FieldIdIsEmpty)?
|
||||
.0;
|
||||
|
||||
let sort_id = match self.sort_id {
|
||||
None => None,
|
||||
Some(sort_id) => Some(
|
||||
NotEmptyStr::parse(sort_id)
|
||||
.map_err(|_| ErrorCode::SortIdIsEmpty)?
|
||||
.0,
|
||||
),
|
||||
};
|
||||
|
||||
Ok(UpdateSortParams {
|
||||
view_id,
|
||||
field_id,
|
||||
sort_id,
|
||||
field_type: self.field_type,
|
||||
condition: self.condition.into(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct UpdateSortParams {
|
||||
pub view_id: String,
|
||||
pub field_id: String,
|
||||
/// Create a new sort if the sort is None
|
||||
pub sort_id: Option<String>,
|
||||
pub field_type: FieldType,
|
||||
pub condition: SortCondition,
|
||||
}
|
||||
|
||||
#[derive(ProtoBuf, Debug, Default, Clone)]
|
||||
#[derive(ProtoBuf, Debug, Default, Clone, Validate)]
|
||||
pub struct DeleteSortPayloadPB {
|
||||
#[pb(index = 1)]
|
||||
#[validate(custom = "lib_infra::validator_fn::required_not_empty_str")]
|
||||
pub view_id: String,
|
||||
|
||||
#[pb(index = 2)]
|
||||
pub field_id: String,
|
||||
|
||||
#[pb(index = 3)]
|
||||
pub field_type: FieldType,
|
||||
|
||||
#[pb(index = 4)]
|
||||
pub sort_id: String,
|
||||
}
|
||||
|
||||
impl TryInto<DeleteSortParams> for DeleteSortPayloadPB {
|
||||
type Error = ErrorCode;
|
||||
|
||||
fn try_into(self) -> Result<DeleteSortParams, Self::Error> {
|
||||
let view_id = NotEmptyStr::parse(self.view_id)
|
||||
.map_err(|_| ErrorCode::DatabaseViewIdIsEmpty)?
|
||||
.0;
|
||||
let field_id = NotEmptyStr::parse(self.field_id)
|
||||
.map_err(|_| ErrorCode::FieldIdIsEmpty)?
|
||||
.0;
|
||||
|
||||
let sort_id = NotEmptyStr::parse(self.sort_id)
|
||||
.map_err(|_| ErrorCode::UnexpectedEmpty)?
|
||||
.0;
|
||||
|
||||
let sort_type = SortType {
|
||||
sort_id: sort_id.clone(),
|
||||
field_id,
|
||||
field_type: self.field_type,
|
||||
};
|
||||
|
||||
Ok(DeleteSortParams {
|
||||
view_id,
|
||||
sort_type,
|
||||
sort_id,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct DeleteSortParams {
|
||||
pub view_id: String,
|
||||
pub sort_type: SortType,
|
||||
#[validate(custom = "lib_infra::validator_fn::required_not_empty_str")]
|
||||
pub sort_id: String,
|
||||
}
|
||||
|
||||
|
@ -87,27 +87,30 @@ pub(crate) async fn update_database_setting_handler(
|
||||
manager: AFPluginState<Weak<DatabaseManager>>,
|
||||
) -> Result<(), FlowyError> {
|
||||
let manager = upgrade_manager(manager)?;
|
||||
let params: DatabaseSettingChangesetParams = data.into_inner().try_into()?;
|
||||
let params = data.try_into_inner()?;
|
||||
let editor = manager.get_database_with_view_id(¶ms.view_id).await?;
|
||||
|
||||
if let Some(update_filter) = params.insert_filter {
|
||||
editor.create_or_update_filter(update_filter).await?;
|
||||
if let Some(update_filter) = params.update_filter {
|
||||
editor
|
||||
.create_or_update_filter(update_filter.try_into()?)
|
||||
.await?;
|
||||
}
|
||||
|
||||
if let Some(delete_filter) = params.delete_filter {
|
||||
editor.delete_filter(delete_filter).await?;
|
||||
}
|
||||
|
||||
if let Some(update_sort) = params.alert_sort {
|
||||
if let Some(update_sort) = params.update_sort {
|
||||
let _ = editor.create_or_update_sort(update_sort).await?;
|
||||
}
|
||||
|
||||
if let Some(delete_sort) = params.delete_sort {
|
||||
editor.delete_sort(delete_sort).await?;
|
||||
}
|
||||
|
||||
if let Some(layout_type) = params.layout_type {
|
||||
editor
|
||||
.update_view_layout(¶ms.view_id, layout_type)
|
||||
.update_view_layout(¶ms.view_id, layout_type.into())
|
||||
.await?;
|
||||
}
|
||||
Ok(())
|
||||
|
@ -208,19 +208,19 @@ impl DatabaseEditor {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn delete_filter(&self, params: DeleteFilterParams) -> FlowyResult<()> {
|
||||
pub async fn delete_filter(&self, params: DeleteFilterPayloadPB) -> FlowyResult<()> {
|
||||
let view_editor = self.database_views.get_view_editor(¶ms.view_id).await?;
|
||||
view_editor.v_delete_filter(params).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn create_or_update_sort(&self, params: UpdateSortParams) -> FlowyResult<Sort> {
|
||||
pub async fn create_or_update_sort(&self, params: UpdateSortPayloadPB) -> FlowyResult<Sort> {
|
||||
let view_editor = self.database_views.get_view_editor(¶ms.view_id).await?;
|
||||
let sort = view_editor.v_insert_sort(params).await?;
|
||||
let sort = view_editor.insert_or_update_sort(params).await?;
|
||||
Ok(sort)
|
||||
}
|
||||
|
||||
pub async fn delete_sort(&self, params: DeleteSortParams) -> FlowyResult<()> {
|
||||
pub async fn delete_sort(&self, params: DeleteSortPayloadPB) -> FlowyResult<()> {
|
||||
let view_editor = self.database_views.get_view_editor(¶ms.view_id).await?;
|
||||
view_editor.v_delete_sort(params).await?;
|
||||
Ok(())
|
||||
|
@ -15,11 +15,11 @@ use flowy_error::{FlowyError, FlowyResult};
|
||||
use lib_dispatch::prelude::af_spawn;
|
||||
|
||||
use crate::entities::{
|
||||
CalendarEventPB, DatabaseLayoutMetaPB, DatabaseLayoutSettingPB, DeleteFilterParams,
|
||||
DeleteSortParams, FieldType, FieldVisibility, GroupChangesPB, GroupPB, InsertedRowPB,
|
||||
CalendarEventPB, DatabaseLayoutMetaPB, DatabaseLayoutSettingPB, DeleteFilterPayloadPB,
|
||||
DeleteSortPayloadPB, FieldType, FieldVisibility, GroupChangesPB, GroupPB, InsertedRowPB,
|
||||
LayoutSettingChangeset, LayoutSettingParams, RemoveCalculationChangesetPB, RowMetaPB,
|
||||
RowsChangePB, SortChangesetNotificationPB, SortPB, UpdateCalculationChangesetPB,
|
||||
UpdateFilterParams, UpdateSortParams,
|
||||
UpdateFilterParams, UpdateSortPayloadPB,
|
||||
};
|
||||
use crate::notification::{send_notification, DatabaseNotification};
|
||||
use crate::services::calculations::{Calculation, CalculationChangeset, CalculationsController};
|
||||
@ -38,11 +38,11 @@ use crate::services::database_view::{
|
||||
};
|
||||
use crate::services::field_settings::FieldSettings;
|
||||
use crate::services::filter::{
|
||||
Filter, FilterChangeset, FilterController, FilterType, UpdatedFilterType,
|
||||
Filter, FilterChangeset, FilterContext, FilterController, UpdatedFilter,
|
||||
};
|
||||
use crate::services::group::{GroupChangesets, GroupController, MoveGroupRowContext, RowChangeset};
|
||||
use crate::services::setting::CalendarLayoutSetting;
|
||||
use crate::services::sort::{DeletedSortType, Sort, SortChangeset, SortController, SortType};
|
||||
use crate::services::sort::{Sort, SortChangeset, SortController};
|
||||
|
||||
use super::notify_did_update_calculation;
|
||||
use super::view_calculations::make_calculations_controller;
|
||||
@ -499,7 +499,7 @@ impl DatabaseViewEditor {
|
||||
}
|
||||
|
||||
#[tracing::instrument(level = "trace", skip(self), err)]
|
||||
pub async fn v_insert_sort(&self, params: UpdateSortParams) -> FlowyResult<Sort> {
|
||||
pub async fn insert_or_update_sort(&self, params: UpdateSortPayloadPB) -> FlowyResult<Sort> {
|
||||
let is_exist = params.sort_id.is_some();
|
||||
let sort_id = match params.sort_id {
|
||||
None => gen_database_sort_id(),
|
||||
@ -510,18 +510,18 @@ impl DatabaseViewEditor {
|
||||
id: sort_id,
|
||||
field_id: params.field_id.clone(),
|
||||
field_type: params.field_type,
|
||||
condition: params.condition,
|
||||
condition: params.condition.into(),
|
||||
};
|
||||
let sort_type = SortType::from(&sort);
|
||||
|
||||
let mut sort_controller = self.sort_controller.write().await;
|
||||
self.delegate.insert_sort(&self.view_id, sort.clone());
|
||||
let changeset = if is_exist {
|
||||
sort_controller
|
||||
.did_receive_changes(SortChangeset::from_update(sort_type))
|
||||
.apply_changeset(SortChangeset::from_update(sort.clone()))
|
||||
.await
|
||||
} else {
|
||||
sort_controller
|
||||
.did_receive_changes(SortChangeset::from_insert(sort_type))
|
||||
.apply_changeset(SortChangeset::from_insert(sort.clone()))
|
||||
.await
|
||||
};
|
||||
drop(sort_controller);
|
||||
@ -529,18 +529,17 @@ impl DatabaseViewEditor {
|
||||
Ok(sort)
|
||||
}
|
||||
|
||||
pub async fn v_delete_sort(&self, params: DeleteSortParams) -> FlowyResult<()> {
|
||||
pub async fn v_delete_sort(&self, params: DeleteSortPayloadPB) -> FlowyResult<()> {
|
||||
let notification = self
|
||||
.sort_controller
|
||||
.write()
|
||||
.await
|
||||
.did_receive_changes(SortChangeset::from_delete(DeletedSortType::from(
|
||||
params.clone(),
|
||||
)))
|
||||
.apply_changeset(SortChangeset::from_delete(params.sort_id.clone()))
|
||||
.await;
|
||||
|
||||
self.delegate.remove_sort(&self.view_id, ¶ms.sort_id);
|
||||
notify_did_update_sort(notification).await;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@ -635,25 +634,20 @@ impl DatabaseViewEditor {
|
||||
condition: params.condition,
|
||||
content: params.content,
|
||||
};
|
||||
let filter_type = FilterType::from(&filter);
|
||||
let filter_controller = self.filter_controller.clone();
|
||||
let changeset = if is_exist {
|
||||
let old_filter_type = self
|
||||
.delegate
|
||||
.get_filter(&self.view_id, &filter.id)
|
||||
.map(|field| FilterType::from(&field));
|
||||
let old_filter = self.delegate.get_filter(&self.view_id, &filter.id);
|
||||
|
||||
self.delegate.insert_filter(&self.view_id, filter);
|
||||
self.delegate.insert_filter(&self.view_id, filter.clone());
|
||||
filter_controller
|
||||
.did_receive_changes(FilterChangeset::from_update(UpdatedFilterType::new(
|
||||
old_filter_type,
|
||||
filter_type,
|
||||
.did_receive_changes(FilterChangeset::from_update(UpdatedFilter::new(
|
||||
old_filter, filter,
|
||||
)))
|
||||
.await
|
||||
} else {
|
||||
self.delegate.insert_filter(&self.view_id, filter);
|
||||
self.delegate.insert_filter(&self.view_id, filter.clone());
|
||||
filter_controller
|
||||
.did_receive_changes(FilterChangeset::from_insert(filter_type))
|
||||
.did_receive_changes(FilterChangeset::from_insert(filter))
|
||||
.await
|
||||
};
|
||||
drop(filter_controller);
|
||||
@ -665,16 +659,20 @@ impl DatabaseViewEditor {
|
||||
}
|
||||
|
||||
#[tracing::instrument(level = "trace", skip(self), err)]
|
||||
pub async fn v_delete_filter(&self, params: DeleteFilterParams) -> FlowyResult<()> {
|
||||
let filter_type = params.filter_type;
|
||||
pub async fn v_delete_filter(&self, params: DeleteFilterPayloadPB) -> FlowyResult<()> {
|
||||
let filter_context = FilterContext {
|
||||
filter_id: params.filter_id.clone(),
|
||||
field_id: params.field_id.clone(),
|
||||
field_type: params.field_type,
|
||||
};
|
||||
let changeset = self
|
||||
.filter_controller
|
||||
.did_receive_changes(FilterChangeset::from_delete(filter_type.clone()))
|
||||
.did_receive_changes(FilterChangeset::from_delete(filter_context.clone()))
|
||||
.await;
|
||||
|
||||
self
|
||||
.delegate
|
||||
.delete_filter(&self.view_id, &filter_type.filter_id);
|
||||
.delete_filter(&self.view_id, ¶ms.filter_id);
|
||||
if changeset.is_some() {
|
||||
notify_did_update_filter(changeset.unwrap()).await;
|
||||
}
|
||||
@ -799,11 +797,12 @@ impl DatabaseViewEditor {
|
||||
.delegate
|
||||
.get_filter_by_field_id(&self.view_id, field_id)
|
||||
{
|
||||
let mut old = FilterType::from(&filter);
|
||||
old.field_type = FieldType::from(old_field.field_type);
|
||||
let new = FilterType::from(&filter);
|
||||
let filter_type = UpdatedFilterType::new(Some(old), new);
|
||||
let filter_changeset = FilterChangeset::from_update(filter_type);
|
||||
let old = Filter {
|
||||
field_type: FieldType::from(old_field.field_type),
|
||||
..filter.clone()
|
||||
};
|
||||
let updated_filter = UpdatedFilter::new(Some(old), filter);
|
||||
let filter_changeset = FilterChangeset::from_update(updated_filter);
|
||||
let filter_controller = self.filter_controller.clone();
|
||||
af_spawn(async move {
|
||||
if let Some(notification) = filter_controller
|
||||
|
@ -237,7 +237,7 @@ impl FilterController {
|
||||
let mut notification: Option<FilterChangesetNotificationPB> = None;
|
||||
|
||||
if let Some(filter_type) = &changeset.insert_filter {
|
||||
if let Some(filter) = self.filter_from_filter_id(&filter_type.filter_id).await {
|
||||
if let Some(filter) = self.filter_from_filter_id(&filter_type.id).await {
|
||||
notification = Some(FilterChangesetNotificationPB::from_insert(
|
||||
&self.view_id,
|
||||
vec![filter],
|
||||
@ -245,7 +245,7 @@ impl FilterController {
|
||||
}
|
||||
if let Some(filter) = self
|
||||
.delegate
|
||||
.get_filter(&self.view_id, &filter_type.filter_id)
|
||||
.get_filter(&self.view_id, &filter_type.id)
|
||||
.await
|
||||
{
|
||||
self.refresh_filters(vec![filter]).await;
|
||||
@ -255,9 +255,9 @@ impl FilterController {
|
||||
if let Some(updated_filter_type) = changeset.update_filter {
|
||||
if let Some(old_filter_type) = updated_filter_type.old {
|
||||
let new_filter = self
|
||||
.filter_from_filter_id(&updated_filter_type.new.filter_id)
|
||||
.filter_from_filter_id(&updated_filter_type.new.id)
|
||||
.await;
|
||||
let old_filter = self.filter_from_filter_id(&old_filter_type.filter_id).await;
|
||||
let old_filter = self.filter_from_filter_id(&old_filter_type.id).await;
|
||||
|
||||
// Get the filter id
|
||||
let mut filter_id = old_filter.map(|filter| filter.id);
|
||||
@ -282,14 +282,17 @@ impl FilterController {
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(filter_type) = &changeset.delete_filter {
|
||||
if let Some(filter) = self.filter_from_filter_id(&filter_type.filter_id).await {
|
||||
if let Some(filter_context) = &changeset.delete_filter {
|
||||
if let Some(filter) = self.filter_from_filter_id(&filter_context.filter_id).await {
|
||||
notification = Some(FilterChangesetNotificationPB::from_delete(
|
||||
&self.view_id,
|
||||
vec![filter],
|
||||
));
|
||||
}
|
||||
self.cell_filter_cache.write().remove(&filter_type.field_id);
|
||||
self
|
||||
.cell_filter_cache
|
||||
.write()
|
||||
.remove(&filter_context.field_id);
|
||||
}
|
||||
|
||||
self
|
||||
|
@ -3,7 +3,7 @@ use collab::core::any_map::AnyMapExtension;
|
||||
use collab_database::rows::RowId;
|
||||
use collab_database::views::{FilterMap, FilterMapBuilder};
|
||||
|
||||
use crate::entities::{DeleteFilterParams, FieldType, FilterPB, InsertedRowPB};
|
||||
use crate::entities::{FieldType, FilterPB, InsertedRowPB};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Filter {
|
||||
@ -63,66 +63,56 @@ impl TryFrom<FilterMap> for Filter {
|
||||
}
|
||||
#[derive(Debug)]
|
||||
pub struct FilterChangeset {
|
||||
pub(crate) insert_filter: Option<FilterType>,
|
||||
pub(crate) update_filter: Option<UpdatedFilterType>,
|
||||
pub(crate) delete_filter: Option<FilterType>,
|
||||
pub(crate) insert_filter: Option<Filter>,
|
||||
pub(crate) update_filter: Option<UpdatedFilter>,
|
||||
pub(crate) delete_filter: Option<FilterContext>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct UpdatedFilterType {
|
||||
pub old: Option<FilterType>,
|
||||
pub new: FilterType,
|
||||
pub struct UpdatedFilter {
|
||||
pub old: Option<Filter>,
|
||||
pub new: Filter,
|
||||
}
|
||||
|
||||
impl UpdatedFilterType {
|
||||
pub fn new(old: Option<FilterType>, new: FilterType) -> UpdatedFilterType {
|
||||
impl UpdatedFilter {
|
||||
pub fn new(old: Option<Filter>, new: Filter) -> UpdatedFilter {
|
||||
Self { old, new }
|
||||
}
|
||||
}
|
||||
|
||||
impl FilterChangeset {
|
||||
pub fn from_insert(filter_type: FilterType) -> Self {
|
||||
pub fn from_insert(filter: Filter) -> Self {
|
||||
Self {
|
||||
insert_filter: Some(filter_type),
|
||||
insert_filter: Some(filter),
|
||||
update_filter: None,
|
||||
delete_filter: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_update(filter_type: UpdatedFilterType) -> Self {
|
||||
pub fn from_update(filter: UpdatedFilter) -> Self {
|
||||
Self {
|
||||
insert_filter: None,
|
||||
update_filter: Some(filter_type),
|
||||
update_filter: Some(filter),
|
||||
delete_filter: None,
|
||||
}
|
||||
}
|
||||
pub fn from_delete(filter_type: FilterType) -> Self {
|
||||
pub fn from_delete(filter_context: FilterContext) -> Self {
|
||||
Self {
|
||||
insert_filter: None,
|
||||
update_filter: None,
|
||||
delete_filter: Some(filter_type),
|
||||
delete_filter: Some(filter_context),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Hash, Eq, PartialEq, Debug, Clone)]
|
||||
pub struct FilterType {
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct FilterContext {
|
||||
pub filter_id: String,
|
||||
pub field_id: String,
|
||||
pub field_type: FieldType,
|
||||
}
|
||||
|
||||
impl std::convert::From<&Filter> for FilterType {
|
||||
fn from(filter: &Filter) -> Self {
|
||||
Self {
|
||||
filter_id: filter.id.clone(),
|
||||
field_id: filter.field_id.clone(),
|
||||
field_type: filter.field_type,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::convert::From<&FilterPB> for FilterType {
|
||||
impl From<&FilterPB> for FilterContext {
|
||||
fn from(filter: &FilterPB) -> Self {
|
||||
Self {
|
||||
filter_id: filter.id.clone(),
|
||||
@ -132,29 +122,6 @@ impl std::convert::From<&FilterPB> for FilterType {
|
||||
}
|
||||
}
|
||||
|
||||
// #[derive(Hash, Eq, PartialEq, Debug, Clone)]
|
||||
// pub struct InsertedFilterType {
|
||||
// pub field_id: String,
|
||||
// pub filter_id: Option<String>,
|
||||
// pub field_type: FieldType,
|
||||
// }
|
||||
//
|
||||
// impl std::convert::From<&Filter> for InsertedFilterType {
|
||||
// fn from(params: &Filter) -> Self {
|
||||
// Self {
|
||||
// field_id: params.field_id.clone(),
|
||||
// filter_id: Some(params.id.clone()),
|
||||
// field_type: params.field_type.clone(),
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
impl std::convert::From<&DeleteFilterParams> for FilterType {
|
||||
fn from(params: &DeleteFilterParams) -> Self {
|
||||
params.filter_type.clone()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct FilterResultNotification {
|
||||
pub view_id: String,
|
||||
|
@ -179,39 +179,25 @@ impl SortController {
|
||||
}
|
||||
|
||||
#[tracing::instrument(level = "trace", skip(self))]
|
||||
pub async fn did_receive_changes(
|
||||
&mut self,
|
||||
changeset: SortChangeset,
|
||||
) -> SortChangesetNotificationPB {
|
||||
pub async fn apply_changeset(&mut self, changeset: SortChangeset) -> SortChangesetNotificationPB {
|
||||
let mut notification = SortChangesetNotificationPB::new(self.view_id.clone());
|
||||
|
||||
if let Some(insert_sort) = changeset.insert_sort {
|
||||
if let Some(sort) = self
|
||||
.delegate
|
||||
.get_sort(&self.view_id, &insert_sort.sort_id)
|
||||
.await
|
||||
{
|
||||
if let Some(sort) = self.delegate.get_sort(&self.view_id, &insert_sort.id).await {
|
||||
notification.insert_sorts.push(sort.as_ref().into());
|
||||
self.sorts.push(sort);
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(delete_sort_type) = changeset.delete_sort {
|
||||
if let Some(index) = self
|
||||
.sorts
|
||||
.iter()
|
||||
.position(|sort| sort.id == delete_sort_type.sort_id)
|
||||
{
|
||||
if let Some(sort_id) = changeset.delete_sort {
|
||||
if let Some(index) = self.sorts.iter().position(|sort| sort.id == sort_id) {
|
||||
let sort = self.sorts.remove(index);
|
||||
notification.delete_sorts.push(sort.as_ref().into());
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(update_sort) = changeset.update_sort {
|
||||
if let Some(updated_sort) = self
|
||||
.delegate
|
||||
.get_sort(&self.view_id, &update_sort.sort_id)
|
||||
.await
|
||||
{
|
||||
if let Some(updated_sort) = self.delegate.get_sort(&self.view_id, &update_sort.id).await {
|
||||
notification.update_sorts.push(updated_sort.as_ref().into());
|
||||
if let Some(index) = self
|
||||
.sorts
|
||||
|
@ -5,7 +5,7 @@ use collab::core::any_map::AnyMapExtension;
|
||||
use collab_database::rows::RowId;
|
||||
use collab_database::views::{SortMap, SortMapBuilder};
|
||||
|
||||
use crate::entities::{DeleteSortParams, FieldType};
|
||||
use crate::entities::FieldType;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Sort {
|
||||
@ -98,23 +98,6 @@ impl From<i64> for SortCondition {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Hash, Eq, PartialEq, Debug, Clone)]
|
||||
pub struct SortType {
|
||||
pub sort_id: String,
|
||||
pub field_id: String,
|
||||
pub field_type: FieldType,
|
||||
}
|
||||
|
||||
impl From<&Sort> for SortType {
|
||||
fn from(data: &Sort) -> Self {
|
||||
Self {
|
||||
sort_id: data.id.clone(),
|
||||
field_id: data.field_id.clone(),
|
||||
field_type: data.field_type,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct ReorderAllRowsResult {
|
||||
pub view_id: String,
|
||||
@ -140,13 +123,13 @@ pub struct ReorderSingleRowResult {
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct SortChangeset {
|
||||
pub(crate) insert_sort: Option<SortType>,
|
||||
pub(crate) update_sort: Option<SortType>,
|
||||
pub(crate) delete_sort: Option<DeletedSortType>,
|
||||
pub(crate) insert_sort: Option<Sort>,
|
||||
pub(crate) update_sort: Option<Sort>,
|
||||
pub(crate) delete_sort: Option<String>,
|
||||
}
|
||||
|
||||
impl SortChangeset {
|
||||
pub fn from_insert(sort: SortType) -> Self {
|
||||
pub fn from_insert(sort: Sort) -> Self {
|
||||
Self {
|
||||
insert_sort: Some(sort),
|
||||
update_sort: None,
|
||||
@ -154,7 +137,7 @@ impl SortChangeset {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_update(sort: SortType) -> Self {
|
||||
pub fn from_update(sort: Sort) -> Self {
|
||||
Self {
|
||||
insert_sort: None,
|
||||
update_sort: Some(sort),
|
||||
@ -162,26 +145,11 @@ impl SortChangeset {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_delete(deleted_sort: DeletedSortType) -> Self {
|
||||
pub fn from_delete(sort_id: String) -> Self {
|
||||
Self {
|
||||
insert_sort: None,
|
||||
update_sort: None,
|
||||
delete_sort: Some(deleted_sort),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct DeletedSortType {
|
||||
pub sort_type: SortType,
|
||||
pub sort_id: String,
|
||||
}
|
||||
|
||||
impl std::convert::From<DeleteSortParams> for DeletedSortType {
|
||||
fn from(params: DeleteSortParams) -> Self {
|
||||
Self {
|
||||
sort_type: params.sort_type,
|
||||
sort_id: params.sort_id,
|
||||
delete_sort: Some(sort_id),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user