178 lines
4.9 KiB
Rust
Raw Normal View History

use crate::entities::parser::NotEmptyStr;
2022-06-21 16:56:50 +08:00
use crate::entities::{
2022-12-12 10:52:14 +08:00
AlterFilterParams, AlterFilterPayloadPB, AlterSortParams, AlterSortPayloadPB, DeleteFilterParams,
DeleteFilterPayloadPB, DeleteGroupParams, DeleteGroupPayloadPB, DeleteSortParams, DeleteSortPayloadPB,
InsertGroupParams, InsertGroupPayloadPB, RepeatedFilterPB, RepeatedGroupConfigurationPB, RepeatedSortPB,
2022-06-21 16:56:50 +08:00
};
2022-06-19 21:10:07 +08:00
use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
use flowy_error::ErrorCode;
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
/// [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)]
pub filters: RepeatedFilterPB,
2022-07-10 17:06:36 +08:00
#[pb(index = 4)]
pub group_configurations: RepeatedGroupConfigurationPB,
#[pb(index = 5)]
pub sorts: RepeatedSortPB,
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,
Calendar = 2,
2022-06-19 21:10:07 +08:00
}
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,
LayoutRevision::Calendar => GridLayout::Calendar,
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,
GridLayout::Calendar => LayoutRevision::Calendar,
2022-06-21 16:56:50 +08:00
}
}
2022-06-15 17:24:46 +08:00
}
#[derive(Default, ProtoBuf)]
2022-11-14 09:59:23 +08:00
pub struct GridSettingChangesetPB {
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-12-12 10:52:14 +08:00
pub alter_filter: Option<AlterFilterPayloadPB>,
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)]
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-12-12 10:52:14 +08:00
#[pb(index = 7, one_of)]
pub alter_sort: Option<AlterSortPayloadPB>,
#[pb(index = 8, one_of)]
pub delete_sort: Option<DeleteSortPayloadPB>,
2022-06-15 17:24:46 +08:00
}
2022-11-14 09:59:23 +08:00
impl TryInto<GridSettingChangesetParams> for GridSettingChangesetPB {
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)
.map_err(|_| ErrorCode::ViewIdInvalid)?
2022-06-15 17:24:46 +08:00
.0;
2022-12-12 10:52:14 +08:00
let insert_filter = match self.alter_filter {
2022-06-21 16:56:50 +08:00
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-12-12 10:52:14 +08:00
let alert_sort = match self.alter_sort {
None => None,
Some(payload) => Some(payload.try_into()?),
};
let delete_sort = match self.delete_sort {
None => None,
Some(payload) => Some(payload.try_into()?),
};
2022-06-19 21:10:07 +08:00
Ok(GridSettingChangesetParams {
2022-06-20 09:37:52 +08:00
grid_id: view_id,
layout_type: self.layout_type.into(),
2022-06-21 16:56:50 +08:00
insert_filter,
delete_filter,
insert_group,
delete_group,
2022-12-12 10:52:14 +08:00
alert_sort,
delete_sort,
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,
pub insert_filter: Option<AlterFilterParams>,
2022-08-19 19:59:09 +08:00
pub delete_filter: Option<DeleteFilterParams>,
pub insert_group: Option<InsertGroupParams>,
2022-08-19 19:59:09 +08:00
pub delete_group: Option<DeleteGroupParams>,
2022-12-12 10:52:14 +08:00
pub alert_sort: Option<AlterSortParams>,
pub delete_sort: Option<DeleteSortParams>,
2022-08-19 19:59:09 +08:00
}
impl GridSettingChangesetParams {
pub fn is_filter_changed(&self) -> bool {
self.insert_filter.is_some() || self.delete_filter.is_some()
}
}