fix: fill field_settings on creating linked view (#5598)

This commit is contained in:
Mohammad Zolfaghari 2024-06-27 04:37:08 +03:30 committed by GitHub
parent bbb3f95a13
commit b24fbc6b60
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 65 additions and 57 deletions

View File

@ -10,6 +10,7 @@ use flowy_document::entities::DocumentDataPB;
use flowy_document::manager::DocumentManager;
use flowy_document::parser::json::parser::JsonToDocumentParser;
use flowy_error::FlowyError;
use flowy_folder::entities::{CreateViewParams, ViewLayoutPB};
use flowy_folder::manager::{FolderManager, FolderUser};
use flowy_folder::share::ImportType;
use flowy_folder::view_operation::{
@ -182,17 +183,13 @@ impl FolderOperationHandler for DocumentFolderOperation {
fn create_view_with_view_data(
&self,
user_id: i64,
view_id: &str,
_name: &str,
data: Vec<u8>,
layout: ViewLayout,
_meta: HashMap<String, String>,
params: CreateViewParams,
) -> FutureResult<(), FlowyError> {
debug_assert_eq!(layout, ViewLayout::Document);
let view_id = view_id.to_string();
debug_assert_eq!(params.layout, ViewLayoutPB::Document);
let view_id = params.view_id.to_string();
let manager = self.0.clone();
FutureResult::new(async move {
let data = DocumentDataPB::try_from(Bytes::from(data))?;
let data = DocumentDataPB::try_from(Bytes::from(params.initial_data))?;
manager
.create_document(user_id, &view_id, Some(data.into()))
.await?;
@ -302,40 +299,43 @@ impl FolderOperationHandler for DatabaseFolderOperation {
fn create_view_with_view_data(
&self,
_user_id: i64,
view_id: &str,
name: &str,
data: Vec<u8>,
layout: ViewLayout,
meta: HashMap<String, String>,
params: CreateViewParams,
) -> FutureResult<(), FlowyError> {
match CreateDatabaseExtParams::from_map(meta) {
match CreateDatabaseExtParams::from_map(params.meta.clone()) {
None => {
let database_manager = self.0.clone();
let view_id = view_id.to_string();
let view_id = params.view_id.to_string();
FutureResult::new(async move {
database_manager
.create_database_with_database_data(&view_id, data)
.create_database_with_database_data(&view_id, params.initial_data)
.await?;
Ok(())
})
},
Some(params) => {
Some(database_params) => {
let database_manager = self.0.clone();
let layout = match layout {
ViewLayout::Board => DatabaseLayoutPB::Board,
ViewLayout::Calendar => DatabaseLayoutPB::Calendar,
ViewLayout::Grid => DatabaseLayoutPB::Grid,
ViewLayout::Document | ViewLayout::Chat => {
let layout = match params.layout {
ViewLayoutPB::Board => DatabaseLayoutPB::Board,
ViewLayoutPB::Calendar => DatabaseLayoutPB::Calendar,
ViewLayoutPB::Grid => DatabaseLayoutPB::Grid,
ViewLayoutPB::Document | ViewLayoutPB::Chat => {
return FutureResult::new(async move { Err(FlowyError::not_support()) });
},
};
let name = name.to_string();
let database_view_id = view_id.to_string();
let name = params.name.to_string();
let database_view_id = params.view_id.to_string();
let database_parent_view_id = params.parent_view_id.to_string();
FutureResult::new(async move {
database_manager
.create_linked_view(name, layout.into(), params.database_id, database_view_id)
.create_linked_view(
name,
layout.into(),
database_params.database_id,
database_view_id,
database_parent_view_id,
)
.await?;
Ok(())
})
@ -504,11 +504,7 @@ impl FolderOperationHandler for ChatFolderOperation {
fn create_view_with_view_data(
&self,
_user_id: i64,
_view_id: &str,
_name: &str,
_data: Vec<u8>,
_layout: ViewLayout,
_meta: HashMap<String, String>,
_params: CreateViewParams,
) -> FutureResult<(), FlowyError> {
FutureResult::new(async move { Err(FlowyError::not_support()) })
}

View File

@ -323,18 +323,23 @@ impl DatabaseManager {
layout: DatabaseLayout,
database_id: String,
database_view_id: String,
database_parent_view_id: String,
) -> FlowyResult<()> {
let wdb = self.get_database_indexer().await?;
let mut params = CreateViewParams::new(database_id.clone(), database_view_id, name, layout);
if let Some(database) = wdb.get_database(&database_id).await {
let (field, layout_setting) = DatabaseLayoutDepsResolver::new(database, layout)
.resolve_deps_when_create_database_linked_view();
let (field, layout_setting, field_settings_map) =
DatabaseLayoutDepsResolver::new(database, layout)
.resolve_deps_when_create_database_linked_view(&database_parent_view_id);
if let Some(field) = field {
params = params.with_deps_fields(vec![field], vec![default_field_settings_by_layout_map()]);
}
if let Some(layout_setting) = layout_setting {
params = params.with_layout_setting(layout_setting);
}
if let Some(field_settings_map) = field_settings_map {
params = params.with_field_settings_map(field_settings_map);
}
};
wdb.create_database_linked_view(params).await?;
Ok(())

View File

@ -1,6 +1,8 @@
use collab_database::database::{gen_field_id, MutexDatabase};
use collab_database::fields::Field;
use collab_database::views::{DatabaseLayout, LayoutSetting, OrderObjectPosition};
use collab_database::views::{
DatabaseLayout, FieldSettingsByFieldIdMap, LayoutSetting, OrderObjectPosition,
};
use std::sync::Arc;
use crate::entities::FieldType;
@ -28,23 +30,40 @@ impl DatabaseLayoutDepsResolver {
pub fn resolve_deps_when_create_database_linked_view(
&self,
) -> (Option<Field>, Option<LayoutSetting>) {
view_id: &str,
) -> (
Option<Field>,
Option<LayoutSetting>,
Option<FieldSettingsByFieldIdMap>,
) {
match self.database_layout {
DatabaseLayout::Grid => (None, None),
DatabaseLayout::Grid => (None, None, None),
DatabaseLayout::Board => {
let layout_settings = BoardLayoutSetting::new().into();
if !self
let field = if !self
.database
.lock()
.get_fields(None)
.into_iter()
.any(|field| FieldType::from(field.field_type).can_be_group())
{
let select_field = self.create_select_field();
(Some(select_field), Some(layout_settings))
Some(self.create_select_field())
} else {
(None, Some(layout_settings))
}
None
};
let field_settings_map = self.database.lock().get_field_settings(view_id, None);
tracing::info!(
"resolve_deps_when_create_database_linked_view {:?}",
field_settings_map
);
(
field,
Some(layout_settings),
Some(field_settings_map.into()),
)
},
DatabaseLayout::Calendar => {
match self
@ -56,12 +75,12 @@ impl DatabaseLayoutDepsResolver {
{
Some(field) => {
let layout_setting = CalendarLayoutSetting::new(field.id).into();
(None, Some(layout_setting))
(None, Some(layout_setting), None)
},
None => {
let date_field = self.create_date_field();
let layout_setting = CalendarLayoutSetting::new(date_field.clone().id).into();
(Some(date_field), Some(layout_setting))
(Some(date_field), Some(layout_setting), None)
},
}
},

View File

@ -376,9 +376,8 @@ impl FolderManager {
let view_layout: ViewLayout = params.layout.clone().into();
let handler = self.get_handler(&view_layout)?;
let user_id = self.user.user_id()?;
let meta = params.meta.clone();
if meta.is_empty() && params.initial_data.is_empty() {
if params.meta.is_empty() && params.initial_data.is_empty() {
tracing::trace!("Create view with build-in data");
handler
.create_built_in_view(user_id, &params.view_id, &params.name, view_layout.clone())
@ -386,14 +385,7 @@ impl FolderManager {
} else {
tracing::trace!("Create view with view data");
handler
.create_view_with_view_data(
user_id,
&params.view_id,
&params.name,
params.initial_data.clone(),
view_layout.clone(),
meta,
)
.create_view_with_view_data(user_id, params.clone())
.await?;
}

View File

@ -63,11 +63,7 @@ pub trait FolderOperationHandler {
fn create_view_with_view_data(
&self,
user_id: i64,
view_id: &str,
name: &str,
data: Vec<u8>,
layout: ViewLayout,
meta: HashMap<String, String>,
params: CreateViewParams,
) -> FutureResult<(), FlowyError>;
/// Create a view with the pre-defined data.