2022-08-17 08:14:25 +08:00
|
|
|
use crate::dart_notification::{send_dart_notification, GridNotification};
|
2022-08-16 15:49:54 +08:00
|
|
|
use crate::entities::{
|
2022-09-04 15:33:07 +08:00
|
|
|
CreateRowParams, DeleteFilterParams, DeleteGroupParams, GridFilterConfigurationPB, GridGroupConfigurationPB,
|
|
|
|
GridLayout, GridLayoutPB, GridSettingPB, GroupChangesetPB, GroupPB, GroupViewChangesetPB, InsertFilterParams,
|
|
|
|
InsertGroupParams, InsertedGroupPB, InsertedRowPB, MoveGroupParams, RepeatedGridFilterConfigurationPB,
|
|
|
|
RepeatedGridGroupConfigurationPB, RowPB,
|
2022-08-16 15:49:54 +08:00
|
|
|
};
|
2022-08-15 20:07:01 +08:00
|
|
|
use crate::services::grid_editor_task::GridServiceTaskScheduler;
|
2022-08-17 08:14:25 +08:00
|
|
|
use crate::services::grid_view_manager::{GridViewFieldDelegate, GridViewRowDelegate};
|
2022-09-01 09:03:33 +08:00
|
|
|
use crate::services::group::{
|
2022-09-04 15:33:07 +08:00
|
|
|
default_group_configuration, find_group_field, make_group_controller, GroupConfigurationReader,
|
|
|
|
GroupConfigurationWriter, GroupController, MoveGroupRowContext,
|
2022-09-01 09:03:33 +08:00
|
|
|
};
|
2022-08-17 08:14:25 +08:00
|
|
|
use flowy_error::{FlowyError, FlowyResult};
|
2022-08-20 15:40:13 +08:00
|
|
|
use flowy_grid_data_model::revision::{
|
2022-08-21 13:56:06 +08:00
|
|
|
gen_grid_filter_id, FieldRevision, FieldTypeRevision, FilterConfigurationRevision, GroupConfigurationRevision,
|
|
|
|
RowChangeset, RowRevision,
|
2022-08-20 15:40:13 +08:00
|
|
|
};
|
2022-08-14 23:01:53 +08:00
|
|
|
use flowy_revision::{RevisionCloudService, RevisionManager, RevisionObjectBuilder};
|
2022-08-15 20:07:01 +08:00
|
|
|
use flowy_sync::client_grid::{GridViewRevisionChangeset, GridViewRevisionPad};
|
2022-08-17 08:14:25 +08:00
|
|
|
use flowy_sync::entities::revision::Revision;
|
2022-08-15 20:07:01 +08:00
|
|
|
use lib_infra::future::{wrap_future, AFFuture, FutureResult};
|
2022-09-01 09:03:33 +08:00
|
|
|
use std::future::Future;
|
2022-08-14 23:01:53 +08:00
|
|
|
use std::sync::Arc;
|
|
|
|
use tokio::sync::RwLock;
|
|
|
|
|
2022-08-14 23:11:30 +08:00
|
|
|
#[allow(dead_code)]
|
2022-08-14 23:01:53 +08:00
|
|
|
pub struct GridViewRevisionEditor {
|
2022-08-15 20:07:01 +08:00
|
|
|
user_id: String,
|
|
|
|
view_id: String,
|
2022-08-14 23:01:53 +08:00
|
|
|
pad: Arc<RwLock<GridViewRevisionPad>>,
|
|
|
|
rev_manager: Arc<RevisionManager>,
|
2022-08-17 08:14:25 +08:00
|
|
|
field_delegate: Arc<dyn GridViewFieldDelegate>,
|
|
|
|
row_delegate: Arc<dyn GridViewRowDelegate>,
|
2022-09-01 09:03:33 +08:00
|
|
|
group_controller: Arc<RwLock<Box<dyn GroupController>>>,
|
2022-08-15 20:07:01 +08:00
|
|
|
scheduler: Arc<dyn GridServiceTaskScheduler>,
|
2022-08-14 23:01:53 +08:00
|
|
|
}
|
|
|
|
impl GridViewRevisionEditor {
|
2022-08-21 22:47:24 +08:00
|
|
|
#[tracing::instrument(level = "trace", skip_all, err)]
|
2022-08-17 08:14:25 +08:00
|
|
|
pub(crate) async fn new(
|
2022-08-15 20:07:01 +08:00
|
|
|
user_id: &str,
|
|
|
|
token: &str,
|
|
|
|
view_id: String,
|
2022-08-17 08:14:25 +08:00
|
|
|
field_delegate: Arc<dyn GridViewFieldDelegate>,
|
|
|
|
row_delegate: Arc<dyn GridViewRowDelegate>,
|
2022-08-15 20:07:01 +08:00
|
|
|
scheduler: Arc<dyn GridServiceTaskScheduler>,
|
|
|
|
mut rev_manager: RevisionManager,
|
2022-08-17 08:14:25 +08:00
|
|
|
) -> FlowyResult<Self> {
|
2022-08-14 23:01:53 +08:00
|
|
|
let cloud = Arc::new(GridViewRevisionCloudService {
|
|
|
|
token: token.to_owned(),
|
|
|
|
});
|
|
|
|
let view_revision_pad = rev_manager.load::<GridViewRevisionPadBuilder>(Some(cloud)).await?;
|
|
|
|
let pad = Arc::new(RwLock::new(view_revision_pad));
|
|
|
|
let rev_manager = Arc::new(rev_manager);
|
2022-09-02 21:34:00 +08:00
|
|
|
let group_controller = new_group_controller(
|
|
|
|
user_id.to_owned(),
|
2022-09-01 09:03:33 +08:00
|
|
|
view_id.clone(),
|
2022-09-02 21:34:00 +08:00
|
|
|
pad.clone(),
|
|
|
|
rev_manager.clone(),
|
|
|
|
field_delegate.clone(),
|
|
|
|
row_delegate.clone(),
|
2022-09-01 09:03:33 +08:00
|
|
|
)
|
|
|
|
.await?;
|
2022-08-15 20:07:01 +08:00
|
|
|
let user_id = user_id.to_owned();
|
|
|
|
Ok(Self {
|
|
|
|
pad,
|
|
|
|
user_id,
|
|
|
|
view_id,
|
|
|
|
rev_manager,
|
|
|
|
scheduler,
|
2022-08-17 08:14:25 +08:00
|
|
|
field_delegate,
|
|
|
|
row_delegate,
|
2022-09-01 09:03:33 +08:00
|
|
|
group_controller: Arc::new(RwLock::new(group_controller)),
|
2022-08-15 20:07:01 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-08-17 16:48:16 +08:00
|
|
|
pub(crate) async fn will_create_row(&self, row_rev: &mut RowRevision, params: &CreateRowParams) {
|
2022-09-01 20:41:15 +08:00
|
|
|
if params.group_id.is_none() {
|
|
|
|
return;
|
2022-08-15 20:07:01 +08:00
|
|
|
}
|
2022-09-01 20:41:15 +08:00
|
|
|
let group_id = params.group_id.as_ref().unwrap();
|
|
|
|
let _ = self
|
|
|
|
.mut_group_controller(|group_controller, field_rev| {
|
|
|
|
group_controller.will_create_row(row_rev, &field_rev, group_id);
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
.await;
|
2022-08-15 20:07:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) async fn did_create_row(&self, row_pb: &RowPB, params: &CreateRowParams) {
|
2022-08-16 15:49:54 +08:00
|
|
|
// Send the group notification if the current view has groups
|
2022-08-15 20:07:01 +08:00
|
|
|
match params.group_id.as_ref() {
|
|
|
|
None => {}
|
|
|
|
Some(group_id) => {
|
2022-08-31 14:56:03 +08:00
|
|
|
let index = match params.start_row_id {
|
|
|
|
None => Some(0),
|
|
|
|
Some(_) => None,
|
|
|
|
};
|
2022-09-05 20:24:33 +08:00
|
|
|
|
|
|
|
self.group_controller.write().await.did_create_row(row_pb, group_id);
|
2022-08-16 17:13:56 +08:00
|
|
|
let inserted_row = InsertedRowPB {
|
|
|
|
row: row_pb.clone(),
|
2022-08-31 14:56:03 +08:00
|
|
|
index,
|
2022-08-29 16:32:20 +08:00
|
|
|
is_new: true,
|
2022-08-16 17:13:56 +08:00
|
|
|
};
|
2022-08-23 20:36:28 +08:00
|
|
|
let changeset = GroupChangesetPB::insert(group_id.clone(), vec![inserted_row]);
|
|
|
|
self.notify_did_update_group(changeset).await;
|
2022-08-15 20:07:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-16 20:00:51 +08:00
|
|
|
#[tracing::instrument(level = "trace", skip_all)]
|
2022-08-17 16:48:16 +08:00
|
|
|
pub(crate) async fn did_delete_row(&self, row_rev: &RowRevision) {
|
2022-08-16 15:49:54 +08:00
|
|
|
// Send the group notification if the current view has groups;
|
2022-09-01 20:41:15 +08:00
|
|
|
let changesets = self
|
|
|
|
.mut_group_controller(|group_controller, field_rev| group_controller.did_delete_row(row_rev, &field_rev))
|
|
|
|
.await;
|
2022-08-16 15:49:54 +08:00
|
|
|
|
2022-09-01 20:41:15 +08:00
|
|
|
if let Some(changesets) = changesets {
|
2022-09-16 20:00:51 +08:00
|
|
|
tracing::trace!("{:?}", changesets);
|
2022-08-17 14:33:45 +08:00
|
|
|
for changeset in changesets {
|
2022-08-23 20:36:28 +08:00
|
|
|
self.notify_did_update_group(changeset).await;
|
2022-08-17 08:14:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-01 20:41:15 +08:00
|
|
|
pub(crate) async fn did_update_row(&self, row_rev: &RowRevision) {
|
|
|
|
let changesets = self
|
|
|
|
.mut_group_controller(|group_controller, field_rev| group_controller.did_update_row(row_rev, &field_rev))
|
|
|
|
.await;
|
|
|
|
|
|
|
|
if let Some(changesets) = changesets {
|
|
|
|
for changeset in changesets {
|
|
|
|
self.notify_did_update_group(changeset).await;
|
2022-09-01 09:03:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-22 16:16:15 +08:00
|
|
|
pub(crate) async fn move_group_row(
|
2022-08-18 17:40:23 +08:00
|
|
|
&self,
|
|
|
|
row_rev: &RowRevision,
|
|
|
|
row_changeset: &mut RowChangeset,
|
2022-08-22 16:16:15 +08:00
|
|
|
to_group_id: &str,
|
|
|
|
to_row_id: Option<String>,
|
2022-08-26 16:07:51 +08:00
|
|
|
) -> Vec<GroupChangesetPB> {
|
2022-09-01 20:41:15 +08:00
|
|
|
let changesets = self
|
|
|
|
.mut_group_controller(|group_controller, field_rev| {
|
|
|
|
let move_row_context = MoveGroupRowContext {
|
|
|
|
row_rev,
|
|
|
|
row_changeset,
|
|
|
|
field_rev: field_rev.as_ref(),
|
|
|
|
to_group_id,
|
|
|
|
to_row_id,
|
|
|
|
};
|
|
|
|
|
|
|
|
let changesets = group_controller.move_group_row(move_row_context)?;
|
|
|
|
Ok(changesets)
|
2022-08-17 19:29:14 +08:00
|
|
|
})
|
2022-09-01 20:41:15 +08:00
|
|
|
.await;
|
|
|
|
|
|
|
|
changesets.unwrap_or_default()
|
2022-08-17 19:29:14 +08:00
|
|
|
}
|
2022-08-24 16:57:53 +08:00
|
|
|
/// Only call once after grid view editor initialized
|
2022-08-21 22:47:24 +08:00
|
|
|
#[tracing::instrument(level = "trace", skip(self))]
|
2022-08-17 08:14:25 +08:00
|
|
|
pub(crate) async fn load_groups(&self) -> FlowyResult<Vec<GroupPB>> {
|
2022-09-01 20:41:15 +08:00
|
|
|
let groups = self.group_controller.read().await.groups();
|
2022-08-21 22:47:24 +08:00
|
|
|
tracing::trace!("Number of groups: {}", groups.len());
|
2022-08-18 21:43:05 +08:00
|
|
|
Ok(groups.into_iter().map(GroupPB::from).collect())
|
2022-08-15 20:07:01 +08:00
|
|
|
}
|
|
|
|
|
2022-08-21 13:56:06 +08:00
|
|
|
pub(crate) async fn move_group(&self, params: MoveGroupParams) -> FlowyResult<()> {
|
2022-08-21 22:47:24 +08:00
|
|
|
let _ = self
|
2022-09-01 20:41:15 +08:00
|
|
|
.group_controller
|
2022-08-22 16:16:15 +08:00
|
|
|
.write()
|
2022-08-21 22:47:24 +08:00
|
|
|
.await
|
2022-09-01 20:41:15 +08:00
|
|
|
.move_group(¶ms.from_group_id, ¶ms.to_group_id)?;
|
|
|
|
match self.group_controller.read().await.get_group(¶ms.from_group_id) {
|
2022-08-21 22:47:24 +08:00
|
|
|
None => {}
|
|
|
|
Some((index, group)) => {
|
|
|
|
let inserted_group = InsertedGroupPB {
|
|
|
|
group: GroupPB::from(group),
|
|
|
|
index: index as i32,
|
|
|
|
};
|
|
|
|
|
|
|
|
let changeset = GroupViewChangesetPB {
|
2022-08-24 16:57:53 +08:00
|
|
|
view_id: self.view_id.clone(),
|
2022-08-21 22:47:24 +08:00
|
|
|
inserted_groups: vec![inserted_group],
|
|
|
|
deleted_groups: vec![params.from_group_id.clone()],
|
2022-08-24 16:57:53 +08:00
|
|
|
update_groups: vec![],
|
2022-09-01 20:41:15 +08:00
|
|
|
new_groups: vec![],
|
2022-08-21 22:47:24 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
self.notify_did_update_view(changeset).await;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
2022-08-21 13:56:06 +08:00
|
|
|
}
|
|
|
|
|
2022-08-15 20:07:01 +08:00
|
|
|
pub(crate) async fn get_setting(&self) -> GridSettingPB {
|
2022-08-17 08:14:25 +08:00
|
|
|
let field_revs = self.field_delegate.get_field_revs().await;
|
2022-08-20 15:40:13 +08:00
|
|
|
let grid_setting = make_grid_setting(&*self.pad.read().await, &field_revs);
|
2022-08-15 20:07:01 +08:00
|
|
|
grid_setting
|
|
|
|
}
|
|
|
|
|
2022-09-03 16:47:58 +08:00
|
|
|
pub(crate) async fn get_filters(&self) -> Vec<GridFilterConfigurationPB> {
|
2022-08-17 08:14:25 +08:00
|
|
|
let field_revs = self.field_delegate.get_field_revs().await;
|
2022-08-20 15:40:13 +08:00
|
|
|
match self.pad.read().await.get_all_filters(&field_revs) {
|
2022-08-15 20:07:01 +08:00
|
|
|
None => vec![],
|
|
|
|
Some(filters) => filters
|
|
|
|
.into_values()
|
|
|
|
.flatten()
|
2022-09-03 16:47:58 +08:00
|
|
|
.map(|filter| GridFilterConfigurationPB::from(filter.as_ref()))
|
2022-08-15 20:07:01 +08:00
|
|
|
.collect(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-04 15:33:07 +08:00
|
|
|
pub(crate) async fn insert_group(&self, params: InsertGroupParams) -> FlowyResult<()> {
|
|
|
|
if let Some(field_rev) = self.field_delegate.get_field_rev(¶ms.field_id).await {
|
|
|
|
let _ = self
|
|
|
|
.modify(|pad| {
|
|
|
|
let configuration = default_group_configuration(&field_rev);
|
|
|
|
let changeset = pad.insert_group(¶ms.field_id, ¶ms.field_type_rev, configuration)?;
|
|
|
|
Ok(changeset)
|
|
|
|
})
|
|
|
|
.await?;
|
|
|
|
}
|
|
|
|
if self.group_controller.read().await.field_id() != params.field_id {
|
|
|
|
let _ = self.group_by_field(¶ms.field_id).await?;
|
|
|
|
self.notify_did_update_setting().await;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) async fn delete_group(&self, params: DeleteGroupParams) -> FlowyResult<()> {
|
|
|
|
self.modify(|pad| {
|
|
|
|
let changeset = pad.delete_filter(¶ms.field_id, ¶ms.field_type_rev, ¶ms.group_id)?;
|
|
|
|
Ok(changeset)
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) async fn insert_filter(&self, params: InsertFilterParams) -> FlowyResult<()> {
|
2022-08-21 13:56:06 +08:00
|
|
|
self.modify(|pad| {
|
|
|
|
let filter_rev = FilterConfigurationRevision {
|
|
|
|
id: gen_grid_filter_id(),
|
2022-09-04 15:33:07 +08:00
|
|
|
field_id: params.field_id.clone(),
|
|
|
|
condition: params.condition,
|
|
|
|
content: params.content,
|
2022-08-21 13:56:06 +08:00
|
|
|
};
|
2022-09-04 15:33:07 +08:00
|
|
|
let changeset = pad.insert_filter(¶ms.field_id, ¶ms.field_type_rev, filter_rev)?;
|
2022-08-21 13:56:06 +08:00
|
|
|
Ok(changeset)
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) async fn delete_filter(&self, delete_filter: DeleteFilterParams) -> FlowyResult<()> {
|
|
|
|
self.modify(|pad| {
|
|
|
|
let changeset = pad.delete_filter(
|
|
|
|
&delete_filter.field_id,
|
|
|
|
&delete_filter.field_type_rev,
|
|
|
|
&delete_filter.filter_id,
|
|
|
|
)?;
|
|
|
|
Ok(changeset)
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
}
|
2022-08-24 16:57:53 +08:00
|
|
|
#[tracing::instrument(level = "trace", skip_all, err)]
|
2022-08-23 20:36:28 +08:00
|
|
|
pub(crate) async fn did_update_field(&self, field_id: &str) -> FlowyResult<()> {
|
2022-08-24 16:57:53 +08:00
|
|
|
if let Some(field_rev) = self.field_delegate.get_field_rev(field_id).await {
|
2022-09-01 20:41:15 +08:00
|
|
|
match self.group_controller.write().await.did_update_field(&field_rev)? {
|
2022-08-24 16:57:53 +08:00
|
|
|
None => {}
|
|
|
|
Some(changeset) => {
|
|
|
|
self.notify_did_update_view(changeset).await;
|
|
|
|
}
|
|
|
|
}
|
2022-08-23 20:36:28 +08:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-09-02 21:34:00 +08:00
|
|
|
pub(crate) async fn group_by_field(&self, field_id: &str) -> FlowyResult<()> {
|
|
|
|
if let Some(field_rev) = self.field_delegate.get_field_rev(field_id).await {
|
|
|
|
let new_group_controller = new_group_controller_with_field_rev(
|
|
|
|
self.user_id.clone(),
|
|
|
|
self.view_id.clone(),
|
|
|
|
self.pad.clone(),
|
|
|
|
self.rev_manager.clone(),
|
|
|
|
field_rev,
|
|
|
|
self.row_delegate.clone(),
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
let new_groups = new_group_controller.groups().into_iter().map(GroupPB::from).collect();
|
|
|
|
|
|
|
|
*self.group_controller.write().await = new_group_controller;
|
|
|
|
let changeset = GroupViewChangesetPB {
|
|
|
|
view_id: self.view_id.clone(),
|
|
|
|
new_groups,
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
|
|
|
|
debug_assert!(!changeset.is_empty());
|
|
|
|
if !changeset.is_empty() {
|
|
|
|
send_dart_notification(&changeset.view_id, GridNotification::DidGroupByNewField)
|
|
|
|
.payload(changeset)
|
|
|
|
.send();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-09-04 15:33:07 +08:00
|
|
|
async fn notify_did_update_setting(&self) {
|
|
|
|
let setting = self.get_setting().await;
|
|
|
|
send_dart_notification(&self.view_id, GridNotification::DidUpdateGridSetting)
|
|
|
|
.payload(setting)
|
|
|
|
.send();
|
|
|
|
}
|
|
|
|
|
2022-08-26 16:07:51 +08:00
|
|
|
pub async fn notify_did_update_group(&self, changeset: GroupChangesetPB) {
|
2022-08-16 17:13:56 +08:00
|
|
|
send_dart_notification(&changeset.group_id, GridNotification::DidUpdateGroup)
|
2022-08-16 15:49:54 +08:00
|
|
|
.payload(changeset)
|
|
|
|
.send();
|
|
|
|
}
|
|
|
|
|
2022-08-21 13:56:06 +08:00
|
|
|
async fn notify_did_update_view(&self, changeset: GroupViewChangesetPB) {
|
|
|
|
send_dart_notification(&self.view_id, GridNotification::DidUpdateGroupView)
|
|
|
|
.payload(changeset)
|
|
|
|
.send();
|
|
|
|
}
|
|
|
|
|
2022-08-15 20:07:01 +08:00
|
|
|
async fn modify<F>(&self, f: F) -> FlowyResult<()>
|
|
|
|
where
|
|
|
|
F: for<'a> FnOnce(&'a mut GridViewRevisionPad) -> FlowyResult<Option<GridViewRevisionChangeset>>,
|
|
|
|
{
|
|
|
|
let mut write_guard = self.pad.write().await;
|
|
|
|
match f(&mut *write_guard)? {
|
|
|
|
None => {}
|
|
|
|
Some(change) => {
|
2022-08-20 15:40:13 +08:00
|
|
|
let _ = apply_change(&self.user_id, self.rev_manager.clone(), change).await?;
|
2022-08-15 20:07:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2022-09-01 20:41:15 +08:00
|
|
|
|
|
|
|
async fn mut_group_controller<F, T>(&self, f: F) -> Option<T>
|
|
|
|
where
|
|
|
|
F: FnOnce(&mut Box<dyn GroupController>, Arc<FieldRevision>) -> FlowyResult<T>,
|
|
|
|
{
|
|
|
|
let group_field_id = self.group_controller.read().await.field_id().to_owned();
|
|
|
|
match self.field_delegate.get_field_rev(&group_field_id).await {
|
|
|
|
None => None,
|
|
|
|
Some(field_rev) => {
|
|
|
|
let mut write_guard = self.group_controller.write().await;
|
|
|
|
f(&mut write_guard, field_rev).ok()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
|
async fn async_mut_group_controller<F, O, T>(&self, f: F) -> Option<T>
|
|
|
|
where
|
|
|
|
F: FnOnce(Arc<RwLock<Box<dyn GroupController>>>, Arc<FieldRevision>) -> O,
|
|
|
|
O: Future<Output = FlowyResult<T>> + Sync + 'static,
|
|
|
|
{
|
|
|
|
let group_field_id = self.group_controller.read().await.field_id().to_owned();
|
|
|
|
match self.field_delegate.get_field_rev(&group_field_id).await {
|
|
|
|
None => None,
|
|
|
|
Some(field_rev) => {
|
|
|
|
let _write_guard = self.group_controller.write().await;
|
|
|
|
f(self.group_controller.clone(), field_rev).await.ok()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-08-20 15:40:13 +08:00
|
|
|
}
|
2022-09-02 21:34:00 +08:00
|
|
|
async fn new_group_controller(
|
|
|
|
user_id: String,
|
|
|
|
view_id: String,
|
|
|
|
pad: Arc<RwLock<GridViewRevisionPad>>,
|
|
|
|
rev_manager: Arc<RevisionManager>,
|
|
|
|
field_delegate: Arc<dyn GridViewFieldDelegate>,
|
|
|
|
row_delegate: Arc<dyn GridViewRowDelegate>,
|
|
|
|
) -> FlowyResult<Box<dyn GroupController>> {
|
|
|
|
let configuration_reader = GroupConfigurationReaderImpl(pad.clone());
|
|
|
|
let field_revs = field_delegate.get_field_revs().await;
|
|
|
|
// Read the group field or find a new group field
|
|
|
|
let field_rev = configuration_reader
|
|
|
|
.get_configuration()
|
|
|
|
.await
|
|
|
|
.and_then(|configuration| {
|
|
|
|
field_revs
|
|
|
|
.iter()
|
|
|
|
.find(|field_rev| field_rev.id == configuration.field_id)
|
|
|
|
.cloned()
|
|
|
|
})
|
|
|
|
.unwrap_or_else(|| find_group_field(&field_revs).unwrap());
|
|
|
|
|
|
|
|
new_group_controller_with_field_rev(user_id, view_id, pad, rev_manager, field_rev, row_delegate).await
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn new_group_controller_with_field_rev(
|
|
|
|
user_id: String,
|
|
|
|
view_id: String,
|
|
|
|
pad: Arc<RwLock<GridViewRevisionPad>>,
|
|
|
|
rev_manager: Arc<RevisionManager>,
|
|
|
|
field_rev: Arc<FieldRevision>,
|
|
|
|
row_delegate: Arc<dyn GridViewRowDelegate>,
|
|
|
|
) -> FlowyResult<Box<dyn GroupController>> {
|
|
|
|
let configuration_reader = GroupConfigurationReaderImpl(pad.clone());
|
|
|
|
let configuration_writer = GroupConfigurationWriterImpl {
|
|
|
|
user_id,
|
|
|
|
rev_manager,
|
|
|
|
view_pad: pad,
|
|
|
|
};
|
|
|
|
let row_revs = row_delegate.gv_row_revs().await;
|
|
|
|
make_group_controller(view_id, field_rev, row_revs, configuration_reader, configuration_writer).await
|
|
|
|
}
|
2022-08-15 20:07:01 +08:00
|
|
|
|
2022-08-20 15:40:13 +08:00
|
|
|
async fn apply_change(
|
|
|
|
user_id: &str,
|
|
|
|
rev_manager: Arc<RevisionManager>,
|
|
|
|
change: GridViewRevisionChangeset,
|
|
|
|
) -> FlowyResult<()> {
|
|
|
|
let GridViewRevisionChangeset { delta, md5 } = change;
|
|
|
|
let (base_rev_id, rev_id) = rev_manager.next_rev_id_pair();
|
|
|
|
let delta_data = delta.json_bytes();
|
2022-08-20 23:54:51 +08:00
|
|
|
let revision = Revision::new(&rev_manager.object_id, base_rev_id, rev_id, delta_data, user_id, md5);
|
2022-08-20 15:40:13 +08:00
|
|
|
let _ = rev_manager.add_local_revision(&revision).await?;
|
|
|
|
Ok(())
|
2022-08-14 23:01:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
struct GridViewRevisionCloudService {
|
|
|
|
#[allow(dead_code)]
|
|
|
|
token: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RevisionCloudService for GridViewRevisionCloudService {
|
|
|
|
#[tracing::instrument(level = "trace", skip(self))]
|
|
|
|
fn fetch_object(&self, _user_id: &str, _object_id: &str) -> FutureResult<Vec<Revision>, FlowyError> {
|
|
|
|
FutureResult::new(async move { Ok(vec![]) })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct GridViewRevisionPadBuilder();
|
|
|
|
impl RevisionObjectBuilder for GridViewRevisionPadBuilder {
|
|
|
|
type Output = GridViewRevisionPad;
|
|
|
|
|
|
|
|
fn build_object(object_id: &str, revisions: Vec<Revision>) -> FlowyResult<Self::Output> {
|
|
|
|
let pad = GridViewRevisionPad::from_revisions(object_id, revisions)?;
|
|
|
|
Ok(pad)
|
|
|
|
}
|
|
|
|
}
|
2022-08-15 20:07:01 +08:00
|
|
|
|
2022-08-20 15:40:13 +08:00
|
|
|
struct GroupConfigurationReaderImpl(Arc<RwLock<GridViewRevisionPad>>);
|
|
|
|
|
|
|
|
impl GroupConfigurationReader for GroupConfigurationReaderImpl {
|
2022-09-02 21:34:00 +08:00
|
|
|
fn get_configuration(&self) -> AFFuture<Option<Arc<GroupConfigurationRevision>>> {
|
2022-08-20 15:40:13 +08:00
|
|
|
let view_pad = self.0.clone();
|
2022-08-15 20:07:01 +08:00
|
|
|
wrap_future(async move {
|
2022-09-02 21:34:00 +08:00
|
|
|
let mut groups = view_pad.read().await.get_all_groups();
|
2022-08-21 22:47:24 +08:00
|
|
|
if groups.is_empty() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
debug_assert_eq!(groups.len(), 1);
|
|
|
|
Some(groups.pop().unwrap())
|
2022-08-15 20:07:01 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-08-20 15:40:13 +08:00
|
|
|
|
|
|
|
struct GroupConfigurationWriterImpl {
|
|
|
|
user_id: String,
|
|
|
|
rev_manager: Arc<RevisionManager>,
|
|
|
|
view_pad: Arc<RwLock<GridViewRevisionPad>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl GroupConfigurationWriter for GroupConfigurationWriterImpl {
|
2022-09-01 20:41:15 +08:00
|
|
|
fn save_configuration(
|
2022-08-20 15:40:13 +08:00
|
|
|
&self,
|
|
|
|
field_id: &str,
|
|
|
|
field_type: FieldTypeRevision,
|
2022-08-22 08:53:02 +08:00
|
|
|
group_configuration: GroupConfigurationRevision,
|
2022-08-20 15:40:13 +08:00
|
|
|
) -> AFFuture<FlowyResult<()>> {
|
|
|
|
let user_id = self.user_id.clone();
|
|
|
|
let rev_manager = self.rev_manager.clone();
|
|
|
|
let view_pad = self.view_pad.clone();
|
|
|
|
let field_id = field_id.to_owned();
|
|
|
|
|
|
|
|
wrap_future(async move {
|
2022-08-22 08:53:02 +08:00
|
|
|
let changeset = view_pad
|
|
|
|
.write()
|
|
|
|
.await
|
|
|
|
.insert_group(&field_id, &field_type, group_configuration)?;
|
2022-08-21 22:47:24 +08:00
|
|
|
|
|
|
|
if let Some(changeset) = changeset {
|
|
|
|
let _ = apply_change(&user_id, rev_manager, changeset).await?;
|
2022-08-20 15:40:13 +08:00
|
|
|
}
|
2022-08-21 22:47:24 +08:00
|
|
|
Ok(())
|
2022-08-20 15:40:13 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn make_grid_setting(view_pad: &GridViewRevisionPad, field_revs: &[Arc<FieldRevision>]) -> GridSettingPB {
|
2022-09-03 16:47:58 +08:00
|
|
|
let layout_type: GridLayout = view_pad.layout.clone().into();
|
|
|
|
let filter_configurations = view_pad
|
2022-08-20 15:40:13 +08:00
|
|
|
.get_all_filters(field_revs)
|
|
|
|
.map(|filters_by_field_id| {
|
|
|
|
filters_by_field_id
|
|
|
|
.into_iter()
|
2022-09-03 16:47:58 +08:00
|
|
|
.map(|(_, v)| {
|
|
|
|
let repeated_filter: RepeatedGridFilterConfigurationPB = v.into();
|
|
|
|
repeated_filter.items
|
|
|
|
})
|
|
|
|
.flatten()
|
|
|
|
.collect::<Vec<GridFilterConfigurationPB>>()
|
2022-08-20 15:40:13 +08:00
|
|
|
})
|
|
|
|
.unwrap_or_default();
|
2022-09-03 16:47:58 +08:00
|
|
|
|
2022-09-02 22:13:38 +08:00
|
|
|
let group_configurations = view_pad
|
2022-09-02 21:34:00 +08:00
|
|
|
.get_groups_by_field_revs(field_revs)
|
2022-08-20 15:40:13 +08:00
|
|
|
.map(|groups_by_field_id| {
|
|
|
|
groups_by_field_id
|
|
|
|
.into_iter()
|
2022-09-03 16:47:58 +08:00
|
|
|
.map(|(_, v)| {
|
|
|
|
let repeated_group: RepeatedGridGroupConfigurationPB = v.into();
|
|
|
|
repeated_group.items
|
|
|
|
})
|
|
|
|
.flatten()
|
|
|
|
.collect::<Vec<GridGroupConfigurationPB>>()
|
2022-08-20 15:40:13 +08:00
|
|
|
})
|
|
|
|
.unwrap_or_default();
|
|
|
|
|
|
|
|
GridSettingPB {
|
|
|
|
layouts: GridLayoutPB::all(),
|
2022-09-03 16:47:58 +08:00
|
|
|
layout_type,
|
|
|
|
filter_configurations: filter_configurations.into(),
|
|
|
|
group_configurations: group_configurations.into(),
|
2022-08-20 15:40:13 +08:00
|
|
|
}
|
|
|
|
}
|
2022-08-21 22:47:24 +08:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2022-09-12 10:44:33 +08:00
|
|
|
use lib_ot::core::Delta;
|
2022-08-21 22:47:24 +08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test() {
|
|
|
|
let s1 = r#"[{"insert":"{\"view_id\":\"fTURELffPr\",\"grid_id\":\"fTURELffPr\",\"layout\":0,\"filters\":[],\"groups\":[]}"}]"#;
|
2022-09-12 10:44:33 +08:00
|
|
|
let _delta_1 = Delta::from_json(s1).unwrap();
|
2022-08-21 22:47:24 +08:00
|
|
|
|
|
|
|
let s2 = r#"[{"retain":195},{"insert":"{\\\"group_id\\\":\\\"wD9i\\\",\\\"visible\\\":true},{\\\"group_id\\\":\\\"xZtv\\\",\\\"visible\\\":true},{\\\"group_id\\\":\\\"tFV2\\\",\\\"visible\\\":true}"},{"retain":10}]"#;
|
2022-09-12 10:44:33 +08:00
|
|
|
let _delta_2 = Delta::from_json(s2).unwrap();
|
2022-08-21 22:47:24 +08:00
|
|
|
}
|
|
|
|
}
|