view_ext.rs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. use crate::entities::{CreateViewParams, ViewLayoutPB};
  2. use bytes::Bytes;
  3. use collab_folder::core::{View, ViewLayout};
  4. use flowy_error::FlowyError;
  5. use lib_infra::future::FutureResult;
  6. use lib_infra::util::timestamp;
  7. use nanoid::nanoid;
  8. use std::collections::HashMap;
  9. use std::sync::Arc;
  10. pub trait ViewDataProcessor {
  11. /// Closes the view and releases the resources that this view has in
  12. /// the backend
  13. fn close_view(&self, view_id: &str) -> FutureResult<(), FlowyError>;
  14. /// Gets the data of the this view.
  15. /// For example, the data can be used to duplicate the view.
  16. fn get_view_data(&self, view_id: &str) -> FutureResult<Bytes, FlowyError>;
  17. /// Create a view with the pre-defined data.
  18. /// For example, the initial data of the grid/calendar/kanban board when
  19. /// you create a new view.
  20. fn create_view_with_build_in_data(
  21. &self,
  22. user_id: i64,
  23. view_id: &str,
  24. name: &str,
  25. layout: ViewLayout,
  26. ext: HashMap<String, String>,
  27. ) -> FutureResult<(), FlowyError>;
  28. /// Create a view with custom data
  29. fn create_view_with_custom_data(
  30. &self,
  31. user_id: i64,
  32. view_id: &str,
  33. name: &str,
  34. data: Vec<u8>,
  35. layout: ViewLayout,
  36. ext: HashMap<String, String>,
  37. ) -> FutureResult<(), FlowyError>;
  38. }
  39. pub type ViewDataProcessorMap = Arc<HashMap<ViewLayout, Arc<dyn ViewDataProcessor + Send + Sync>>>;
  40. impl From<ViewLayoutPB> for ViewLayout {
  41. fn from(pb: ViewLayoutPB) -> Self {
  42. match pb {
  43. ViewLayoutPB::Document => ViewLayout::Document,
  44. ViewLayoutPB::Grid => ViewLayout::Grid,
  45. ViewLayoutPB::Board => ViewLayout::Board,
  46. ViewLayoutPB::Calendar => ViewLayout::Calendar,
  47. }
  48. }
  49. }
  50. pub fn view_from_create_view_params(params: CreateViewParams, layout: ViewLayout) -> View {
  51. let time = timestamp();
  52. View {
  53. id: params.view_id,
  54. bid: params.belong_to_id,
  55. name: params.name,
  56. desc: params.desc,
  57. belongings: Default::default(),
  58. created_at: time,
  59. layout,
  60. database_id: None,
  61. }
  62. }
  63. pub fn gen_view_id() -> String {
  64. format!("v:{}", nanoid!(10))
  65. }