2022-08-20 16:44:44 +08:00
|
|
|
use crate::services::group::Group;
|
2022-08-20 15:40:13 +08:00
|
|
|
use flowy_error::FlowyResult;
|
|
|
|
use flowy_grid_data_model::revision::{
|
|
|
|
FieldRevision, FieldTypeRevision, GroupConfigurationContentSerde, GroupConfigurationRevision, GroupRecordRevision,
|
|
|
|
};
|
|
|
|
use lib_infra::future::AFFuture;
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
pub trait GroupConfigurationReader: Send + Sync + 'static {
|
|
|
|
fn get_group_configuration(&self, field_rev: Arc<FieldRevision>) -> AFFuture<Arc<GroupConfigurationRevision>>;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait GroupConfigurationWriter: Send + Sync + 'static {
|
|
|
|
fn save_group_configuration(
|
|
|
|
&self,
|
|
|
|
field_id: &str,
|
|
|
|
field_type: FieldTypeRevision,
|
2022-08-20 16:44:44 +08:00
|
|
|
configuration: GroupConfigurationRevision,
|
2022-08-20 15:40:13 +08:00
|
|
|
) -> AFFuture<FlowyResult<()>>;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait GroupConfigurationAction: Send + Sync {
|
|
|
|
fn group_records(&self) -> &[GroupRecordRevision];
|
2022-08-20 16:44:44 +08:00
|
|
|
fn merge_groups(&self, groups: Vec<Group>) -> FlowyResult<()>;
|
2022-08-20 15:40:13 +08:00
|
|
|
fn hide_group(&self, group_id: &str) -> FlowyResult<()>;
|
|
|
|
fn show_group(&self, group_id: &str) -> FlowyResult<()>;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct GenericGroupConfiguration<C> {
|
|
|
|
field_rev: Arc<FieldRevision>,
|
|
|
|
reader: Arc<dyn GroupConfigurationReader>,
|
2022-08-20 16:44:44 +08:00
|
|
|
configuration_rev: Arc<GroupConfigurationRevision>,
|
2022-08-20 15:40:13 +08:00
|
|
|
writer: Arc<dyn GroupConfigurationWriter>,
|
2022-08-20 16:44:44 +08:00
|
|
|
pub(crate) configuration: C,
|
2022-08-20 15:40:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<C> GenericGroupConfiguration<C>
|
|
|
|
where
|
|
|
|
C: GroupConfigurationContentSerde,
|
|
|
|
{
|
|
|
|
pub async fn new(
|
|
|
|
field_rev: Arc<FieldRevision>,
|
|
|
|
reader: Arc<dyn GroupConfigurationReader>,
|
|
|
|
writer: Arc<dyn GroupConfigurationWriter>,
|
|
|
|
) -> FlowyResult<Self> {
|
2022-08-20 16:44:44 +08:00
|
|
|
let configuration_rev = reader.get_group_configuration(field_rev.clone()).await;
|
|
|
|
let configuration = C::from_configuration_content(&configuration.content)?;
|
2022-08-20 15:40:13 +08:00
|
|
|
Ok(Self {
|
|
|
|
field_rev,
|
2022-08-20 16:44:44 +08:00
|
|
|
configuration_rev,
|
2022-08-20 15:40:13 +08:00
|
|
|
reader,
|
|
|
|
writer,
|
|
|
|
configuration,
|
|
|
|
})
|
|
|
|
}
|
2022-08-20 16:44:44 +08:00
|
|
|
|
|
|
|
pub async fn save_configuration(&self) {}
|
2022-08-20 15:40:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> GroupConfigurationReader for Arc<T>
|
|
|
|
where
|
|
|
|
T: GroupConfigurationReader,
|
|
|
|
{
|
|
|
|
fn get_group_configuration(&self, field_rev: Arc<FieldRevision>) -> AFFuture<Arc<GroupConfigurationRevision>> {
|
|
|
|
(**self).get_group_configuration(field_rev)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> GroupConfigurationWriter for Arc<T>
|
|
|
|
where
|
|
|
|
T: GroupConfigurationWriter,
|
|
|
|
{
|
|
|
|
fn save_group_configuration(
|
|
|
|
&self,
|
|
|
|
field_id: &str,
|
|
|
|
field_type: FieldTypeRevision,
|
2022-08-20 16:44:44 +08:00
|
|
|
configuration: GroupConfigurationRevision,
|
2022-08-20 15:40:13 +08:00
|
|
|
) -> AFFuture<FlowyResult<()>> {
|
|
|
|
(**self).save_group_configuration(field_id, field_type, configuration)
|
|
|
|
}
|
|
|
|
}
|