notification.rs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. use crate::entities::{view_pb_without_child_views, WorkspacePB, WorkspaceSettingPB};
  2. use collab_folder::core::{View, Workspace};
  3. use flowy_derive::ProtoBuf_Enum;
  4. use flowy_notification::NotificationBuilder;
  5. use lib_dispatch::prelude::ToBytes;
  6. const OBSERVABLE_CATEGORY: &str = "Workspace";
  7. #[derive(ProtoBuf_Enum, Debug)]
  8. pub(crate) enum FolderNotification {
  9. Unknown = 0,
  10. /// Trigger after creating a workspace
  11. DidCreateWorkspace = 1,
  12. // /// Trigger after updating a workspace
  13. DidUpdateWorkspace = 2,
  14. DidUpdateWorkspaceViews = 3,
  15. /// Trigger when the settings of the workspace are changed. The changes including the latest visiting view, etc
  16. DidUpdateWorkspaceSetting = 4,
  17. DidUpdateView = 29,
  18. /// Trigger when the properties including rename,update description of the view are changed
  19. DidUpdateChildViews = 30,
  20. /// Trigger after deleting the view
  21. DidDeleteView = 31,
  22. /// Trigger when restore the view from trash
  23. DidRestoreView = 32,
  24. /// Trigger after moving the view to trash
  25. DidMoveViewToTrash = 33,
  26. /// Trigger when the number of trash is changed
  27. DidUpdateTrash = 34,
  28. }
  29. impl std::default::Default for FolderNotification {
  30. fn default() -> Self {
  31. FolderNotification::Unknown
  32. }
  33. }
  34. impl std::convert::From<FolderNotification> for i32 {
  35. fn from(notification: FolderNotification) -> Self {
  36. notification as i32
  37. }
  38. }
  39. #[tracing::instrument(level = "trace")]
  40. pub(crate) fn send_notification(id: &str, ty: FolderNotification) -> NotificationBuilder {
  41. NotificationBuilder::new(id, ty, OBSERVABLE_CATEGORY)
  42. }
  43. /// The [CURRENT_WORKSPACE] represents as the current workspace that opened by the
  44. /// user. Only one workspace can be opened at a time.
  45. const CURRENT_WORKSPACE: &str = "current-workspace";
  46. pub(crate) fn send_workspace_notification<T: ToBytes>(ty: FolderNotification, payload: T) {
  47. send_notification(CURRENT_WORKSPACE, ty)
  48. .payload(payload)
  49. .send();
  50. }
  51. pub(crate) fn send_workspace_setting_notification(
  52. current_workspace: Option<Workspace>,
  53. current_view: Option<View>,
  54. ) -> Option<()> {
  55. let workspace: WorkspacePB = current_workspace?.into();
  56. let latest_view = current_view.map(view_pb_without_child_views);
  57. let setting = WorkspaceSettingPB {
  58. workspace,
  59. latest_view,
  60. };
  61. send_workspace_notification(FolderNotification::DidUpdateWorkspaceSetting, setting);
  62. None
  63. }