controller.rs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. use crate::entities::{GroupRowsChangesetPB, RowPB};
  2. use crate::services::cell::{decode_any_cell_data, CellBytesParser};
  3. use crate::services::group::action::GroupAction;
  4. use crate::services::group::configuration::GenericGroupConfiguration;
  5. use crate::services::group::entities::Group;
  6. use flowy_error::FlowyResult;
  7. use flowy_grid_data_model::revision::{
  8. FieldRevision, GroupConfigurationContent, RowChangeset, RowRevision, TypeOptionDataDeserializer,
  9. };
  10. use std::marker::PhantomData;
  11. use std::sync::Arc;
  12. const DEFAULT_GROUP_ID: &str = "default_group";
  13. // Each kind of group must implement this trait to provide custom group
  14. // operations. For example, insert cell data to the row_rev when creating
  15. // a new row.
  16. pub trait GroupController: GroupControllerSharedOperation + Send + Sync {
  17. fn will_create_row(&mut self, row_rev: &mut RowRevision, field_rev: &FieldRevision, group_id: &str);
  18. }
  19. pub trait GroupGenerator {
  20. type ConfigurationType;
  21. type TypeOptionType;
  22. fn generate_groups(
  23. field_id: &str,
  24. configuration: &Self::ConfigurationType,
  25. type_option: &Option<Self::TypeOptionType>,
  26. ) -> Vec<Group>;
  27. }
  28. // Defines the shared actions each group controller can perform.
  29. pub trait GroupControllerSharedOperation: Send + Sync {
  30. // The field that is used for grouping the rows
  31. fn field_id(&self) -> &str;
  32. fn groups(&self) -> Vec<Group>;
  33. fn get_group(&self, group_id: &str) -> Option<(usize, Group)>;
  34. fn fill_groups(&mut self, row_revs: &[Arc<RowRevision>], field_rev: &FieldRevision) -> FlowyResult<Vec<Group>>;
  35. fn move_group(&mut self, from_group_id: &str, to_group_id: &str) -> FlowyResult<()>;
  36. fn did_update_row(
  37. &mut self,
  38. row_rev: &RowRevision,
  39. field_rev: &FieldRevision,
  40. ) -> FlowyResult<Vec<GroupRowsChangesetPB>>;
  41. fn did_delete_row(
  42. &mut self,
  43. row_rev: &RowRevision,
  44. field_rev: &FieldRevision,
  45. ) -> FlowyResult<Vec<GroupRowsChangesetPB>>;
  46. fn did_move_row(
  47. &mut self,
  48. row_rev: &RowRevision,
  49. row_changeset: &mut RowChangeset,
  50. field_rev: &FieldRevision,
  51. to_row_id: &str,
  52. ) -> FlowyResult<Vec<GroupRowsChangesetPB>>;
  53. }
  54. /// C: represents the group configuration that impl [GroupConfigurationSerde]
  55. /// T: the type option data deserializer that impl [TypeOptionDataDeserializer]
  56. /// G: the group generator, [GroupGenerator]
  57. /// P: the parser that impl [CellBytesParser] for the CellBytes
  58. pub struct GenericGroupController<C, T, G, P> {
  59. pub field_id: String,
  60. pub type_option: Option<T>,
  61. pub configuration: GenericGroupConfiguration<C>,
  62. /// default_group is used to store the rows that don't belong to any groups.
  63. default_group: Group,
  64. group_action_phantom: PhantomData<G>,
  65. cell_parser_phantom: PhantomData<P>,
  66. }
  67. impl<C, T, G, P> GenericGroupController<C, T, G, P>
  68. where
  69. C: GroupConfigurationContent,
  70. T: TypeOptionDataDeserializer,
  71. G: GroupGenerator<ConfigurationType = GenericGroupConfiguration<C>, TypeOptionType = T>,
  72. {
  73. pub async fn new(
  74. field_rev: &Arc<FieldRevision>,
  75. mut configuration: GenericGroupConfiguration<C>,
  76. ) -> FlowyResult<Self> {
  77. let field_type_rev = field_rev.ty;
  78. let type_option = field_rev.get_type_option_entry::<T>(field_type_rev);
  79. let groups = G::generate_groups(&field_rev.id, &configuration, &type_option);
  80. let _ = configuration.merge_groups(groups).await?;
  81. let default_group = Group::new(
  82. DEFAULT_GROUP_ID.to_owned(),
  83. field_rev.id.clone(),
  84. format!("No {}", field_rev.name),
  85. "".to_string(),
  86. );
  87. Ok(Self {
  88. field_id: field_rev.id.clone(),
  89. default_group,
  90. type_option,
  91. configuration,
  92. group_action_phantom: PhantomData,
  93. cell_parser_phantom: PhantomData,
  94. })
  95. }
  96. }
  97. impl<C, T, G, P> GroupControllerSharedOperation for GenericGroupController<C, T, G, P>
  98. where
  99. P: CellBytesParser,
  100. C: GroupConfigurationContent,
  101. Self: GroupAction<CellDataType = P::Object>,
  102. {
  103. fn field_id(&self) -> &str {
  104. &self.field_id
  105. }
  106. fn groups(&self) -> Vec<Group> {
  107. self.configuration.clone_groups()
  108. }
  109. fn get_group(&self, group_id: &str) -> Option<(usize, Group)> {
  110. let group = self.configuration.get_group(group_id)?;
  111. Some((group.0, group.1.clone()))
  112. }
  113. fn fill_groups(&mut self, row_revs: &[Arc<RowRevision>], field_rev: &FieldRevision) -> FlowyResult<Vec<Group>> {
  114. for row_rev in row_revs {
  115. if let Some(cell_rev) = row_rev.cells.get(&self.field_id) {
  116. let mut group_rows: Vec<GroupRow> = vec![];
  117. let cell_bytes = decode_any_cell_data(cell_rev.data.clone(), field_rev);
  118. let cell_data = cell_bytes.parser::<P>()?;
  119. for group in self.configuration.groups() {
  120. if self.can_group(&group.content, &cell_data) {
  121. group_rows.push(GroupRow {
  122. row: row_rev.into(),
  123. group_id: group.id.clone(),
  124. });
  125. }
  126. }
  127. if group_rows.is_empty() {
  128. self.default_group.add_row(row_rev.into());
  129. } else {
  130. for group_row in group_rows {
  131. if let Some(group) = self.configuration.get_mut_group(&group_row.group_id) {
  132. group.add_row(group_row.row);
  133. }
  134. }
  135. }
  136. } else {
  137. self.default_group.add_row(row_rev.into());
  138. }
  139. }
  140. let default_group = self.default_group.clone();
  141. let mut groups: Vec<Group> = self.configuration.clone_groups();
  142. if !default_group.number_of_row() == 0 {
  143. groups.push(default_group);
  144. }
  145. Ok(groups)
  146. }
  147. fn move_group(&mut self, from_group_id: &str, to_group_id: &str) -> FlowyResult<()> {
  148. self.configuration.move_group(from_group_id, to_group_id)
  149. }
  150. fn did_update_row(
  151. &mut self,
  152. row_rev: &RowRevision,
  153. field_rev: &FieldRevision,
  154. ) -> FlowyResult<Vec<GroupRowsChangesetPB>> {
  155. if let Some(cell_rev) = row_rev.cells.get(&self.field_id) {
  156. let cell_bytes = decode_any_cell_data(cell_rev.data.clone(), field_rev);
  157. let cell_data = cell_bytes.parser::<P>()?;
  158. Ok(self.add_row_if_match(row_rev, &cell_data))
  159. } else {
  160. Ok(vec![])
  161. }
  162. }
  163. fn did_delete_row(
  164. &mut self,
  165. row_rev: &RowRevision,
  166. field_rev: &FieldRevision,
  167. ) -> FlowyResult<Vec<GroupRowsChangesetPB>> {
  168. if let Some(cell_rev) = row_rev.cells.get(&self.field_id) {
  169. let cell_bytes = decode_any_cell_data(cell_rev.data.clone(), field_rev);
  170. let cell_data = cell_bytes.parser::<P>()?;
  171. Ok(self.remove_row_if_match(row_rev, &cell_data))
  172. } else {
  173. Ok(vec![])
  174. }
  175. }
  176. fn did_move_row(
  177. &mut self,
  178. row_rev: &RowRevision,
  179. row_changeset: &mut RowChangeset,
  180. field_rev: &FieldRevision,
  181. to_row_id: &str,
  182. ) -> FlowyResult<Vec<GroupRowsChangesetPB>> {
  183. if let Some(cell_rev) = row_rev.cells.get(&self.field_id) {
  184. let cell_bytes = decode_any_cell_data(cell_rev.data.clone(), field_rev);
  185. let cell_data = cell_bytes.parser::<P>()?;
  186. tracing::trace!("Move row:{} to row:{}", row_rev.id, to_row_id);
  187. Ok(self.move_row_if_match(field_rev, row_rev, row_changeset, &cell_data, to_row_id))
  188. } else {
  189. Ok(vec![])
  190. }
  191. }
  192. }
  193. struct GroupRow {
  194. row: RowPB,
  195. group_id: String,
  196. }