2023-04-04 06:08:50 +00:00
|
|
|
use crate::entities::{CreateViewParams, ViewLayoutPB};
|
2023-04-04 00:41:16 +00:00
|
|
|
use bytes::Bytes;
|
|
|
|
use collab_folder::core::{View, ViewLayout};
|
|
|
|
use flowy_error::FlowyError;
|
|
|
|
use lib_infra::future::FutureResult;
|
|
|
|
use lib_infra::util::timestamp;
|
2023-05-21 10:53:59 +00:00
|
|
|
|
2023-04-04 00:41:16 +00:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
2023-05-31 06:08:54 +00:00
|
|
|
pub type ViewData = Bytes;
|
|
|
|
|
|
|
|
/// The handler will be used to handler the folder operation for a specific
|
|
|
|
/// view layout. Each [ViewLayout] will have a handler. So when creating a new
|
|
|
|
/// view, the [ViewLayout] will be used to get the handler.
|
|
|
|
///
|
|
|
|
pub trait FolderOperationHandler {
|
2023-04-04 00:41:16 +00:00
|
|
|
/// Closes the view and releases the resources that this view has in
|
|
|
|
/// the backend
|
|
|
|
fn close_view(&self, view_id: &str) -> FutureResult<(), FlowyError>;
|
|
|
|
|
2023-05-31 06:08:54 +00:00
|
|
|
/// Returns the [ViewData] that can be used to create the same view.
|
|
|
|
fn duplicate_view(&self, view_id: &str) -> FutureResult<ViewData, FlowyError>;
|
2023-04-04 00:41:16 +00:00
|
|
|
|
2023-05-31 06:08:54 +00:00
|
|
|
/// Create a view with custom data
|
|
|
|
fn create_view_with_view_data(
|
2023-04-04 00:41:16 +00:00
|
|
|
&self,
|
|
|
|
user_id: i64,
|
|
|
|
view_id: &str,
|
|
|
|
name: &str,
|
2023-05-31 06:08:54 +00:00
|
|
|
data: Vec<u8>,
|
2023-04-04 00:41:16 +00:00
|
|
|
layout: ViewLayout,
|
|
|
|
ext: HashMap<String, String>,
|
|
|
|
) -> FutureResult<(), FlowyError>;
|
|
|
|
|
2023-05-31 06:08:54 +00:00
|
|
|
/// Create a view with the pre-defined data.
|
|
|
|
/// For example, the initial data of the grid/calendar/kanban board when
|
|
|
|
/// you create a new view.
|
|
|
|
fn create_built_in_view(
|
2023-04-04 00:41:16 +00:00
|
|
|
&self,
|
|
|
|
user_id: i64,
|
|
|
|
view_id: &str,
|
|
|
|
name: &str,
|
|
|
|
layout: ViewLayout,
|
2023-05-31 06:08:54 +00:00
|
|
|
meta: HashMap<String, String>,
|
|
|
|
) -> FutureResult<(), FlowyError>;
|
|
|
|
|
|
|
|
/// Create a view by importing data
|
|
|
|
fn import_from_bytes(
|
|
|
|
&self,
|
|
|
|
view_id: &str,
|
|
|
|
name: &str,
|
|
|
|
bytes: Vec<u8>,
|
|
|
|
) -> FutureResult<(), FlowyError>;
|
|
|
|
|
|
|
|
/// Create a view by importing data from a file
|
|
|
|
fn import_from_file_path(
|
|
|
|
&self,
|
|
|
|
view_id: &str,
|
|
|
|
name: &str,
|
|
|
|
path: String,
|
2023-04-04 00:41:16 +00:00
|
|
|
) -> FutureResult<(), FlowyError>;
|
|
|
|
}
|
|
|
|
|
2023-05-31 06:08:54 +00:00
|
|
|
pub type FolderOperationHandlers =
|
|
|
|
Arc<HashMap<ViewLayout, Arc<dyn FolderOperationHandler + Send + Sync>>>;
|
2023-04-04 00:41:16 +00:00
|
|
|
|
2023-04-04 06:08:50 +00:00
|
|
|
impl From<ViewLayoutPB> for ViewLayout {
|
|
|
|
fn from(pb: ViewLayoutPB) -> Self {
|
2023-04-04 00:41:16 +00:00
|
|
|
match pb {
|
2023-04-04 06:08:50 +00:00
|
|
|
ViewLayoutPB::Document => ViewLayout::Document,
|
|
|
|
ViewLayoutPB::Grid => ViewLayout::Grid,
|
|
|
|
ViewLayoutPB::Board => ViewLayout::Board,
|
|
|
|
ViewLayoutPB::Calendar => ViewLayout::Calendar,
|
2023-04-04 00:41:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-31 06:08:54 +00:00
|
|
|
pub(crate) fn create_view(params: CreateViewParams, layout: ViewLayout) -> View {
|
2023-04-04 00:41:16 +00:00
|
|
|
let time = timestamp();
|
|
|
|
View {
|
|
|
|
id: params.view_id,
|
2023-05-31 06:08:54 +00:00
|
|
|
bid: params.parent_view_id,
|
2023-04-04 00:41:16 +00:00
|
|
|
name: params.name,
|
|
|
|
desc: params.desc,
|
2023-05-31 09:42:14 +00:00
|
|
|
children: Default::default(),
|
2023-04-04 00:41:16 +00:00
|
|
|
created_at: time,
|
|
|
|
layout,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn gen_view_id() -> String {
|
2023-05-21 10:53:59 +00:00
|
|
|
uuid::Uuid::new_v4().to_string()
|
2023-04-04 00:41:16 +00:00
|
|
|
}
|