action.rs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. use crate::entities::{GroupChangesPB, GroupPB, GroupRowsNotificationPB, InsertedGroupPB};
  2. use crate::services::cell::DecodedCellData;
  3. use crate::services::group::controller::MoveGroupRowContext;
  4. use crate::services::group::{GroupData, GroupSettingChangeset};
  5. use collab_database::fields::Field;
  6. use collab_database::rows::{Cell, Row};
  7. use flowy_error::FlowyResult;
  8. /// Using polymorphism to provides the customs action for different group controller.
  9. ///
  10. /// For example, the `CheckboxGroupController` implements this trait to provide custom behavior.
  11. ///
  12. pub trait GroupCustomize: Send + Sync {
  13. type CellData: DecodedCellData;
  14. /// Returns the a value of the cell if the cell data is not exist.
  15. /// The default value is `None`
  16. ///
  17. /// Determine which group the row is placed in based on the data of the cell. If the cell data
  18. /// is None. The row will be put in to the `No status` group
  19. ///
  20. fn placeholder_cell(&self) -> Option<Cell> {
  21. None
  22. }
  23. /// Returns a bool value to determine whether the group should contain this cell or not.
  24. fn can_group(&self, content: &str, cell_data: &Self::CellData) -> bool;
  25. fn create_or_delete_group_when_cell_changed(
  26. &mut self,
  27. _row: &Row,
  28. _old_cell_data: Option<&Self::CellData>,
  29. _cell_data: &Self::CellData,
  30. ) -> FlowyResult<(Option<InsertedGroupPB>, Option<GroupPB>)> {
  31. Ok((None, None))
  32. }
  33. /// Adds or removes a row if the cell data match the group filter.
  34. /// It gets called after editing the cell or row
  35. ///
  36. fn add_or_remove_row_when_cell_changed(
  37. &mut self,
  38. row: &Row,
  39. cell_data: &Self::CellData,
  40. ) -> Vec<GroupRowsNotificationPB>;
  41. /// Deletes the row from the group
  42. fn delete_row(&mut self, row: &Row, cell_data: &Self::CellData) -> Vec<GroupRowsNotificationPB>;
  43. /// Move row from one group to another
  44. fn move_row(
  45. &mut self,
  46. cell_data: &Self::CellData,
  47. context: MoveGroupRowContext,
  48. ) -> Vec<GroupRowsNotificationPB>;
  49. /// Returns None if there is no need to delete the group when corresponding row get removed
  50. fn delete_group_when_move_row(
  51. &mut self,
  52. _row: &Row,
  53. _cell_data: &Self::CellData,
  54. ) -> Option<GroupPB> {
  55. None
  56. }
  57. }
  58. /// Defines the shared actions any group controller can perform.
  59. pub trait GroupControllerOperation: Send + Sync {
  60. /// The field that is used for grouping the rows
  61. fn field_id(&self) -> &str;
  62. /// Returns number of groups the current field has
  63. fn groups(&self) -> Vec<&GroupData>;
  64. /// Returns the index and the group data with group_id
  65. fn get_group(&self, group_id: &str) -> Option<(usize, GroupData)>;
  66. /// Separates the rows into different groups
  67. fn fill_groups(&mut self, rows: &[&Row], field: &Field) -> FlowyResult<()>;
  68. /// Remove the group with from_group_id and insert it to the index with to_group_id
  69. fn move_group(&mut self, from_group_id: &str, to_group_id: &str) -> FlowyResult<()>;
  70. /// Insert/Remove the row to the group if the corresponding cell data is changed
  71. fn did_update_group_row(
  72. &mut self,
  73. old_row: &Option<Row>,
  74. row: &Row,
  75. field: &Field,
  76. ) -> FlowyResult<DidUpdateGroupRowResult>;
  77. /// Remove the row from the group if the row gets deleted
  78. fn did_delete_delete_row(
  79. &mut self,
  80. row: &Row,
  81. field: &Field,
  82. ) -> FlowyResult<DidMoveGroupRowResult>;
  83. /// Move the row from one group to another group
  84. fn move_group_row(&mut self, context: MoveGroupRowContext) -> FlowyResult<DidMoveGroupRowResult>;
  85. /// Update the group if the corresponding field is changed
  86. fn did_update_group_field(&mut self, field: &Field) -> FlowyResult<Option<GroupChangesPB>>;
  87. fn apply_group_setting_changeset(&mut self, changeset: GroupSettingChangeset) -> FlowyResult<()>;
  88. }
  89. #[derive(Debug)]
  90. pub struct DidUpdateGroupRowResult {
  91. pub(crate) inserted_group: Option<InsertedGroupPB>,
  92. pub(crate) deleted_group: Option<GroupPB>,
  93. pub(crate) row_changesets: Vec<GroupRowsNotificationPB>,
  94. }
  95. #[derive(Debug)]
  96. pub struct DidMoveGroupRowResult {
  97. pub(crate) deleted_group: Option<GroupPB>,
  98. pub(crate) row_changesets: Vec<GroupRowsNotificationPB>,
  99. }