chore: update view info

This commit is contained in:
appflowy 2022-06-15 11:43:24 +08:00
parent d4d7ba7452
commit 50605186fe
13 changed files with 175 additions and 21 deletions

View File

@ -43,6 +43,7 @@ pub fn create(folder: Arc<FolderManager>) -> Module {
.data(folder.trash_controller.clone())
.data(folder.clone());
// Workspace
module = module
.event(FolderEvent::CreateWorkspace, create_workspace_handler)
.event(FolderEvent::ReadCurWorkspace, read_cur_workspace_handler)
@ -50,12 +51,14 @@ pub fn create(folder: Arc<FolderManager>) -> Module {
.event(FolderEvent::OpenWorkspace, open_workspace_handler)
.event(FolderEvent::ReadWorkspaceApps, read_workspace_apps_handler);
// App
module = module
.event(FolderEvent::CreateApp, create_app_handler)
.event(FolderEvent::ReadApp, read_app_handler)
.event(FolderEvent::UpdateApp, update_app_handler)
.event(FolderEvent::DeleteApp, delete_app_handler);
// View
module = module
.event(FolderEvent::CreateView, create_view_handler)
.event(FolderEvent::ReadView, read_view_handler)
@ -68,6 +71,7 @@ pub fn create(folder: Arc<FolderManager>) -> Module {
.event(FolderEvent::CloseView, close_view_handler)
.event(FolderEvent::MoveFolderItem, move_item_handler);
// Trash
module = module
.event(FolderEvent::ReadTrash, read_trash_handler)
.event(FolderEvent::PutbackTrash, putback_trash_handler)

View File

@ -14,6 +14,7 @@ use flowy_sync::client_document::default::{initial_quill_delta_string, initial_r
use flowy_error::FlowyError;
use flowy_folder_data_model::entities::view::ViewDataType;
use flowy_folder_data_model::entities::UpdateViewInfoParams;
use flowy_folder_data_model::user_default;
use flowy_revision::disk::SQLiteTextBlockRevisionPersistence;
use flowy_revision::{RevisionManager, RevisionPersistence, RevisionWebSocket};
@ -244,11 +245,18 @@ pub trait ViewDataProcessor {
fn close_container(&self, view_id: &str) -> FutureResult<(), FlowyError>;
fn view_delta_data(&self, view_id: &str) -> FutureResult<Bytes, FlowyError>;
fn get_delta_data(&self, view_id: &str) -> FutureResult<Bytes, FlowyError>;
fn create_default_view(&self, user_id: &str, view_id: &str) -> FutureResult<Bytes, FlowyError>;
fn process_view_delta_data(&self, user_id: &str, view_id: &str, data: Vec<u8>) -> FutureResult<Bytes, FlowyError>;
fn create_view_from_delta_data(
&self,
user_id: &str,
view_id: &str,
data: Vec<u8>,
) -> FutureResult<Bytes, FlowyError>;
fn handle_view_info_updated(&self, params: UpdateViewInfoParams) -> FutureResult<(), FlowyError>;
fn data_type(&self) -> ViewDataType;
}

View File

@ -15,7 +15,7 @@ use crate::{
use bytes::Bytes;
use flowy_database::kv::KV;
use flowy_folder_data_model::entities::view::{gen_view_id, ViewDataType};
use flowy_folder_data_model::entities::{ViewExtData, ViewInfo};
use flowy_folder_data_model::entities::{UpdateViewInfoParams, ViewExtData, ViewInfo};
use flowy_folder_data_model::revision::ViewRevision;
use flowy_sync::entities::text_block_info::TextBlockId;
use futures::{FutureExt, StreamExt};
@ -65,7 +65,7 @@ impl ViewController {
params.data = view_data.to_vec();
} else {
let delta_data = processor
.process_view_delta_data(&user_id, &params.view_id, params.data.clone())
.create_view_from_delta_data(&user_id, &params.view_id, params.data.clone())
.await?;
let _ = self
.create_view(&params.view_id, params.data_type.clone(), delta_data)
@ -211,7 +211,7 @@ impl ViewController {
.await?;
let processor = self.get_data_processor(&view_rev.data_type)?;
let delta_bytes = processor.view_delta_data(view_id).await?;
let delta_bytes = processor.get_delta_data(view_id).await?;
let duplicate_params = CreateViewParams {
belong_to_id: view_rev.belong_to_id.clone(),
name: format!("{} (copy)", &view_rev.name),
@ -259,6 +259,28 @@ impl ViewController {
Ok(view_rev)
}
#[tracing::instrument(level = "debug", skip(self, params), err)]
pub(crate) async fn update_view_info(&self, params: UpdateViewInfoParams) -> Result<ViewRevision, FlowyError> {
let changeset = ViewChangeset::new(params.clone());
let view_id = changeset.id.clone();
let view_rev = self
.persistence
.begin_transaction(|transaction| {
let _ = transaction.update_view(changeset)?;
let view_rev = transaction.read_view(&view_id)?;
let view: View = view_rev.clone().into();
send_dart_notification(&view_id, FolderNotification::ViewUpdated)
.payload(view)
.send();
let _ = notify_views_changed(&view_rev.belong_to_id, self.trash_controller.clone(), &transaction)?;
Ok(view_rev)
})
.await?;
let _ = self.update_view_on_server(params);
Ok(view_rev)
}
pub(crate) async fn latest_visit_view(&self) -> FlowyResult<Option<ViewRevision>> {
match KV::get_str(LATEST_VIEW_ID) {
None => Ok(None),

View File

@ -11,7 +11,7 @@ use crate::{
services::{TrashController, ViewController},
};
use flowy_folder_data_model::entities::view::{MoveFolderItemParams, MoveFolderItemPayload, MoveFolderItemType};
use flowy_folder_data_model::entities::ViewInfo;
use flowy_folder_data_model::entities::{UpdateViewInfoParams, UpdateViewInfoPayload, ViewInfo};
use flowy_folder_data_model::revision::TrashRevision;
use lib_dispatch::prelude::{data_result, AppData, Data, DataResult};
use std::{convert::TryInto, sync::Arc};
@ -56,11 +56,11 @@ pub(crate) async fn update_view_handler(
#[tracing::instrument(level = "debug", skip(data, controller), err)]
pub(crate) async fn update_view_info_handler(
data: Data<UpdateViewPayload>,
data: Data<UpdateViewInfoPayload>,
controller: AppData<Arc<ViewController>>,
) -> Result<(), FlowyError> {
let params: UpdateViewParams = data.into_inner().try_into()?;
let _ = controller.update_view(params).await?;
let params: UpdateViewInfoParams = data.into_inner().try_into()?;
let _ = controller.update_view_info(params).await?;
Ok(())
}

View File

@ -89,6 +89,8 @@ impl GridManager {
Ok(())
}
// pub fn update_grid_info()
// #[tracing::instrument(level = "debug", skip(self), err)]
pub fn get_grid_editor(&self, grid_id: &str) -> FlowyResult<Arc<GridMetaEditor>> {
match self.editor_map.get(grid_id) {

View File

@ -26,6 +26,7 @@ use lib_infra::future::{BoxResultFuture, FutureResult};
use lib_ws::{WSChannel, WSMessageReceiver, WebSocketRawMessage};
use std::collections::HashMap;
use flowy_folder::entities::UpdateViewInfoParams;
use flowy_grid_data_model::entities::BuildGridContext;
use std::convert::TryFrom;
use std::{convert::TryInto, sync::Arc};
@ -173,7 +174,7 @@ impl ViewDataProcessor for TextBlockViewDataProcessor {
})
}
fn view_delta_data(&self, view_id: &str) -> FutureResult<Bytes, FlowyError> {
fn get_delta_data(&self, view_id: &str) -> FutureResult<Bytes, FlowyError> {
let view_id = view_id.to_string();
let manager = self.0.clone();
FutureResult::new(async move {
@ -197,7 +198,7 @@ impl ViewDataProcessor for TextBlockViewDataProcessor {
})
}
fn process_view_delta_data(
fn create_view_from_delta_data(
&self,
_user_id: &str,
_view_id: &str,
@ -206,6 +207,11 @@ impl ViewDataProcessor for TextBlockViewDataProcessor {
FutureResult::new(async move { Ok(Bytes::from(data)) })
}
fn handle_view_info_updated(&self, _params: UpdateViewInfoParams) -> FutureResult<(), FlowyError> {
tracing::warn!("Unimplemented the handle_view_info_updated in TextBlock");
FutureResult::new(async { Ok(()) })
}
fn data_type(&self) -> ViewDataType {
ViewDataType::TextBlock
}
@ -245,7 +251,7 @@ impl ViewDataProcessor for GridViewDataProcessor {
})
}
fn view_delta_data(&self, view_id: &str) -> FutureResult<Bytes, FlowyError> {
fn get_delta_data(&self, view_id: &str) -> FutureResult<Bytes, FlowyError> {
let view_id = view_id.to_string();
let grid_manager = self.0.clone();
FutureResult::new(async move {
@ -264,7 +270,12 @@ impl ViewDataProcessor for GridViewDataProcessor {
FutureResult::new(async move { make_grid_view_data(&user_id, &view_id, grid_manager, build_context).await })
}
fn process_view_delta_data(&self, user_id: &str, view_id: &str, data: Vec<u8>) -> FutureResult<Bytes, FlowyError> {
fn create_view_from_delta_data(
&self,
user_id: &str,
view_id: &str,
data: Vec<u8>,
) -> FutureResult<Bytes, FlowyError> {
let user_id = user_id.to_string();
let view_id = view_id.to_string();
let grid_manager = self.0.clone();
@ -276,6 +287,10 @@ impl ViewDataProcessor for GridViewDataProcessor {
})
}
fn handle_view_info_updated(&self, params: UpdateViewInfoParams) -> FutureResult<(), FlowyError> {
todo!()
}
fn data_type(&self) -> ViewDataType {
ViewDataType::Grid
}

View File

@ -114,6 +114,9 @@ pub enum ErrorCode {
#[display(fmt = "Invalid date time format")]
InvalidDateTimeFormat = 500,
#[display(fmt = "The input string is empty or contains invalid characters")]
UnexpectedEmptyString = 999,
#[display(fmt = "Invalid data")]
InvalidData = 1000,
}

View File

@ -1,5 +1,6 @@
use crate::entities::{RepeatedView, ViewDataType};
use crate::parser::view::ViewIdentify;
use crate::parser::view_info::{ViewFilterParser, ViewGroupParser, ViewSortParser};
use flowy_derive::ProtoBuf;
use flowy_error_code::ErrorCode;
use std::convert::TryInto;
@ -43,22 +44,22 @@ pub struct ViewExtData {
#[derive(Eq, PartialEq, ProtoBuf, Debug, Default, Clone)]
pub struct ViewFilter {
#[pb(index = 1)]
pub field_id: String,
pub object_id: String,
}
#[derive(Eq, PartialEq, ProtoBuf, Debug, Default, Clone)]
pub struct ViewGroup {
#[pb(index = 1)]
pub group_field_id: String,
pub group_object_id: String,
#[pb(index = 2, one_of)]
pub sub_group_field_id: Option<String>,
pub sub_group_object_id: Option<String>,
}
#[derive(Eq, PartialEq, ProtoBuf, Debug, Default, Clone)]
pub struct ViewSort {
#[pb(index = 1)]
pub field_id: String,
pub object_id: String,
}
#[derive(Default, ProtoBuf)]
@ -82,3 +83,33 @@ pub struct UpdateViewInfoParams {
pub group: Option<ViewGroup>,
pub sort: Option<ViewSort>,
}
impl TryInto<UpdateViewInfoParams> for UpdateViewInfoPayload {
type Error = ErrorCode;
fn try_into(self) -> Result<UpdateViewInfoParams, Self::Error> {
let view_id = ViewIdentify::parse(self.view_id)?.0;
let filter = match self.filter {
None => None,
Some(filter) => Some(ViewFilterParser::parse(filter)?),
};
let group = match self.group {
None => None,
Some(group) => Some(ViewGroupParser::parse(group)?),
};
let sort = match self.sort {
None => None,
Some(sort) => Some(ViewSortParser::parse(sort)?),
};
Ok(UpdateViewInfoParams {
view_id,
filter,
group,
sort,
})
}
}

View File

@ -1,4 +1,5 @@
pub mod app;
pub mod trash;
pub mod view;
pub mod view_info;
pub mod workspace;

View File

@ -0,0 +1,5 @@
mod object_id;
mod view_ext;
pub use object_id::*;
pub use view_ext::*;

View File

@ -0,0 +1,19 @@
use crate::errors::ErrorCode;
#[derive(Debug)]
pub struct ObjectId(pub String);
impl ObjectId {
pub fn parse(s: String) -> Result<ObjectId, ErrorCode> {
if s.trim().is_empty() {
return Err(ErrorCode::UnexpectedEmptyString);
}
Ok(Self(s))
}
}
impl AsRef<str> for ObjectId {
fn as_ref(&self) -> &str {
&self.0
}
}

View File

@ -0,0 +1,40 @@
use crate::entities::{ViewFilter, ViewGroup, ViewSort};
use crate::errors::ErrorCode;
use crate::parser::view_info::ObjectId;
pub struct ViewFilterParser(pub ViewFilter);
impl ViewFilterParser {
pub fn parse(value: ViewFilter) -> Result<ViewFilter, ErrorCode> {
let object_id = ObjectId::parse(value.object_id)?.0;
Ok(ViewFilter { object_id })
}
}
pub struct ViewGroupParser(pub ViewGroup);
impl ViewGroupParser {
pub fn parse(value: ViewGroup) -> Result<ViewGroup, ErrorCode> {
let group_object_id = ObjectId::parse(value.group_object_id)?.0;
let sub_group_object_id = match value.sub_group_object_id {
None => None,
Some(object_id) => Some(ObjectId::parse(object_id)?.0),
};
Ok(ViewGroup {
group_object_id,
sub_group_object_id,
})
}
}
pub struct ViewSortParser(pub ViewSort);
impl ViewSortParser {
pub fn parse(value: ViewSort) -> Result<ViewSort, ErrorCode> {
let object_id = ObjectId::parse(value.object_id)?.0;
Ok(ViewSort { object_id })
}
}

View File

@ -111,22 +111,26 @@ impl std::convert::From<ViewExtDataRevision> for ViewExtData {
impl std::convert::From<ViewFilterRevision> for ViewFilter {
fn from(rev: ViewFilterRevision) -> Self {
ViewFilter { field_id: rev.field_id }
ViewFilter {
object_id: rev.field_id,
}
}
}
impl std::convert::From<ViewGroupRevision> for ViewGroup {
fn from(rev: ViewGroupRevision) -> Self {
ViewGroup {
group_field_id: rev.group_field_id,
sub_group_field_id: rev.sub_group_field_id,
group_object_id: rev.group_field_id,
sub_group_object_id: rev.sub_group_field_id,
}
}
}
impl std::convert::From<ViewSortRevision> for ViewSort {
fn from(rev: ViewSortRevision) -> Self {
ViewSort { field_id: rev.field_id }
ViewSort {
object_id: rev.field_id,
}
}
}