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-08-16 17:13:56 +08:00
|
|
|
CreateRowParams, GridFilterConfiguration, GridLayout, GridSettingPB, GroupPB, GroupRowsChangesetPB, InsertedRowPB,
|
|
|
|
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-08-17 14:33:45 +08:00
|
|
|
use crate::services::group::{default_group_configuration, GroupConfigurationDelegate, GroupService};
|
2022-08-17 08:14:25 +08:00
|
|
|
use crate::services::setting::make_grid_setting;
|
|
|
|
use flowy_error::{FlowyError, FlowyResult};
|
2022-08-15 20:07:01 +08:00
|
|
|
use flowy_grid_data_model::revision::{FieldRevision, GroupConfigurationRevision, RowRevision};
|
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};
|
|
|
|
use flowy_sync::entities::grid::GridSettingChangesetParams;
|
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-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-08-15 20:07:01 +08:00
|
|
|
group_service: Arc<RwLock<GroupService>>,
|
|
|
|
scheduler: Arc<dyn GridServiceTaskScheduler>,
|
2022-08-14 23:01:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl GridViewRevisionEditor {
|
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-08-15 20:07:01 +08:00
|
|
|
let group_service = GroupService::new(Box::new(pad.clone())).await;
|
|
|
|
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-08-15 20:07:01 +08:00
|
|
|
group_service: Arc::new(RwLock::new(group_service)),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-08-16 20:34:12 +08:00
|
|
|
pub(crate) async fn fill_row(&self, row_rev: &mut RowRevision, params: &CreateRowParams) {
|
2022-08-16 15:49:54 +08:00
|
|
|
match params.layout {
|
|
|
|
GridLayout::Table => {
|
|
|
|
// Table can be grouped too
|
2022-08-15 20:07:01 +08:00
|
|
|
}
|
2022-08-16 15:49:54 +08:00
|
|
|
GridLayout::Board => match params.group_id.as_ref() {
|
|
|
|
None => {}
|
|
|
|
Some(group_id) => {
|
|
|
|
self.group_service
|
|
|
|
.read()
|
|
|
|
.await
|
2022-08-17 08:14:25 +08:00
|
|
|
.fill_row(row_rev, group_id, |field_id| {
|
|
|
|
self.field_delegate.get_field_rev(&field_id)
|
|
|
|
})
|
2022-08-16 15:49:54 +08:00
|
|
|
.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-16 17:13:56 +08:00
|
|
|
let inserted_row = InsertedRowPB {
|
|
|
|
row: row_pb.clone(),
|
|
|
|
index: None,
|
|
|
|
};
|
|
|
|
let changeset = GroupRowsChangesetPB::insert(group_id.clone(), vec![inserted_row]);
|
2022-08-16 15:49:54 +08:00
|
|
|
self.notify_did_update_group(changeset).await;
|
2022-08-15 20:07:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-16 15:49:54 +08:00
|
|
|
pub(crate) async fn did_delete_row(&self, row_id: &str) {
|
|
|
|
// Send the group notification if the current view has groups;
|
|
|
|
match self.group_id_of_row(row_id).await {
|
|
|
|
None => {}
|
|
|
|
Some(group_id) => {
|
|
|
|
let changeset = GroupRowsChangesetPB::delete(group_id, vec![row_id.to_owned()]);
|
|
|
|
self.notify_did_update_group(changeset).await;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-17 14:33:45 +08:00
|
|
|
pub(crate) async fn did_update_row(&self, row_rev: &RowRevision) {
|
|
|
|
if let Some(changesets) = self
|
|
|
|
.group_service
|
|
|
|
.write()
|
|
|
|
.await
|
|
|
|
.did_update_row(row_rev, |field_id| self.field_delegate.get_field_rev(&field_id))
|
|
|
|
.await
|
|
|
|
{
|
|
|
|
for changeset in changesets {
|
|
|
|
self.notify_did_update_group(changeset).await;
|
2022-08-17 08:14:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-16 15:49:54 +08:00
|
|
|
async fn group_id_of_row(&self, row_id: &str) -> Option<String> {
|
2022-08-17 08:14:25 +08:00
|
|
|
let read_guard = &self.group_service.read().await.groups;
|
2022-08-16 15:49:54 +08:00
|
|
|
for group in read_guard.iter() {
|
2022-08-17 14:33:45 +08:00
|
|
|
if group.contains_row(row_id) {
|
2022-08-16 15:49:54 +08:00
|
|
|
return Some(group.id.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
2022-08-15 20:07:01 +08:00
|
|
|
}
|
|
|
|
|
2022-08-17 08:14:25 +08:00
|
|
|
// async fn get_mut_group<F>(&self, group_id: &str, f: F) -> FlowyResult<()>
|
|
|
|
// where
|
|
|
|
// F: Fn(&mut Group) -> FlowyResult<()>,
|
|
|
|
// {
|
|
|
|
// for group in self.groups.write().await.iter_mut() {
|
|
|
|
// if group.id == group_id {
|
|
|
|
// let _ = f(group)?;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// Ok(())
|
|
|
|
// }
|
2022-08-16 15:49:54 +08:00
|
|
|
|
2022-08-17 08:14:25 +08:00
|
|
|
pub(crate) async fn load_groups(&self) -> FlowyResult<Vec<GroupPB>> {
|
|
|
|
let field_revs = self.field_delegate.get_field_revs().await;
|
|
|
|
let row_revs = self.row_delegate.gv_row_revs().await;
|
|
|
|
|
|
|
|
match self
|
|
|
|
.group_service
|
|
|
|
.write()
|
|
|
|
.await
|
|
|
|
.load_groups(&field_revs, row_revs)
|
|
|
|
.await
|
|
|
|
{
|
2022-08-15 20:07:01 +08:00
|
|
|
None => Ok(vec![]),
|
2022-08-17 08:14:25 +08:00
|
|
|
Some(groups) => Ok(groups.into_iter().map(GroupPB::from).collect()),
|
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-15 20:07:01 +08:00
|
|
|
let grid_setting = make_grid_setting(self.pad.read().await.get_setting_rev(), &field_revs);
|
|
|
|
grid_setting
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) async fn update_setting(&self, changeset: GridSettingChangesetParams) -> FlowyResult<()> {
|
|
|
|
let _ = self.modify(|pad| Ok(pad.update_setting(changeset)?)).await;
|
|
|
|
Ok(())
|
|
|
|
}
|
2022-08-14 23:01:53 +08:00
|
|
|
|
2022-08-15 20:07:01 +08:00
|
|
|
pub(crate) async fn get_filters(&self) -> Vec<GridFilterConfiguration> {
|
2022-08-17 08:14:25 +08:00
|
|
|
let field_revs = self.field_delegate.get_field_revs().await;
|
2022-08-15 20:07:01 +08:00
|
|
|
match self.pad.read().await.get_setting_rev().get_all_filters(&field_revs) {
|
|
|
|
None => vec![],
|
|
|
|
Some(filters) => filters
|
|
|
|
.into_values()
|
|
|
|
.flatten()
|
|
|
|
.map(|filter| GridFilterConfiguration::from(filter.as_ref()))
|
|
|
|
.collect(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-16 15:49:54 +08:00
|
|
|
async fn notify_did_update_group(&self, changeset: GroupRowsChangesetPB) {
|
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-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) => {
|
|
|
|
let _ = self.apply_change(change).await?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn apply_change(&self, change: GridViewRevisionChangeset) -> FlowyResult<()> {
|
|
|
|
let GridViewRevisionChangeset { delta, md5 } = change;
|
|
|
|
let user_id = self.user_id.clone();
|
|
|
|
let (base_rev_id, rev_id) = self.rev_manager.next_rev_id_pair();
|
|
|
|
let delta_data = delta.json_bytes();
|
|
|
|
let revision = Revision::new(
|
|
|
|
&self.rev_manager.object_id,
|
|
|
|
base_rev_id,
|
|
|
|
rev_id,
|
|
|
|
delta_data,
|
|
|
|
&user_id,
|
|
|
|
md5,
|
|
|
|
);
|
|
|
|
let _ = self.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
|
|
|
|
|
|
|
impl GroupConfigurationDelegate for Arc<RwLock<GridViewRevisionPad>> {
|
|
|
|
fn get_group_configuration(&self, field_rev: Arc<FieldRevision>) -> AFFuture<GroupConfigurationRevision> {
|
|
|
|
let view_pad = self.clone();
|
|
|
|
wrap_future(async move {
|
|
|
|
let grid_pad = view_pad.read().await;
|
|
|
|
let configurations = grid_pad.get_groups(&field_rev.id, &field_rev.field_type_rev);
|
|
|
|
match configurations {
|
|
|
|
None => default_group_configuration(&field_rev),
|
|
|
|
Some(mut configurations) => {
|
|
|
|
assert_eq!(configurations.len(), 1);
|
|
|
|
(&*configurations.pop().unwrap()).clone()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|