2022-11-08 13:51:12 +08:00
|
|
|
use crate::entities::parser::NotEmptyStr;
|
2022-06-21 16:56:50 +08:00
|
|
|
use crate::entities::{
|
2022-11-13 22:23:57 +08:00
|
|
|
CreateFilterParams, CreateFilterPayloadPB, DeleteFilterParams, DeleteFilterPayloadPB, DeleteGroupParams,
|
|
|
|
DeleteGroupPayloadPB, InsertGroupParams, InsertGroupPayloadPB, RepeatedGridFilterConfigurationPB,
|
2022-08-20 15:40:13 +08:00
|
|
|
RepeatedGridGroupConfigurationPB,
|
2022-06-21 16:56:50 +08:00
|
|
|
};
|
2022-06-19 21:10:07 +08:00
|
|
|
use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
|
2022-07-01 20:32:11 +08:00
|
|
|
use flowy_error::ErrorCode;
|
2022-11-08 13:51:12 +08:00
|
|
|
use grid_rev_model::LayoutRevision;
|
2022-06-15 17:24:46 +08:00
|
|
|
use std::convert::TryInto;
|
2022-07-10 17:06:36 +08:00
|
|
|
use strum::IntoEnumIterator;
|
|
|
|
use strum_macros::EnumIter;
|
2022-06-15 17:24:46 +08:00
|
|
|
|
2022-07-25 12:45:35 +08:00
|
|
|
/// [GridSettingPB] defines the setting options for the grid. Such as the filter, group, and sort.
|
2022-06-15 17:24:46 +08:00
|
|
|
#[derive(Eq, PartialEq, ProtoBuf, Debug, Default, Clone)]
|
2022-07-17 13:38:53 +08:00
|
|
|
pub struct GridSettingPB {
|
2022-06-15 17:24:46 +08:00
|
|
|
#[pb(index = 1)]
|
2022-07-17 13:38:53 +08:00
|
|
|
pub layouts: Vec<GridLayoutPB>,
|
2022-06-15 17:24:46 +08:00
|
|
|
|
|
|
|
#[pb(index = 2)]
|
2022-09-03 16:47:58 +08:00
|
|
|
pub layout_type: GridLayout,
|
2022-06-15 17:24:46 +08:00
|
|
|
|
|
|
|
#[pb(index = 3)]
|
2022-09-03 16:47:58 +08:00
|
|
|
pub filter_configurations: RepeatedGridFilterConfigurationPB,
|
2022-07-10 17:06:36 +08:00
|
|
|
|
|
|
|
#[pb(index = 4)]
|
2022-09-02 22:13:38 +08:00
|
|
|
pub group_configurations: RepeatedGridGroupConfigurationPB,
|
2022-07-10 17:06:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Eq, PartialEq, ProtoBuf, Debug, Default, Clone)]
|
2022-07-17 13:38:53 +08:00
|
|
|
pub struct GridLayoutPB {
|
2022-07-10 17:06:36 +08:00
|
|
|
#[pb(index = 1)]
|
2022-08-16 15:49:54 +08:00
|
|
|
ty: GridLayout,
|
2022-07-10 17:06:36 +08:00
|
|
|
}
|
|
|
|
|
2022-07-17 13:38:53 +08:00
|
|
|
impl GridLayoutPB {
|
|
|
|
pub fn all() -> Vec<GridLayoutPB> {
|
2022-07-10 17:06:36 +08:00
|
|
|
let mut layouts = vec![];
|
2022-08-16 15:49:54 +08:00
|
|
|
for layout_ty in GridLayout::iter() {
|
2022-07-17 13:38:53 +08:00
|
|
|
layouts.push(GridLayoutPB { ty: layout_ty })
|
2022-07-10 17:06:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
layouts
|
|
|
|
}
|
2022-06-21 16:56:50 +08:00
|
|
|
}
|
|
|
|
|
2022-07-10 17:06:36 +08:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, ProtoBuf_Enum, EnumIter)]
|
2022-06-19 21:10:07 +08:00
|
|
|
#[repr(u8)]
|
2022-08-16 15:49:54 +08:00
|
|
|
pub enum GridLayout {
|
2022-06-19 21:10:07 +08:00
|
|
|
Table = 0,
|
|
|
|
Board = 1,
|
|
|
|
}
|
|
|
|
|
2022-08-16 15:49:54 +08:00
|
|
|
impl std::default::Default for GridLayout {
|
2022-06-19 21:10:07 +08:00
|
|
|
fn default() -> Self {
|
2022-08-16 15:49:54 +08:00
|
|
|
GridLayout::Table
|
2022-06-19 21:10:07 +08:00
|
|
|
}
|
2022-06-15 17:24:46 +08:00
|
|
|
}
|
|
|
|
|
2022-08-16 15:49:54 +08:00
|
|
|
impl std::convert::From<LayoutRevision> for GridLayout {
|
2022-08-14 21:09:18 +08:00
|
|
|
fn from(rev: LayoutRevision) -> Self {
|
2022-06-21 16:56:50 +08:00
|
|
|
match rev {
|
2022-08-16 15:49:54 +08:00
|
|
|
LayoutRevision::Table => GridLayout::Table,
|
|
|
|
LayoutRevision::Board => GridLayout::Board,
|
2022-06-21 16:56:50 +08:00
|
|
|
}
|
|
|
|
}
|
2022-06-15 17:24:46 +08:00
|
|
|
}
|
|
|
|
|
2022-08-16 15:49:54 +08:00
|
|
|
impl std::convert::From<GridLayout> for LayoutRevision {
|
|
|
|
fn from(layout: GridLayout) -> Self {
|
2022-06-21 16:56:50 +08:00
|
|
|
match layout {
|
2022-08-16 15:49:54 +08:00
|
|
|
GridLayout::Table => LayoutRevision::Table,
|
|
|
|
GridLayout::Board => LayoutRevision::Board,
|
2022-06-21 16:56:50 +08:00
|
|
|
}
|
|
|
|
}
|
2022-06-15 17:24:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default, ProtoBuf)]
|
2022-07-17 13:38:53 +08:00
|
|
|
pub struct GridSettingChangesetPayloadPB {
|
2022-06-15 17:24:46 +08:00
|
|
|
#[pb(index = 1)]
|
|
|
|
pub grid_id: String,
|
|
|
|
|
2022-06-19 21:10:07 +08:00
|
|
|
#[pb(index = 2)]
|
2022-08-16 15:49:54 +08:00
|
|
|
pub layout_type: GridLayout,
|
2022-06-15 17:24:46 +08:00
|
|
|
|
|
|
|
#[pb(index = 3, one_of)]
|
2022-11-13 22:23:57 +08:00
|
|
|
pub insert_filter: Option<CreateFilterPayloadPB>,
|
2022-06-15 17:24:46 +08:00
|
|
|
|
|
|
|
#[pb(index = 4, one_of)]
|
2022-07-17 13:38:53 +08:00
|
|
|
pub delete_filter: Option<DeleteFilterPayloadPB>,
|
2022-06-19 21:10:07 +08:00
|
|
|
|
|
|
|
#[pb(index = 5, one_of)]
|
2022-09-04 15:33:07 +08:00
|
|
|
pub insert_group: Option<InsertGroupPayloadPB>,
|
2022-06-21 16:56:50 +08:00
|
|
|
|
|
|
|
#[pb(index = 6, one_of)]
|
2022-08-11 10:08:42 +08:00
|
|
|
pub delete_group: Option<DeleteGroupPayloadPB>,
|
2022-06-15 17:24:46 +08:00
|
|
|
}
|
|
|
|
|
2022-07-17 13:38:53 +08:00
|
|
|
impl TryInto<GridSettingChangesetParams> for GridSettingChangesetPayloadPB {
|
2022-06-15 17:24:46 +08:00
|
|
|
type Error = ErrorCode;
|
|
|
|
|
2022-06-19 21:10:07 +08:00
|
|
|
fn try_into(self) -> Result<GridSettingChangesetParams, Self::Error> {
|
2022-06-15 17:24:46 +08:00
|
|
|
let view_id = NotEmptyStr::parse(self.grid_id)
|
2022-09-04 15:33:07 +08:00
|
|
|
.map_err(|_| ErrorCode::ViewIdInvalid)?
|
2022-06-15 17:24:46 +08:00
|
|
|
.0;
|
|
|
|
|
2022-06-21 16:56:50 +08:00
|
|
|
let insert_filter = match self.insert_filter {
|
|
|
|
None => None,
|
|
|
|
Some(payload) => Some(payload.try_into()?),
|
|
|
|
};
|
|
|
|
|
|
|
|
let delete_filter = match self.delete_filter {
|
|
|
|
None => None,
|
2022-06-30 23:00:03 +08:00
|
|
|
Some(payload) => Some(payload.try_into()?),
|
2022-06-21 16:56:50 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
let insert_group = match self.insert_group {
|
|
|
|
Some(payload) => Some(payload.try_into()?),
|
|
|
|
None => None,
|
|
|
|
};
|
|
|
|
|
|
|
|
let delete_group = match self.delete_group {
|
2022-08-11 10:08:42 +08:00
|
|
|
Some(payload) => Some(payload.try_into()?),
|
2022-06-15 17:24:46 +08:00
|
|
|
None => None,
|
|
|
|
};
|
|
|
|
|
2022-06-19 21:10:07 +08:00
|
|
|
Ok(GridSettingChangesetParams {
|
2022-06-20 09:37:52 +08:00
|
|
|
grid_id: view_id,
|
2022-07-01 20:32:11 +08:00
|
|
|
layout_type: self.layout_type.into(),
|
2022-06-21 16:56:50 +08:00
|
|
|
insert_filter,
|
|
|
|
delete_filter,
|
|
|
|
insert_group,
|
|
|
|
delete_group,
|
2022-06-15 17:24:46 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-08-19 19:59:09 +08:00
|
|
|
|
|
|
|
pub struct GridSettingChangesetParams {
|
|
|
|
pub grid_id: String,
|
|
|
|
pub layout_type: LayoutRevision,
|
2022-11-13 22:23:57 +08:00
|
|
|
pub insert_filter: Option<CreateFilterParams>,
|
2022-08-19 19:59:09 +08:00
|
|
|
pub delete_filter: Option<DeleteFilterParams>,
|
2022-09-04 15:33:07 +08:00
|
|
|
pub insert_group: Option<InsertGroupParams>,
|
2022-08-19 19:59:09 +08:00
|
|
|
pub delete_group: Option<DeleteGroupParams>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl GridSettingChangesetParams {
|
|
|
|
pub fn is_filter_changed(&self) -> bool {
|
|
|
|
self.insert_filter.is_some() || self.delete_filter.is_some()
|
|
|
|
}
|
|
|
|
}
|